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

Switch CLI to yargs #1109

merged 35 commits into from
Mar 27, 2019

Conversation

DanielMSchmidt
Copy link
Contributor

@DanielMSchmidt DanielMSchmidt commented Jan 6, 2019

Switch commander interface out with yargs, which is a more reliable interface and the following awesome features:

  • We can abstract common patterns (which config do I have, which platform am I supposed to run, etc) into nice middlewares, making the actual CLI code more human readable (To be done, if we agree that it makes sense)
  • We can test each CLI command, which is really nice to be sure we don't break anything while improving the user experience
  • It supports CLI plugins out of the box (which I think is cool because it allows third parties to make the CLI more powerful without writing their own wrappers with shell outs
  • Yargs allows bash completion, I will investigate how this works, it was not my top prio
  • Extendable configs from files and package.json like with babel presets.
  • We can write a lot of validation inside the yargs real already, making this outer bound of our system as rigorous as possible, so we don't have to check everything 100 times within the main code base.

Resolves #1132.
Addresses #1238.

@DanielMSchmidt DanielMSchmidt force-pushed the danielmschmidt/move-to-yargs branch 2 times, most recently from cbe9916 to 3fcd99c Compare January 13, 2019 18:01
@DanielMSchmidt
Copy link
Contributor Author

@rotemmiz This PR is done now. It enables users to use the detox cli via environment variables, over the package.json and via configuration file.

The CI seems to be flaky, so 🤷‍♂️

@@ -0,0 +1,26 @@
describe('build-framework-cache', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these kind of tests have not a lot of meaning, it just reimplements the production code, and checks that they are the same. I have a feeling we can give build-framework-cache.test.js completely up.

@@ -0,0 +1,115 @@
describe('build', () => {
it('shows help text', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably drop shows help text test, what does it check?

mockPackageJson({
configurations: {
only: {
build: 'echo "I was build"'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the sake of readability, can you change the strings to corespond to the config names?

}
}
});
const mockExec = jest.fn();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can

const mockExec= jest.fn();
 jest.mock('child_process', () => ({
      execSync: mockExec
    }));

be moved to the top of the suite? you add it in beforeEach

it('shows help text', async () => {
jest.spyOn(process, 'exit'); // otherwise tests are aborted

expect(await callCli('./clean-framework-cache', '--help')).toMatchInlineSnapshot(`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, meaningless test

'artifacts-location': {
alias: ['a', 'artifactsLocation'],
group: 'Debugging',
describe: '[EXPERIMENTAL] Artifacts (logs, screenshots, etc) root directory.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it is not experimental anymore, if you can drop the [EXPERIMENTAL] text across the artifacts args, it would be super


clearDeviceRegistryLockFile();

if (!program.configuration) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many validation/assigning steps here, does yargs have a custom processing functionality for args? if it does, it should probably go in there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does through middlewares, but I would suggest to move it into a separate PR :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great!!

}

function runMocha() {
const loglevel = program.loglevel ? `--loglevel ${program.loglevel}` : '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do something automatic with this section? I hate adding a new param every time its being added to cli api.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♂️ Not that I'm aware of, maybe we should have a map between our flags and mocha or jest flags.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still hate the fact that mocha has no mechanism like jest for having environment. This would be way more natural from a jest perspective than the wrapping that we currently do

detox/local-cli/test.test.js Show resolved Hide resolved
@@ -34,6 +34,53 @@ detox [options] [command]
| --- | --- |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

@DanielMSchmidt
Copy link
Contributor Author

Also to be done: #1083 (comment)

@rotemmiz rotemmiz mentioned this pull request Jan 25, 2019
2 tasks
@DanielMSchmidt DanielMSchmidt force-pushed the danielmschmidt/move-to-yargs branch from b7b726c to b189264 Compare January 29, 2019 21:54
@DanielMSchmidt DanielMSchmidt force-pushed the danielmschmidt/move-to-yargs branch from b189264 to 5ff5dfd Compare January 29, 2019 22:00
@DanielMSchmidt
Copy link
Contributor Author

@rotemmiz This is ready for review now

Copy link
Member

@rotemmiz rotemmiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Mr Schmidt!
There are a few minor issues regarding cleanup and location of new files, please see my notes.

detox/setupJest.js Outdated Show resolved Hide resolved
docs/APIRef.DetoxCLI.md Outdated Show resolved Hide resolved
detox/package.json Show resolved Hide resolved
detox/cli.js Outdated
@@ -0,0 +1,23 @@
#!/usr/bin/env node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you prefer putting this here and not inside /local-cli ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can not put . as commandDir and I did not want to have this extra level of nesting 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

-  "detox": "local-cli/detox.js"
+  "detox": "cli.js"

This is a part of the changes you made in this PR, why can't it be reverted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this part in my changes. At least as far as I understand it. Feel free to try it, though.

console.log("init handler", argv)
switch (argv.runner) {
case "mocha":
createMochaFolderE2E();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please indent the code


clearDeviceRegistryLockFile();

if (!program.configuration) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great!!

@DanielMSchmidt
Copy link
Contributor Author

@rotemmiz Answered and fixed everything 💪

@rotemmiz
Copy link
Member

rotemmiz commented Feb 7, 2019

@DanielMSchmidt I moved cli.js to local-cli.js.
I am still having a weird issue I can not trace:
running detox test --configuration ios.sim.release results in

node_modules/.bin/mocha e2e --opts e2e/mocha.opts --configuration ios.sim.release  --no-colors    --grep :android: --invert  --record-logs none --take-screenshots none --record-videos none --artifacts-location "artifacts/ios.sim.release.2019-02-07 20-17-13Z"  --testRunner mocha --configurations [object Object]

I couldn't find where --configurations is being added to program. The only place it exists in is:

const config = require(path.join(process.cwd(), 'package.json')).detox;

But I can't see how that is applied into program.
Your help is appreciated here :)

@DanielMSchmidt
Copy link
Contributor Author

@rotemmiz I think the problem is that we don't successfully filter out the options passed via CLI, they just get passed through as it seems

@rotemmiz
Copy link
Member

rotemmiz commented Feb 7, 2019

The issue seems bigger than the purposed fix in a0582e3.
Currently --testRunner is also leaked into collectExtraArgs.
Seems like these are all parameter of the detox config from package.json. I couldn't find where this is injected, but it probably should be filtered from collectExtraArgs as well.

@DanielMSchmidt
Copy link
Contributor Author

@rotemmiz Ah yeah, that explains why I did not catch it in the very first moment, when this feature is not enabled, interesting 🤔 I'll think a bit about how to fix this, I mean we basically would also need to be carful about all other input options, so just filtering them out might not be the best option

@rotemmiz
Copy link
Member

rotemmiz commented Feb 7, 2019

Both confiurations and test-runner/testRunner are not CLI args, they are configuration parameters, they are somehow leaked into program.

@DanielMSchmidt
Copy link
Contributor Author

@rotemmiz I think I got it, at least theoretically. We could create a new instance of a yargs program without any configuration within collectExtraArgs, then it should work just fine.

@noomorph noomorph mentioned this pull request Mar 22, 2019
4 tasks
@noomorph noomorph mentioned this pull request Mar 27, 2019
13 tasks
@noomorph noomorph merged commit 9f4cb01 into master Mar 27, 2019
@noomorph noomorph deleted the danielmschmidt/move-to-yargs branch March 27, 2019 08:10
@lock lock bot locked as resolved and limited conversation to collaborators Mar 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants