11import { execSync } from 'child_process'
22import path from 'path'
33import fs from 'fs-extra'
4+ import { createHash } from 'crypto'
45
56async function globalSetup ( ) {
67 console . log ( 'Setting up extension for Playwright tests...' )
@@ -10,8 +11,6 @@ async function globalSetup() {
1011 const extensionsDir = path . join ( testSetupDir , 'extensions' )
1112
1213 // Clean up any existing test setup directory
13- await fs . remove ( testSetupDir )
14- await fs . ensureDir ( extensionsDir )
1514
1615 // Get the extension version from package.json
1716 const packageJson = JSON . parse (
@@ -30,26 +29,52 @@ async function globalSetup() {
3029 )
3130 }
3231
33- console . log ( `Installing extension: ${ vsixFileName } ` )
34-
3532 // Create a temporary user data directory for the installation
3633 const tempUserDataDir = await fs . mkdtemp (
3734 path . join ( require ( 'os' ) . tmpdir ( ) , 'vscode-test-install-user-data-' ) ,
3835 )
3936
4037 try {
38+ // Check if in .test_setup there is a extension hash file which contains the hash of the extension
39+ // If it does, check if the hash is the same as the hash of the extension in the vsix file
40+ // If it is, skip the installation
41+ // If it is not, remove the extension hash file and install the extension
42+ const extensionHashFile = path . join ( testSetupDir , 'extension-hash.txt' )
43+ console . log ( 'extensionHashFile' , extensionHashFile )
44+ if ( fs . existsSync ( extensionHashFile ) ) {
45+ const extensionHash = fs . readFileSync ( extensionHashFile , 'utf-8' )
46+ const vsixHash = await hashFile ( vsixPath )
47+ if ( extensionHash === vsixHash ) {
48+ console . log ( 'Extension already installed' )
49+ return
50+ }
51+ }
52+
53+ await fs . remove ( testSetupDir )
54+ await fs . ensureDir ( testSetupDir )
55+ await fs . ensureDir ( extensionsDir )
56+
57+ console . log ( `Installing extension: ${ vsixFileName } ` )
4158 execSync (
4259 `pnpm run code-server --user-data-dir "${ tempUserDataDir } " --extensions-dir "${ extensionsDir } " --install-extension "${ vsixPath } "` ,
4360 {
4461 stdio : 'inherit' ,
4562 cwd : extensionDir ,
4663 } ,
4764 )
48- console . log ( 'Extension installed successfully to .test_setup/extensions' )
65+
66+ // Write the hash of the extension to the extension hash file
67+ const extensionHash = await hashFile ( vsixPath )
68+ await fs . writeFile ( extensionHashFile , extensionHash )
4969 } finally {
5070 // Clean up temporary user data directory
5171 await fs . remove ( tempUserDataDir )
5272 }
5373}
5474
75+ async function hashFile ( filePath : string ) : Promise < string > {
76+ const fileBuffer = await fs . readFile ( filePath )
77+ return createHash ( 'sha256' ) . update ( fileBuffer ) . digest ( 'hex' )
78+ }
79+
5580export default globalSetup
0 commit comments