Skip to content

release: v4.3.0 #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions __tests__/git-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import {
testChildProcess,
spyOnSpawn,
execCalledWith,
spyOnStdout,
stdoutCalledWith,
} from '@technote-space/github-action-test-helper';
import {Logger} from '@technote-space/github-action-log-helper';
import {ExecException} from 'child_process';
import {GitHelper} from '../src';

beforeEach(() => {
Logger.resetForTesting();
});
const workDir = '.work';
const setExists = testFs(true);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -895,3 +901,75 @@ describe('GitHelper without params', () => {
});
});
});

describe('Debug', () => {
testEnv();
testChildProcess();

const helper = new GitHelper(new Logger(), {token: 'token1'});

it('should add command to suppress error and output', async() => {
const mockExec = spyOnSpawn();
const mockStdout = spyOnStdout();
mockExec.mockImplementation(() => {
const error: ExecException = new Error('test error');
error.code = 123;
throw error;
});

await expect(helper.addOrigin(workDir, context())).rejects.toThrow('command [git remote add origin] exited with code 123.');

execCalledWith(mockExec, [
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
]);
stdoutCalledWith(mockStdout, [
'[command]git remote add origin',
'undefined',
'{}',
]);
});

it('should not add command to suppress error', async() => {
process.env.ACTIONS_UTILS_DEBUG = 'true';
const mockExec = spyOnSpawn();
const mockStdout = spyOnStdout();
mockExec.mockImplementation(() => {
const error: ExecException = new Error('test error');
error.code = 123;
throw error;
});

await expect(helper.addOrigin(workDir, context())).rejects.toThrow('command [git remote add origin] exited with code 123.');

execCalledWith(mockExec, [
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1',
]);
stdoutCalledWith(mockStdout, [
'[command]git remote add origin',
'undefined',
'{}',
]);
});

it('should not add command to suppress error output', async() => {
process.env.ACTIONS_STEP_DEBUG = 'true';
const mockExec = spyOnSpawn();
const mockStdout = spyOnStdout();
mockExec.mockImplementation(() => {
const error: ExecException = new Error('test error');
error.code = 123;
throw error;
});

await expect(helper.addOrigin(workDir, context())).rejects.toThrow('command [git remote add origin] exited with code 123.');

execCalledWith(mockExec, [
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' || :',
]);
stdoutCalledWith(mockStdout, [
'[command]git remote add origin',
'undefined',
'{}',
]);
});
});
60 changes: 58 additions & 2 deletions __tests__/utils2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import {testEnv} from '@technote-space/github-action-test-helper';
import {Utils} from '../src';

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

jest.useFakeTimers();

Expand Down Expand Up @@ -263,3 +263,59 @@ describe('replaceVariables', () => {
])).toBe('1/2/3/${test4}');
});
});

describe('isDebug', () => {
testEnv();

it('should return true', () => {
process.env.ACTIONS_UTILS_DEBUG = 'true';
expect(isDebug()).toBe(true);
});

it('should return false 1', () => {
expect(isDebug()).toBe(false);
});

it('should return false 2', () => {
process.env.ACTIONS_UTILS_DEBUG = '';
expect(isDebug()).toBe(false);
});

it('should return false 3', () => {
process.env.ACTIONS_UTILS_DEBUG = '1';
expect(isDebug()).toBe(false);
});

it('should return false 4', () => {
process.env.ACTIONS_UTILS_DEBUG = 'abc';
expect(isDebug()).toBe(false);
});
});

describe('isActionsStepDebug', () => {
testEnv();

it('should return true', () => {
process.env.ACTIONS_STEP_DEBUG = 'true';
expect(isActionsStepDebug()).toBe(true);
});

it('should return false 1', () => {
expect(isActionsStepDebug()).toBe(false);
});

it('should return false 2', () => {
process.env.ACTIONS_STEP_DEBUG = '';
expect(isActionsStepDebug()).toBe(false);
});

it('should return false 3', () => {
process.env.ACTIONS_STEP_DEBUG = '1';
expect(isActionsStepDebug()).toBe(false);
});

it('should return false 4', () => {
process.env.ACTIONS_STEP_DEBUG = 'abc';
expect(isActionsStepDebug()).toBe(false);
});
});
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@technote-space/github-action-helper",
"version": "4.2.7",
"version": "4.3.0",
"description": "Helper for GitHub Action.",
"keywords": [
"github",
Expand Down Expand Up @@ -46,17 +46,17 @@
"@commitlint/config-conventional": "^11.0.0",
"@technote-space/github-action-test-helper": "^0.6.4",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.8",
"@types/node": "^14.14.9",
"@typescript-eslint/eslint-plugin": "^4.8.1",
"@typescript-eslint/parser": "^4.8.1",
"eslint": "^7.13.0",
"eslint": "^7.14.0",
"husky": "^4.3.0",
"jest": "^26.6.3",
"jest-circus": "^26.6.3",
"lint-staged": "^10.5.1",
"nock": "^13.0.5",
"ts-jest": "^26.4.4",
"typescript": "^4.0.5"
"typescript": "^4.1.2"
},
"publishConfig": {
"access": "public"
Expand Down
37 changes: 22 additions & 15 deletions src/git-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
getAccessToken,
generateNewMinorVersion,
generateNewMajorVersion,
isDebug,
isActionsStepDebug,
} from './utils';
import {getGitUrlWithToken} from './context-helper';

Expand Down Expand Up @@ -63,6 +65,16 @@ export default class GitHelper {
}
}

/**
* @return {boolean} should suppress error
*/
private shouldSuppressError = (): boolean => !isDebug();

/**
* @return {boolean} is quiet?
*/
private isQuiet = (): boolean => !isActionsStepDebug() && (!this.origin || this.quietIfNotOrigin);

/**
* @param {string} workDir work dir
* @param {string[]} commands commands
Expand Down Expand Up @@ -147,15 +159,10 @@ export default class GitHelper {
args: [this.getRemoteName(), getGitUrlWithToken(context, this.token)],
quiet: this.isQuiet(),
altCommand: `git remote add ${this.getRemoteName()}`,
suppressError: true,
suppressError: this.shouldSuppressError(),
});
};

/**
* @return {boolean} is quiet?
*/
private isQuiet = (): boolean => !this.origin || this.quietIfNotOrigin;

/**
* @param {string} workDir work dir
* @return {Promise<string>} branch name
Expand All @@ -167,7 +174,7 @@ export default class GitHelper {
return (await this.runCommand(workDir, {
command: 'git rev-parse',
args: ['--abbrev-ref', 'HEAD'],
suppressError: true,
suppressError: this.shouldSuppressError(),
stderrToStdout: true,
}))[0].stdout[0]?.trim() ?? '';
};
Expand All @@ -184,7 +191,7 @@ export default class GitHelper {
args: [`--branch=${branch}`, this.cloneDepth, this.getRemote(context), '.'],
quiet: this.isQuiet(),
altCommand: `git clone --branch=${branch}`,
suppressError: true,
suppressError: this.shouldSuppressError(),
});
};

Expand All @@ -200,7 +207,7 @@ export default class GitHelper {
args: [this.cloneDepth, this.getRemote(context), '.'],
quiet: this.isQuiet(),
altCommand: 'git clone',
suppressError: true,
suppressError: this.shouldSuppressError(),
},
{
command: 'git fetch',
Expand Down Expand Up @@ -261,7 +268,7 @@ export default class GitHelper {
this.getRemoteName(),
...(refspec ?? []),
],
suppressError: true,
suppressError: this.shouldSuppressError(),
stderrToStdout: true,
});
};
Expand Down Expand Up @@ -295,7 +302,7 @@ export default class GitHelper {
args: ['--prune', '--no-recurse-submodules', this.cloneDepth, this.getRemote(context), `+refs/heads/${branchName}:refs/remotes/${this.getRemoteName()}/${branchName}`],
quiet: this.isQuiet(),
altCommand: `git fetch --prune --no-recurse-submodules${this.cloneDepth} ${this.getRemoteName()} +refs/heads/${branchName}:refs/remotes/${this.getRemoteName()}/${branchName}`,
suppressError: true,
suppressError: this.shouldSuppressError(),
});
};

Expand All @@ -317,7 +324,7 @@ export default class GitHelper {
await this.runCommand(workDir, {
command: 'git checkout',
args: ['-b', branch, `${this.getRemoteName()}/${branch}`],
suppressError: true,
suppressError: this.shouldSuppressError(),
stderrToStdout: true,
});
};
Expand Down Expand Up @@ -465,7 +472,7 @@ export default class GitHelper {
args: [this.getRemote(context), '--delete', ...tags],
quiet: this.isQuiet(),
altCommand: `git push ${this.getRemoteName()} --delete ${tags.join(' ')}`,
suppressError: true,
suppressError: this.shouldSuppressError(),
})),
);
await this.deleteLocalTag(workDir, tags, splitSize);
Expand Down Expand Up @@ -506,7 +513,7 @@ export default class GitHelper {
tags => ({
command: 'git tag',
args: ['-d', ...tags],
suppressError: true,
suppressError: this.shouldSuppressError(),
stderrToStdout: true,
}),
));
Expand Down Expand Up @@ -550,7 +557,7 @@ export default class GitHelper {
args: args.concat([this.getRemote(context), `${branch}:refs/heads/${branch}`]),
quiet: this.isQuiet(),
altCommand: `git push ${args.concat([this.getRemoteName(), `${branch}:refs/heads/${branch}`]).join(' ')}`,
suppressError: true,
suppressError: this.shouldSuppressError(),
});
};

Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,7 @@ export const replaceVariables = async(string: string, variables: { key: string;

return replaced;
};

export const isDebug = (): boolean => process.env.ACTIONS_UTILS_DEBUG === 'true';

export const isActionsStepDebug = (): boolean => process.env.ACTIONS_STEP_DEBUG === 'true';
Loading