make logging of git diff unconditional, remove parameter

This commit is contained in:
dogboydog 2020-07-08 19:48:46 -04:00 committed by Webber Takken
parent 6fb8550919
commit 91ec427695
7 changed files with 34 additions and 79 deletions

View File

@ -362,21 +362,6 @@ be needed. (use with care).
_**required:** `false`_ _**required:** `false`_
_**default:** false_ _**default:** false_
#### logDiffIfDirty
Print a summary of changed files if the branch is determined to be dirty.
This can be useful in debugging the reason your project files changed unexpectedly.
This option works independently of `allowDirtyBuild`.
```yaml
- uses: webbertakken/unity-builder@<version>
with:
logDiffIfDirty: true
```
_**required:** `false`_
_**default:** false_
#### customParameters #### customParameters
Custom parameters to configure the build. Custom parameters to configure the build.

View File

@ -79,14 +79,7 @@ inputs:
Note that it is generally bad practice to modify your branch Note that it is generally bad practice to modify your branch
in a CI Pipeline. However there are exceptions where this might in a CI Pipeline. However there are exceptions where this might
be needed. (use with care). be needed. (use with care).
logDiffIfDirty:
required: false
default: ''
description: >
Print a summary of changed files if the branch is determined to be dirty.
This can be useful in debugging the reason your project files changed unexpectedly.
This option works independently of allowDirtyBuild.
outputs: {} outputs: {}
branding: branding:
icon: 'box' icon: 'box'

File diff suppressed because one or more lines are too long

View File

@ -77,12 +77,6 @@ class Input {
return input === 'true' ? 'true' : 'false'; return input === 'true' ? 'true' : 'false';
} }
static get logDiffIfDirty() {
const input = core.getInput('logDiffIfDirty') || 'false';
return input === 'true' ? 'true' : 'false';
}
static get customParameters() { static get customParameters() {
return core.getInput('customParameters') || ''; return core.getInput('customParameters') || '';
} }

View File

@ -232,24 +232,6 @@ describe('Input', () => {
}); });
}); });
describe('logDiffIfDirty', () => {
it('returns the default value', () => {
expect(Input.logDiffIfDirty).toStrictEqual('false');
});
it('returns true when string true is passed', () => {
const spy = jest.spyOn(core, 'getInput').mockReturnValue('true');
expect(Input.logDiffIfDirty).toStrictEqual('true');
expect(spy).toHaveBeenCalledTimes(1);
});
it('returns false when string false is passed', () => {
const spy = jest.spyOn(core, 'getInput').mockReturnValue('false');
expect(Input.logDiffIfDirty).toStrictEqual('false');
expect(spy).toHaveBeenCalledTimes(1);
});
});
describe('customParameters', () => { describe('customParameters', () => {
it('returns the default value', () => { it('returns the default value', () => {
expect(Input.customParameters).toStrictEqual(''); expect(Input.customParameters).toStrictEqual('');

View File

@ -13,10 +13,6 @@ export default class Versioning {
return Input.allowDirtyBuild === 'true'; return Input.allowDirtyBuild === 'true';
} }
static get logDiffIfDirty() {
return Input.logDiffIfDirty === 'true';
}
static get strategies() { static get strategies() {
return { None: 'None', Semantic: 'Semantic', Tag: 'Tag', Custom: 'Custom' }; return { None: 'None', Semantic: 'Semantic', Tag: 'Tag', Custom: 'Custom' };
} }
@ -50,6 +46,20 @@ export default class Versioning {
return process.env.GITHUB_SHA; return process.env.GITHUB_SHA;
} }
/**
* Maximum number of lines to print when logging the git diff
*/
static get maxDiffLines() {
return 60;
}
/**
* Log up to maxDiffLines of the git diff.
*/
static async logDiff() {
this.git(['--no-pager', 'diff', '|', 'head', '-n', this.maxDiffLines.toString()]);
}
/** /**
* Regex to parse version description into separate fields * Regex to parse version description into separate fields
*/ */
@ -99,6 +109,8 @@ export default class Versioning {
static async generateSemanticVersion() { static async generateSemanticVersion() {
await this.fetch(); await this.fetch();
await this.logDiff();
if ((await this.isDirty()) && !this.isDirtyAllowed) { if ((await this.isDirty()) && !this.isDirtyAllowed) {
throw new Error('Branch is dirty. Refusing to base semantic version on uncommitted changes'); throw new Error('Branch is dirty. Refusing to base semantic version on uncommitted changes');
} }
@ -182,12 +194,7 @@ export default class Versioning {
static async isDirty() { static async isDirty() {
const output = await this.git(['status', '--porcelain']); const output = await this.git(['status', '--porcelain']);
const dirty = output !== ''; return output !== '';
if (dirty && this.logDiffIfDirty) {
await this.git(['--no-pager', 'diff']);
}
return dirty;
} }
/** /**

View File

@ -100,32 +100,26 @@ describe('Versioning', () => {
}); });
}); });
describe('logDiffIfDirty', () => { describe('logging git diff', () => {
it('does not throw', () => { it('calls git diff', async () => {
expect(() => Versioning.logDiffIfDirty).not.toThrow(); // allowDirtyBuild: true
});
it('returns false by default', () => {
expect(Versioning.logDiffIfDirty).toStrictEqual(false);
});
it('does not call git diff if logDiffIfDirty is false', async () => {
jest.spyOn(core, 'getInput').mockReturnValue('false');
const gitSpy = jest.spyOn(Versioning, 'git').mockReturnValue('');
await Versioning.isDirty();
expect(gitSpy).toHaveBeenCalledTimes(1);
expect(gitSpy).toBeCalledWith(['status', '--porcelain']);
});
it('calls git diff if logDiffIfDirty is true', async () => {
jest.spyOn(core, 'getInput').mockReturnValue('true'); jest.spyOn(core, 'getInput').mockReturnValue('true');
jest.spyOn(Versioning, 'isDirty').mockResolvedValue(false);
jest.spyOn(Versioning, 'fetch').mockResolvedValue(undefined);
jest.spyOn(Versioning, 'hasAnyVersionTags').mockResolvedValue(true);
jest
.spyOn(Versioning, 'parseSemanticVersion')
.mockResolvedValue({ tag: 'mocktag', commits: 'abcdef', hash: '75822BCAF' });
const logDiffSpy = jest.spyOn(Versioning, 'logDiff');
const gitSpy = jest const gitSpy = jest
.spyOn(Versioning, 'git') .spyOn(Versioning, 'git')
.mockReturnValue('There is a diff actually! \n M My_Dirty_File.txt'); .mockReturnValue('There is a diff actually! \n M My_Dirty_File.txt');
await Versioning.isDirty();
expect(gitSpy).toHaveBeenCalledTimes(2); await Versioning.generateSemanticVersion();
expect(Versioning.git.mock.calls[0][0].indexOf('status')).toBeGreaterThan(-1);
expect(Versioning.git.mock.calls[1][0].indexOf('diff')).toBeGreaterThan(-1); expect(logDiffSpy).toHaveBeenCalledTimes(1);
expect(gitSpy).toHaveBeenCalledTimes(1);
expect(Versioning.git.mock.calls[0][0].indexOf('diff')).toBeGreaterThan(-1);
}); });
}); });