Skip to content
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

test(changelog): add tests for changelog #110

Merged
merged 1 commit into from
Nov 6, 2024
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
209 changes: 209 additions & 0 deletions __tests__/changelog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import { getModuleChangelog, getModuleReleaseChangelog, getPullRequestChangelog } from '@/changelog';
import { context } from '@/mocks/context';
import type { TerraformChangedModule, TerraformModule } from '@/terraform-module';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

describe('changelog', () => {
const mockDate = new Date('2024-11-05');

beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(mockDate);

// Reset context mock before each test
context.set({
prNumber: 123,
prTitle: 'Test PR Title',
});
});

afterEach(() => {
vi.useRealTimers();
});

describe('getPullRequestChangelog()', () => {
it('should generate changelog for multiple modules with PR info', () => {
const terraformChangedModules: TerraformChangedModule[] = [
{
moduleName: 'module1',
directory: 'modules/module1',
tags: [],
releases: [],
isChanged: true,
latestTag: null,
latestTagVersion: null,
releaseType: 'patch',
nextTag: 'module1/v1.0.0',
nextTagVersion: '1.0.0',
commitMessages: ['Test PR Title', 'feat: Add new feature', 'fix: Fix bug\nWith multiple lines'],
},
{
moduleName: 'module2',
directory: 'modules/module2',
tags: [],
releases: [],
isChanged: true,
latestTag: null,
latestTagVersion: null,
releaseType: 'patch',
nextTag: 'module2/v2.0.0',
nextTagVersion: '2.0.0',
commitMessages: ['Another commit'],
},
];

const expectedChangelog = [
'## `module1/v1.0.0` (2024-11-05)',
'',
'- PR #123 - Test PR Title',
'- feat: Add new feature',
'- fix: Fix bug<br>With multiple lines',
'',
'## `module2/v2.0.0` (2024-11-05)',
'',
'- PR #123 - Test PR Title',
'- Another commit',
].join('\n');

expect(getPullRequestChangelog(terraformChangedModules)).toBe(expectedChangelog);
});

it('should handle empty commit messages array', () => {
const terraformChangedModules: TerraformChangedModule[] = [
{
moduleName: 'module1',
directory: 'modules/module2',
tags: [],
releases: [],
isChanged: true,
latestTag: null,
latestTagVersion: null,
releaseType: 'patch',
nextTag: 'module2/v2.0.0',
nextTagVersion: '2.0.0',
commitMessages: [],
},
];

const expectedChangelog = ['## `module2/v2.0.0` (2024-11-05)', '', '- PR #123 - Test PR Title'].join('\n');

expect(getPullRequestChangelog(terraformChangedModules)).toBe(expectedChangelog);
});

it('should handle empty modules array', () => {
expect(getPullRequestChangelog([])).toBe('');
});

it('should remove duplicate PR title from commit messages', () => {
const terraformChangedModules: TerraformChangedModule[] = [
{
moduleName: 'module1',
directory: 'modules/module1',
tags: [],
releases: [],
isChanged: true,
latestTag: null,
latestTagVersion: null,
releaseType: 'patch',
nextTag: 'module1/v1.0.0',
nextTagVersion: '1.0.0',
commitMessages: ['Test PR Title', 'Another commit'],
},
];

const expectedChangelog = [
'## `module1/v1.0.0` (2024-11-05)',
'',
'- PR #123 - Test PR Title',
'- Another commit',
].join('\n');

expect(getPullRequestChangelog(terraformChangedModules)).toBe(expectedChangelog);
});
});

describe('getModuleChangelog()', () => {
const baseTerraformChangedModule: TerraformChangedModule = {
moduleName: 'module1',
directory: 'modules/module1',
tags: [],
releases: [],
isChanged: true,
latestTag: null,
latestTagVersion: null,
releaseType: 'patch',
nextTag: 'module1/v1.0.0',
nextTagVersion: '1.0.0',
commitMessages: [],
};

it('should generate changelog for a single module with PR link', () => {
const terraformChangedModule = Object.assign(baseTerraformChangedModule, {
commitMessages: ['Test PR Title', 'feat: Add new feature', 'fix: Fix bug\nWith multiple lines'],
});

const expectedChangelog = [
'## `1.0.0` (2024-11-05)',
'',
'- [PR #123](https://github.com/techpivot/terraform-module-releaser/pull/123) - Test PR Title',
'- feat: Add new feature',
'- fix: Fix bug<br>With multiple lines',
].join('\n');

expect(getModuleChangelog(terraformChangedModule)).toBe(expectedChangelog);
});

it('should handle multiline commit messages', () => {
const terraformChangedModule = Object.assign(baseTerraformChangedModule, {
commitMessages: ['feat: Multiple\nline\ncommit', 'fix: Another\nMultiline'],
});

const expectedChangelog = [
'## `1.0.0` (2024-11-05)',
'',
'- [PR #123](https://github.com/techpivot/terraform-module-releaser/pull/123) - Test PR Title',
'- feat: Multiple<br>line<br>commit',
'- fix: Another<br>Multiline',
].join('\n');

expect(getModuleChangelog(terraformChangedModule)).toBe(expectedChangelog);
});
});

describe('getModuleReleaseChangelog()', () => {
const baseTerraformModule: TerraformModule = {
moduleName: 'aws/vpc',
directory: 'modules/aws/vpc',
tags: [],
releases: [],
latestTag: null,
latestTagVersion: null,
};

it('should concatenate release bodies', () => {
const terraformModule = Object.assign(baseTerraformModule, {
releases: [{ body: 'Release 1 content' }, { body: 'Release 2 content' }],
});

const expectedChangelog = ['Release 1 content', '', 'Release 2 content'].join('\n');

expect(getModuleReleaseChangelog(terraformModule)).toBe(expectedChangelog);
});

it('should handle empty releases array', () => {
const terraformModule = Object.assign(baseTerraformModule, {
releases: [],
});

expect(getModuleReleaseChangelog(terraformModule)).toBe('');
});

it('should handle single release', () => {
const terraformModule = Object.assign(baseTerraformModule, {
releases: [{ body: 'Single release content' }],
});

expect(getModuleReleaseChangelog(terraformModule)).toBe('Single release content');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { copyModuleContents, removeDirectoryContents, shouldExcludeFile } from '@/file-util';
import { copyModuleContents, removeDirectoryContents, shouldExcludeFile } from '@/utils/file';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

describe('file-util', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from '@/mocks/config';
import { determineReleaseType, getNextTagVersion } from '@/semver';
import { determineReleaseType, getNextTagVersion } from '@/utils/semver';
import { beforeEach, describe, expect, it } from 'vitest';

describe('semver', () => {
Expand Down
36 changes: 36 additions & 0 deletions __tests__/utils/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { trimSlashes } from '@/utils/string';
import { describe, expect, it } from 'vitest';

describe('string', () => {
describe('trimSlashes', () => {
it('should remove leading and trailing slashes while preserving internal ones', () => {
const testCases = [
{ input: '/example/path/', expected: 'example/path' },
{ input: '///another/example///', expected: 'another/example' },
{ input: 'no/slashes', expected: 'no/slashes' },
{ input: '/', expected: '' },
{ input: '//', expected: '' },
{ input: '', expected: '' },
{ input: '/single/', expected: 'single' },
{ input: 'leading/', expected: 'leading' },
{ input: '/trailing', expected: 'trailing' },
{ input: '////multiple////slashes////', expected: 'multiple////slashes' },
];
for (const { input, expected } of testCases) {
expect(trimSlashes(input)).toBe(expected);
}
});

it('should handle strings without any slashes', () => {
expect(trimSlashes('hello')).toBe('hello');
});

it('should return empty string when given only slashes', () => {
expect(trimSlashes('//////')).toBe('');
});

it('should preserve internal multiple slashes', () => {
expect(trimSlashes('/path//with///internal////slashes/')).toBe('path//with///internal////slashes');
});
});
});
2 changes: 1 addition & 1 deletion assets/coverage-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/releases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { getModuleChangelog } from '@/changelog';
import { config } from '@/config';
import { GITHUB_ACTIONS_BOT_EMAIL, GITHUB_ACTIONS_BOT_NAME } from '@/constants';
import { context } from '@/context';
import { copyModuleContents } from '@/file-util';
import type { TerraformChangedModule } from '@/terraform-module';
import { copyModuleContents } from '@/utils/file';
import { debug, endGroup, info, startGroup } from '@actions/core';
import { RequestError } from '@octokit/request-error';
import which from 'which';
Expand Down
1 change: 1 addition & 0 deletions src/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function getAllTags(options: GetAllTagsOptions = { per_page: 100 })
}

throw new Error(errorMessage, { cause: error });
/* c8 ignore next */
} finally {
console.timeEnd('Elapsed time fetching tags');
endGroup();
Expand Down
6 changes: 3 additions & 3 deletions src/terraform-module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { existsSync, readdirSync, statSync } from 'node:fs';
import { dirname, extname, join, relative, resolve } from 'node:path';
import { config } from '@/config';
import { shouldExcludeFile } from '@/file-util';
import type { CommitDetails } from '@/pull-request';
import type { GitHubRelease } from '@/releases';
import type { ReleaseType } from '@/semver';
import { determineReleaseType, getNextTagVersion } from '@/semver';
import { shouldExcludeFile } from '@/utils/file';
import type { ReleaseType } from '@/utils/semver';
import { determineReleaseType, getNextTagVersion } from '@/utils/semver';
import { debug, endGroup, info, startGroup } from '@actions/core';

/**
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/wiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
WIKI_TITLE_REPLACEMENTS,
} from '@/constants';
import { context } from '@/context';
import { removeDirectoryContents } from '@/file-util';
import { generateTerraformDocs } from '@/terraform-docs';
import type { TerraformModule } from '@/terraform-module';
import { removeDirectoryContents } from '@/utils/file';
import { endGroup, info, startGroup } from '@actions/core';
import pLimit from 'p-limit';
import which from 'which';
Expand Down