Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch CLI to yargs #1109

Merged
merged 35 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
aa6bbbd
move from commander to yargs
Dec 26, 2018
b3aac24
enable tests for local cli
Dec 27, 2018
3437723
write tests for local-cli
Dec 27, 2018
8327903
refactor test helpers into jest setup
Jan 6, 2019
99254f7
add tests to test command
Jan 13, 2019
848b582
allow environment variables to set argument values
Jan 13, 2019
c19dca9
allow package.json to configure command line flags
Jan 13, 2019
92a6f14
allow file to configure flags
Jan 13, 2019
d8e9d23
remove unnecessary tests
Jan 29, 2019
461a13c
remove EXPERIMENTAL note
Jan 29, 2019
9c401ca
refactor tests
Jan 29, 2019
5ff5dfd
quote test folder path
Jan 29, 2019
d1fb062
document order of precedent for CLI
Feb 3, 2019
edb2f13
move setup jest to test dir
Feb 3, 2019
9fd7189
remove commander
Feb 3, 2019
27350bb
indent code
Feb 3, 2019
5a28f11
moved cli.js to local-cli dir + minor text changes
rotemmiz Feb 7, 2019
a0582e3
blacklist configurations
Feb 7, 2019
accf54e
Merge branch 'master' into danielmschmidt/move-to-yargs
noomorph Mar 14, 2019
d51d4ec
Merge branch 'master' into danielmschmidt/move-to-yargs
noomorph Mar 14, 2019
8836aee
test: update local-cli test
noomorph Mar 15, 2019
8d535e8
Merge PR #1133 functionality
noomorph Mar 15, 2019
950b595
code: refine cli arg description
noomorph Mar 19, 2019
4228446
fix: bringing backward-compatibility
noomorph Mar 20, 2019
cbe2a76
ci: ignore __tests__ coverage
noomorph Mar 20, 2019
8f5b654
finalized deprecations, updated docs, fixed Windows issues, simplifie…
noomorph Mar 22, 2019
e28441f
fix: broken link on CI
noomorph Mar 22, 2019
00d4d42
fix: documentation and scripts per migration guide
noomorph Mar 26, 2019
555ab79
Merge branch 'master' into danielmschmidt/move-to-yargs
noomorph Mar 26, 2019
715b672
code: fixed phrasing
noomorph Mar 26, 2019
f30774f
docs: fix wording
noomorph Mar 26, 2019
0bb6fbb
docs: fix formatting in migration guide
noomorph Mar 26, 2019
6f6483b
docs: markdown formatting fix
noomorph Mar 26, 2019
9e00d46
bump ci
noomorph Mar 26, 2019
cba0a4c
bump ci
noomorph Mar 27, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions detox/__tests__/setupJest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const yargs = require('yargs');
const path = require('path');

function callCli(modulePath, cmd) {
return new Promise((resolve, reject) => {
try {
yargs
.scriptName('detox')
.command(require(path.join(__dirname, "../local-cli", modulePath)))
.exitProcess(false)
.fail((msg, err) => reject(err || msg))
.parse(cmd, (err, argv, output) => {
err ? reject(err) : setImmediate(() => resolve(output));
});
} catch (e) {
reject(e);
}
});
}

function mockPackageJson(mockContent) {
jest.mock(path.join(process.cwd(), 'package.json'), () => ({
detox: mockContent
}));
}

global.mockPackageJson = mockPackageJson;
global.callCli = callCli;
13 changes: 13 additions & 0 deletions detox/local-cli/__snapshots__/build.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`build fails with multiple configs if none is selected 1`] = `
"Cannot determine which configuration to use. Use --configuration to choose one of the following:
* only
* myconf"
`;

exports[`build fails without build script 1`] = `"Could not find build script in detox.configurations[\\"only\\"].build"`;

exports[`build fails without build script and configuration 1`] = `"Could not find build script in detox.configurations[\\"only\\"].build"`;

exports[`build fails without configurations 1`] = `"There are no \\"configurations\\" in \\"detox\\" section of package.json"`;
7 changes: 7 additions & 0 deletions detox/local-cli/__snapshots__/run-server.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`run-server throws if the port number is out of range 1`] = `"The port should be between 1 and 65535, got NaN"`;

exports[`run-server throws if the port number is out of range 2`] = `"The port should be between 1 and 65535, got 0"`;

exports[`run-server throws if the port number is out of range 3`] = `"The port should be between 1 and 65535, got 100000"`;
3 changes: 3 additions & 0 deletions detox/local-cli/__snapshots__/test.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test fails with a different runner 1`] = `"ava is not supported in detox cli tools. You can still run your tests with the runner's own cli tool"`;
10 changes: 10 additions & 0 deletions detox/local-cli/build-framework-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');
const cp = require('child_process');
const log = require('../src/utils/logger').child({ __filename });

module.exports.command = 'build-framework-cache';
module.exports.desc = 'MacOS only. Build Detox.framework to ~/Library/Detox. The framework cache is specific for each combination of Xcode and Detox versions';

module.exports.handler = async function buildFrameworkCache() {
cp.execSync(path.join(__dirname, '../scripts/build_framework.ios.sh'), {stdio: 'inherit'});
};
27 changes: 27 additions & 0 deletions detox/local-cli/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const _ = require('lodash');
const cp = require('child_process');
const log = require('../src/utils/logger').child({ __filename });
const {getDefaultConfiguration, getConfigurationByKey} = require('./utils/configurationUtils');

module.exports.command = 'build';
module.exports.desc = "Convenience method. Run the command defined in 'build' property of the specified configuration.";
module.exports.builder = {
c: {
alias: 'configuration',
group: 'Configuration:',
describe:
"Select a device configuration from your defined configurations, if not supplied, and there's only one configuration, detox will default to it",
default: getDefaultConfiguration(),
}
};

module.exports.handler = async function build(argv) {
const buildScript = getConfigurationByKey(argv.configuration).build;

if (buildScript) {
log.info(buildScript);
cp.execSync(buildScript, { stdio: 'inherit' });
} else {
throw new Error(`Could not find build script in detox.configurations["${argv.configuration}"].build`);
}
};
85 changes: 85 additions & 0 deletions detox/local-cli/build.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
jest.mock('../src/utils/logger');

describe('build', () => {
let mockExec;
beforeEach(() => {
mockExec = jest.fn();
jest.mock('child_process', () => ({
execSync: mockExec
}));
});

it('runs the build script if there is only one config', async () => {
mockPackageJson({
configurations: {
only: {
build: 'echo "only"'
}
}
});

await callCli('./build', 'build');
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('only'), expect.anything());
});

it('runs the build script of selected config', async () => {
mockPackageJson({
configurations: {
only: {
build: 'echo "only"'
},
myconf: {
build: 'echo "myconf"'
}
}
});

await callCli('./build', 'build -c myconf');
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('myconf'), expect.anything());
});

it('fails with multiple configs if none is selected', async () => {
mockPackageJson({
configurations: {
only: {
build: 'echo "only"'
},
myconf: {
build: 'echo "myconf"'
}
}
});

await expect(callCli('./build', 'build')).rejects.toThrowErrorMatchingSnapshot();
expect(mockExec).not.toHaveBeenCalled();
});

it('fails without configurations', async () => {
mockPackageJson({});

await expect(callCli('./build', 'build')).rejects.toThrowErrorMatchingSnapshot();
expect(mockExec).not.toHaveBeenCalled();
});

it('fails without build script', async () => {
mockPackageJson({
configurations: {
only: {}
}
});

await expect(callCli('./build', 'build -c only')).rejects.toThrowErrorMatchingSnapshot();
expect(mockExec).not.toHaveBeenCalled();
});

it('fails without build script and configuration', async () => {
mockPackageJson({
configurations: {
only: {}
}
});

await expect(callCli('./build', 'build')).rejects.toThrowErrorMatchingSnapshot();
expect(mockExec).not.toHaveBeenCalled();
});
});
17 changes: 17 additions & 0 deletions detox/local-cli/clean-framework-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const log = require('../src/utils/logger').child({ __filename });

module.exports.command = 'clean-framework-cache';
module.exports.desc = "MacOS only. Delete all compiled framework binaries from ~/Library/Detox, they will be rebuilt on 'npm install' or when running 'build-framework-cache'";

module.exports.handler = async function cleanFrameworkCache() {
if (os.platform() === 'darwin') {
const frameworkPath = path.join(os.homedir(), '/Library/Detox');
log.info(`Removing framework binaries from ${frameworkPath}`);
await fs.remove(frameworkPath);
} else {
log.info(`The command is supported only on MacOS, skipping the execution.`);
}
};
28 changes: 28 additions & 0 deletions detox/local-cli/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node
const yargs = require('yargs');
const logger = require('../src/utils/logger').child({ __filename });

yargs
.scriptName('detox')
.parserConfiguration({
'boolean-negation': false,
'dot-notation': false,
'duplicate-arguments-array': false,
})
.commandDir('./', {
exclude: function(path) {
// This is a test file
return /\.test\.js$/.test(path);
}
})
.demandCommand()
.recommendCommands()
.help()
.wrap(yargs.terminalWidth() * 0.9)
.fail((_msg, err) => {
const lines = (err ? err.toString() : msg).split('\n');
for (const line of lines) {
logger.error(line);
}
})
.parse();
6 changes: 0 additions & 6 deletions detox/local-cli/detox-build-framework-cache.js

This file was deleted.

29 changes: 0 additions & 29 deletions detox/local-cli/detox-build.js

This file was deleted.

15 changes: 0 additions & 15 deletions detox/local-cli/detox-clean-framework-cache.js

This file was deleted.

119 changes: 0 additions & 119 deletions detox/local-cli/detox-init.js

This file was deleted.

Loading