Skip to content

Commit 2751e4d

Browse files
feat: consider ACTIONS_STEP_DEBUG (#334)
1 parent b9cb9b2 commit 2751e4d

File tree

4 files changed

+109
-17
lines changed

4 files changed

+109
-17
lines changed

__tests__/git-helper.test.ts

Lines changed: 56 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,53 @@ describe('GitHelper without params', () => {
895901
});
896902
});
897903
});
904+
905+
describe('ACTIONS_STEP_DEBUG test', () => {
906+
testEnv();
907+
testChildProcess();
908+
909+
const helper = new GitHelper(new Logger(), {token: 'token1'});
910+
911+
it('should add suppress error command', 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 suppress error command', async() => {
933+
process.env.ACTIONS_STEP_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\'',
946+
]);
947+
stdoutCalledWith(mockStdout, [
948+
'[command]git remote add origin',
949+
'undefined',
950+
'{}',
951+
]);
952+
});
953+
});

__tests__/utils2.test.ts

Lines changed: 30 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} = Utils;
77

88
jest.useFakeTimers();
99

@@ -263,3 +263,31 @@ describe('replaceVariables', () => {
263263
])).toBe('1/2/3/${test4}');
264264
});
265265
});
266+
267+
describe('isActionsStepDebug', () => {
268+
testEnv();
269+
270+
it('should return true', () => {
271+
process.env.ACTIONS_STEP_DEBUG = 'true';
272+
expect(isActionsStepDebug()).toBe(true);
273+
});
274+
275+
it('should return false 1', () => {
276+
expect(isActionsStepDebug()).toBe(false);
277+
});
278+
279+
it('should return false 2', () => {
280+
process.env.ACTIONS_STEP_DEBUG = '';
281+
expect(isActionsStepDebug()).toBe(false);
282+
});
283+
284+
it('should return false 3', () => {
285+
process.env.ACTIONS_STEP_DEBUG = '1';
286+
expect(isActionsStepDebug()).toBe(false);
287+
});
288+
289+
it('should return false 4', () => {
290+
process.env.ACTIONS_STEP_DEBUG = 'abc';
291+
expect(isActionsStepDebug()).toBe(false);
292+
});
293+
});

src/git-helper.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
getAccessToken,
1616
generateNewMinorVersion,
1717
generateNewMajorVersion,
18+
isActionsStepDebug,
1819
} from './utils';
1920
import {getGitUrlWithToken} from './context-helper';
2021

@@ -63,6 +64,16 @@ export default class GitHelper {
6364
}
6465
}
6566

67+
/**
68+
* @return {boolean} should suppress error
69+
*/
70+
private shouldSuppressError = (): boolean => !isActionsStepDebug();
71+
72+
/**
73+
* @return {boolean} is quiet?
74+
*/
75+
private isQuiet = (): boolean => this.shouldSuppressError() && (!this.origin || this.quietIfNotOrigin);
76+
6677
/**
6778
* @param {string} workDir work dir
6879
* @param {string[]} commands commands
@@ -147,15 +158,10 @@ export default class GitHelper {
147158
args: [this.getRemoteName(), getGitUrlWithToken(context, this.token)],
148159
quiet: this.isQuiet(),
149160
altCommand: `git remote add ${this.getRemoteName()}`,
150-
suppressError: true,
161+
suppressError: this.shouldSuppressError(),
151162
});
152163
};
153164

154-
/**
155-
* @return {boolean} is quiet?
156-
*/
157-
private isQuiet = (): boolean => !this.origin || this.quietIfNotOrigin;
158-
159165
/**
160166
* @param {string} workDir work dir
161167
* @return {Promise<string>} branch name
@@ -167,7 +173,7 @@ export default class GitHelper {
167173
return (await this.runCommand(workDir, {
168174
command: 'git rev-parse',
169175
args: ['--abbrev-ref', 'HEAD'],
170-
suppressError: true,
176+
suppressError: this.shouldSuppressError(),
171177
stderrToStdout: true,
172178
}))[0].stdout[0]?.trim() ?? '';
173179
};
@@ -184,7 +190,7 @@ export default class GitHelper {
184190
args: [`--branch=${branch}`, this.cloneDepth, this.getRemote(context), '.'],
185191
quiet: this.isQuiet(),
186192
altCommand: `git clone --branch=${branch}`,
187-
suppressError: true,
193+
suppressError: this.shouldSuppressError(),
188194
});
189195
};
190196

@@ -200,7 +206,7 @@ export default class GitHelper {
200206
args: [this.cloneDepth, this.getRemote(context), '.'],
201207
quiet: this.isQuiet(),
202208
altCommand: 'git clone',
203-
suppressError: true,
209+
suppressError: this.shouldSuppressError(),
204210
},
205211
{
206212
command: 'git fetch',
@@ -261,7 +267,7 @@ export default class GitHelper {
261267
this.getRemoteName(),
262268
...(refspec ?? []),
263269
],
264-
suppressError: true,
270+
suppressError: this.shouldSuppressError(),
265271
stderrToStdout: true,
266272
});
267273
};
@@ -295,7 +301,7 @@ export default class GitHelper {
295301
args: ['--prune', '--no-recurse-submodules', this.cloneDepth, this.getRemote(context), `+refs/heads/${branchName}:refs/remotes/${this.getRemoteName()}/${branchName}`],
296302
quiet: this.isQuiet(),
297303
altCommand: `git fetch --prune --no-recurse-submodules${this.cloneDepth} ${this.getRemoteName()} +refs/heads/${branchName}:refs/remotes/${this.getRemoteName()}/${branchName}`,
298-
suppressError: true,
304+
suppressError: this.shouldSuppressError(),
299305
});
300306
};
301307

@@ -317,7 +323,7 @@ export default class GitHelper {
317323
await this.runCommand(workDir, {
318324
command: 'git checkout',
319325
args: ['-b', branch, `${this.getRemoteName()}/${branch}`],
320-
suppressError: true,
326+
suppressError: this.shouldSuppressError(),
321327
stderrToStdout: true,
322328
});
323329
};
@@ -465,7 +471,7 @@ export default class GitHelper {
465471
args: [this.getRemote(context), '--delete', ...tags],
466472
quiet: this.isQuiet(),
467473
altCommand: `git push ${this.getRemoteName()} --delete ${tags.join(' ')}`,
468-
suppressError: true,
474+
suppressError: this.shouldSuppressError(),
469475
})),
470476
);
471477
await this.deleteLocalTag(workDir, tags, splitSize);
@@ -506,7 +512,7 @@ export default class GitHelper {
506512
tags => ({
507513
command: 'git tag',
508514
args: ['-d', ...tags],
509-
suppressError: true,
515+
suppressError: this.shouldSuppressError(),
510516
stderrToStdout: true,
511517
}),
512518
));
@@ -550,7 +556,7 @@ export default class GitHelper {
550556
args: args.concat([this.getRemote(context), `${branch}:refs/heads/${branch}`]),
551557
quiet: this.isQuiet(),
552558
altCommand: `git push ${args.concat([this.getRemoteName(), `${branch}:refs/heads/${branch}`]).join(' ')}`,
553-
suppressError: true,
559+
suppressError: this.shouldSuppressError(),
554560
});
555561
};
556562

src/utils.ts

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

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

0 commit comments

Comments
 (0)