Skip to content

Commit

Permalink
test: replace absolute cwd in output paths before taking snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher J. Brody committed Feb 22, 2021
1 parent daa1d9e commit fa6387e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const lib = require('../../../../../lib/lib.js');

const ioInject = require('../../../helpers/io-inject.js');

const mycwd = require('process').cwd();

test('create alice-bobbi module with example, with `exampleFileLinkage: true` then recover from missing scripts in example package.json', async () => {
const mysnap = [];

Expand All @@ -13,7 +15,7 @@ test('create alice-bobbi module with example, with `exampleFileLinkage: true` th
readFileSync: (jsonFilePath) => {
mysnap.push({
call: 'fs.readFileSync',
jsonFilePath: jsonFilePath.replace(/\\/g, '/'),
jsonFilePath: jsonFilePath.replace(mycwd, '...').replace(/\\/g, '/'),
});
return `{ "name": "example", "version": "0.0.1" }`;
}
Expand Down
13 changes: 8 additions & 5 deletions tests/with-injection/helpers/io-inject.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const mycwd = require('process').cwd();

module.exports = (mysnap) => ({
fs: {
outputFile: (name, content) => {
mysnap.push(
`* outputFile name: ${name.replace(/\\/g, '/')}
`* outputFile name: ${name.replace(mycwd, '...').replace(/\\/g, '/')}
content:
--------
${content}
Expand All @@ -13,13 +15,13 @@ ${content}
},

ensureDir: (dir) => {
mysnap.push(`* ensureDir dir: ${dir.replace(/\\/g, '/')}\n`);
mysnap.push(`* ensureDir dir: ${dir.replace(mycwd, '...').replace(/\\/g, '/')}\n`);
return Promise.resolve();
},
readFileSync: (jsonFilePath) => {
mysnap.push({
call: 'fs.readFileSync',
jsonFilePath: jsonFilePath.replace(/\\/g, '/'),
jsonFilePath: jsonFilePath.replace(mycwd, '...').replace(/\\/g, '/'),
});
return `{
"name": "example",
Expand All @@ -31,14 +33,15 @@ ${content}
writeFileSync: (path, json, options) => {
mysnap.push({
call: 'fs.writeFileSync',
filePath: path.replace(/\\/g, '/'),
filePath: path.replace(mycwd, '...').replace(/\\/g, '/'),
json,
options,
});
},
},
execa: {
commandSync: (command, options) => {
commandSync: (command, opts) => {
const options = { ...opts, ...(opts.cwd ? { cwd: opts.cwd.replace(mycwd, '...').replace(/\\/g, '/') } : {}) };
mysnap.push(
`* execa.commandSync command: ${command} options: ${JSON.stringify(options)}\n`);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const mockcwd = require('process').cwd();

// special compact mocks for this test:
const mysnap = [];
const mockpushit = x => mysnap.push(x);
Expand All @@ -16,13 +18,13 @@ jest.mock('update-notifier', () => ({ pkg }) => {
jest.mock('fs-extra', () => ({
outputFile: (outputFileName, theContent) => {
mockpushit({
outputFileName: outputFileName.replace(/\\/g, '/'),
outputFileName: outputFileName.replace(mockcwd, '...').replace(/\\/g, '/'),
theContent
});
return Promise.resolve();
},
ensureDir: (dir) => {
mockpushit({ ensureDir: dir.replace(/\\/g, '/') });
mockpushit({ ensureDir: dir.replace(mockcwd, '...').replace(/\\/g, '/') });
return Promise.resolve();
},
}));
Expand Down Expand Up @@ -67,7 +69,8 @@ const mockCommander = {
};
jest.mock('commander', () => mockCommander);
jest.mock('execa', () => ({
commandSync: (command, options) => {
commandSync: (command, opts) => {
const options = { ...opts, ...(opts.cwd ? { cwd: opts.cwd.replace(mockcwd, '...').replace(/\\/g, '/') } : {}) };
mockpushit({ commandSync: command, options });
}
}));
Expand All @@ -78,8 +81,8 @@ jest.mock('react-native-init-func', () => (projectNameArray, opts) => {
jest.mock('console', () => ({
info: (...args) => {
mockpushit({
// TBD EXTRA WORKAROUND HACK for non-deterministic elapsed time in log
info: args.map(line => line.replace(/It took.*s/g, 'It took XXX'))
// TBD EXTRA WORKAROUND HACK for non-deterministic elapsed time in log etc.
info: args.map(line => line.replace(mockcwd, '...').replace(/\\/g, '/').replace(/It took.*s/g, 'It took XXX'))
});
},
// console.log is no longer expected
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const lib = require('../../../../../../../lib/lib.js');

const mockcwd = require('process').cwd();

// special compact mocks for this test:
const mysnap = [];
const mockpushit = x => mysnap.push(x);
jest.mock('fs-extra', () => ({
outputFile: (outputFileName, theContent) => {
mockpushit({
outputFileName: outputFileName.replace(/\\/g, '/'),
outputFileName: outputFileName.replace(mockcwd, '...').replace(/\\/g, '/'),
theContent
});
return Promise.resolve();
},
ensureDir: (dir) => {
mockpushit({ ensureDir: dir.replace(/\\/g, '/') });
mockpushit({ ensureDir: dir.replace(mockcwd, '...').replace(/\\/g, '/') });
return Promise.resolve();
},
readFileSync: (path) => {
Expand All @@ -21,14 +23,15 @@ jest.mock('fs-extra', () => ({
},
writeFileSync: (path, json, options) => {
mockpushit({
writeFileSyncToPath: path.replace(/\\/g, '/'),
writeFileSyncToPath: path.replace(mockcwd, '...').replace(/\\/g, '/'),
json,
options
});
},
}));
jest.mock('execa', () => ({
commandSync: (command, options) => {
commandSync: (command, opts) => {
const options = { ...opts, ...(opts.cwd ? { cwd: opts.cwd.replace(mockcwd, '...').replace(/\\/g, '/') } : {}) };
mockpushit({ commandSync: command, options });
}
}));
Expand All @@ -38,7 +41,9 @@ jest.mock('react-native-init-func', () => (projectNameArray, opts) => {
});
jest.mock('console', () => ({
info: (...args) => {
mockpushit({ info: [].concat(args) });
mockpushit({
info: args.map(line => line.replace(mockcwd, '...').replace(/\\/g, '/'))
});
},
log: (...args) => {
mockpushit({ log: [].concat(args) });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const lib = require('../../../../../../../lib/lib.js');

const mockcwd = require('process').cwd();

// special compact mocks for this test:
const mysnap = [];
const mockpushit = x => mysnap.push(x);
jest.mock('fs-extra', () => ({
outputFile: (outputFileName, theContent) => {
mockpushit({
outputFileName: outputFileName.replace(/\\/g, '/'),
outputFileName: outputFileName.replace(mockcwd, '...').replace(/\\/g, '/'),
theContent
});
return Promise.resolve();
},
ensureDir: (dir) => {
mockpushit({ ensureDir: dir.replace(/\\/g, '/') });
mockpushit({ ensureDir: dir.replace(mockcwd, '...').replace(/\\/g, '/') });
return Promise.resolve();
},
readFileSync: (path) => {
Expand All @@ -28,7 +30,8 @@ jest.mock('fs-extra', () => ({
},
}));
jest.mock('execa', () => ({
commandSync: (command, options) => {
commandSync: (command, opts) => {
const options = { ...opts, ...(opts.cwd ? { cwd: opts.cwd.replace(mockcwd, '...').replace(/\\/g, '/') } : {}) };
mockpushit({ commandSync: command, options });
if (/yarn add/.test(command)) {
throw new Error('ENOPERM not permitted');
Expand All @@ -41,7 +44,9 @@ jest.mock('react-native-init-func', () => (projectNameArray, opts) => {
});
jest.mock('console', () => ({
info: (...args) => {
mockpushit({ info: [].concat(args) });
mockpushit({
info: args.map(line => line.replace(mockcwd, '...').replace(/\\/g, '/'))
});
},
log: (...args) => {
mockpushit({ log: [].concat(args) });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const lib = require('../../../../../../../../lib/lib.js');

const mockcwd = require('process').cwd();

// special compact mocks for this test:
const mysnap = [];
const mockpushit = x => mysnap.push(x);
jest.mock('fs-extra', () => ({
outputFile: (outputFileName, theContent) => {
mockpushit({
outputFileName: outputFileName.replace(/\\/g, '/'),
outputFileName: outputFileName.replace(mockcwd, '...').replace(/\\/g, '/'),
theContent
});
return Promise.resolve();
},
ensureDir: (dir) => {
mockpushit({ ensureDir: dir.replace(/\\/g, '/') });
mockpushit({ ensureDir: dir.replace(mockcwd, '...').replace(/\\/g, '/') });
return Promise.resolve();
},
readFileSync: (path) => {
Expand All @@ -28,7 +30,8 @@ jest.mock('fs-extra', () => ({
},
}));
jest.mock('execa', () => ({
commandSync: (command, options) => {
commandSync: (command, opts) => {
const options = { ...opts, ...(opts.cwd ? { cwd: opts.cwd.replace(mockcwd, '...').replace(/\\/g, '/') } : {}) };
mockpushit({ commandSync: command, options });
}
}));
Expand All @@ -38,7 +41,9 @@ jest.mock('react-native-init-func', () => (projectNameArray, opts) => {
});
jest.mock('console', () => ({
info: (...args) => {
mockpushit({ info: [].concat(args) });
mockpushit({
info: args.map(line => line.replace(mockcwd, '...').replace(/\\/g, '/'))
});
},
log: (...args) => {
mockpushit({ log: [].concat(args) });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
const lib = require('../../../../../../../lib/lib.js');

const mockcwd = require('process').cwd();

// special compact mocks for this test:
const mysnap = [];
const mockpushit = x => mysnap.push(x);
jest.mock('fs-extra', () => ({
outputFile: (outputFileName, theContent) => {
mockpushit({
outputFileName: outputFileName.replace(/\\/g, '/'),
outputFileName: outputFileName.replace(mockcwd, '...').replace(/\\/g, '/'),
theContent
});
return Promise.resolve();
},
ensureDir: (dir) => {
mockpushit({ ensureDir: dir.replace(/\\/g, '/') });
mockpushit({ ensureDir: dir.replace(mockcwd, '...').replace(/\\/g, '/') });
return Promise.resolve();
},
readFileSync: (path) => {
mockpushit({ readFileSyncFromPath: path.replace(/\\/g, '/') });
mockpushit({ readFileSyncFromPath: path.replace(mockcwd, '...').replace(/\\/g, '/') });
return `{ "name": "x", "scripts": { "test": "exit 1" } }`;
},
writeFileSync: (path, json, options) => {
mockpushit({
writeFileSyncToPath: path.replace(/\\/g, '/'),
writeFileSyncToPath: path.replace(mockcwd, '...').replace(/\\/g, '/'),
json,
options
});
},
}));
jest.mock('execa', () => ({
commandSync: (command, options) => {
commandSync: (command, opts) => {
const options = { ...opts, ...(opts.cwd ? { cwd: opts.cwd.replace(mockcwd, '...').replace(/\\/g, '/') } : {}) };
mockpushit({ commandSync: command, options });
}
}));
Expand All @@ -38,7 +41,9 @@ jest.mock('react-native-init-func', () => (projectNameArray, opts) => {
});
jest.mock('console', () => ({
info: (...args) => {
mockpushit({ info: [].concat(args) });
mockpushit({
info: args.map(line => line.replace(mockcwd, '...').replace(/\\/g, '/'))
});
},
log: (...args) => {
mockpushit({ log: [].concat(args) });
Expand Down

0 comments on commit fa6387e

Please sign in to comment.