Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions vscode/extension/tests/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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...')
Expand All @@ -10,8 +11,6 @@ async function globalSetup() {
const extensionsDir = path.join(testSetupDir, 'extensions')

// Clean up any existing test setup directory
await fs.remove(testSetupDir)
await fs.ensureDir(extensionsDir)

// Get the extension version from package.json
const packageJson = JSON.parse(
Expand All @@ -30,26 +29,52 @@ async function globalSetup() {
)
}

console.log(`Installing extension: ${vsixFileName}`)

// 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,
},
)
console.log('Extension installed successfully to .test_setup/extensions')

// 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