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

Resolve issue #91 - CLI arguments and local scanner no more supported #96

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ async function scan(params, cliArgs = [], localScanner = false) {
}

function scanWithCallback(params, cliArgs, localScanner, callback) {
scan(params).then(() => {
// here we make the code unit-testable - i.e. by making the scan property stub-able
// this is not nice - and anyway when we move to ESM it won't work anymore because the module will be read-only -
// but for now, it allows us to unit test the module
module.exports.scan(params, cliArgs, localScanner).then(() => {
callback();
});
}
Expand Down
62 changes: 57 additions & 5 deletions test/unit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,63 @@

const assert = require('assert');
const index = require('../../src/index');
const { spy, stub, restore } = require('sinon');

describe('fromParam', function () {
it('should provide the correct identity', function () {
assert.deepEqual(index.fromParam(), [
'--from=ScannerNpm/' + require('../../package.json').version,
]);
describe('index', function () {
afterEach(restore);

describe('::fromParam', () => {
it('should provide the correct identity', function () {
assert.deepEqual(index.fromParam(), [
'--from=ScannerNpm/' + require('../../package.json').version,
]);
});
});

describe('::cli', () => {
it('pass the expected arguments to the scan method', () => {
const parameters = {
foo: 'bar',
};
const cliArguments = ['--foo', 'bar'];

const scanStub = stub(index, 'scan').resolves();
const callbackSpy = spy(() => {
assert.equal(scanStub.callCount, 1);
assert.equal(scanStub.firstCall.args[0], parameters);
assert.equal(scanStub.firstCall.args[1], cliArguments);
assert.equal(
scanStub.firstCall.args[2],
false,
'the localScanner argument is passed as false',
);
assert.equal(callbackSpy.callCount, 1);
});

index.cli(cliArguments, parameters, callbackSpy);
});
});

describe('::customScanner', () => {
it('pass the expected arguments to the scan method', () => {
const parameters = {
foo: 'bar',
};

const scanStub = stub(index, 'scan').resolves(null);
const callbackSpy = spy(() => {
assert.equal(scanStub.callCount, 1);
assert.equal(scanStub.firstCall.args[0], parameters);
assert.equal(scanStub.firstCall.args[1].length, 0, 'no CLI arguments are passed');
assert.equal(
scanStub.firstCall.args[2],
true,
'the localScanner argument is passed as true',
);
assert.equal(callbackSpy.callCount, 1);
});

index.customScanner(parameters, callbackSpy);
});
});
});