Skip to content

Commit

Permalink
refactor: remove shelljs from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed May 2, 2020
1 parent c7af218 commit 88aa26f
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 103 deletions.
26 changes: 0 additions & 26 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"proxyquire": "^2.1.0",
"semantic-release": "15.13.18",
"semver": "6.2.0",
"shelljs": "0.7.6",
"sinon": "^6.3.4",
"uuid": "3.3.2"
},
Expand Down
6 changes: 3 additions & 3 deletions src/commitizen/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function addPathToAdapterConfig (cliPath, repoPath, adapterNpmName) {
}
};

let packageJsonPath = path.join(getNearestProjectRootDirectory(), 'package.json');
let packageJsonPath = path.join(getNearestProjectRootDirectory(repoPath), 'package.json');
let packageJsonString = fs.readFileSync(packageJsonPath, 'utf-8');
// tries to detect the indentation and falls back to a default if it can't
let indent = detectIndent(packageJsonString).indent || ' ';
Expand Down Expand Up @@ -111,8 +111,8 @@ function getNearestNodeModulesDirectory (options) {
/**
* Gets the nearest project root directory
*/
function getNearestProjectRootDirectory (options) {
return path.join(process.cwd(), getNearestNodeModulesDirectory(options), '/../');
function getNearestProjectRootDirectory (repoPath, options) {
return path.join(repoPath, getNearestNodeModulesDirectory(options), '/../');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/commitizen/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function init (repoPath, adapterNpmName, {
checkRequiredArguments(repoPath, adapterNpmName);

// Load the current adapter config
let adapterConfig = loadAdapterConfig();
let adapterConfig = loadAdapterConfig(repoPath);

// Get the npm string mappings based on the arguments provided
let stringMappings = yarn ? getYarnAddStringMappings(dev, exact, force) : getNpmInstallStringMappings(save, saveDev, saveExact, force);
Expand Down Expand Up @@ -112,8 +112,8 @@ function checkRequiredArguments (path, adapterNpmName) {
* CONFIG
* Loads and returns the adapter config at key config.commitizen, if it exists
*/
function loadAdapterConfig () {
let config = configLoader.load();
function loadAdapterConfig (cwd) {
let config = configLoader.load(null, cwd);
if (config) {
return config;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/commitizen/staging.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export { isClean };
function isClean (repoPath, done) {
exec('git diff --no-ext-diff --name-only && git diff --no-ext-diff --cached --name-only', {
maxBuffer: Infinity,
cwd: repoPath || process.cwd()
cwd: repoPath
}, function (error, stdout) {
if (error) {
return done(error);
Expand Down
4 changes: 2 additions & 2 deletions src/configLoader/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export default loader;
/**
* Get content of the configuration file
* @param {String} config - partial path to configuration file
* @param {String} [cwd = process.cwd()] - directory path which will be joined with config argument
* @param {String} cwd - directory path which will be joined with config argument
* @return {Object|undefined}
*/
function loader (configs, config, cwd) {
var content;
var directory = cwd || process.cwd();
var directory = cwd;

// If config option is given, attempt to load it
if (config) {
Expand Down
6 changes: 0 additions & 6 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ let config = {
*/
maxTimeout: 240000,

/**
* Whether shelljs should suppress output, should be true
* unless debugging.
*/
silent: true,

/**
* Whether or not to keep the artifacts of the tests after
* they've run.
Expand Down
7 changes: 1 addition & 6 deletions test/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ import * as clean from './tools/clean';
import * as files from './tools/files';
import * as util from '../src/common/util';
import { config as userConfig } from './config';
import * as sh from 'shelljs'; // local instance
import _ from 'lodash';

// Clone the user's config so we don't get caught w/our pants down
let patchedConfig = _.cloneDeep(userConfig);

function bootstrap () {

// Patch any shelljs specific config settings
sh.config.silent = patchedConfig.silent || true;

// Return the patched config and shelljs instance
// Return the patched config
return {
config: patchedConfig,
sh,
repo,
clean,
util,
Expand Down
10 changes: 5 additions & 5 deletions test/tests/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import { isFunction } from '../../src/common/util';
import { bootstrap } from '../tester';

// Destructure some things based on the bootstrap process
let { config, sh, repo, clean } = bootstrap();
let { config, repo, clean } = bootstrap();

before(function () {
// Creates the temp path
clean.before(sh, config.paths.tmp);
clean.before(config.paths.tmp);
});

beforeEach(function () {
this.timeout(config.maxTimeout); // this could take a while
repo.createEndUser(sh, config.paths.endUserRepo);
repo.createEndUser(config.paths.endUserRepo);
});

describe('adapter', function () {
Expand Down Expand Up @@ -180,12 +180,12 @@ describe('adapter', function () {
afterEach(function () {
this.timeout(config.maxTimeout); // this could take a while
// All this should do is archive the tmp path to the artifacts
clean.afterEach(sh, config.paths.tmp, config.preserve);
clean.afterEach(config.paths.tmp, config.preserve);
});

after(function () {
this.timeout(config.maxTimeout); // this could take a while
// Once everything is done, the artifacts should be cleaned up based on
// the preserve setting in the config
clean.after(sh, config.paths.tmp, config.preserve);
clean.after(config.paths.tmp, config.preserve);
});
10 changes: 5 additions & 5 deletions test/tests/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import { addFile as gitAddFile, init as gitInit, log, whatChanged } from '../../
import { commit as commitizenCommit, init as commitizenInit } from '../../src/commitizen';

// Destructure some things for cleaner tests
let { config, sh, repo, clean, files } = bootstrap();
let { config, repo, clean, files } = bootstrap();
let { writeFilesToPath } = files;

before(function () {
// Creates the temp path
clean.before(sh, config.paths.tmp);
clean.before(config.paths.tmp);
});

beforeEach(function () {
this.timeout(config.maxTimeout); // this could take a while
/* istanbul ignore next */
repo.createEndUser(sh, config.paths.endUserRepo);
repo.createEndUser(config.paths.endUserRepo);
});

describe('commit', function () {
Expand Down Expand Up @@ -316,14 +316,14 @@ ${(os.platform === 'win32') ? '' : ' '}
afterEach(function () {
this.timeout(config.maxTimeout); // this could take a while
// All this should do is archive the tmp path to the artifacts
clean.afterEach(sh, config.paths.tmp, config.preserve);
clean.afterEach(config.paths.tmp, config.preserve);
});

after(function () {
this.timeout(config.maxTimeout); // this could take a while
// Once everything is done, the artifacts should be cleaned up based on
// the preserve setting in the config
clean.after(sh, config.paths.tmp, config.preserve);
clean.after(config.paths.tmp, config.preserve);
});

/**
Expand Down
21 changes: 5 additions & 16 deletions test/tests/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import { init as commitizenInit } from '../../src/commitizen';
import { bootstrap } from '../tester';

// Destructure some things based on the bootstrap process
let { config, sh, repo, clean, util } = bootstrap();
let { config, repo, clean, util } = bootstrap();

before(function () {
// Creates the temp path
clean.before(sh, config.paths.tmp);
clean.before(config.paths.tmp);
});

beforeEach(function () {
this.timeout(config.maxTimeout); // this could take a while
repo.createEndUser(sh, config.paths.endUserRepo);
repo.createEndUser(config.paths.endUserRepo);
});

describe('init', function () {
Expand Down Expand Up @@ -65,11 +65,9 @@ describe('init', function () {
// SETUP

// Add a first adapter
sh.cd(config.paths.endUserRepo);
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { saveDev: true });

// TEST
sh.cd(config.paths.endUserRepo);
// Adding a second adapter
expect(function () {
commitizenInit(config.paths.endUserRepo, 'cz-jira-smart-commit', { saveDev: true });
Expand All @@ -90,7 +88,6 @@ describe('init', function () {
// SETUP

// Add a first adapter
sh.cd(config.paths.endUserRepo);
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { saveDev: true });

// TEST
Expand All @@ -113,7 +110,6 @@ describe('init', function () {
// SETUP

// Add a first adapter
sh.cd(config.paths.endUserRepo);
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog');
let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo);

Expand All @@ -139,7 +135,6 @@ describe('init', function () {
// SETUP

// Add a first adapter
sh.cd(config.paths.endUserRepo);
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { saveExact: true });
let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo);

Expand All @@ -162,7 +157,6 @@ describe('init', function () {
// SETUP

// Add a first adapter
sh.cd(config.paths.endUserRepo);
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { includeCommitizen: true });
let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo);

Expand Down Expand Up @@ -220,11 +214,9 @@ describe('init', function () {
// SETUP

// Add a first adapter
sh.cd(config.paths.endUserRepo);
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true });

// TEST
sh.cd(config.paths.endUserRepo);
// Adding a second adapter
expect(function () {
commitizenInit(config.paths.endUserRepo, 'cz-jira-smart-commit', { yarn: true, dev: true });
Expand All @@ -245,7 +237,6 @@ describe('init', function () {
// SETUP

// Add a first adapter
sh.cd(config.paths.endUserRepo);
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true });

// TEST
Expand All @@ -268,7 +259,6 @@ describe('init', function () {
// SETUP

// Add a first adapter
sh.cd(config.paths.endUserRepo);
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true });
let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo);

Expand All @@ -294,7 +284,6 @@ describe('init', function () {
// SETUP

// Add a first adapter
sh.cd(config.paths.endUserRepo);
commitizenInit(config.paths.endUserRepo, 'cz-conventional-changelog', { yarn: true, dev: true, exact: true });
let packageJson = util.getParsedPackageJsonFromPath(config.paths.endUserRepo);

Expand All @@ -315,12 +304,12 @@ describe('init', function () {
afterEach(function () {
this.timeout(config.maxTimeout); // this could take a while
// All this should do is archive the tmp path to the artifacts
clean.afterEach(sh, config.paths.tmp, config.preserve);
clean.afterEach(config.paths.tmp, config.preserve);
});

after(function () {
this.timeout(config.maxTimeout); // this could take a while
// Once everything is done, the artifacts should be cleaned up based on
// the preserve setting in the config
clean.after(sh, config.paths.tmp, config.preserve);
clean.after(config.paths.tmp, config.preserve);
});
10 changes: 5 additions & 5 deletions test/tests/staging.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import { init as gitInit, addPath as gitAdd } from '../../src/git';
import { staging } from '../../src/commitizen';

// Destructure some things for cleaner tests
let { config, sh, repo, clean, files } = bootstrap();
let { config, repo, clean, files } = bootstrap();
let { writeFilesToPath } = files;

before(function () {
// Creates the temp path
clean.before(sh, config.paths.tmp);
clean.before(config.paths.tmp);
});

beforeEach(function () {
this.timeout(config.maxTimeout); // this could take a while
repo.createEndUser(sh, config.paths.endUserRepo);
repo.createEndUser(config.paths.endUserRepo);
});

describe('staging', function () {
Expand Down Expand Up @@ -83,12 +83,12 @@ describe('staging', function () {
afterEach(function () {
this.timeout(config.maxTimeout); // this could take a while
// All this should do is archive the tmp path to the artifacts
clean.afterEach(sh, config.paths.tmp, config.preserve);
clean.afterEach(config.paths.tmp, config.preserve);
});

after(function () {
this.timeout(config.maxTimeout); // this could take a while
// Once everything is done, the artifacts should be cleaned up based on
// the preserve setting in the config
clean.after(sh, config.paths.tmp, config.preserve);
clean.after(config.paths.tmp, config.preserve);
});
Loading

0 comments on commit 88aa26f

Please sign in to comment.