Skip to content

Commit

Permalink
fix: get-root issues and tests on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Dec 8, 2021
1 parent 78fb46f commit 3f8433e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"install": "node src/install.js",
"lint": "vjsstandard",
"pretest": "npm run lint",
"test": "npm run test:unit && npm run test:verify",
"test:unit": "ava --timeout 5s ./test/index.test.js",
"test": "npm run test:unit",
"test:unit": "ava --timeout 1m ./test/index.test.js",
"test:verify": "vjsverify --skip-syntax --verbose",
"preversion": "npm test",
"version": "is-prerelease || npm run update-changelog && git add CHANGELOG.md",
Expand Down
2 changes: 1 addition & 1 deletion src/get-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const getRoot = () => {
const cwd = process.env.INIT_CWD ? process.env.INIT_CWD : process.cwd();
const gitRootResult = spawnSync('git', ['rev-parse', '--show-toplevel'], {cwd});
const rootDir = gitRootResult.stdout.toString().trim();
const rootDir = gitRootResult.stdout ? gitRootResult.stdout.toString().trim() : '';

if (gitRootResult.status !== 0 || !rootDir || !fs.existsSync(path.join(rootDir, '.git'))) {
return '';
Expand Down
4 changes: 2 additions & 2 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if (!fs.existsSync(infoDir)) {
fs.mkdirSync(infoDir);
}

const uninstall = spawnSync(path.join(__dirname, 'uninstall.js'), [], {cwd: rootDir});
const uninstall = spawnSync('node', [path.join(__dirname, 'uninstall.js')], {cwd: rootDir});

// add to git config
const configOne = spawnSync(
Expand All @@ -39,7 +39,7 @@ const configOne = spawnSync(
);
const configTwo = spawnSync(
'git',
['config', '--local', 'merge.npm-merge-driver-install.driver', `${merge} %A %O %B %P`],
['config', '--local', 'merge.npm-merge-driver-install.driver', `node ${merge} %A %O %B %P`],
{cwd: rootDir}
);

Expand Down
2 changes: 2 additions & 0 deletions test/fakegit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
process.exit(1);
62 changes: 50 additions & 12 deletions test.js → test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const path = require('path');
const shell = require('shelljs');
const uuid = require('uuid');
const spawnPromise = require('@brandonocasey/spawn-promise');
const os = require('os');

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

const getTempDir = function() {
Expand All @@ -25,6 +26,10 @@ const isInstalled = function(dir) {
return false;
}

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

const attr = shell.cat(gitAttrPath);

if (!(/npm-merge-driver-install/i).test(attr)) {
Expand All @@ -39,6 +44,8 @@ const promiseSpawn = function(bin, args, options = {}) {

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) {
Expand All @@ -57,21 +64,27 @@ test.before((t) => {
shell.mkdir(t.context.template);

// create the package.json
return promiseSpawn('npm', ['init', '-y'], {cwd: t.context.template}).then(function() {
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('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});
});

});

test.beforeEach((t) => {
t.context.dir = getTempDir();
shell.cp('-R', t.context.template, t.context.dir);

t.context.link = function(env = {}) {
return promiseSpawn('npm', ['install', BASE_DIR], {
cwd: t.context.dir,
env: Object.assign({PATH: process.env.PATH}, env)
return promiseSpawn('npm', ['install', BASE_DIR], {cwd: t.context.dir, env}).then(function(result) {
return Promise.resolve(result);
});
};

Expand All @@ -93,6 +106,26 @@ test('does not install without .git', (t) => {
});
});

test('does not install with bad git command', (t) => {
// put the tempdir path as highest priorty in PATH
let separator = ':';
let gitDest = path.join(t.context.dir, 'git');

if (os.platform() === 'win32') {
separator = ';';
gitDest += '.exe';
}
const PATH = `${t.context.dir}${separator}${process.env.PATH}`;

// move a fake git binary into the temp context dir
// this will cause git to fail to run
shell.cp(path.join(__dirname, 'fakegit.js'), gitDest);

return t.context.link({PATH}).then(function(result) {
t.false(isInstalled(t.context.dir));
});
});

test('does not install if in ci', (t) => {
return t.context.link({TRAVIS: 'some-value'}).then(function(result) {
t.false(isInstalled(t.context.dir));
Expand All @@ -117,17 +150,22 @@ test('installs in ci if NPM_MERGE_DRIVER_IGNORE_CI=true', (t) => {
});
});

test('installs in cwd if run as binary', (t) => {
return promiseSpawn(path.join(BASE_DIR, 'src', 'install.js'), [], {cwd: t.context.dir, env: {PATH: process.env.PATH}}).then(function(result) {
t.true(isInstalled(t.context.dir));
// windows can't run install.js as a binary
if (os.platform() !== 'win32') {
test('installs in cwd if run as binary', (t) => {
return promiseSpawn(path.join(BASE_DIR, 'src', 'install.js'), [], {cwd: t.context.dir}).then(function(result) {
t.true(isInstalled(t.context.dir));
});
});
});
}

test('Can install after npm install with binary', (t) => {
return t.context.link({NPM_MERGE_DRIVER_SKIP_INSTALL: 'some-value'}).then(function(result) {
t.false(isInstalled(t.context.dir));
return promiseSpawn('npm-merge-driver-install', [], {cwd: t.context.dir, env: {PATH: process.env.PATH}});
}).then(function(retult) {
return promiseSpawn('npm', ['bin'], {cwd: t.context.dir});
}).then(function(result) {
return promiseSpawn(path.join(result.stdout.trim(), 'npm-merge-driver-install'), [], {cwd: t.context.dir});
}).then(function(result) {
t.true(isInstalled(t.context.dir));
});
});

0 comments on commit 3f8433e

Please sign in to comment.