diff --git a/package.json b/package.json index 9c90a95502932e0220eca731de4af1b845883f96..f2d2e3d92df96a9f4a22e18d143e31625256ed7b 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "precommit": "node build/gulpfile.hygiene.js", "gulp": "gulp --max_old_space_size=4096", "7z": "7z", - "update-grammars": "node build/npm/update-all-grammars.js" + "update-grammars": "node build/npm/update-all-grammars.js", + "smoketest": "node test/smoke/out/main.js" }, "dependencies": { "applicationinsights": "0.17.1", diff --git a/test/smoke/package.json b/test/smoke/package.json index ac7ee9a0426dfd43d4d4c2366b2e6a04e637a62a..1c3b3a11350296f14a61ec532b5ccad09ab4d277 100644 --- a/test/smoke/package.json +++ b/test/smoke/package.json @@ -19,9 +19,8 @@ "htmlparser2": "^3.9.2", "mocha": "^3.2.0", "rimraf": "^2.6.1", - "simple-git": "^1.73.0", "spectron": "~3.6.4", "strip-json-comments": "^2.0.1", "typescript": "^2.2.2" } -} +} \ No newline at end of file diff --git a/test/smoke/src/main.ts b/test/smoke/src/main.ts index 36f934b79baaa73c7d75ffa7d7a731700ba17f14..aee3966224369ea7144182821bf9693bd7c25f92 100644 --- a/test/smoke/src/main.ts +++ b/test/smoke/src/main.ts @@ -5,187 +5,144 @@ import * as fs from 'fs'; import * as https from 'https'; -import * as program from 'commander'; -import * as simplegit from 'simple-git'; +// import * as program from 'commander'; import * as cp from 'child_process'; import * as path from 'path'; import * as mkdirp from 'mkdirp'; -const git = simplegit(); - -const testDataPath = path.join(process.cwd(), 'test_data'); +const testDataPath = path.join(__dirname, '..', 'test_data'); const codeWorkspacePath = path.join(testDataPath, 'smoketest.code-workspace'); const testRepoUrl = 'https://github.com/Microsoft/vscode-smoketest-express'; const testRepoLocalDir = path.join(testDataPath, 'vscode-smoketest-express'); -const keybindingsUrl = 'https://raw.githubusercontent.com/Microsoft/vscode-docs/master/scripts/keybindings'; mkdirp.sync(testDataPath); -program - .option('-l, --latest ', 'path to the latest VS Code to test') - .option('-s, --stable [file path]', 'path to the stable VS Code to be used in data migration tests'); - -program.on('--help', () => { - console.log(' Examples:'); - console.log(''); - console.log(' $ npm test -- --latest path/to/binary'); - console.log(' $ npm test -- -l path/to/binary'); - console.log(''); - console.log(' $ npm test -- --latest path/to/latest/binary --stable path/to/stable/binary'); - console.log(' $ npm test -- -l path/to/latest/binary -s path/to/stable/binary'); - console.log(''); -}); - -program.parse(process.argv); - -if (!program.latest) { - fail('You must specify the binary to run the smoke test against'); -} -if (!fs.existsSync(program.latest) || (program.stable && !fs.existsSync(program.stable))) { - fail('The file path to electron binary does not exist or permissions do not allow to execute it. Please check the path provided.'); -} -if (parseInt(process.version.substr(1)) < 6) { - fail('Please update your Node version to greater than 6 to run the smoke test.'); +// program +// .option('-l, --latest ', 'path to the latest VS Code to test') +// .option('-s, --stable [file path]', 'path to the stable VS Code to be used in data migration tests'); + +// program.on('--help', () => { +// console.log(' Examples:'); +// console.log(''); +// console.log(' $ npm test -- --latest path/to/binary'); +// console.log(' $ npm test -- -l path/to/binary'); +// console.log(''); +// console.log(' $ npm test -- --latest path/to/latest/binary --stable path/to/stable/binary'); +// console.log(' $ npm test -- -l path/to/latest/binary -s path/to/stable/binary'); +// console.log(''); +// }); + +// program.parse(process.argv); + +function fail(errorMessage): void { + console.error(errorMessage); + process.exit(1); } +// if (!program.latest) { +// fail('You must specify the binary to run the smoke test against'); +// } +// if (!fs.existsSync(program.latest) || (program.stable && !fs.existsSync(program.stable))) { +// fail('The file path to electron binary does not exist or permissions do not allow to execute it. Please check the path provided.'); +// } +// if (parseInt(process.version.substr(1)) < 6) { +// fail('Please update your Node version to greater than 6 to run the smoke test.'); +// } + // Setting up environment variables -process.env.VSCODE_LATEST_PATH = program.latest; -if (program.stable) { - process.env.VSCODE_STABLE_PATH = program.stable; -} -process.env.SMOKETEST_REPO = testRepoLocalDir; -if (program.latest && (program.latest.indexOf('Code - Insiders') /* macOS/Windows */ || program.latest.indexOf('code-insiders') /* Linux */) >= 0) { - process.env.VSCODE_EDITION = 'insiders'; -} -process.env.VSCODE_WORKSPACE_PATH = codeWorkspacePath; -// Setting up 'vscode-smoketest-express' project -let os = process.platform.toString(); -if (os === 'darwin') { - os = 'osx'; -} -else if (os === 'win32') { - os = 'win'; -} -main().catch(err => console.error(err)); +const repoPath = path.join(__dirname, '..', '..', '..'); -async function main(): Promise { - await getKeybindings(`${keybindingsUrl}/doc.keybindings.${os}.json`, path.join(testDataPath, 'keybindings.json')); - - const workspace = { - id: (Date.now() + Math.round(Math.random() * 1000)).toString(), - folders: [ - toUri(path.join(testRepoLocalDir, 'public')), - toUri(path.join(testRepoLocalDir, 'routes')), - toUri(path.join(testRepoLocalDir, 'views')) - ] - }; - - await createWorkspaceFile(codeWorkspacePath, workspace); - await cleanOrClone(testRepoUrl, testRepoLocalDir); - cp.execSync('npm install', { cwd: testRepoLocalDir }); - await runTests(); +function getDevElectronPath() { + const buildPath = path.join(repoPath, '.build'); + const product = require(path.join(repoPath, 'product.json')); + + switch (process.platform) { + case 'darwin': + return path.join(buildPath, `${product.nameLong}.app`, 'Contents', 'MacOS', 'Electron'); + case 'linux': + return path.join(buildPath, 'electron', `${product.applicationName}`); + case 'win32': + return path.join(buildPath, 'electron', `${product.nameShort}.exe`); + } } -function fail(errorMessage): void { - console.error(errorMessage); - process.exit(1); +// TODO@joao: make this change +process.env.VSCODE_PATH = getDevElectronPath(); +process.env.VSCODE_REPOSITORY = repoPath; +process.env.VSCODE_DEV = '1'; +process.env.VSCODE_CLI = '1'; + +// if (program.stable) { +// process.env.VSCODE_STABLE_PATH = program.stable; +// } +process.env.SMOKETEST_REPO = testRepoLocalDir; +// if (program.latest && (program.latest.indexOf('Code - Insiders') /* macOS/Windows */ || program.latest.indexOf('code-insiders') /* Linux */) >= 0) { +// process.env.VSCODE_EDITION = 'insiders'; +// } +process.env.VSCODE_WORKSPACE_PATH = codeWorkspacePath; + +function getKeybindingPlatform(): string { + switch (process.platform) { + case 'darwin': return 'osx'; + case 'win32': return 'win'; + default: return process.platform; + } } function toUri(path: string): string { - if (os === 'win') { + if (process.platform === 'win32') { return `file:///${path.replace(/\\/g, '/')}`; } return `file://${path}`; } -function runTests(): void { - console.log('Running tests...'); - var proc = cp.spawn(process.execPath, [ - 'out/mocha-runner.js' - ]); - proc.stdout.on('data', data => { - console.log(data.toString()); - }); - proc.stderr.on('data', data => { - var date = new Date().toLocaleString(); - fs.appendFile(path.join(testDataPath, 'errors.log'), `${date}: ${data.toString()}`, (err) => { - if (err) { - throw new Error(`Could not write stderr to errors.log with the following error: ${err}`); - }; - }); - }); - proc.on('exit', (code) => { - process.exit(code); +async function main(): Promise { + const keybindingsUrl = `https://raw.githubusercontent.com/Microsoft/vscode-docs/master/scripts/keybindings/doc.keybindings.${getKeybindingPlatform()}.json`; + console.log(`Fetching keybindings from ${keybindingsUrl}...`); + + await new Promise((c, e) => { + https.get(keybindingsUrl, res => { + const output = fs.createWriteStream(path.join(testDataPath, 'keybindings.json')); + res.on('error', e); + output.on('error', e); + output.on('close', c); + res.pipe(output); + }).on('error', e); }); -} -async function cleanOrClone(repo: string, dir: string): Promise { - console.log('Cleaning or cloning test project repository...'); + if (!fs.existsSync(codeWorkspacePath)) { + console.log('Creating workspace file...'); + const workspace = { + id: (Date.now() + Math.round(Math.random() * 1000)).toString(), + folders: [ + toUri(path.join(testRepoLocalDir, 'public')), + toUri(path.join(testRepoLocalDir, 'routes')), + toUri(path.join(testRepoLocalDir, 'views')) + ] + }; + + fs.writeFileSync(codeWorkspacePath, JSON.stringify(workspace, null, '\t')); + } - if (!fs.existsSync(dir)) { - await gitClone(repo, dir); + if (!fs.existsSync(testRepoLocalDir)) { + console.log('Cloning test project repository...'); + cp.spawnSync('git', ['clone', testRepoUrl, testRepoLocalDir]); } else { - git.cwd(dir); - await new Promise((c, e) => git.fetch(err => err ? e(err) : c())); - await gitResetAndClean(); + console.log('Cleaning test project repository...'); + cp.spawnSync('git', ['fetch'], { cwd: testRepoLocalDir }); + cp.spawnSync('git', ['reset', '--hard', 'FETCH_HEAD'], { cwd: testRepoLocalDir }); + cp.spawnSync('git', ['clean', '-xdf'], { cwd: testRepoLocalDir }); } -} - -function gitClone(repo: string, dir: string): Promise { - return new Promise((res, rej) => { - git.clone(repo, dir, () => { - console.log('Test repository successfully cloned.'); - res(); - }); - }); -} -async function gitResetAndClean(): Promise { - await new Promise((c, e) => git.reset(['FETCH_HEAD', '--hard'], err => err ? e(err) : c())); - await new Promise((c, e) => git.clean('f', ['-d'], err => err ? e(err) : c())); - console.log('Test project was successfully reset to initial state.'); -} + console.log('Running npm install...'); + cp.execSync('npm install', { cwd: testRepoLocalDir, stdio: 'inherit' }); -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) { - reject(`Failed to obtain key bindings with response code: ${res.statusCode}`); - } - - var buffer: Buffer[] = []; - res.on('data', (chunk: Buffer) => buffer.push(chunk)); - res.on('end', () => { - fs.writeFile(location, Buffer.concat(buffer), 'utf8', () => { - console.log('Keybindings were successfully fetched.'); - resolve(); - }); - }); - }).on('error', (e) => { - reject(`Failed to obtain key bindings with an error: ${e}`); - }); - }); + console.log('Running tests...'); + const mocha = cp.spawnSync(process.execPath, ['out/mocha-runner.js'], { cwd: path.join(__dirname, '..'), stdio: 'inherit' }); + process.exit(mocha.status); } -function createWorkspaceFile(path: string, workspace: any): Promise { - console.log(`Creating workspace file at ${path}...`); - return new Promise((resolve, reject) => { - fs.exists(path, exists => { - if (exists) { - return resolve(); - } - - fs.writeFile(path, JSON.stringify(workspace, null, '\t'), error => { - if (error) { - reject(error); - } else { - resolve(); - } - }); - }); - }); -} \ No newline at end of file +main().catch(fail); \ No newline at end of file diff --git a/test/smoke/src/mocha-runner.ts b/test/smoke/src/mocha-runner.ts index a711a84ca0e825ce5aa9b120752064d91ddf73e3..fb7f0bb79fb03ebbbefbf1f04bf09f36184b7bb3 100644 --- a/test/smoke/src/mocha-runner.ts +++ b/test/smoke/src/mocha-runner.ts @@ -4,13 +4,14 @@ *--------------------------------------------------------------------------------------------*/ const MochaTest = require('mocha'); +const path = require('path'); const mochaTest = new MochaTest({ timeout: 60000, slow: 10000, useColors: true }); -mochaTest.addFile(require('path').join(process.cwd(), 'out/test.js')); +mochaTest.addFile(path.join(__dirname, 'test.js')); mochaTest.run((failures) => { process.exit(failures); }); \ No newline at end of file diff --git a/test/smoke/src/spectron/application.ts b/test/smoke/src/spectron/application.ts index b4effc57b722b1be695964bc9b8203a121b698d7..dad6d171269012ac7f2c0c9b20da7dc9c6cf34a6 100644 --- a/test/smoke/src/spectron/application.ts +++ b/test/smoke/src/spectron/application.ts @@ -9,7 +9,7 @@ import { Screenshot } from '../helpers/screenshot'; var fs = require('fs'); var path = require('path'); -export const LATEST_PATH = process.env.VSCODE_LATEST_PATH || ''; +export const LATEST_PATH = process.env.VSCODE_PATH || ''; export const STABLE_PATH = process.env.VSCODE_STABLE_PATH || ''; export const WORKSPACE_PATH = process.env.SMOKETEST_REPO || ''; export const CODE_WORKSPACE_PATH = process.env.VSCODE_WORKSPACE_PATH || ''; @@ -50,6 +50,11 @@ export class SpectronApplication { args.push(`--extensions-dir=${this.sampleExtensionsDir}`); } + const repo = process.env.VSCODE_REPOSITORY; + if (repo) { + args = [repo, ...args]; + } + this.spectron = new Application({ path: electronPath, args: args, @@ -96,7 +101,7 @@ export class SpectronApplication { } private retrieveKeybindings() { - fs.readFile(path.join(process.cwd(), `test_data/keybindings.json`), 'utf8', (err, data) => { + fs.readFile(path.join(__dirname, '../../test_data/keybindings.json'), 'utf8', (err, data) => { if (err) { throw err; } diff --git a/test_data/vscode-smoketest-express b/test_data/vscode-smoketest-express new file mode 160000 index 0000000000000000000000000000000000000000..5ac7ff24e8c4028e00d8b7d093a920db31513d2e --- /dev/null +++ b/test_data/vscode-smoketest-express @@ -0,0 +1 @@ +Subproject commit 5ac7ff24e8c4028e00d8b7d093a920db31513d2e