Skip to content

Commit

Permalink
fix: Fix a bug where the git hash was inappropriately truncated when …
Browse files Browse the repository at this point in the history
…there were no tags (fixes #3)
  • Loading branch information
TimothyJones committed Mar 17, 2023
1 parent 4322437 commit aad13ca
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 150 deletions.
3 changes: 2 additions & 1 deletion src/gitTagDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const getBranchString = (clean: (s: string) => string): string =>
const dirtyString = (gitInfo: GitInfo, clean: (s: string) => string) =>
gitInfo.dirty ? `.SNAPSHOT.${clean(getHostnameString())}` : '';

const gitHash = (gitInfo: GitInfo) => gitInfo.hash.substring(1);
const gitHash = (gitInfo: GitInfo) =>
gitInfo.hash.startsWith('g') ? gitInfo.hash.substring(1) : gitInfo.hash;

const tagOrHash = (gitInfo: GitInfo) =>
gitInfo.tag ? gitInfo.tag : gitHash(gitInfo);
Expand Down
306 changes: 157 additions & 149 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jest.mock('os');
jest.mock('git-rev-sync');

describe('version-from-git-tag', () => {
const hash = 'g9234bfb';
describe('with no tag', () => {
const hash = '9234bfb';
const gitDescribe = {
hash,
};
Expand Down Expand Up @@ -52,95 +52,19 @@ describe('version-from-git-tag', () => {
});
});

describe('with no semver', () => {
const tag = 'sometag';
const gitDescribe = {
tag,
hash,
};
describe('when the repo is not on an exact tag', () => {
const distance = 1;
describe('when the repo is dirty', () => {
const dirty = true;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'sometag-featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
);
});
});
describe('when the repo is not dirty', () => {
const dirty = false;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual('sometag-featwhatever+1.9234bfb');
});
});
});
describe('when the repo is on an exact tag', () => {
const distance = 0;
describe('when the repo is dirty', () => {
const dirty = true;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'sometag-featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
);
});
});
describe('when the repo is not dirty', () => {
const dirty = false;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual('sometag');
});
});
});
});

describe('with a release version', () => {
const version = '1.2.3';
const tag = `v${version}`;
const gitDescribe = {
hash,
tag,
semver: {
version,
},
};

describe('when on a branch named feat/whatever', () => {
(branch as jest.Mock).mockReturnValue('feat/whatever');
describe('with a tag', () => {
const hash = 'g9234bfb';

describe('with no semver', () => {
const tag = 'sometag';
const gitDescribe = {
tag,
hash,
};
describe('when the repo is not on an exact tag', () => {
const distance = 1;
describe('when the repo is dirty', () => {
const dirty = true;

beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
Expand All @@ -150,7 +74,7 @@ describe('version-from-git-tag', () => {
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
'sometag-featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
);
});
});
Expand All @@ -164,7 +88,9 @@ describe('version-from-git-tag', () => {
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual('1.2.3-featwhatever+1.9234bfb');
expect(versionFromGitTag()).toEqual(
'sometag-featwhatever+1.9234bfb'
);
});
});
});
Expand All @@ -181,7 +107,7 @@ describe('version-from-git-tag', () => {
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
'sometag-featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
);
});
});
Expand All @@ -195,84 +121,166 @@ describe('version-from-git-tag', () => {
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual('1.2.3');
expect(versionFromGitTag()).toEqual('sometag');
});
});
});
});
});
describe('with a prerelease version', () => {
const version = '1.2.3-beta';
const tag = `v${version}`;
const gitDescribe = {
hash,
tag,
semver: {
version,
},
};
describe('when the repo is not on an exact tag', () => {
const distance = 1;
describe('when the repo is dirty', () => {
const dirty = true;

beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
describe('with a release version', () => {
const version = '1.2.3';
const tag = `v${version}`;
const gitDescribe = {
hash,
tag,
semver: {
version,
},
};

describe('when on a branch named feat/whatever', () => {
(branch as jest.Mock).mockReturnValue('feat/whatever');

describe('when the repo is not on an exact tag', () => {
const distance = 1;
describe('when the repo is dirty', () => {
const dirty = true;

beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
);
});
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-beta.featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
);
});
});
describe('when the repo is not dirty', () => {
const dirty = false;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
describe('when the repo is not dirty', () => {
const dirty = false;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-featwhatever+1.9234bfb'
);
});
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-beta.featwhatever+1.9234bfb'
);
describe('when the repo is on an exact tag', () => {
const distance = 0;
describe('when the repo is dirty', () => {
const dirty = true;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
);
});
});
describe('when the repo is not dirty', () => {
const dirty = false;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual('1.2.3');
});
});
});
});
});
describe('when the repo is on an exact tag', () => {
const distance = 0;
describe('when the repo is dirty', () => {
const dirty = true;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
describe('with a prerelease version', () => {
const version = '1.2.3-beta';
const tag = `v${version}`;
const gitDescribe = {
hash,
tag,
semver: {
version,
},
};
describe('when the repo is not on an exact tag', () => {
const distance = 1;
describe('when the repo is dirty', () => {
const dirty = true;

beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-beta.featwhatever+1.9234bfb.SNAPSHOT.HOSTNAME'
);
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-beta.featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
);
describe('when the repo is not dirty', () => {
const dirty = false;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-beta.featwhatever+1.9234bfb'
);
});
});
});
describe('when the repo is not dirty', () => {
const dirty = false;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
describe('when the repo is on an exact tag', () => {
const distance = 0;
describe('when the repo is dirty', () => {
const dirty = true;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual(
'1.2.3-beta.featwhatever+0.9234bfb.SNAPSHOT.HOSTNAME'
);
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual('1.2.3-beta');
describe('when the repo is not dirty', () => {
const dirty = false;
beforeEach(() => {
(gitDescribeSync as jest.Mock).mockReturnValue({
...gitDescribe,
dirty,
distance,
});
});
it('displays the version number correctly', () => {
expect(versionFromGitTag()).toEqual('1.2.3-beta');
});
});
});
});
Expand Down

0 comments on commit aad13ca

Please sign in to comment.