-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use mocked filesystem for tests
- Loading branch information
1 parent
40a2209
commit a8a2dcc
Showing
8 changed files
with
138 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#! /usr/bin/env node | ||
|
||
require('./'); | ||
require('./').run(process.argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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" | ||
|