Skip to content

Commit

Permalink
test: use mocked filesystem for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kirbysayshi committed Jan 29, 2020
1 parent 40a2209 commit a8a2dcc
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 24 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
run: yarn
- name: lint
run: yarn lint
- name: test
run: yarn test
- name: build
run: yarn build
- name: test
run: yarn test

2 changes: 1 addition & 1 deletion cli
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#! /usr/bin/env node

require('./');
require('./').run(process.argv);
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@
"@kirbysayshi/ts-run": "^3.0.1",
"@spotify/web-scripts": "^3.3.0",
"husky": "^3.1.0"
},
"dependencies": {
"@types/mock-fs": "^4.10.0",
"mock-fs": "^4.10.4"
}
}
90 changes: 73 additions & 17 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,75 @@
import { exec } from 'child_process';
import { promisify } from 'util';

const execp = promisify(exec);

test('display help', async () => {
try {
await execp('npx ts-run ./src/index.ts --help');
} catch (e) {
expect(e.code).toBe(1);
expect(e.stdout).toMatch(/--age/);
expect(e.stdout).toMatch(/--root/);
expect(e.stdout).toMatch(/--archive/);
expect(e.stdout).toMatch(/--projects/);
expect(e.stdout).toMatch(/--yes/);
}
import fs from 'fs';
import { mockFS } from './test-utils/mockfs';
import { run } from './index';

afterEach(() => {
mockFS.restore();
jest.resetAllMocks();
});

// TODO: expand these with mock-fs: https://www.npmjs.com/package/mock-fs
// const spiedConsoleLog = jest.spyOn(global.console, 'log');
// function getConsoleLogs() {
// // Filter out (remove) log lines that are nondeterministic, like timings.
// return spiedConsoleLog.mock.calls.filter(args => {
// return args.length ? args[0].match(/\d\dm\d\ds\d\d\dms/) === null : true;
// });
// }

test('_Archive is not mandatory nor created', async () => {
mockFS(
{
'/froot/proj01/f01': '',
},
{ createCwd: false, createTmp: false },
);

process.chdir('/froot');
await run(['--projects']);

const projects = fs.readdirSync('/froot');

// Must restore to enable snapshots.
mockFS.restore();

expect(projects).toMatchInlineSnapshot(`
Array [
"proj01",
]
`);
});

test('_Archive is only created if something moves', async () => {
const days181Ago = new Date();
days181Ago.setDate(days181Ago.getDate() - 181);

mockFS(
{
'/froot/proj01/f01': mockFS.file({
content: '',
mtime: days181Ago,
}),
'/froot/proj02/f01': '',
},
{ createCwd: false, createTmp: false },
);

process.chdir('/froot');
await run(['-y']);
const projects = fs.readdirSync('/froot');
const archive = fs.readdirSync('/froot/_Archive');
// Must restore to enable snapshots.
mockFS.restore();

expect(projects).toMatchInlineSnapshot(`
Array [
"_Archive",
"proj02",
]
`);

expect(archive).toMatchInlineSnapshot(`
Array [
"proj01",
]
`);
});
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ function ensureArchiveExists(archive: string) {
}
}

async function run() {
const config = parseArgv(process.argv);
export async function run(argv: NodeJS.Process['argv']) {
const config = parseArgv(argv);

const start = Date.now();
const files = collectFiles(config.root, config.root, config.exclude);
Expand Down Expand Up @@ -335,5 +335,3 @@ async function run() {
return;
}
}

run();
27 changes: 27 additions & 0 deletions src/integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { exec } from 'child_process';
import { promisify } from 'util';
import fs from 'fs';
const execp = promisify(exec);

try {
fs.statSync('./esm');
fs.statSync('./cjs');
fs.statSync('./types');
} catch (e) {
throw new Error(
'Could not find compiled sources to test CLI. Did you run `yarn build`?',
);
}

test('display help via bin', async () => {
try {
await execp('./cli --help');
} catch (e) {
expect(e.code).toBe(1);
expect(e.stdout).toMatch(/--age/);
expect(e.stdout).toMatch(/--root/);
expect(e.stdout).toMatch(/--archive/);
expect(e.stdout).toMatch(/--projects/);
expect(e.stdout).toMatch(/--yes/);
}
});
16 changes: 16 additions & 0 deletions src/test-utils/mockfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mockFSReal from 'mock-fs';

// This entire file exists to avoid https://github.com/tschaub/mock-fs/issues/234
// aka "ENOENT, no such file or directory '... /node_modules/callsites'

function setupFS(config: Parameters<typeof mockFSReal>[0]) {
// eslint-disable-next-line no-console
console.log();
return mockFSReal(config);
}

Object.keys(mockFSReal).forEach(key => {
(setupFS as any)[key] = (mockFSReal as any)[key];
});

export const mockFS = setupFS as typeof mockFSReal;
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,13 @@
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==

"@types/mock-fs@^4.10.0":
version "4.10.0"
resolved "https://registry.yarnpkg.com/@types/mock-fs/-/mock-fs-4.10.0.tgz#460061b186993d76856f669d5317cda8a007c24b"
integrity sha512-FQ5alSzmHMmliqcL36JqIA4Yyn9jyJKvRSGV3mvPh108VFatX7naJDzSG4fnFQNZFq9dIx0Dzoe6ddflMB2Xkg==
dependencies:
"@types/node" "*"

"@types/node@*", "@types/node@>= 8":
version "13.1.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.1.4.tgz#4cfd90175a200ee9b02bd6b1cd19bc349741607e"
Expand Down Expand Up @@ -5976,6 +5983,11 @@ [email protected], mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1:
dependencies:
minimist "0.0.8"

mock-fs@^4.10.4:
version "4.10.4"
resolved "https://registry.yarnpkg.com/mock-fs/-/mock-fs-4.10.4.tgz#4eaa3d6f7da2f44e1f3dd6b462cbbcb7b082e3d4"
integrity sha512-gDfZDLaPIvtOusbusLinfx6YSe2YpQsDT8qdP41P47dQ/NQggtkHukz7hwqgt8QvMBmAv+Z6DGmXPyb5BWX2nQ==

modify-values@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
Expand Down

0 comments on commit a8a2dcc

Please sign in to comment.