-
Notifications
You must be signed in to change notification settings - Fork 76
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
cli: add simple integration tests #937
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const { execSync } = require('node:child_process'); | ||
const uuid = require('uuid').v4; | ||
|
||
describe('cli', () => { | ||
it('should return status code 1 if no command is issued', () => { | ||
let thrown = false; // pattern from test/unit/util/crypto.js | ||
try { | ||
// eslint-disable-next-line no-use-before-define | ||
cli('', { stdio: ['pipe', 'pipe', 'ignore'] }); | ||
} catch (err) { | ||
err.status.should.equal(1); | ||
thrown = true; | ||
} | ||
thrown.should.equal(true); | ||
}); | ||
|
||
it('should return status code 1 if non-existent command is issued', () => { | ||
let thrown = false; // pattern from test/unit/util/crypto.js | ||
try { | ||
// eslint-disable-next-line no-use-before-define | ||
cli('user-congratulate', { stdio: ['pipe', 'pipe', 'ignore'] }); | ||
} catch (err) { | ||
err.status.should.equal(1); | ||
thrown = true; | ||
} | ||
thrown.should.equal(true); | ||
}); | ||
|
||
describe('command: user-create', () => { | ||
it('should return status code 0 and user details on success', () => { | ||
// eslint-disable-next-line no-use-before-define | ||
const email = randomEmail(); | ||
|
||
// eslint-disable-next-line no-use-before-define | ||
const [, , js] = cli(`--email ${email} user-create`, { input: 'strong-password-101' }) | ||
.match(/^([^{]*)(.*)$/ms); | ||
|
||
js.should.match(new RegExp(`email: '${email}',`)); | ||
js.should.not.match(/password/); | ||
}); | ||
}); | ||
}); | ||
|
||
function cli(argString, opts) { | ||
return execSync( | ||
'node lib/bin/cli.js ' + argString, | ||
{ encoding: 'utf-8', ...opts }, | ||
); | ||
} | ||
|
||
function randomEmail() { | ||
return uuid() + '@example.com'; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels unclear to me what should be tested in this file vs. test/integration/task/account.js. It makes sense that this file is testing status codes. For testing output, my instinct is that that should be done in test/integration/task/account.js along with the other tests related to the behavior of each task.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an integration test for
lib/bin/cli.js
. I'd be surprised to find it intest/integration/task/account.js
. Perhapscli.js
is named misleading though, as it only deals with accounts(?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's right that cli.js only deals with accounts. It may have been named the way it was with the thought that it would eventually do more things. In the user docs, we mention an
odk-cmd
, which just calls cli.js I think. As far as I know, the only other script that users run directly is restore.js for restoring a backup (user docs).