-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathglobal-setup.ts
More file actions
80 lines (67 loc) · 2.75 KB
/
global-setup.ts
File metadata and controls
80 lines (67 loc) · 2.75 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
import { execSync } from 'child_process'
import path from 'path'
import fs from 'fs-extra'
import { createHash } from 'crypto'
async function globalSetup() {
console.log('Setting up extension for Playwright tests...')
const extensionDir = path.join(__dirname, '..')
const testSetupDir = path.join(extensionDir, '.test_setup')
const extensionsDir = path.join(testSetupDir, 'extensions')
// Clean up any existing test setup directory
// Get the extension version from package.json
const packageJson = JSON.parse(
fs.readFileSync(path.join(extensionDir, 'package.json'), 'utf-8'),
)
const version = packageJson.version
const extensionName = packageJson.name || 'sqlmesh'
// Look for the specific version .vsix file
const vsixFileName = `${extensionName}-${version}.vsix`
const vsixPath = path.join(extensionDir, vsixFileName)
if (!fs.existsSync(vsixPath)) {
throw new Error(
`Extension file ${vsixFileName} not found. Run "pnpm run vscode:package" first.`,
)
}
// Create a temporary user data directory for the installation
const tempUserDataDir = await fs.mkdtemp(
path.join(require('os').tmpdir(), 'vscode-test-install-user-data-'),
)
try {
// Check if in .test_setup there is a extension hash file which contains the hash of the extension
// If it does, check if the hash is the same as the hash of the extension in the vsix file
// If it is, skip the installation
// If it is not, remove the extension hash file and install the extension
const extensionHashFile = path.join(testSetupDir, 'extension-hash.txt')
console.log('extensionHashFile', extensionHashFile)
if (fs.existsSync(extensionHashFile)) {
const extensionHash = fs.readFileSync(extensionHashFile, 'utf-8')
const vsixHash = await hashFile(vsixPath)
if (extensionHash === vsixHash) {
console.log('Extension already installed')
return
}
}
await fs.remove(testSetupDir)
await fs.ensureDir(testSetupDir)
await fs.ensureDir(extensionsDir)
console.log(`Installing extension: ${vsixFileName}`)
execSync(
`pnpm run code-server --user-data-dir "${tempUserDataDir}" --extensions-dir "${extensionsDir}" --install-extension "${vsixPath}"`,
{
stdio: 'inherit',
cwd: extensionDir,
},
)
// Write the hash of the extension to the extension hash file
const extensionHash = await hashFile(vsixPath)
await fs.writeFile(extensionHashFile, extensionHash)
} finally {
// Clean up temporary user data directory
await fs.remove(tempUserDataDir)
}
}
async function hashFile(filePath: string): Promise<string> {
const fileBuffer = await fs.readFile(filePath)
return createHash('sha256').update(fileBuffer).digest('hex')
}
export default globalSetup