Skip to content

Commit

Permalink
Merge pull request #1029 from bertdeblock/remove-use-of-mockery
Browse files Browse the repository at this point in the history
Remove use of `mockery`
  • Loading branch information
bertdeblock authored Dec 17, 2024
2 parents 762ca35 + d162b0e commit 98ec8fe
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 38 deletions.
24 changes: 22 additions & 2 deletions lib/utils/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const execa = require('execa');
const debug = require('debug')('ember-try:utils:run');

module.exports = async function run(command, args, _options) {
let runFunction = originalRunFunction;

async function originalRunFunction(command, args, _options) {
let options = Object.assign({ stdio: 'inherit', shell: true }, _options);

if (process.env.SHUT_UP) {
Expand All @@ -21,4 +23,22 @@ module.exports = async function run(command, args, _options) {
// TODO: should refactor this to throw an error (easier to track down stack traces)
throw error.exitCode;
}
};
}

function run(command, args, options) {
return runFunction(command, args, options);
}

function mockRun(mockedRunFunction) {
runFunction = mockedRunFunction;
}

function restoreRun() {
runFunction = originalRunFunction;
}

module.exports = run;

module.exports._originalRunFunction = originalRunFunction;
module.exports._mockRun = mockRun;
module.exports._restoreRun = restoreRun;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-n": "^17.15.0",
"mocha": "^10.8.2",
"mockery": "^2.1.0",
"nyc": "^17.1.0",
"prettier": "^3.3.3",
"release-it": "^17.10.0",
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/generate-mock-run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { _originalRunFunction } = require('../../lib/utils/run');

module.exports = function generateMockRun() {
let mockedRuns = [];
let options = { allowPassthrough: true };
Expand Down Expand Up @@ -39,7 +41,7 @@ function isCommandMocked(mockedRun, actualCommand, actualArgs) {
}

function passthrough() {
return require('../../lib/utils/run');
return _originalRunFunction;
}

function mockedCommandIsEmberAndArgumentsMatch(
Expand Down
38 changes: 20 additions & 18 deletions test/tasks/try-each-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path');
const fs = require('fs-extra');
const fixturePackage = require('../fixtures/package.json');
const writeJSONFile = require('../helpers/write-json-file');
const mockery = require('mockery');
const { _mockRun, _restoreRun } = require('../../lib/utils/run');

/* Some of the tests in this file intentionally DO NOT stub dependency manager adapter*/
const StubDependencyAdapter = require('../helpers/stub-dependency-manager-adapter');
Expand Down Expand Up @@ -53,16 +53,11 @@ describe('tryEach', () => {
beforeEach(() => {
tmpdir = tmp.in(tmproot);
process.chdir(tmpdir);
mockery.enable({
warnOnUnregistered: false,
useCleanCache: true,
});
require('chalk').level = 0;
});

afterEach(() => {
mockery.deregisterAll();
mockery.disable();
_restoreRun();
process.chdir(root);
return fs.remove(tmproot);
});
Expand All @@ -75,7 +70,7 @@ describe('tryEach', () => {
return Promise.resolve(0);
});

mockery.registerMock('./run', mockedRun);
_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -130,7 +125,7 @@ describe('tryEach', () => {
}
});

mockery.registerMock('./run', mockedRun);
_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -194,7 +189,8 @@ describe('tryEach', () => {
},
},
]);
mockery.registerMock('./run', mockedRun);

_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -242,7 +238,7 @@ describe('tryEach', () => {
return Promise.resolve(0);
});

mockery.registerMock('./run', mockedRun);
_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -295,7 +291,8 @@ describe('tryEach', () => {
let mockedRun = generateMockRun('ember test', () => {
return Promise.reject(1);
});
mockery.registerMock('./run', mockedRun);

_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -345,7 +342,8 @@ describe('tryEach', () => {
let mockedRun = generateMockRun('ember test', () => {
return Promise.reject(1);
});
mockery.registerMock('./run', mockedRun);

_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -396,7 +394,8 @@ describe('tryEach', () => {
let mockedRun = generateMockRun('ember test', () => {
return Promise.resolve(0);
});
mockery.registerMock('./run', mockedRun);

_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -451,7 +450,7 @@ describe('tryEach', () => {
return Promise.resolve(0);
});

mockery.registerMock('./run', mockedRun);
_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -498,7 +497,8 @@ describe('tryEach', () => {
ranPassedInCommand = true;
return Promise.resolve(0);
});
mockery.registerMock('./run', mockedRun);

_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -570,7 +570,8 @@ describe('tryEach', () => {
},
},
]);
mockery.registerMock('./run', mockedRun);

_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down Expand Up @@ -673,7 +674,8 @@ describe('tryEach', () => {
actualOptions.push(opts);
return Promise.resolve(0);
});
mockery.registerMock('./run', mockedRun);

_mockRun(mockedRun);

let output = [];
let outputFn = function (log) {
Expand Down
14 changes: 3 additions & 11 deletions test/utils/run-command-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
'use strict';

const expect = require('chai').expect;
const mockery = require('mockery');
const { _mockRun, _restoreRun } = require('../../lib/utils/run');

describe('utils/run-command', () => {
beforeEach(() => {
mockery.enable({
warnOnUnregistered: false,
useCleanCache: true,
});
});

afterEach(() => {
mockery.deregisterAll();
mockery.disable();
_restoreRun();
});

it('passes arguments to run', () => {
Expand All @@ -26,7 +18,7 @@ describe('utils/run-command', () => {
return Promise.resolve(0);
};

mockery.registerMock('./run', mockedRun);
_mockRun(mockedRun);

let runCommand = require('../../lib/utils/run-command');

Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6328,11 +6328,6 @@ mocha@^10.8.2:
yargs-parser "^20.2.9"
yargs-unparser "^2.0.0"

mockery@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/mockery/-/mockery-2.1.0.tgz#5b0aef1ff564f0f8139445e165536c7909713470"
integrity sha512-9VkOmxKlWXoDO/h1jDZaS4lH33aWfRiJiNT/tKj+8OGzrcFDLo8d0syGdbsc3Bc4GvRXPb+NMMvojotmuGJTvA==

morgan@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/morgan/-/morgan-1.10.0.tgz#091778abc1fc47cd3509824653dae1faab6b17d7"
Expand Down

0 comments on commit 98ec8fe

Please sign in to comment.