Skip to content

Commit ebcefac

Browse files
committed
feature: @putout/cli-staged: get rid of mock-require
1 parent fee8ab2 commit ebcefac

File tree

3 files changed

+34
-76
lines changed

3 files changed

+34
-76
lines changed

packages/cli-staged/lib/staged.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

33
const {join} = require('node:path');
4-
const {spawnSync} = require('node:child_process');
4+
const {spawnSync: _spawnSync} = require('node:child_process');
5+
6+
const _porcelain = require('@putout/git-status-porcelain');
57

6-
const porcelain = require('@putout/git-status-porcelain');
7-
const once = require('once');
88
const fullstore = require('fullstore');
99

1010
const namesStore = fullstore([]);
1111

12-
const findGit = once(async ({findUp}) => {
12+
const findGit = async ({findUp}) => {
1313
const type = 'directory';
1414
const gitDir = await findUp('.git', {
1515
type,
@@ -19,11 +19,17 @@ const findGit = once(async ({findUp}) => {
1919
throw Error('not git repository');
2020

2121
return gitDir.replace(/\.git$/, '');
22-
});
22+
};
2323

2424
const joinDir = (a) => (b) => join(a, b);
2525

26-
module.exports.get = async function get({findUp, isSupported}) {
26+
module.exports.get = async function get(overrides = {}) {
27+
const {
28+
findUp,
29+
isSupported,
30+
porcelain = _porcelain,
31+
} = overrides;
32+
2733
const dir = await findGit({
2834
findUp,
2935
});
@@ -39,7 +45,13 @@ module.exports.get = async function get({findUp, isSupported}) {
3945
return names.map(joinDir(dir));
4046
};
4147

42-
module.exports.set = async function set({findUp}) {
48+
module.exports.set = async function set(overrides = {}) {
49+
const {
50+
findUp,
51+
porcelain = _porcelain,
52+
spawnSync = _spawnSync,
53+
} = overrides;
54+
4355
const dir = await findGit({
4456
findUp,
4557
});
@@ -58,12 +70,14 @@ module.exports.set = async function set({findUp}) {
5870
}
5971

6072
if (namesToAdd.length)
61-
add(namesToAdd.map(joinDir(dir)));
73+
add(namesToAdd.map(joinDir(dir)), {
74+
spawnSync,
75+
});
6276

6377
return staged;
6478
};
6579

66-
function add(names) {
80+
function add(names, {spawnSync}) {
6781
spawnSync('git', [
6882
'add',
6983
...names,

packages/cli-staged/lib/staged.spec.js

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22

33
const tryToCatch = require('try-to-catch');
44
const {test, stub} = require('supertape');
5-
const mockRequire = require('mock-require');
6-
7-
const {reRequire, stopAll} = mockRequire;
5+
const {get, set} = require('./staged');
86

97
test('putout: cli: staged', async (t) => {
108
const findUp = stub().returns('.');
119

12-
const {get} = reRequire('./staged');
13-
1410
await get({
1511
findUp,
1612
isSupported: Boolean,
1713
});
1814

19-
stopAll();
20-
2115
const type = 'directory';
2216

2317
const args = ['.git', {
@@ -34,16 +28,11 @@ test('putout: cli: staged: set: porcelain', async (t) => {
3428

3529
const porcelain = stub().returns(['packages/putout/lib/cli/index.js']);
3630

37-
mockRequire('@putout/git-status-porcelain', porcelain);
38-
39-
const {set} = reRequire('./staged');
40-
4131
await set({
4232
findUp,
33+
porcelain,
4334
});
4435

45-
stopAll();
46-
4736
const expected = [{
4837
unstaged: true,
4938
}];
@@ -58,17 +47,12 @@ test('putout: cli: staged: get: statusMatrix', async (t) => {
5847

5948
const porcelain = stub().returns(['packages/putout/lib/cli/index.js']);
6049

61-
mockRequire('@putout/git-status-porcelain', porcelain);
62-
63-
const {get} = reRequire('./staged');
64-
6550
await get({
6651
findUp,
6752
isSupported: Boolean,
53+
porcelain,
6854
});
6955

70-
stopAll();
71-
7256
const expected = {
7357
added: true,
7458
modified: true,
@@ -85,16 +69,12 @@ test('putout: cli: staged: get: statusMatrix: result', async (t) => {
8569

8670
const porcelain = stub().returns(['packages/putout/lib/cli/index.js']);
8771

88-
mockRequire('@putout/git-status-porcelain', porcelain);
89-
const {get} = reRequire('./staged');
90-
9172
const names = await get({
9273
findUp,
9374
isSupported: Boolean,
75+
porcelain,
9476
});
9577

96-
stopAll();
97-
9878
const expected = [
9979
'/putout/packages/putout/lib/cli/index.js',
10080
];
@@ -109,16 +89,11 @@ test('putout: cli: staged: set: findUp', async (t) => {
10989

11090
const porcelain = stub().returns(['packages/putout/lib/cli/index.js']);
11191

112-
mockRequire('@putout/git-status-porcelain', porcelain);
113-
114-
const {set} = reRequire('./staged');
115-
11692
await set({
11793
findUp,
94+
porcelain,
11895
});
11996

120-
stopAll();
121-
12297
const type = 'directory';
12398

12499
t.calledWith(findUp, ['.git', {
@@ -131,27 +106,10 @@ test('putout: cli: staged: set: findUp: not found', async (t) => {
131106
const dir = '';
132107
const findUp = stub().returns(dir);
133108

134-
const statusMatrix = stub().returns([
135-
[
136-
'packages/putout/lib/cli/index.js',
137-
1,
138-
2,
139-
2,
140-
],
141-
]);
142-
143-
mockRequire('isomorphic-git', {
144-
statusMatrix,
145-
});
146-
147-
const {set} = reRequire('./staged');
148-
149109
await tryToCatch(set, {
150110
findUp,
151111
});
152112

153-
stopAll();
154-
155113
const type = 'directory';
156114

157115
t.calledWith(findUp, ['.git', {
@@ -179,24 +137,18 @@ test('putout: cli: staged: add', async (t) => {
179137

180138
const spawnSync = stub();
181139

182-
mockRequire('node:child_process', {
183-
spawnSync,
184-
});
185-
mockRequire('@putout/git-status-porcelain', porcelain);
186-
187-
const {get, set} = reRequire('./staged');
188-
189140
await get({
190141
findUp,
191142
isSupported: Boolean,
143+
porcelain,
192144
});
193145

194146
await set({
195147
findUp,
148+
spawnSync,
149+
porcelain,
196150
});
197151

198-
stopAll();
199-
200152
const filepath = 'packages/putout/lib/cli/index.js';
201153

202154
const args = ['git', [
@@ -228,24 +180,18 @@ test('putout: cli: staged: no files', async (t) => {
228180
const spawnSync = stub();
229181
const isSupported = stub().returns(false);
230182

231-
mockRequire('node:child_process', {
232-
spawnSync,
233-
});
234-
mockRequire('@putout/git-status-porcelain', porcelain);
235-
236-
const {get, set} = reRequire('./staged');
237-
238183
await get({
239184
findUp,
240185
isSupported,
186+
porcelain,
241187
});
242188

243189
await set({
244190
findUp,
191+
spawnSync,
192+
porcelain,
245193
});
246194

247-
stopAll();
248-
249195
t.notCalled(spawnSync);
250196
t.end();
251197
});

packages/cli-staged/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@
3636
"gitignore"
3737
],
3838
"devDependencies": {
39-
"@putout/eslint-flat": "^3.0.0",
4039
"c8": "^10.0.0",
4140
"eslint": "^10.0.0-alpha.0",
4241
"eslint-plugin-n": "^17.0.0",
4342
"eslint-plugin-putout": "^29.0.0",
4443
"madrun": "^11.0.0",
45-
"mock-require": "^3.0.3",
4644
"nodemon": "^3.0.1",
4745
"putout": "*",
4846
"supertape": "^11.0.3"

0 commit comments

Comments
 (0)