feat: cli exit code matches test, bisect status

This commit is contained in:
Charles Kerr 2021-07-28 21:40:28 -05:00
Родитель 935a34a98d
Коммит d2822cee7c
6 изменённых файлов: 51 добавлений и 49 удалений

Просмотреть файл

@ -45,7 +45,7 @@ Done in 28.19s.
import { Runner } from 'electron-fiddle-runner';
const runner = await Runner.create();
const { status } = await runner.test(versionString, '/path/to/fiddle');
const { status } = await runner.run(versionString, '/path/to/fiddle');
console.log(status);
```
@ -57,13 +57,17 @@ import { Runner } from 'electron-fiddle-runner';
const runner = await Runner.create();
// use a specific Electron version to run code from a local folder
const result = await runner.test('13.1.7', '/path/to/fiddle');
const result = await runner.run('13.1.7', '/path/to/fiddle');
// use a specific Electron version to run code from a github gist
const result = await runner.test('14.0.0-beta.17', '642fa8daaebea6044c9079e3f8a46390');
const result = await runner.run('14.0.0-beta.17', '642fa8daaebea6044c9079e3f8a46390');
// use a specific Electron version to run code from a git repo
const result = await runner.test('15.0.0-alpha.1', 'https://github.com/my/repo.git');
const result = await runner.run('15.0.0-alpha.1', 'https://github.com/my/repo.git');
// use a specific Electron version to run code from iterable filename/content pairs
const files = new Map<string, string>([['main.js', '"use strict";']]);
const result = await runner.run('15.0.0-alpha.1', files);
// bisect a regression test across a range of Electron versions
const result = await runner.bisect('10.0.0', '13.1.7', path_or_gist_or_git_repo);
@ -84,16 +88,16 @@ electron.on('removed', (version) => console.log(`Removed "${version}"`));
// download a version of electron
await electron.ensureDownloaded('12.0.15');
// expect(await electron.isDownloaded('12.0.15')).toBe(true);
// expect(await electron.downloaded().map((semver) => semver.version)).toContain('12.0.5');
// expect(await electron.downloaded()).toContain('12.0.5');
// remove a download
await electron.remove('12.0.15');
// expect(await electron.isDownloaded('12.0.15')).toBe(false);
// expect(await electron.downloaded().map((semver) => semver.version)).not.toContain('12.0.5');
// expect(await electron.downloadedVersions()).not.toContain('12.0.5');
// install a specific version for the runner to use
const exec = await electron.install('11.4.10');
// expect(await electron.isDownloaded('11.4.10')).toBe(true);
// expect(fs.accessSync(exec, fs.constants.X_OK)).toBe(true);
```
### Versions
@ -145,7 +149,7 @@ const result = await runner.spawnSync('12.0.0', fiddle, nodeSpawnSyncOpts);
// third argument is same as node.spawn()'s opts
const child = await runner.spawn('12.0.1', fiddle, nodeSpawnOpts);
// see also `Runner.test()` and `Runner.bisect()` above
// see also `Runner.run()` and `Runner.bisect()` above
```
### Using Local Builds
@ -154,7 +158,7 @@ const child = await runner.spawn('12.0.1', fiddle, nodeSpawnOpts);
import { Runner } from 'electron-fiddle-runner';
const runner = await Runner.create();
const testResult = await runner.test('/path/to/electron/build', fiddle);
const result = await runner.run('/path/to/electron/build', fiddle);
```
### Using Custom Paths

Просмотреть файл

@ -17,7 +17,7 @@
"lint:prettier": "prettier --check package.json src/**/*.ts",
"lint:prettier:fix": "prettier --write package.json src/**/*.ts",
"make": "run-p build",
"start": "node lib/cli.js",
"start": "node lib/index.js",
"test": "jest",
"test:ci": "jest --runInBand --coverage"
},

Просмотреть файл

@ -52,23 +52,20 @@ export async function runFromCommandLine(argv: string[]): Promise<void> {
process.exit(1);
}
if (cmd === 'test') {
if (versionArgs.length === 1) {
await runner.test(versionArgs[0], fiddle);
} else {
console.error(
`Test must use one Electron version. Got: ${versionArgs.join(', ')}`,
);
process.exit(1);
}
} else if (cmd === 'bisect') {
if (versionArgs.length === 2) {
await runner.bisect(versionArgs[0], versionArgs[1], fiddle);
} else {
console.error(
`Bisect must use two Electron versions. Got: ${versionArgs.join(', ')}`,
);
process.exit(1);
}
if (cmd === 'test' && versionArgs.length === 1) {
const result = await runner.run(versionArgs[0], fiddle);
const vals = ['test_passed', 'test_failed', 'test_error', 'system_error'];
process.exitCode = vals.indexOf(result.status);
return;
}
if (cmd === 'bisect' && versionArgs.length === 2) {
const result = await runner.bisect(versionArgs[0], versionArgs[1], fiddle);
const vals = ['bisect_succeeded', 'test_error', 'system_error'];
process.exitCode = vals.indexOf(result.status);
return;
}
console.error(`Invalid parameters. Got ${cmd}, ${versionArgs.join(', ')}`);
process.exit(1);
}

Просмотреть файл

@ -25,6 +25,9 @@ function getZipName(version: string): string {
type ProgressObject = { percent: number };
/**
* Manage downloading and installation of Electron versions for use with Runner.
*/
export class Electron extends EventEmitter {
private readonly paths: Paths;
@ -39,7 +42,7 @@ export class Electron extends EventEmitter {
this.emit('removed', version);
}
public async installed(): Promise<string | undefined> {
public async installedVersion(): Promise<string | undefined> {
try {
const versionFile = path.join(this.paths.electronInstall, 'version');
return await fs.readFile(versionFile, 'utf8');
@ -53,7 +56,7 @@ export class Electron extends EventEmitter {
return fs.existsSync(zip);
}
public async downloaded(): Promise<string[]> {
public async downloadedVersions(): Promise<string[]> {
const version = 'fnord';
const test = getZipName(version);
const prefix = test.substring(0, test.indexOf(version));
@ -74,7 +77,7 @@ export class Electron extends EventEmitter {
const getProgressCallback = (progress: ProgressObject) => {
const pct = Math.round(progress.percent * 100);
if (pctDone + 10 <= pct) {
console.log(`${pct >= 100 ? '🏁' : '⏳'} downloaded ${pct}%`);
console.log(`${pct >= 100 ? '🏁' : '⏳'} downloading ${version} - ${pct}%`);
pctDone = pct;
}
};
@ -129,7 +132,7 @@ export class Electron extends EventEmitter {
const { electronInstall } = this.paths;
// see if the current version (if any) is already `version`
const currentVersion = await this.installed();
const currentVersion = await this.installedVersion();
if (currentVersion === version) {
d(`already installed`);
} else {

Просмотреть файл

@ -3,6 +3,7 @@ import * as path from 'path';
import debug from 'debug';
import simpleGit from 'simple-git';
import { createHash } from 'crypto';
import { inspect } from 'util';
import { DefaultPaths } from './paths';
@ -14,11 +15,17 @@ function hashString(str: string): string {
export class Fiddle {
constructor(
public readonly mainPath: string,
public readonly mainPath: string, // /path/to/main.js
public readonly source: string,
) {}
}
/**
* - Iterable<string, string> - filename-to-content key/value pairs
* - string of form '/path/to/fiddle' - a fiddle on the filesystem
* - string of form 'https://github.com/my/repo.git' - a git repo fiddle
* - string of form '642fa8daaebea6044c9079e3f8a46390' - a github gist fiddle
*/
export type FiddleSource = Fiddle | string | Iterable<[string, string]>;
export class FiddleFactory {
@ -37,10 +44,7 @@ export class FiddleFactory {
await fs.remove(folder);
await fs.copy(source, folder);
return {
mainPath: path.join(folder, 'main.js'),
source,
};
return new Fiddle(path.join(folder, 'main.js'), source);
}
public async fromRepo(url: string, checkout = 'master'): Promise<Fiddle> {
@ -59,10 +63,7 @@ export class FiddleFactory {
await git.checkout(checkout);
await git.pull('origin', checkout);
return {
mainPath: path.join(folder, 'main.js'),
source: url,
};
return new Fiddle(path.join(folder, 'main.js'), url);
}
public async fromEntries(src: Iterable<[string, string]>): Promise<Fiddle> {
@ -80,10 +81,7 @@ export class FiddleFactory {
}
await Promise.all(promises);
return {
mainPath: path.join(folder, 'main.js'),
source: 'entries',
};
return new Fiddle(path.join(folder, 'main.js'), 'entries');
}
public async create(src: FiddleSource): Promise<Fiddle | undefined> {

Просмотреть файл

@ -49,7 +49,7 @@ export class Runner {
paths?: Partial<Paths>;
versions?: Versions;
}): Promise<Runner> {
const paths = { ...DefaultPaths, ...(opts.paths || {}) };
const paths = Object.freeze({ ...DefaultPaths, ...(opts.paths || {}) });
const electron = opts.electron || new Electron(paths);
const versions = opts.versions || (await ElectronVersions.create(paths));
const factory = opts.fiddleFactory || new FiddleFactory(paths.fiddles);
@ -71,7 +71,7 @@ export class Runner {
'',
` - date: ${new Date().toISOString()}`,
'',
` - fiddle:`,
' - fiddle:',
` - source: ${fiddle.source}`,
` - local copy: ${path.dirname(fiddle.mainPath)}`,
'',
@ -191,7 +191,7 @@ export class Runner {
}
}
public async test(
public async run(
version: string | SemVer,
fiddle: FiddleSource,
out = process.stdout,
@ -248,7 +248,7 @@ export class Runner {
testOrder.push(mid);
log(`bisecting, range [${left}..${right}], mid ${mid} (${ver.version})`);
result = await this.test(ver.version, fiddle, out);
result = await this.run(ver.version, fiddle, out);
results[mid] = result;
log(`${Runner.displayResult(result)} ${versions[mid].version}\n`);