Skip to content

Commit

Permalink
chore: simplify test helpers (#250)
Browse files Browse the repository at this point in the history
* chore: simplify test helpers
  • Loading branch information
ghiscoding committed Jul 12, 2022
1 parent 44e0fe3 commit 3774807
Show file tree
Hide file tree
Showing 111 changed files with 715 additions and 641 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
],
"node/no-extraneous-require": "off",
"node/no-unpublished-require": "off",
"node/no-unsupported-features/es-syntax": "off",
"prefer-destructuring": "off",
"prefer-object-spread": "off",
"strict": "off",
Expand All @@ -55,7 +56,7 @@
}
},
{
"files": ["integration/**", "**/__tests__/**", "**/__mocks__/**", "**/__helpers__/**"],
"files": ["integration/**", "helpers/**", "**/__tests__/**", "**/__mocks__/**", "**/__helpers__/**"],
"env": {
"jest": true
},
Expand Down
33 changes: 26 additions & 7 deletions helpers/command-runner/index.js → helpers/cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const execa = require('execa');
const path = require('path');
const lernaCLI = require('../../packages/cli/src/lerna-cli');
const lernaCLI = require('../packages/cli/src/lerna-cli');

module.exports = commandRunner;
// eslint-disable-next-line node/no-unpublished-require
const LERNA_BIN = require.resolve('../packages/cli/src/cli');

/**
* A higher-order function to help with passing _actual_ yargs-parsed argv
Expand All @@ -12,7 +14,7 @@ module.exports = commandRunner;
* @param {Object} commandModule The yargs command exports
* @return {Function} with partially-applied yargs config
*/
function commandRunner(commandModule) {
exports.commandRunner = function commandRunner(commandModule) {
/* eslint-disable import/no-dynamic-require, global-require */
const cmd = commandModule.command.split(' ')[0];

Expand All @@ -28,8 +30,8 @@ function commandRunner(commandModule) {
.wrap(null)
.command(commandModule);

return (...args) => {
return new Promise((resolve, reject) => {
return (...args) =>
new Promise((resolve, reject) => {
const yargsMeta = {};

const context = {
Expand Down Expand Up @@ -65,6 +67,23 @@ function commandRunner(commandModule) {
})
.parse([cmd, ...args], context, parseFn);
});
};
};
}
};

exports.cliRunner = function cliRunner(cwd, env) {
const opts = {
cwd,
env: Object.assign(
{
CI: 'true',
// always turn off chalk
FORCE_COLOR: '0',
},
env
),
// when debugging integration test snapshots, uncomment next line
// stdio: ["ignore", "inherit", "inherit"],
};

return (...args) => execa('node', [LERNA_BIN].concat(args), opts);
};
27 changes: 0 additions & 27 deletions helpers/clone-fixture/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions helpers/clone-fixture/package.json

This file was deleted.

11 changes: 0 additions & 11 deletions helpers/command-runner/package.json

This file was deleted.

23 changes: 0 additions & 23 deletions helpers/commit-change-to-package/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions helpers/commit-change-to-package/package.json

This file was deleted.

10 changes: 0 additions & 10 deletions helpers/copy-fixture/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions helpers/find-fixture/index.js

This file was deleted.

83 changes: 83 additions & 0 deletions helpers/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict';

const execa = require('execa');
const fileUrl = require('file-url');
const tempy = require('tempy');
const fs = require('fs-extra');
const findUp = require('find-up');
const path = require('path');
const { gitAdd, gitCommit, gitInit } = require('./git');

function cloneFixtureFactory(startDir) {
const initFixture = initFixtureFactory(startDir);

return (...args) =>
initFixture(...args).then((cwd) => {
const repoDir = tempy.directory();
const repoUrl = fileUrl(repoDir, { resolve: false });

return execa('git', ['init', '--bare'], { cwd: repoDir })
.then(() => execa('git', ['checkout', '-B', 'main'], { cwd }))
.then(() => execa('git', ['remote', 'add', 'origin', repoUrl], { cwd }))
.then(() => execa('git', ['push', '-u', 'origin', 'main'], { cwd }))
.then(() => ({
cwd,
repository: repoUrl,
}));
});
}
exports.cloneFixtureFactory = cloneFixtureFactory;

function findFixture(cwd, fixtureName) {
return findUp(path.join('__fixtures__', fixtureName), { cwd, type: 'directory' }).then((fixturePath) => {
if (fixturePath === undefined) {
throw new Error(`Could not find fixture with name "${fixtureName}"`);
}

return fixturePath;
});
}

function copyFixture(targetDir, fixtureName, cwd) {
return findFixture(cwd, fixtureName).then((fp) => fs.copy(fp, targetDir));
}
exports.copyFixture = copyFixture;

function initFixtureFactory(startDir) {
return (fixtureName, commitMessage = 'Init commit') => {
const cwd = tempy.directory();
let chain = Promise.resolve();

chain = chain.then(() => process.chdir(cwd));
chain = chain.then(() => copyFixture(cwd, fixtureName, startDir));
chain = chain.then(() => gitInit(cwd, '.'));

if (commitMessage) {
chain = chain.then(() => gitAdd(cwd, '-A'));
chain = chain.then(() => gitCommit(cwd, commitMessage));
}

return chain.then(() => cwd);
};
}
exports.initFixtureFactory = initFixtureFactory;

function initNamedFixtureFactory(startDir) {
return (dirName, fixtureName, commitMessage = 'Init commit') => {
const cwd = path.join(tempy.directory(), dirName);
let chain = Promise.resolve();

chain = chain.then(() => fs.ensureDir(cwd));
chain = chain.then(() => process.chdir(cwd));
chain = chain.then(() => copyFixture(cwd, fixtureName, startDir));
chain = chain.then(() => gitInit(cwd, '.'));

if (commitMessage) {
chain = chain.then(() => gitAdd(cwd, '-A'));
chain = chain.then(() => gitCommit(cwd, commitMessage));
}

return chain.then(() => cwd);
};
}
exports.initNamedFixtureFactory = initNamedFixtureFactory;
9 changes: 0 additions & 9 deletions helpers/get-commit-message/index.js

This file was deleted.

11 changes: 0 additions & 11 deletions helpers/get-commit-message/package.json

This file was deleted.

7 changes: 0 additions & 7 deletions helpers/git-add/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions helpers/git-checkout/index.js

This file was deleted.

11 changes: 0 additions & 11 deletions helpers/git-checkout/package.json

This file was deleted.

16 changes: 0 additions & 16 deletions helpers/git-commit/index.js

This file was deleted.

15 changes: 0 additions & 15 deletions helpers/git-init/index.js

This file was deleted.

1 change: 0 additions & 1 deletion helpers/git-init/template/description

This file was deleted.

9 changes: 0 additions & 9 deletions helpers/git-merge/index.js

This file was deleted.

11 changes: 0 additions & 11 deletions helpers/git-merge/package.json

This file was deleted.

0 comments on commit 3774807

Please sign in to comment.