Skip to content

Commit

Permalink
Added actual testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBieg committed Nov 24, 2019
1 parent 213e971 commit 3de9bf5
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 50 deletions.
11 changes: 8 additions & 3 deletions dewey.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ class Dewey {
if (typeof matcher === 'string') {
return matcher === name;
} else if (typeof matcher === 'function') {
return matcher(path.slice().reverse(), name) === name;
const newMatcher = matcher(path.slice().reverse(), name);
if (typeof newMatcher === "boolean") {
return newMatcher;
} else {
return this.match(name, newMatcher, path);
}
} else if (matcher instanceof RegExp) {
return name.match(matcher);
return name.match(matcher);
}
}

Expand Down Expand Up @@ -218,7 +223,7 @@ class Dewey {
}

if (this.errors.length) {
throw new Error(`Too many errors`);
throw new Error('Tests failed.');
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"test": "jest --config ./jest.config.js",
"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./test,./jest.config.js,./node_modules,./.babelrc,./package.json,./npm-debug.log",
"prepublishOnly": "npm run build",
"prepublishOnly": "npm test && npm run build",
"watch": "npm-watch"
},
"repository": {
Expand Down
45 changes: 0 additions & 45 deletions test/dewey.test.js

This file was deleted.

113 changes: 113 additions & 0 deletions test/full.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import Dewey from '../dewey';
import fs from 'fs';
import { FakeFileObject } from './utils';

jest.mock('fs');

describe('matching full structure', () => {
beforeAll(() => {
fs.readdirSync.mockImplementation((dirName) => {
switch (dirName) {
case 'dir1':
return ['dir1file1', 'dir1file2', 'dir2', 'dir1ignoreFile', 'dir1ignoreDir'];
case 'dir1/dir2':
return ['dir1dir2file1', 'dir1dir2ignoreFile', 'dir1dir2ignoreDir'];
default:
return ['dir1', 'file1', 'ignoreFile', 'ignoreDir'];
}
});
})

test('Passes when testing a full directory structure using strings', () => {
fs.lstatSync.mockImplementation((name) => new FakeFileObject(name.includes('file') ? false : true));
const dewey = new Dewey(
'',
{
ignore: ['ignoreFile', 'ignoreDir'],
files: [{ name: 'file1' }],
dirs: [
{
name: 'dir1',
config: {
ignore: ['dir1ignoreFile', 'dir1ignoreDir'],
files: [{ name: 'dir1file1' }, { name: 'dir1file2'}],
dirs: [
{
name: 'dir2',
config: {
ignore: ['dir1dir2ignoreFile', 'dir1dir2ignoreDir'],
files: [{ name: 'dir1dir2file1'}]
}
}
]
}
}
]
},
false,
);
dewey.run();
});

test('Passes when testing a full directory structure using regex', () => {
fs.lstatSync.mockImplementation((name) => new FakeFileObject(name.includes('file') ? false : true));
const dewey = new Dewey(
'',
{
ignore: [/ignore[File|Dir]/],
files: [{ name: /file1/ }],
dirs: [
{
name: /dir1/,
config: {
ignore: [/dir1ignore[File|Dir]/],
files: [{ name: /dir1file[0-9]/ }],
dirs: [
{
name: /dir2/,
config: {
ignore: [/dir1dir2ignore[File|Dir]/],
files: [{ name: /dir1dir2file1/}]
}
}
]
}
}
]
},
false,
);
dewey.run();
});

test('Passes when testing a full directory structure using functions', () => {
fs.lstatSync.mockImplementation((name) => new FakeFileObject(name.includes('file') ? false : true));
const dewey = new Dewey(
'',
{
ignore: [(path, name) => name.includes('ignore')],
files: [{ name: () => 'file1' }],
dirs: [
{
name: (path, name) => /dir1/,
config: {
ignore: [(path, name) => name.includes('ignore')],
files: [{ name: (path) => new RegExp(`${path[0]}file[0-9]`)}],
dirs: [
{
name: () => 'dir2',
config: {
ignore: [(path, name) => name.includes('ignore')],
files: [{ name: (path) => `${path[1]}${path[0]}file1` }]
}
}
]
}
}
]
},
false,
);
dewey.run();
});
});
71 changes: 71 additions & 0 deletions test/rexex.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Dewey from '../dewey';
import fs from 'fs';
import { FakeFileObject } from './utils';

jest.mock('fs');

describe('matching regex names', () => {
beforeAll(() => {
fs.readdirSync.mockImplementation(() => ['test1', 'test2']);
})

test('Passes with regex file names in objectForm', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(false));
const dewey = new Dewey(
'',
{ files: [{ name: /test1/ }, { name: /test2/ }] },
false,
);
dewey.run();
});

test('Passes with regex file names in list form', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(false));
const dewey = new Dewey(
'',
{ files: [/test1/, /test2/] },
false,
);
dewey.run();
});

test('Passes with multiple regex matches', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(false));
const dewey = new Dewey(
'',
{ files: [{ name: /test[0-9]/ }] },
false,
);
dewey.run();
});

test('Passes with regex file names ignored', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(false));
const dewey = new Dewey(
'',
{ ignore: [/test1/, /test2/] },
false,
);
dewey.run();
});

test('Passes with string dir names', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(true));
const dewey = new Dewey(
'',
{ dirs: [{ name: /test1/ }, { name: /test2/ }] },
false,
);
dewey.run();
});

test('Passes with regex dir names ignored', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(true));
const dewey = new Dewey(
'',
{ ignore: [/test1/, /test2/] },
false,
);
dewey.run();
});
});
61 changes: 61 additions & 0 deletions test/string.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Dewey from '../dewey';
import fs from 'fs';
import { FakeFileObject } from './utils';

jest.mock('fs');

describe('matching string names', () => {
beforeAll(() => {
fs.readdirSync.mockImplementation(() => ['test1', 'test2']);
})

test('Passes with string file names in objectForm', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(false));
const dewey = new Dewey(
'',
{ files: [{ name: 'test1' }, { name: 'test2' }] },
false,
);
dewey.run();
});

test('Passes with string file names in list form', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(false));
const dewey = new Dewey(
'',
{ files: ['test1', 'test2'] },
false,
);
dewey.run();
});

test('Passes with string file names ignored', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(false));
const dewey = new Dewey(
'',
{ ignore: ['test1', 'test2'] },
false,
);
dewey.run();
});

test('Passes with string dir names', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(true));
const dewey = new Dewey(
'',
{ dirs: [{ name: 'test1' }, { name: 'test2' }] },
false,
);
dewey.run();
});

test('Passes with string dir names ignored', () => {
fs.lstatSync.mockImplementation(() => new FakeFileObject(true));
const dewey = new Dewey(
'',
{ ignore: ['test1', 'test2'] },
false,
);
dewey.run();
});
});
17 changes: 17 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class FakeFileObject {
constructor(isDir) {
this.isDir = isDir;
}

isFile() {
return !this.isDir;
}

isDirectory() {
return this.isDir;
}
}

export {
FakeFileObject,
};

0 comments on commit 3de9bf5

Please sign in to comment.