Skip to content

Commit

Permalink
test: refactor fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 3, 2023
1 parent 9a841d9 commit eda3462
Show file tree
Hide file tree
Showing 15 changed files with 20 additions and 29 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
42 changes: 13 additions & 29 deletions tests/specs/openai/conventional-commits.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { readFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import { expect, testSuite } from 'manten';
import {
generateCommitMessage,
} from '../../../src/utils/openai.js';
import type { ValidConfig } from '../../../src/utils/config.js';
import { getDiff } from '../../utils.js';

const { OPENAI_KEY } = process.env;

Expand All @@ -19,7 +17,7 @@ export default testSuite(({ describe }) => {
await test('Should not translate conventional commit type to Japanase when locale config is set to japanese', async () => {
const japaneseConventionalCommitPattern = /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?: [\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFF9F\u4E00-\u9FAF\u3400-\u4DBF]/;

const gitDiff = await readDiffFromFile('new-feature.txt');
const gitDiff = await getDiff('new-feature.diff');

const commitMessage = await runGenerateCommitMessage(gitDiff, {
locale: 'ja',
Expand All @@ -30,7 +28,7 @@ export default testSuite(({ describe }) => {
});

await test('Should use "feat:" conventional commit when change relate to adding a new feature', async () => {
const gitDiff = await readDiffFromFile('new-feature.txt');
const gitDiff = await getDiff('new-feature.diff');

const commitMessage = await runGenerateCommitMessage(gitDiff);

Expand All @@ -40,7 +38,7 @@ export default testSuite(({ describe }) => {
});

await test('Should use "refactor:" conventional commit when change relate to code refactoring', async () => {
const gitDiff = await readDiffFromFile('code-refactoring.txt');
const gitDiff = await getDiff('code-refactoring.diff');

const commitMessage = await runGenerateCommitMessage(gitDiff);

Expand All @@ -50,7 +48,7 @@ export default testSuite(({ describe }) => {
});

await test('Should use "test:" conventional commit when change relate to testing a React application', async () => {
const gitDiff = await readDiffFromFile('testing-react-application.txt');
const gitDiff = await getDiff('testing-react-application.diff');

const commitMessage = await runGenerateCommitMessage(gitDiff);

Expand All @@ -60,8 +58,8 @@ export default testSuite(({ describe }) => {
});

await test('Should use "build:" conventional commit when change relate to github action build pipeline', async () => {
const gitDiff = await readDiffFromFile(
'github-action-build-pipeline.txt',
const gitDiff = await getDiff(
'github-action-build-pipeline.diff',
);

const commitMessage = await runGenerateCommitMessage(gitDiff);
Expand All @@ -72,7 +70,7 @@ export default testSuite(({ describe }) => {
});

await test('Should use "(ci|build):" conventional commit when change relate to continious integration', async () => {
const gitDiff = await readDiffFromFile('continous-integration.txt');
const gitDiff = await getDiff('continous-integration.diff');

const commitMessage = await runGenerateCommitMessage(gitDiff);

Expand All @@ -83,7 +81,7 @@ export default testSuite(({ describe }) => {
});

await test('Should use "docs:" conventional commit when change relate to documentation changes', async () => {
const gitDiff = await readDiffFromFile('documentation-changes.txt');
const gitDiff = await getDiff('documentation-changes.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);

// should match "docs:" or "docs(<scope>):"
Expand All @@ -92,7 +90,7 @@ export default testSuite(({ describe }) => {
});

await test('Should use "fix:" conventional commit when change relate to fixing code', async () => {
const gitDiff = await readDiffFromFile('fix-nullpointer-exception.txt');
const gitDiff = await getDiff('fix-nullpointer-exception.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);

// should match "fix:" or "fix(<scope>):"
Expand All @@ -102,7 +100,7 @@ export default testSuite(({ describe }) => {
});

await test('Should use "style:" conventional commit when change relate to code style improvements', async () => {
const gitDiff = await readDiffFromFile('code-style.txt');
const gitDiff = await getDiff('code-style.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);

// should match "style:" or "style(<style>):"
Expand All @@ -111,7 +109,7 @@ export default testSuite(({ describe }) => {
});

await test('Should use "chore:" conventional commit when change relate to a chore or maintenance', async () => {
const gitDiff = await readDiffFromFile('chore.txt');
const gitDiff = await getDiff('chore.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);

// should match "chore:" or "chore(<style>):"
Expand All @@ -121,7 +119,7 @@ export default testSuite(({ describe }) => {
});

await test('Should use "perf:" conventional commit when change relate to a performance improvement', async () => {
const gitDiff = await readDiffFromFile('performance-improvement.txt');
const gitDiff = await getDiff('performance-improvement.diff');
const commitMessage = await runGenerateCommitMessage(gitDiff);

// should match "perf:" or "perf(<style>):"
Expand All @@ -143,19 +141,5 @@ export default testSuite(({ describe }) => {

return commitMessages[0];
}

/*
* See ./diffs/README.md in order to generate diff files
*/
async function readDiffFromFile(filename: string): Promise<string> {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const gitDiff = await readFile(
path.resolve(__dirname, `./diff-fixtures/${filename}`),
'utf8',
);

return gitDiff;
}
});
});
7 changes: 7 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import fs from 'fs/promises';
import { execa, execaNode, type Options } from 'execa';
import {
createFixture as createFixtureBase,
Expand Down Expand Up @@ -83,3 +84,9 @@ export const assertOpenAiToken = () => {
throw new Error('⚠️ process.env.OPENAI_KEY is necessary to run these tests. Skipping...');
}
};

// See ./diffs/README.md in order to generate diff files
export const getDiff = async (diffName: string): Promise<string> => fs.readFile(
new URL(`fixtures/${diffName}`, import.meta.url),
'utf8',
);

0 comments on commit eda3462

Please sign in to comment.