-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcypress.config.ts
More file actions
222 lines (201 loc) · 6.86 KB
/
Copy pathcypress.config.ts
File metadata and controls
222 lines (201 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { defineConfig } from 'cypress'
import vitePreprocessor from 'cypress-vite'
import path from 'path'
import fs from 'node:fs'
import os from 'node:os'
import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
const execFileAsync = promisify(execFile)
const solrCollectionUrl = 'http://localhost:8983/solr/collection1'
const solrCoreAdminUrl = 'http://localhost:8983/solr/admin/cores'
const solrSchemaPath = '/var/solr/data/collection1/conf/schema.xml'
type ExecFileError = Error & {
stderr?: string
stdout?: string
}
export default defineConfig({
video: false,
e2e: {
baseUrl: 'http://localhost:8000',
specPattern: 'tests/e2e-integration/**/*.spec.{js,jsx,ts,tsx}',
fixturesFolder: 'tests/e2e-integration/fixtures',
screenshotOnRunFailure: false,
video: false,
viewportWidth: 1920,
viewportHeight: 1080,
supportFile: 'tests/support/e2e.ts',
setupNodeEvents(on, config) {
on('file:preprocessor', vitePreprocessor(path.resolve(__dirname, './vite.config.ts')))
on('task', {
async solrSchemaFieldExists(fieldName: string): Promise<boolean> {
const statusCode = await runDockerCommand(config, [
'exec',
getSolrContainerName(config),
'curl',
'-sS',
'-o',
'/tmp/solr-schema-field-response.json',
'-w',
'%{http_code}',
`${solrCollectionUrl}/schema/fields/${encodeURIComponent(fieldName)}`
])
if (statusCode.trim() === '200') {
return true
}
if (statusCode.trim() === '404') {
return false
}
throw new Error(`Unexpected Solr schema field check status for ${fieldName}: ${statusCode}`)
},
async replaceSolrSchemaWithDataverseGeneratedSchema(): Promise<null> {
const generatedSchemaFragment = await getDataverseGeneratedSolrSchemaFragment(config)
const currentSchema = await runDockerCommand(config, [
'exec',
getSolrContainerName(config),
'cat',
solrSchemaPath
])
const mergedSchema = mergeGeneratedSchemaFragment(currentSchema, generatedSchemaFragment)
await copySchemaToSolrContainer(config, mergedSchema)
await reloadSolrCore(config)
return null
}
})
},
defaultCommandTimeout: 10_000 // https://docs.cypress.io/guides/references/configuration#Timeouts
},
component: {
indexHtmlFile: 'tests/support/component-index.html',
specPattern: ['tests/component/**/*.spec.{js,jsx,ts,tsx}'],
supportFile: 'tests/support/component.ts',
fixturesFolder: 'tests/component/fixtures',
devServer: {
framework: 'react',
bundler: 'vite'
},
setupNodeEvents(on, config) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-var-requires
require('@cypress/code-coverage/task')(on, config)
return config
}
},
env: {
frontendBasePath: '/modern',
backendUrl: 'http://localhost:8000',
oidcClientId: 'test',
oidcAuthorizationEndpoint: 'http://localhost:8000/realms/test/protocol/openid-connect/auth',
oidcTokenEndpoint: 'http://localhost:8000/realms/test/protocol/openid-connect/token',
oidcLogoutEndpoint: 'http://localhost:8000/realms/test/protocol/openid-connect/logout',
oidcLocalStorageKeyPrefix: 'DV_',
languages: [
{ code: 'en', name: 'English' },
{ code: 'es', name: 'Español' }
],
defaultLanguage: 'en',
branding: {
dataverseName: 'Dataverse'
},
homepage: {
supportUrl: 'https://support.dataverse.harvard.edu/'
},
footer: {
copyrightHolder: 'The President & Fellows of Harvard College',
privacyPolicyUrl: 'https://support.dataverse.harvard.edu/harvard-dataverse-privacy-policy'
},
codeCoverage: {
exclude: ['tests/**/*.*', '**/ErrorPage.tsx', '**/EditGuestBook.tsx']
}
}
})
function getSolrContainerName(config: Cypress.PluginConfigOptions): string {
return (config.env.solrContainerName as string | undefined) ?? 'dev_solr'
}
async function getDataverseGeneratedSolrSchemaFragment(
config: Cypress.PluginConfigOptions
): Promise<string> {
const backendUrl = config.env.backendUrl as string
const response = await fetch(`${backendUrl}/api/v1/admin/index/solr/schema`)
if (!response.ok) {
throw new Error(`Error while getting Dataverse-generated Solr schema: ${response.status}`)
}
return response.text()
}
async function copySchemaToSolrContainer(
config: Cypress.PluginConfigOptions,
schemaXml: string
): Promise<void> {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dataverse-solr-schema-'))
const tempSchemaPath = path.join(tempDir, 'schema.xml')
try {
fs.writeFileSync(tempSchemaPath, schemaXml)
await runDockerCommand(config, [
'cp',
tempSchemaPath,
`${getSolrContainerName(config)}:${solrSchemaPath}`
])
} finally {
fs.rmSync(tempDir, { recursive: true, force: true })
}
}
async function reloadSolrCore(config: Cypress.PluginConfigOptions): Promise<void> {
await runDockerCommand(config, [
'exec',
getSolrContainerName(config),
'curl',
'-sS',
`${solrCoreAdminUrl}?action=RELOAD&core=collection1&wt=json`
])
}
function mergeGeneratedSchemaFragment(
currentSchema: string,
generatedSchemaFragment: string
): string {
const generatedSchemaLines = generatedSchemaFragment.split('\n')
const fieldLines = generatedSchemaLines.filter((line) => line.includes('<field '))
const copyFieldLines = generatedSchemaLines.filter((line) => line.includes('<copyField '))
return replaceSchemaSection(
replaceSchemaSection(
currentSchema,
'<!-- SCHEMA-FIELDS::BEGIN -->',
'<!-- SCHEMA-FIELDS::END -->',
fieldLines
),
'<!-- SCHEMA-COPY-FIELDS::BEGIN -->',
'<!-- SCHEMA-COPY-FIELDS::END -->',
copyFieldLines
)
}
function replaceSchemaSection(
schema: string,
beginMarker: string,
endMarker: string,
replacementLines: string[]
): string {
const beginIndex = schema.indexOf(beginMarker)
const endIndex = schema.indexOf(endMarker)
if (beginIndex === -1 || endIndex === -1 || beginIndex > endIndex) {
throw new Error(`Could not find Solr schema section ${beginMarker}.`)
}
return [
schema.slice(0, beginIndex + beginMarker.length),
'',
replacementLines.join('\n'),
schema.slice(endIndex)
].join('\n')
}
async function runDockerCommand(
_config: Cypress.PluginConfigOptions,
args: string[]
): Promise<string> {
try {
const { stdout } = await execFileAsync('docker', args, { maxBuffer: 10 * 1024 * 1024 })
return stdout
} catch (error) {
const execError = error as ExecFileError
throw new Error(
`Docker command failed: docker ${args.join(' ')}. Reason was: ${
execError.stderr ?? execError.stdout ?? execError.message
}`
)
}
}