Skip to content

Commit

Permalink
test: add lots of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Dec 9, 2021
1 parent 3f8433e commit efa36ae
Show file tree
Hide file tree
Showing 11 changed files with 415 additions and 178 deletions.
58 changes: 58 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"lint": "vjsstandard",
"pretest": "npm run lint",
"test": "npm run test:unit",
"test:unit": "ava --timeout 1m ./test/index.test.js",
"test:unit": "ava --timeout 1m './test/*.test.js'",
"test:verify": "vjsverify --skip-syntax --verbose",
"preversion": "npm test",
"version": "is-prerelease || npm run update-changelog && git add CHANGELOG.md",
Expand All @@ -43,6 +43,7 @@
"conventional-changelog-videojs": "^3.0.2",
"doctoc": "^2.1.0",
"husky": "^4.3.8",
"install-local": "^3.0.1",
"lint-staged": "^12.1.2",
"not-prerelease": "^1.0.1",
"shelljs": "^0.8.4",
Expand Down
6 changes: 4 additions & 2 deletions src/get-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const spawnSync = require('child_process').spawnSync;
const fs = require('fs');
const path = require('path');

const getRoot = () => {
const cwd = process.env.INIT_CWD ? process.env.INIT_CWD : process.cwd();
const getRoot = (cwd) => {
if (!cwd) {
cwd = process.env.INIT_CWD ? process.env.INIT_CWD : process.cwd();
}
const gitRootResult = spawnSync('git', ['rev-parse', '--show-toplevel'], {cwd});
const rootDir = gitRootResult.stdout ? gitRootResult.stdout.toString().trim() : '';

Expand Down
4 changes: 2 additions & 2 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const spawnSync = require('child_process').spawnSync;
const isCI = require('is-ci');
const rootDir = require('./get-root.js')();
const logger = require('./console');
const merge = path.join(__dirname, 'merge.js');
const mergePath = path.relative(rootDir, path.resolve(__dirname, 'merge.js'));

if (!rootDir) {
logger.log('Current working directory is not using git, skipping install.');
Expand Down Expand Up @@ -39,7 +39,7 @@ const configOne = spawnSync(
);
const configTwo = spawnSync(
'git',
['config', '--local', 'merge.npm-merge-driver-install.driver', `node ${merge} %A %O %B %P`],
['config', '--local', 'merge.npm-merge-driver-install.driver', `node '${mergePath}' %A %O %B %P`],
{cwd: rootDir}
);

Expand Down
5 changes: 3 additions & 2 deletions src/merge.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env node
/* eslint-disable no-console */
const spawnSync = require('child_process').spawnSync;
const path = require('path');
const fs = require('fs');
const logger = require('./console');
const os = require('os');

const currentVersion = process.argv[2];
const ancestorVersion = process.argv[3];
Expand All @@ -14,15 +16,14 @@ const ret = spawnSync('git', ['merge-file', '-p', currentVersion, ancestorVersio

fs.writeFileSync(file, ret.stdout);
const install = spawnSync(
'npm',
os.platform() === 'win32' ? 'npm.cmd' : 'npm',
['install', '--package-lock-only', '--prefer-offline', '--no-audit', '--progress=false'],
{cwd: path.dirname(file)}
);

if (install.status !== 0) {
logger.log(`${file} merge failure`);
logger.log('resolve package.json conflicts. Then run npm i --package-lock-only');
// eslint-disable-next-line
console.log();
process.exit(1);
}
Expand Down
34 changes: 34 additions & 0 deletions test/get-root.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const test = require('ava');
const path = require('path');
const shell = require('shelljs');
const getRoot = require('../src/get-root.js');
const {sharedHooks} = require('./helpers.js');

test.before((t) => {
return sharedHooks.before(t).then(function() {
shell.mkdir(path.join(t.context.template, 'subdir'));

});
});
test.beforeEach(sharedHooks.beforeEach);
test.afterEach.always(sharedHooks.afterEach);
test.after.always(sharedHooks.after);

test('can find root when it exists', (t) => {
const result = getRoot(t.context.dir);

t.truthy(result, 'is a valid path');
});

test('can find root from sub directory', (t) => {
const result = getRoot(path.join(t.context.dir, 'subdir'));

t.truthy(result, 'is a valid path');
});

test('no root without .git', (t) => {
shell.rm('-rf', path.join(t.context.dir, '.git'));
const result = getRoot(t.context.dir);

t.falsy(result, 'is an empty string');
});
110 changes: 110 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const path = require('path');
const spawnPromise = require('@brandonocasey/spawn-promise');
const shell = require('shelljs');
const uuid = require('uuid');
const fs = require('fs');
const installLocalBin = require.resolve('install-local/bin/install-local');

const BASE_DIR = path.resolve(__dirname, '..');
const TEMP_DIR = shell.tempdir();

const getTempDir = function() {
return path.join(TEMP_DIR, uuid.v4());
};

const isInstalled = function(dir) {
const gitConfigPath = path.join(dir, '.git', 'config');
const gitAttrPath = path.join(dir, '.git', 'info', 'attributes');

if (!shell.test('-f', gitConfigPath)) {
return false;
}

const config = shell.cat(gitConfigPath);

if (!(/npm-merge-driver-install/i).test(config)) {
return false;
}

if (!shell.test('-f', gitAttrPath)) {
return false;
}

const attr = shell.cat(gitAttrPath);

if (!(/npm-merge-driver-install/i).test(attr)) {
return false;
}

return true;
};

const promiseSpawn = function(bin, args, options = {}) {
const ignoreExitCode = options.ignoreExitCode;

delete options.ignoreExitCode;
options = Object.assign({shell: true, stdio: 'pipe', encoding: 'utf8'}, options);
options.env = options.env || {};
options.env.PATH = options.env.PATH || process.env.PATH;

return spawnPromise(bin, args, options).then(function({status, stderr, stdout, combined}) {
if (!ignoreExitCode && status !== 0) {
return Promise.reject(`command ${bin} ${args.join(' ')} failed with code ${status}\n` + combined);
}
return Promise.resolve({exitCode: status, stderr, stdout});

});
};

const sharedHooks = {

before: (t) => {
t.context = {
template: getTempDir()
};

shell.mkdir(t.context.template);
fs.writeFileSync(path.join(t.context.template, '.gitignore'), 'node_modules\n');

// create the package.json
return promiseSpawn('npm', ['init', '-y'], {cwd: t.context.template}).then(function(result) {
// create the .git dir
return promiseSpawn('git', ['init'], {cwd: t.context.template});
}).then(function(result) {
return promiseSpawn('npm', ['install', '--package-lock-only']);
}).then(function(result) {
return promiseSpawn('git', ['add', '--all'], {cwd: t.context.template});
}).then(function(result) {
return promiseSpawn('git', ['config', '--local', 'user.email', '"you@example.com"'], {cwd: t.context.template});
}).then(function(result) {
return promiseSpawn('git', ['config', '--local', 'user.name', '"Your Name"'], {cwd: t.context.template});
}).then(function(result) {
return promiseSpawn('git', ['commit', '-a', '-m', '"initial"'], {cwd: t.context.template});
});
},
beforeEach: (t) => {
t.context.dir = getTempDir();
shell.cp('-R', t.context.template, t.context.dir);

t.context.install = function(env = {}) {
return promiseSpawn('node', [installLocalBin, BASE_DIR], {cwd: t.context.dir, env});
};

},

afterEach: (t) => {
shell.rm('-rf', t.context.dir);
},

after: (t) => {
shell.rm('-rf', t.context.template);
}
};

module.exports = {
BASE_DIR,
promiseSpawn,
isInstalled,
getTempDir,
sharedHooks
};
Loading

0 comments on commit efa36ae

Please sign in to comment.