Skip to content

Commit

Permalink
Add CLI tests (#22)
Browse files Browse the repository at this point in the history
* tests: Verify exit code when running without parameters

Adds a lightweight testing framework, ospec.

* feat: Add GitHub Actions pipeline with test job

Runs on all non-deprecated GitHub runners for maximum portability
guarantees.

* docs: Explain how to test and how to disable testing

* tests: Verify error message when running without parameters
  • Loading branch information
l0b0 committed Dec 8, 2021
1 parent bb5769f commit ad26792
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
on:
push:
branches:
- master
pull_request:

jobs:
test:
strategy:
matrix:
runner: [
'macos-10.15',
'macos-11',
'macos-latest',
'ubuntu-18.04',
'ubuntu-20.04',
'ubuntu-latest',
'windows-2019',
'windows-2022',
'windows-latest',
]
node: [ '12', '14', '16', 'lts/*' ]
runs-on: ${{ matrix.runner }}
name: ${{ matrix.runner }} runner with Node.js ${{ matrix.node }}
steps:
- uses: actions/checkout@v2
- uses: actions/[email protected]
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm test
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ It only checks the contained items/collections, but not the other parts of the r
1. `git clone https://github.com/stac-utils/stac-node-validator` to clone the repo
2. `cd stac-node-validator` to switch into the new folder created by git
3. `npm install` to install dependencies
4. Run the commands as above, but replace `stac-node-validator` with `node bin/cli.js`, for example `node bin/cli.js /path/to/your/file.json`
4. Run the commands as above, but replace `stac-node-validator` with `node bin/cli.js`, for example `node bin/cli.js /path/to/your/file.json`

### Test

Simply run `npm test` in a working [development environment](#development).

If you want to disable tests for your fork of the repository, simply delete `.github/workflows/test.yaml`.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@
"jest-diff": "^26.6.2",
"klaw": "^3.0.0",
"minimist": "^1.2.5"
},
"devDependencies": {
"ospec": "^4.1.1"
},
"scripts": {
"test": "npx ospec"
}
}
25 changes: 25 additions & 0 deletions tests/test-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const o = require('ospec');

const app = require('..');

o.before(() => {
originalConsoleError = console.error;
originalExit = process.exit;
});

o('Should return exit code 1 when run without parameters', () => {
process.exit = o.spy();
app();
o(process.exit.args).deepEquals([1]);
});

o('Should print error message when run without parameters', () => {
console.error = o.spy();
app();
o(console.error.calls[0].args[0].message).equals('No path or URL specified.');
});

o.after(() => {
console.error = originalConsoleError
process.exit = originalExit;
});

0 comments on commit ad26792

Please sign in to comment.