From 91ab83071414bddbeb4001ab0c1cd27d551464de Mon Sep 17 00:00:00 2001 From: Michel Kaporin Date: Mon, 26 Jun 2017 14:36:58 +0200 Subject: [PATCH] Refactored main.js to TS --- build/tfs/darwin/smoketest.sh | 3 +- build/tfs/linux/smoketest.sh | 3 +- build/tfs/win32/smoketest.ps1 | 26 +++---- test/smoke/package.json | 4 +- test/smoke/src/{main.js => main.ts} | 114 +++++++++++++++++----------- test/smoke/src/mocha-runner.ts | 4 +- 6 files changed, 86 insertions(+), 68 deletions(-) rename test/smoke/src/{main.js => main.ts} (63%) diff --git a/build/tfs/darwin/smoketest.sh b/build/tfs/darwin/smoketest.sh index 60e1863f08d..f750f4e159c 100755 --- a/build/tfs/darwin/smoketest.sh +++ b/build/tfs/darwin/smoketest.sh @@ -23,6 +23,5 @@ step "Build minified & upload source maps" \ step "Run smoke test" \ pushd test/smoke npm install - npm run compile - node src/main.js --latest "$AGENT_BUILDDIRECTORY/VSCode-darwin/Visual Studio Code - Insiders.app/Contents/MacOS/Electron" + npm test -- --latest "$AGENT_BUILDDIRECTORY/VSCode-darwin/Visual Studio Code - Insiders.app/Contents/MacOS/Electron" popd \ No newline at end of file diff --git a/build/tfs/linux/smoketest.sh b/build/tfs/linux/smoketest.sh index a8cc950eba5..609b088103e 100644 --- a/build/tfs/linux/smoketest.sh +++ b/build/tfs/linux/smoketest.sh @@ -35,8 +35,7 @@ function configureEnvironment { function runTest { pushd test/smoke npm install - npm run compile - sudo -u testuser -H xvfb-run -a -s "-screen 0 1024x768x8" node src/main.js --latest "$AGENT_BUILDDIRECTORY/VSCode-linux-ia32/code-insiders" + sudo -u testuser -H xvfb-run -a -s "-screen 0 1024x768x8" npm test -- --latest "$AGENT_BUILDDIRECTORY/VSCode-linux-ia32/code-insiders" popd } diff --git a/build/tfs/win32/smoketest.ps1 b/build/tfs/win32/smoketest.ps1 index e7737a9f895..bfc8b94a933 100644 --- a/build/tfs/win32/smoketest.ps1 +++ b/build/tfs/win32/smoketest.ps1 @@ -1,7 +1,7 @@ Param( - [string]$arch, - [string]$mixinPassword, - [string]$vsoPAT + [string]$arch, + [string]$mixinPassword, + [string]$vsoPAT ) . .\scripts\env.ps1 @@ -9,38 +9,38 @@ Param( # Create a _netrc file to download distro dependencies # In order to get _netrc to work, we need a HOME variable setup -$env:HOME=$env:USERPROFILE +$env:HOME = $env:USERPROFILE "machine monacotools.visualstudio.com password ${vsoPAT}" | Out-File "$env:USERPROFILE\_netrc" -Encoding ASCII # Set the right architecture -$env:npm_config_arch="$arch" +$env:npm_config_arch = "$arch" step "Install dependencies" { - exec { & npm install } + exec { & npm install } } $env:VSCODE_MIXIN_PASSWORD = $mixinPassword step "Mix in repository from vscode-distro" { - exec { & npm run gulp -- mixin } + exec { & npm run gulp -- mixin } } step "Get Electron" { - exec { & npm run gulp -- "electron-$global:arch" } + exec { & npm run gulp -- "electron-$global:arch" } } step "Install distro dependencies" { - exec { & node build\tfs\common\installDistro.js } + exec { & node build\tfs\common\installDistro.js } } step "Build minified" { - exec { & npm run gulp -- --max_old_space_size=4096 "vscode-win32-$global:arch-min" } + exec { & npm run gulp -- --max_old_space_size=4096 "vscode-win32-$global:arch-min" } } step "Run smoke test" { exec { & Push-Location test\smoke } - exec { & npm install; npm run compile } - exec { & node src/main.js --latest "$env:AGENT_BUILDDIRECTORY\VSCode-win32-$global:arch\Code - Insiders.exe" } - exec { & Pop-Location } + exec { & npm install } + exec { & npm test -- --latest "$env:AGENT_BUILDDIRECTORY\VSCode-win32-$global:arch\Code - Insiders.exe" } + exec { & Pop-Location } } done \ No newline at end of file diff --git a/test/smoke/package.json b/test/smoke/package.json index c8b869205d4..b5d425c29ae 100644 --- a/test/smoke/package.json +++ b/test/smoke/package.json @@ -5,7 +5,7 @@ "scripts": { "compile": "tsc", "pretest": "tsc", - "test": "node src/main.js" + "test": "node out/main.js" }, "devDependencies": { "@types/mocha": "^2.2.41", @@ -23,4 +23,4 @@ "strip-json-comments": "^2.0.1", "htmlparser2": "^3.9.2" } -} +} \ No newline at end of file diff --git a/test/smoke/src/main.js b/test/smoke/src/main.ts similarity index 63% rename from test/smoke/src/main.js rename to test/smoke/src/main.ts index acf9db502d2..1a94b3c9899 100644 --- a/test/smoke/src/main.js +++ b/test/smoke/src/main.ts @@ -3,17 +3,17 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -var fs = require('fs'); -var https = require('https'); -var program = require('commander'); -var git = require('simple-git')(); -var child_process = require('child_process'); -var path = require('path'); +const fs = require('fs'); +const https = require('https'); +const program = require('commander'); +const git = require('simple-git')(); +const child_process = require('child_process'); +const path = require('path'); -var tempFolder = 'test_data'; -var testRepoUrl = 'https://github.com/Microsoft/vscode-smoketest-express'; -var testRepoLocalDir = path.join(process.cwd(), `${tempFolder}/vscode-smoketest-express`); -var keybindingsUrl = 'https://raw.githubusercontent.com/Microsoft/vscode-docs/master/scripts/keybindings'; +const tempFolder = 'test_data'; +const testRepoUrl = 'https://github.com/Microsoft/vscode-smoketest-express'; +const testRepoLocalDir = path.join(process.cwd(), `${tempFolder}/vscode-smoketest-express`); +const keybindingsUrl = 'https://raw.githubusercontent.com/Microsoft/vscode-docs/master/scripts/keybindings'; program .option('-l, --latest ', 'path to the latest VS Code to test') @@ -43,32 +43,42 @@ if (parseInt(process.version.substr(1)) < 6) { // Setting up environment variables process.env.VSCODE_LATEST_PATH = program.latest; -if (program.stable) process.env.VSCODE_STABLE_PATH = program.stable; +if (program.stable) { + process.env.VSCODE_STABLE_PATH = program.stable; +} process.env.SMOKETEST_REPO = testRepoLocalDir; -if (program.stable && program.stable.toLowerCase().startsWith('insiders')) process.env.VSCODE_EDITION = 'insiders'; - -// Setting up 'vscode-smoketest-express' project -var os = process.platform; -if (os === 'darwin') os = 'osx'; -else if (os === 'win32') os = 'win'; -var promises = []; - -try { - promises.push(getKeybindings(`${keybindingsUrl}/doc.keybindings.${os}.json`, `${tempFolder}/keybindings.json`)); - promises.push(cleanOrClone(testRepoUrl, testRepoLocalDir)); - - Promise.all(promises).then(() => { execute('npm install', testRepoLocalDir).then(() => runTests()); }); -} catch (e) { - throw new Error('Error caught running the smoke test: ' + e); +if (program.stable && program.stable.toLowerCase().startsWith('insiders')) { + process.env.VSCODE_EDITION = 'insiders'; } -function fail(errorMessage) { +// Setting up 'vscode-smoketest-express' project +let os = process.platform.toString(); +if (os === 'darwin') { + os = 'osx'; +} +else if (os === 'win32') { + os = 'win'; +} + +var promises: Promise[] = []; + +promises.push(getKeybindings(`${keybindingsUrl}/doc.keybindings.${os}.json`, `${tempFolder}/keybindings.json`)); +promises.push(cleanOrClone(testRepoUrl, testRepoLocalDir)); + +Promise.all(promises) + .then(() => execute('npm install', testRepoLocalDir)) + .then(() => runTests()) + .catch(reason => { + throw new Error('Error caught running the smoke test: ' + reason); + }); + +function fail(errorMessage): void { console.error(errorMessage); process.exit(1); } -function runTests() { - console.log('Running tests...') +function runTests(): void { + console.log('Running tests...'); const spawn = require('child_process').spawn; var proc = spawn(process.execPath, [ 'out/mocha-runner.js' @@ -79,7 +89,9 @@ function runTests() { proc.stderr.on('data', data => { var date = new Date().toLocaleString(); fs.appendFile(`${tempFolder}/errors.log`, `${date}: ${data.toString()}`, (err) => { - if (err) throw new Error(`Could not write stderr to errors.log with the following error: ${err}`); + if (err) { + throw new Error(`Could not write stderr to errors.log with the following error: ${err}`); + }; }); }); proc.on('exit', (code) => { @@ -87,7 +99,7 @@ function runTests() { }); } -function cleanOrClone(repo, dir) { +function cleanOrClone(repo: string, dir: string): Promise { console.log('Cleaning or cloning test project repository...'); return new Promise((res, rej) => { if (!folderExists(dir)) { @@ -97,47 +109,57 @@ function cleanOrClone(repo, dir) { }); } else { git.cwd(dir); - git.fetch((err) => { - if (err) rej(err); + git.fetch(err => { + if (err) { + rej(err); + } resetAndClean(); }); } var resetAndClean = () => { - git.reset(['FETCH_HEAD', '--hard'], (err) => { - if (err) rej(err); + git.reset(['FETCH_HEAD', '--hard'], err => { + if (err) { + rej(err); + } - git.clean('f', ['-d'], (err) => { - if (err) rej(err); + git.clean('f', ['-d'], err => { + if (err) { + rej(err); + } console.log('Test project was successfully reset to initial state.'); res(); }); }); - } + }; }); } -function execute(cmd, dir) { +function execute(cmd, dir): Promise { return new Promise((res, rej) => { console.log(`Running ${cmd}...`); child_process.exec(cmd, { cwd: dir, stdio: [0, 1, 2] }, (error, stdout, stderr) => { - if (error) rej(error); - if (stderr) console.error(stderr); + if (error) { + rej(error); + } + if (stderr) { + console.error(stderr); + } console.log(stdout); res(); }); }); } -function getKeybindings(url, location) { +function getKeybindings(url: string, location: string): Promise { console.log(`Fetching keybindings from ${url}...`); return new Promise((resolve, reject) => { https.get(url, (res) => { - if (res.statusCode != 200) { + if (res.statusCode !== 200) { reject(`Failed to obtain key bindings with response code: ${res.statusCode}`); } - var buffer = []; + var buffer: Buffer[] = []; res.on('data', (chunk) => buffer.push(chunk)); res.on('end', () => { fs.writeFile(location, Buffer.concat(buffer), 'utf8', () => { @@ -151,7 +173,7 @@ function getKeybindings(url, location) { }); } -function folderExists(folder) { +function folderExists(folder: string): boolean { try { fs.accessSync(folder, 'rw'); return true; @@ -160,7 +182,7 @@ function folderExists(folder) { } } -function binaryExists(filePath) { +function binaryExists(filePath: string): boolean { try { fs.accessSync(filePath, 'x'); return true; diff --git a/test/smoke/src/mocha-runner.ts b/test/smoke/src/mocha-runner.ts index 62171a6dbc2..c6d6e4d1295 100644 --- a/test/smoke/src/mocha-runner.ts +++ b/test/smoke/src/mocha-runner.ts @@ -4,7 +4,6 @@ *--------------------------------------------------------------------------------------------*/ const MochaTest = require('mocha'); -const path = require('path'); const mochaTest = new MochaTest({ timeout: 360000, @@ -12,8 +11,7 @@ const mochaTest = new MochaTest({ slow: 50000, useColors: true }); - -mochaTest.addFile(path.join(process.cwd(), 'out/test.js')); +mochaTest.addFile(require('path').join(process.cwd(), 'out/test.js')); mochaTest.run((failures) => { process.exit(failures); }); \ No newline at end of file