Skip to content

Commit

Permalink
changing scanFiles function to return a list of files (#1)
Browse files Browse the repository at this point in the history
* changing scanFiles function to return a list of files with credit card numbers found

* moving changelog entry to the top of the file.
  • Loading branch information
usernameseb authored Nov 6, 2019
1 parent 6bf8593 commit 380eef9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.0.1 (November 6, 2019)

Modified `scanFiles` function to return a list of file names where credit card numbers are found.

## 1.0.0 (October 29, 2019)

Initial release! :tada:
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ccscan",
"description": "Scan files for credit card numbers",
"version": "1.0.0",
"version": "1.0.1",
"author": "Neo Financial Engineering <engineering@neofinancial.com>",
"license": "MIT",
"main": "build/app.js",
Expand Down
8 changes: 4 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ const scanFile = async (fileName: string, args: Args): Promise<boolean> => {
}
};

const scanFiles = async (args: Args): Promise<boolean> => {
const scanFiles = async (args: Args): Promise<string[]> => {
const mergedArgs = { ...defaultArgs, ...args };
let found = false;
const found = [];

try {
const pattern = [mergedArgs.files, '!node_modules'];
Expand All @@ -121,7 +121,7 @@ const scanFiles = async (args: Args): Promise<boolean> => {

for (const fileName of files) {
if (await scanFile(fileName, mergedArgs)) {
found = true;
found.push(fileName);
}
}

Expand Down Expand Up @@ -149,7 +149,7 @@ const run = async (): Promise<void> => {
type: 'string'
}),
handler: async (args: Args): Promise<void> => {
if (await scanFiles(args)) {
if ((await scanFiles(args)).length > 0) {
process.exit(1);
}
}
Expand Down
30 changes: 17 additions & 13 deletions test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let consoleLogSpy: jest.SpyInstance;

const formatConsoleOutput = (consoleOutput: string[][]): string[] => {
return consoleOutput.map((line: string[]): string => stripAnsi(line[0]));
}
};

beforeAll(() => {
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
Expand All @@ -22,36 +22,38 @@ afterEach(() => {

describe('TypeScript', () => {
test('card numbers are detected', async () => {
expect(await scanFiles({ files: '**/test/fixtures/typescript/*.{ts,js}' })).toEqual(true);
expect(await scanFiles({ files: '**/test/fixtures/typescript/*.{ts,js}' })).toEqual([
'test/fixtures/typescript/bad.ts'
]);
expect(consoleLogSpy).toHaveBeenCalledTimes(12);
expect(formatConsoleOutput(consoleLogSpy.mock.calls)).toMatchSnapshot();
});

test('card numbers are detected in silent mode', async () => {
expect(
await scanFiles({ files: '**/test/fixtures/typescript/*.{ts,js}', silent: true })
).toEqual(true);
).toEqual(['test/fixtures/typescript/bad.ts']);
expect(consoleLogSpy).not.toHaveBeenCalled();
});

test('card numbers are detected in verbose mode', async () => {
expect(
await scanFiles({ files: '**/test/fixtures/typescript/*.{ts,js}', verbose: true })
).toEqual(true);
).toEqual(['test/fixtures/typescript/bad.ts']);
expect(consoleLogSpy).toHaveBeenCalledTimes(14);
expect(formatConsoleOutput(consoleLogSpy.mock.calls)).toMatchSnapshot();
});

test('card-like numbers are detected when luhn check is disabled', async () => {
expect(
await scanFiles({ files: '**/test/fixtures/typescript/*.{ts,js}', 'luhn-check': false })
).toEqual(true);
).toEqual(['test/fixtures/typescript/bad.ts', 'test/fixtures/typescript/questionable.ts']);
expect(consoleLogSpy).toHaveBeenCalledTimes(23);
expect(formatConsoleOutput(consoleLogSpy.mock.calls)).toMatchSnapshot();
});

test('card numbers are not detected when no files match pattern', async () => {
expect(await scanFiles({ files: '**/test/fixtures/typescript/*.js' })).toEqual(false);
expect(await scanFiles({ files: '**/test/fixtures/typescript/*.js' })).toEqual([]);
expect(consoleLogSpy).toHaveBeenCalledTimes(0);
});

Expand All @@ -61,43 +63,45 @@ describe('TypeScript', () => {
files: '**/test/fixtures/typescript/*.{ts,js}',
exclude: ['test/fixtures/typescript']
})
).toEqual(false);
).toEqual([]);
expect(consoleLogSpy).toHaveBeenCalledTimes(0);
});
});

describe('JavaScript', () => {
test('card numbers are detected', async () => {
expect(await scanFiles({ files: '**/test/fixtures/javascript/*.{ts,js}' })).toEqual(true);
expect(await scanFiles({ files: '**/test/fixtures/javascript/*.{ts,js}' })).toEqual([
'test/fixtures/javascript/bad.js'
]);
expect(consoleLogSpy).toHaveBeenCalledTimes(12);
expect(formatConsoleOutput(consoleLogSpy.mock.calls)).toMatchSnapshot();
});

test('card numbers are detected in silent mode', async () => {
expect(
await scanFiles({ files: '**/test/fixtures/javascript/*.{ts,js}', silent: true })
).toEqual(true);
).toEqual(['test/fixtures/javascript/bad.js']);
expect(consoleLogSpy).not.toHaveBeenCalled();
});

test('card numbers are detected in verbose mode', async () => {
expect(
await scanFiles({ files: '**/test/fixtures/javascript/*.{ts,js}', verbose: true })
).toEqual(true);
).toEqual(['test/fixtures/javascript/bad.js']);
expect(consoleLogSpy).toHaveBeenCalledTimes(14);
expect(formatConsoleOutput(consoleLogSpy.mock.calls)).toMatchSnapshot();
});

test('card-like numbers are detected when luhn check is disabled', async () => {
expect(
await scanFiles({ files: '**/test/fixtures/javascript/*.{ts,js}', 'luhn-check': false })
).toEqual(true);
).toEqual(['test/fixtures/javascript/bad.js', 'test/fixtures/javascript/questionable.js']);
expect(consoleLogSpy).toHaveBeenCalledTimes(23);
expect(formatConsoleOutput(consoleLogSpy.mock.calls)).toMatchSnapshot();
});

test('card numbers are not detected when no files match pattern', async () => {
expect(await scanFiles({ files: '**/test/fixtures/javascript/*.ts' })).toEqual(false);
expect(await scanFiles({ files: '**/test/fixtures/javascript/*.ts' })).toEqual([]);
expect(consoleLogSpy).toHaveBeenCalledTimes(0);
});

Expand All @@ -107,7 +111,7 @@ describe('JavaScript', () => {
files: '**/test/fixtures/javascript/*.{ts,js}',
exclude: ['test/fixtures/javascript']
})
).toEqual(false);
).toEqual([]);
expect(consoleLogSpy).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 380eef9

Please sign in to comment.