Skip to content

Commit

Permalink
Add test for validator function
Browse files Browse the repository at this point in the history
  • Loading branch information
samirsilwal committed Jul 18, 2024
1 parent 9893665 commit 6f80db4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/util/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function copy(fromPath: string, toPath: string): Promise<void> {
export function validateScriptFileName(filename: string): string {
const ext = filename.split('.').pop();

if (!ext || ![FileExtensions.JS, FileExtensions.SQL, FileExtensions.JS].includes(ext as FileExtensions)) {
if (!ext || ![FileExtensions.TS, FileExtensions.SQL, FileExtensions.JS].includes(ext as FileExtensions)) {
throw new Error('Invalid file name or extension');
}

Expand Down
28 changes: 26 additions & 2 deletions test/unit/util/fs.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as fs from 'fs';
import * as path from 'path';
import { expect } from 'chai';
import { expect, assert } from 'chai';
import { it, describe } from 'mocha';

import { write, read, remove, exists, mkdtemp, glob, existsDir } from '../../../src/util/fs';
import { write, read, remove, exists, mkdtemp, glob, existsDir, validateScriptFileName } from '../../../src/util/fs';

describe('UTIL: fs', () => {
let filePath: string;
Expand Down Expand Up @@ -114,4 +114,28 @@ describe('UTIL: fs', () => {
expect(result).to.deep.equal([]);
});
});

describe('validateScriptFileName', () => {
it('should throw error for invalid file name', async () => {
return expect(() => validateScriptFileName('test')).to.throw('Invalid file name or extension');
});

it('should support only JS/TS or SQL script', () => {
const fname0 = validateScriptFileName('test.sql');
const fname1 = validateScriptFileName('test.js');
const fname2 = validateScriptFileName('test.ts');

assert(fname0 === 'test.sql');
assert(fname1 === 'test.js');
assert(fname2 === 'test.ts');

expect(() => validateScriptFileName('test.c')).to.throw('Invalid file name or extension');
});

it('should return filename if validation is successfull', () => {
const fname = validateScriptFileName('test.sql');

return expect(fname).to.eq('test.sql');
});
});
});

0 comments on commit 6f80db4

Please sign in to comment.