Skip to content

Commit 6716b1d

Browse files
Merge pull request #333 from technote-space/release/next-v4.2.7
release: v4.3.0
2 parents f591e1f + 5701b92 commit 6716b1d

File tree

6 files changed

+220
-75
lines changed

6 files changed

+220
-75
lines changed

__tests__/git-helper.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import {
88
testChildProcess,
99
spyOnSpawn,
1010
execCalledWith,
11+
spyOnStdout,
12+
stdoutCalledWith,
1113
} from '@technote-space/github-action-test-helper';
1214
import {Logger} from '@technote-space/github-action-log-helper';
15+
import {ExecException} from 'child_process';
1316
import {GitHelper} from '../src';
1417

18+
beforeEach(() => {
19+
Logger.resetForTesting();
20+
});
1521
const workDir = '.work';
1622
const setExists = testFs(true);
1723
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -895,3 +901,75 @@ describe('GitHelper without params', () => {
895901
});
896902
});
897903
});
904+
905+
describe('Debug', () => {
906+
testEnv();
907+
testChildProcess();
908+
909+
const helper = new GitHelper(new Logger(), {token: 'token1'});
910+
911+
it('should add command to suppress error and output', async() => {
912+
const mockExec = spyOnSpawn();
913+
const mockStdout = spyOnStdout();
914+
mockExec.mockImplementation(() => {
915+
const error: ExecException = new Error('test error');
916+
error.code = 123;
917+
throw error;
918+
});
919+
920+
await expect(helper.addOrigin(workDir, context())).rejects.toThrow('command [git remote add origin] exited with code 123.');
921+
922+
execCalledWith(mockExec, [
923+
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
924+
]);
925+
stdoutCalledWith(mockStdout, [
926+
'[command]git remote add origin',
927+
'undefined',
928+
'{}',
929+
]);
930+
});
931+
932+
it('should not add command to suppress error', async() => {
933+
process.env.ACTIONS_UTILS_DEBUG = 'true';
934+
const mockExec = spyOnSpawn();
935+
const mockStdout = spyOnStdout();
936+
mockExec.mockImplementation(() => {
937+
const error: ExecException = new Error('test error');
938+
error.code = 123;
939+
throw error;
940+
});
941+
942+
await expect(helper.addOrigin(workDir, context())).rejects.toThrow('command [git remote add origin] exited with code 123.');
943+
944+
execCalledWith(mockExec, [
945+
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1',
946+
]);
947+
stdoutCalledWith(mockStdout, [
948+
'[command]git remote add origin',
949+
'undefined',
950+
'{}',
951+
]);
952+
});
953+
954+
it('should not add command to suppress error output', async() => {
955+
process.env.ACTIONS_STEP_DEBUG = 'true';
956+
const mockExec = spyOnSpawn();
957+
const mockStdout = spyOnStdout();
958+
mockExec.mockImplementation(() => {
959+
const error: ExecException = new Error('test error');
960+
error.code = 123;
961+
throw error;
962+
});
963+
964+
await expect(helper.addOrigin(workDir, context())).rejects.toThrow('command [git remote add origin] exited with code 123.');
965+
966+
execCalledWith(mockExec, [
967+
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' || :',
968+
]);
969+
stdoutCalledWith(mockStdout, [
970+
'[command]git remote add origin',
971+
'undefined',
972+
'{}',
973+
]);
974+
});
975+
});

__tests__/utils2.test.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import {testEnv} from '@technote-space/github-action-test-helper';
33
import {Utils} from '../src';
44

5-
const {generateNewPatchVersion, generateNewMinorVersion, generateNewMajorVersion, arrayChunk, versionCompare, mask} = Utils;
6-
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit, replaceVariables} = Utils;
5+
const {generateNewPatchVersion, generateNewMinorVersion, generateNewMajorVersion, arrayChunk, versionCompare, mask, isActionsStepDebug} = Utils;
6+
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit, replaceVariables, isDebug} = Utils;
77

88
jest.useFakeTimers();
99

@@ -263,3 +263,59 @@ describe('replaceVariables', () => {
263263
])).toBe('1/2/3/${test4}');
264264
});
265265
});
266+
267+
describe('isDebug', () => {
268+
testEnv();
269+
270+
it('should return true', () => {
271+
process.env.ACTIONS_UTILS_DEBUG = 'true';
272+
expect(isDebug()).toBe(true);
273+
});
274+
275+
it('should return false 1', () => {
276+
expect(isDebug()).toBe(false);
277+
});
278+
279+
it('should return false 2', () => {
280+
process.env.ACTIONS_UTILS_DEBUG = '';
281+
expect(isDebug()).toBe(false);
282+
});
283+
284+
it('should return false 3', () => {
285+
process.env.ACTIONS_UTILS_DEBUG = '1';
286+
expect(isDebug()).toBe(false);
287+
});
288+
289+
it('should return false 4', () => {
290+
process.env.ACTIONS_UTILS_DEBUG = 'abc';
291+
expect(isDebug()).toBe(false);
292+
});
293+
});
294+
295+
describe('isActionsStepDebug', () => {
296+
testEnv();
297+
298+
it('should return true', () => {
299+
process.env.ACTIONS_STEP_DEBUG = 'true';
300+
expect(isActionsStepDebug()).toBe(true);
301+
});
302+
303+
it('should return false 1', () => {
304+
expect(isActionsStepDebug()).toBe(false);
305+
});
306+
307+
it('should return false 2', () => {
308+
process.env.ACTIONS_STEP_DEBUG = '';
309+
expect(isActionsStepDebug()).toBe(false);
310+
});
311+
312+
it('should return false 3', () => {
313+
process.env.ACTIONS_STEP_DEBUG = '1';
314+
expect(isActionsStepDebug()).toBe(false);
315+
});
316+
317+
it('should return false 4', () => {
318+
process.env.ACTIONS_STEP_DEBUG = 'abc';
319+
expect(isActionsStepDebug()).toBe(false);
320+
});
321+
});

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/github-action-helper",
3-
"version": "4.2.7",
3+
"version": "4.3.0",
44
"description": "Helper for GitHub Action.",
55
"keywords": [
66
"github",
@@ -46,17 +46,17 @@
4646
"@commitlint/config-conventional": "^11.0.0",
4747
"@technote-space/github-action-test-helper": "^0.6.4",
4848
"@types/jest": "^26.0.15",
49-
"@types/node": "^14.14.8",
49+
"@types/node": "^14.14.9",
5050
"@typescript-eslint/eslint-plugin": "^4.8.1",
5151
"@typescript-eslint/parser": "^4.8.1",
52-
"eslint": "^7.13.0",
52+
"eslint": "^7.14.0",
5353
"husky": "^4.3.0",
5454
"jest": "^26.6.3",
5555
"jest-circus": "^26.6.3",
5656
"lint-staged": "^10.5.1",
5757
"nock": "^13.0.5",
5858
"ts-jest": "^26.4.4",
59-
"typescript": "^4.0.5"
59+
"typescript": "^4.1.2"
6060
},
6161
"publishConfig": {
6262
"access": "public"

src/git-helper.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
getAccessToken,
1616
generateNewMinorVersion,
1717
generateNewMajorVersion,
18+
isDebug,
19+
isActionsStepDebug,
1820
} from './utils';
1921
import {getGitUrlWithToken} from './context-helper';
2022

@@ -63,6 +65,16 @@ export default class GitHelper {
6365
}
6466
}
6567

68+
/**
69+
* @return {boolean} should suppress error
70+
*/
71+
private shouldSuppressError = (): boolean => !isDebug();
72+
73+
/**
74+
* @return {boolean} is quiet?
75+
*/
76+
private isQuiet = (): boolean => !isActionsStepDebug() && (!this.origin || this.quietIfNotOrigin);
77+
6678
/**
6779
* @param {string} workDir work dir
6880
* @param {string[]} commands commands
@@ -147,15 +159,10 @@ export default class GitHelper {
147159
args: [this.getRemoteName(), getGitUrlWithToken(context, this.token)],
148160
quiet: this.isQuiet(),
149161
altCommand: `git remote add ${this.getRemoteName()}`,
150-
suppressError: true,
162+
suppressError: this.shouldSuppressError(),
151163
});
152164
};
153165

154-
/**
155-
* @return {boolean} is quiet?
156-
*/
157-
private isQuiet = (): boolean => !this.origin || this.quietIfNotOrigin;
158-
159166
/**
160167
* @param {string} workDir work dir
161168
* @return {Promise<string>} branch name
@@ -167,7 +174,7 @@ export default class GitHelper {
167174
return (await this.runCommand(workDir, {
168175
command: 'git rev-parse',
169176
args: ['--abbrev-ref', 'HEAD'],
170-
suppressError: true,
177+
suppressError: this.shouldSuppressError(),
171178
stderrToStdout: true,
172179
}))[0].stdout[0]?.trim() ?? '';
173180
};
@@ -184,7 +191,7 @@ export default class GitHelper {
184191
args: [`--branch=${branch}`, this.cloneDepth, this.getRemote(context), '.'],
185192
quiet: this.isQuiet(),
186193
altCommand: `git clone --branch=${branch}`,
187-
suppressError: true,
194+
suppressError: this.shouldSuppressError(),
188195
});
189196
};
190197

@@ -200,7 +207,7 @@ export default class GitHelper {
200207
args: [this.cloneDepth, this.getRemote(context), '.'],
201208
quiet: this.isQuiet(),
202209
altCommand: 'git clone',
203-
suppressError: true,
210+
suppressError: this.shouldSuppressError(),
204211
},
205212
{
206213
command: 'git fetch',
@@ -261,7 +268,7 @@ export default class GitHelper {
261268
this.getRemoteName(),
262269
...(refspec ?? []),
263270
],
264-
suppressError: true,
271+
suppressError: this.shouldSuppressError(),
265272
stderrToStdout: true,
266273
});
267274
};
@@ -295,7 +302,7 @@ export default class GitHelper {
295302
args: ['--prune', '--no-recurse-submodules', this.cloneDepth, this.getRemote(context), `+refs/heads/${branchName}:refs/remotes/${this.getRemoteName()}/${branchName}`],
296303
quiet: this.isQuiet(),
297304
altCommand: `git fetch --prune --no-recurse-submodules${this.cloneDepth} ${this.getRemoteName()} +refs/heads/${branchName}:refs/remotes/${this.getRemoteName()}/${branchName}`,
298-
suppressError: true,
305+
suppressError: this.shouldSuppressError(),
299306
});
300307
};
301308

@@ -317,7 +324,7 @@ export default class GitHelper {
317324
await this.runCommand(workDir, {
318325
command: 'git checkout',
319326
args: ['-b', branch, `${this.getRemoteName()}/${branch}`],
320-
suppressError: true,
327+
suppressError: this.shouldSuppressError(),
321328
stderrToStdout: true,
322329
});
323330
};
@@ -465,7 +472,7 @@ export default class GitHelper {
465472
args: [this.getRemote(context), '--delete', ...tags],
466473
quiet: this.isQuiet(),
467474
altCommand: `git push ${this.getRemoteName()} --delete ${tags.join(' ')}`,
468-
suppressError: true,
475+
suppressError: this.shouldSuppressError(),
469476
})),
470477
);
471478
await this.deleteLocalTag(workDir, tags, splitSize);
@@ -506,7 +513,7 @@ export default class GitHelper {
506513
tags => ({
507514
command: 'git tag',
508515
args: ['-d', ...tags],
509-
suppressError: true,
516+
suppressError: this.shouldSuppressError(),
510517
stderrToStdout: true,
511518
}),
512519
));
@@ -550,7 +557,7 @@ export default class GitHelper {
550557
args: args.concat([this.getRemote(context), `${branch}:refs/heads/${branch}`]),
551558
quiet: this.isQuiet(),
552559
altCommand: `git push ${args.concat([this.getRemoteName(), `${branch}:refs/heads/${branch}`]).join(' ')}`,
553-
suppressError: true,
560+
suppressError: this.shouldSuppressError(),
554561
});
555562
};
556563

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,7 @@ export const replaceVariables = async(string: string, variables: { key: string;
252252

253253
return replaced;
254254
};
255+
256+
export const isDebug = (): boolean => process.env.ACTIONS_UTILS_DEBUG === 'true';
257+
258+
export const isActionsStepDebug = (): boolean => process.env.ACTIONS_STEP_DEBUG === 'true';

0 commit comments

Comments
 (0)