-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #6409: api integration testing setup
Signed-off-by: Anton Kosyakov <[email protected]>
- Loading branch information
Showing
47 changed files
with
1,306 additions
and
1,805 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
--require reflect-metadata/Reflect | ||
--reporter spec | ||
--watch-extensions js | ||
--exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import * as path from 'path'; | ||
import * as webpack from 'webpack'; | ||
// tslint:disable:no-implicit-dependencies | ||
import { RawSourceMap } from 'source-map'; | ||
import { ApplicationPackage } from '@theia/application-package/lib/application-package'; | ||
|
||
const modulePackages: { dir: string, name?: string }[] = []; | ||
for (const extensionPackage of new ApplicationPackage({ projectPath: process.cwd() }).extensionPackages) { | ||
modulePackages.push({ | ||
name: extensionPackage.name, | ||
dir: path.dirname(extensionPackage.raw.installed!.packagePath) | ||
}); | ||
} | ||
|
||
function exposeModule(modulePackage: { dir: string, name?: string }, resourcePath: string, source: string): string { | ||
if (!modulePackage.name) { | ||
return source; | ||
} | ||
const { dir, name } = path.parse(resourcePath); | ||
let moduleName = path.join(modulePackage.name, dir.substring(modulePackage.dir.length)); | ||
if (name !== 'index') { | ||
moduleName = path.join(moduleName, name); | ||
} | ||
if (path.sep !== '/') { | ||
moduleName = moduleName.split(path.sep).join('/'); | ||
} | ||
return source + `\nif (!global) global = {};\n(global['theia'] = global['theia'] || {})['${moduleName}'] = this;\n`; | ||
} | ||
|
||
export = function (this: webpack.loader.LoaderContext, source: string, sourceMap?: RawSourceMap): string | undefined { | ||
if (this.cacheable) { | ||
this.cacheable(); | ||
} | ||
|
||
let modulePackage = modulePackages.find(({ dir }) => this.resourcePath.startsWith(dir)); | ||
if (modulePackage) { | ||
this.callback(undefined, exposeModule(modulePackage, this.resourcePath, source), sourceMap); | ||
return; | ||
} | ||
const index = this.resourcePath.lastIndexOf('/node_modules'); | ||
if (index !== -1) { | ||
const nodeModulesPath = this.resourcePath.substring(0, index + '/node_modules'.length); | ||
let dir = this.resourcePath; | ||
while ((dir = path.dirname(dir)) !== nodeModulesPath) { | ||
try { | ||
const { name } = require(path.join(dir, 'package.json')); | ||
modulePackage = { name, dir }; | ||
modulePackages.push(modulePackage); | ||
this.callback(undefined, exposeModule(modulePackage, this.resourcePath, source), sourceMap); | ||
return; | ||
} catch { | ||
/** no-op */ | ||
} | ||
} | ||
} | ||
this.callback(undefined, source, sourceMap); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
// tslint:disable:no-any | ||
|
||
import * as net from 'net'; | ||
import * as puppeteer from 'puppeteer'; | ||
const collectFiles = require('mocha/lib/cli/collect-files'); | ||
|
||
export interface TestOptions { | ||
start: () => Promise<net.AddressInfo>; | ||
launch?: puppeteer.LaunchOptions | ||
files: { | ||
ignore: string[] | ||
extension: string[] | ||
file: string[] | ||
recursive: boolean | ||
sort: boolean | ||
spec: string[] | ||
} | ||
} | ||
|
||
export default async function runTest(options: TestOptions): Promise<void> { | ||
const { start, launch, files: fileOptions } = options; | ||
const exit = !(launch && launch.devtools); | ||
|
||
// quick check whether test files exist | ||
collectFiles(fileOptions); | ||
|
||
const server = await start(); | ||
const browser = await puppeteer.launch(launch); | ||
|
||
const page = await browser.newPage(); | ||
page.on('dialog', dialog => dialog.dismiss()); | ||
page.on('pageerror', console.error); | ||
|
||
let theiaLoaded = false; | ||
page.exposeFunction('fireDidUnloadTheia', () => theiaLoaded = false); | ||
const preLoad = () => { | ||
if (theiaLoaded) { | ||
return; | ||
} | ||
console.log('loading chai...'); | ||
theiaLoaded = true; | ||
page.addScriptTag({ path: require.resolve('chai/chai.js') }); | ||
page.evaluate(() => | ||
window.addEventListener('beforeunload', () => (window as any)['fireDidUnloadTheia']()) | ||
); | ||
}; | ||
page.on('frameattached', preLoad); | ||
page.on('framenavigated', preLoad); | ||
|
||
page.on('load', async () => { | ||
console.log('loading mocha...'); | ||
// replace console.log by theia logger for mocha | ||
await page.waitForFunction(() => !!(window as any)['theia']['@theia/core/lib/common/logger'].logger, { | ||
timeout: 30 * 1000 | ||
}); | ||
await page.addScriptTag({ path: require.resolve('mocha/mocha.js') }); | ||
await page.waitForFunction(() => !!(window as any)['chai'] && !!(window as any)['mocha'] && !!(window as any)['theia'].container, { timeout: 30 * 1000 }); | ||
|
||
console.log('loading Theia...'); | ||
await page.evaluate(() => { | ||
const { FrontendApplicationStateService } = (window as any)['theia']['@theia/core/lib/browser/frontend-application-state']; | ||
const { PreferenceService } = (window as any)['theia']['@theia/core/lib/browser/preferences/preference-service']; | ||
const { WorkspaceService } = (window as any)['theia']['@theia/workspace/lib/browser/workspace-service']; | ||
|
||
const container = (window as any)['theia'].container; | ||
const frontendApplicationState = container.get(FrontendApplicationStateService); | ||
const preferenceService = container.get(PreferenceService); | ||
const workspaceService = container.get(WorkspaceService); | ||
|
||
return Promise.all([ | ||
frontendApplicationState.reachedState('ready'), | ||
preferenceService.ready, | ||
workspaceService.roots | ||
]); | ||
}); | ||
|
||
console.log('loading test files...'); | ||
await page.evaluate(() => { | ||
// replace require to load modules from theia namespace | ||
(window as any)['require'] = (moduleName: string) => (window as any)['theia'][moduleName]; | ||
mocha.setup({ | ||
reporter: 'spec', | ||
ui: 'bdd', | ||
useColors: true | ||
}); | ||
}); | ||
const files = collectFiles(fileOptions); | ||
for (const file of files) { | ||
await page.addScriptTag({ path: file }); | ||
} | ||
|
||
console.log('running test files...'); | ||
const failures = await page.evaluate(() => | ||
new Promise<number>(resolve => mocha.run(resolve)) | ||
); | ||
if (exit) { | ||
await page.close(); | ||
process.exit(failures > 0 ? 1 : 0); | ||
} | ||
}); | ||
page.goto(`http://${server.address}:${server.port}`); | ||
} |
Oops, something went wrong.