Skip to content
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
41 changes: 41 additions & 0 deletions src/utils/__tests__/git.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getLatestTag } from '../git';
import * as loggerModule from '../../logger';

describe('getLatestTag', () => {
it('returns latest tag in the repo by calling `git describe`', async () => {
const git = {
raw: jest.fn().mockResolvedValue('1.0.0'),
} as any;

const latestTag = await getLatestTag(git);
expect(latestTag).toBe('1.0.0');

expect(git.raw).toHaveBeenCalledWith('describe', '--tags', '--abbrev=0');
});

it('logs a helpful error message if the git call throws', async () => {
loggerModule.setLevel(loggerModule.LogLevel.Debug);
const loggerErrorSpy = jest
.spyOn(loggerModule.logger, 'error')
.mockImplementation(() => {
// just to avoid spamming the test output
});

const error = new Error('Nothing to describe');
const git = {
raw: jest.fn().mockRejectedValue(error),
} as any;

try {
await getLatestTag(git);
} catch (e) {
expect(e).toBe(error);
}

expect(loggerErrorSpy).toHaveBeenCalledWith(
expect.stringContaining(
"If you're releasing for the first time, check if your repo contains any tags"
)
);
});
});
10 changes: 9 additions & 1 deletion src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ export async function getDefaultBranch(

export async function getLatestTag(git: SimpleGit): Promise<string> {
// This part is courtesy of https://stackoverflow.com/a/7261049/90297
return (await git.raw('describe', '--tags', '--abbrev=0')).trim();
try {
return (await git.raw('describe', '--tags', '--abbrev=0')).trim();
} catch (e) {
logger.error(
'Couldn\'t get the latest tag! If you\'re releasing for the first time, check if your repo contains any tags. If not, add one manually and try again: `git tag 0.0.0 "$(git log -1 --reverse --format=%h)"'
);
// handle this error in the global error handler
throw e;
}
}

export async function getChangesSince(
Expand Down
Loading