From 604def8284361b8827087350fe6fcb6d9e2de836 Mon Sep 17 00:00:00 2001 From: Hassan El Mghari Date: Fri, 26 Jan 2024 10:02:33 -0800 Subject: [PATCH] formatting and fixed type error --- .github/FUNDING.yml | 1 - .github/workflows/release.yml | 33 +++--- src/utils/openai.ts | 2 +- tests/specs/cli/commits.ts | 118 +++++++++++++++------ tests/specs/cli/error-cases.ts | 4 +- tests/specs/config.ts | 13 ++- tests/specs/openai/conventional-commits.ts | 32 ++++-- tests/utils.ts | 75 ++++++------- 8 files changed, 165 insertions(+), 113 deletions(-) delete mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index 33fc208e..00000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1 +0,0 @@ -github: privatenumber diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7159ebd1..62fc1c4f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,23 +13,22 @@ jobs: contents: write steps: + - name: Checkout + uses: actions/checkout@v3 - - name: Checkout - uses: actions/checkout@v3 + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version-file: '.nvmrc' - - name: Setup Node.js - uses: actions/setup-node@v3 - with: - node-version-file: '.nvmrc' + - name: Setup pnpm + uses: pnpm/action-setup@v2 + with: + version: 7 + run_install: true - - name: Setup pnpm - uses: pnpm/action-setup@v2 - with: - version: 7 - run_install: true - - - name: Release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - run: pnpm dlx semantic-release + - name: Release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + run: pnpm dlx semantic-release diff --git a/src/utils/openai.ts b/src/utils/openai.ts index 854f384a..d8c767ed 100644 --- a/src/utils/openai.ts +++ b/src/utils/openai.ts @@ -171,7 +171,7 @@ export const generateCommitMessage = async ( return deduplicateMessages( completion.choices .filter((choice) => choice.message?.content) - .map((choice) => sanitizeMessage(choice.message!.content)) + .map((choice) => sanitizeMessage(choice.message!.content as string)) ); } catch (error) { const errorAsAny = error as any; diff --git a/tests/specs/cli/commits.ts b/tests/specs/cli/commits.ts index f88916ad..f7e62f1e 100644 --- a/tests/specs/cli/commits.ts +++ b/tests/specs/cli/commits.ts @@ -9,7 +9,9 @@ import { export default testSuite(({ describe }) => { if (process.platform === 'win32') { // https://github.com/nodejs/node/issues/31409 - console.warn('Skipping tests on Windows because Node.js spawn cant open TTYs'); + console.warn( + 'Skipping tests on Windows because Node.js spawn cant open TTYs' + ); return; } @@ -21,10 +23,15 @@ export default testSuite(({ describe }) => { const git = await createGit(fixture.path); await git('add', ['data.json']); - const statusBefore = await git('status', ['--porcelain', '--untracked-files=no']); + const statusBefore = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusBefore.stdout).toBe('A data.json'); - const { stdout, exitCode } = await aicommits(['--exclude', 'data.json'], { reject: false }); + const { stdout, exitCode } = await aicommits(['--exclude', 'data.json'], { + reject: false, + }); expect(exitCode).toBe(1); expect(stdout).toMatch('No staged changes found.'); await fixture.rm(); @@ -47,10 +54,15 @@ export default testSuite(({ describe }) => { await committing; - const statusAfter = await git('status', ['--porcelain', '--untracked-files=no']); + const statusAfter = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusAfter.stdout).toBe(''); - const { stdout: commitMessage } = await git('log', ['--pretty=format:%s']); + const { stdout: commitMessage } = await git('log', [ + '--pretty=format:%s', + ]); console.log({ commitMessage, length: commitMessage.length, @@ -81,7 +93,9 @@ export default testSuite(({ describe }) => { await committing; - const { stdout: commitMessage } = await git('log', ['--pretty=format:%s']); + const { stdout: commitMessage } = await git('log', [ + '--pretty=format:%s', + ]); console.log({ commitMessage, length: commitMessage.length, @@ -118,7 +132,10 @@ export default testSuite(({ describe }) => { const statusAfter = await git('status', ['--short']); expect(statusAfter.stdout).toBe('?? .aicommits'); - const { stdout: commitMessage } = await git('log', ['-n1', '--pretty=format:%s']); + const { stdout: commitMessage } = await git('log', [ + '-n1', + '--pretty=format:%s', + ]); console.log({ commitMessage, length: commitMessage.length, @@ -128,7 +145,9 @@ export default testSuite(({ describe }) => { await fixture.rm(); }); - test('Accepts --generate flag, overriding config', async ({ onTestFail }) => { + test('Accepts --generate flag, overriding config', async ({ + onTestFail, + }) => { const { fixture, aicommits } = await createFixture({ ...files, '.aicommits': `${files['.aicommits']}\ngenerate=4`, @@ -138,9 +157,7 @@ export default testSuite(({ describe }) => { await git('add', ['data.json']); // Generate flag should override generate config - const committing = aicommits([ - '--generate', '2', - ]); + const committing = aicommits(['--generate', '2']); // Hit enter to accept the commit message committing.stdout!.on('data', function onPrompt(buffer: Buffer) { @@ -158,10 +175,15 @@ export default testSuite(({ describe }) => { onTestFail(() => console.log({ stdout })); expect(countChoices).toBe(2); - const statusAfter = await git('status', ['--porcelain', '--untracked-files=no']); + const statusAfter = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusAfter.stdout).toBe(''); - const { stdout: commitMessage } = await git('log', ['--pretty=format:%s']); + const { stdout: commitMessage } = await git('log', [ + '--pretty=format:%s', + ]); console.log({ commitMessage, length: commitMessage.length, @@ -173,7 +195,8 @@ export default testSuite(({ describe }) => { test('Generates Japanese commit message via locale config', async () => { // https://stackoverflow.com/a/15034560/911407 - const japanesePattern = /[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFF9F\u4E00-\u9FAF\u3400-\u4DBF]/; + const japanesePattern = + /[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFF9F\u4E00-\u9FAF\u3400-\u4DBF]/; const { fixture, aicommits } = await createFixture({ ...files, @@ -195,10 +218,15 @@ export default testSuite(({ describe }) => { await committing; - const statusAfter = await git('status', ['--porcelain', '--untracked-files=no']); + const statusAfter = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusAfter.stdout).toBe(''); - const { stdout: commitMessage } = await git('log', ['--pretty=format:%s']); + const { stdout: commitMessage } = await git('log', [ + '--pretty=format:%s', + ]); console.log({ commitMessage, length: commitMessage.length, @@ -211,7 +239,8 @@ export default testSuite(({ describe }) => { describe('commit types', ({ test }) => { test('Should not use conventional commits by default', async () => { - const conventionalCommitPattern = /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):\s/; + const conventionalCommitPattern = + /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):\s/; const { fixture, aicommits } = await createFixture({ ...files, }); @@ -231,7 +260,10 @@ export default testSuite(({ describe }) => { await committing; - const statusAfter = await git('status', ['--porcelain', '--untracked-files=no']); + const statusAfter = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusAfter.stdout).toBe(''); const { stdout: commitMessage } = await git('log', ['--oneline']); @@ -242,7 +274,8 @@ export default testSuite(({ describe }) => { }); test('Conventional commits', async () => { - const conventionalCommitPattern = /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):\s/; + const conventionalCommitPattern = + /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):\s/; const { fixture, aicommits } = await createFixture({ ...files, '.aicommits': `${files['.aicommits']}\ntype=conventional`, @@ -263,7 +296,10 @@ export default testSuite(({ describe }) => { await committing; - const statusAfter = await git('status', ['--porcelain', '--untracked-files=no']); + const statusAfter = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusAfter.stdout).toBe(''); const { stdout: commitMessage } = await git('log', ['--oneline']); @@ -274,7 +310,8 @@ export default testSuite(({ describe }) => { }); test('Accepts --type flag, overriding config', async () => { - const conventionalCommitPattern = /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):\s/; + const conventionalCommitPattern = + /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):\s/; const { fixture, aicommits } = await createFixture({ ...files, '.aicommits': `${files['.aicommits']}\ntype=other`, @@ -284,9 +321,7 @@ export default testSuite(({ describe }) => { await git('add', ['data.json']); // Generate flag should override generate config - const committing = aicommits([ - '--type', 'conventional', - ]); + const committing = aicommits(['--type', 'conventional']); committing.stdout!.on('data', (buffer: Buffer) => { const stdout = buffer.toString(); @@ -298,7 +333,10 @@ export default testSuite(({ describe }) => { await committing; - const statusAfter = await git('status', ['--porcelain', '--untracked-files=no']); + const statusAfter = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusAfter.stdout).toBe(''); const { stdout: commitMessage } = await git('log', ['--oneline']); @@ -309,7 +347,8 @@ export default testSuite(({ describe }) => { }); test('Accepts empty --type flag', async () => { - const conventionalCommitPattern = /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):\s/; + const conventionalCommitPattern = + /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test):\s/; const { fixture, aicommits } = await createFixture({ ...files, '.aicommits': `${files['.aicommits']}\ntype=conventional`, @@ -318,9 +357,7 @@ export default testSuite(({ describe }) => { await git('add', ['data.json']); - const committing = aicommits([ - '--type', '', - ]); + const committing = aicommits(['--type', '']); committing.stdout!.on('data', (buffer: Buffer) => { const stdout = buffer.toString(); @@ -332,7 +369,10 @@ export default testSuite(({ describe }) => { await committing; - const statusAfter = await git('status', ['--porcelain', '--untracked-files=no']); + const statusAfter = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusAfter.stdout).toBe(''); const { stdout: commitMessage } = await git('log', ['--oneline']); @@ -394,10 +434,15 @@ export default testSuite(({ describe }) => { await committing; - const statusAfter = await git('status', ['--porcelain', '--untracked-files=no']); + const statusAfter = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusAfter.stdout).toBe(''); - const { stdout: commitMessage } = await git('log', ['--pretty=format:%s']); + const { stdout: commitMessage } = await git('log', [ + '--pretty=format:%s', + ]); console.log({ commitMessage, length: commitMessage.length, @@ -429,10 +474,15 @@ export default testSuite(({ describe }) => { await committing; - const statusAfter = await git('status', ['--porcelain', '--untracked-files=no']); + const statusAfter = await git('status', [ + '--porcelain', + '--untracked-files=no', + ]); expect(statusAfter.stdout).toBe(''); - const { stdout: commitMessage } = await git('log', ['--pretty=format:%s']); + const { stdout: commitMessage } = await git('log', [ + '--pretty=format:%s', + ]); console.log({ commitMessage, length: commitMessage.length, diff --git a/tests/specs/cli/error-cases.ts b/tests/specs/cli/error-cases.ts index 6695e080..1c43acb7 100644 --- a/tests/specs/cli/error-cases.ts +++ b/tests/specs/cli/error-cases.ts @@ -17,7 +17,9 @@ export default testSuite(({ describe }) => { const { stdout, exitCode } = await aicommits([], { reject: false }); expect(exitCode).toBe(1); - expect(stdout).toMatch('No staged changes found. Stage your changes manually, or automatically stage all changes with the `--all` flag.'); + expect(stdout).toMatch( + 'No staged changes found. Stage your changes manually, or automatically stage all changes with the `--all` flag.' + ); await fixture.rm(); }); }); diff --git a/tests/specs/config.ts b/tests/specs/config.ts index 43f566c3..9bfe45fc 100644 --- a/tests/specs/config.ts +++ b/tests/specs/config.ts @@ -22,7 +22,9 @@ export default testSuite(({ describe }) => { reject: false, }); - expect(stderr).toMatch('Invalid config property OPENAI_KEY: Must start with "sk-"'); + expect(stderr).toMatch( + 'Invalid config property OPENAI_KEY: Must start with "sk-"' + ); }); await test('set config file', async () => { @@ -71,9 +73,12 @@ export default testSuite(({ describe }) => { await describe('max-length', ({ test }) => { test('must be an integer', async () => { - const { stderr } = await aicommits(['config', 'set', 'max-length=abc'], { - reject: false, - }); + const { stderr } = await aicommits( + ['config', 'set', 'max-length=abc'], + { + reject: false, + } + ); expect(stderr).toMatch('Must be an integer'); }); diff --git a/tests/specs/openai/conventional-commits.ts b/tests/specs/openai/conventional-commits.ts index b89255eb..7a3e43e6 100644 --- a/tests/specs/openai/conventional-commits.ts +++ b/tests/specs/openai/conventional-commits.ts @@ -1,7 +1,5 @@ import { expect, testSuite } from 'manten'; -import { - generateCommitMessage, -} from '../../../src/utils/openai.js'; +import { generateCommitMessage } from '../../../src/utils/openai.js'; import type { ValidConfig } from '../../../src/utils/config.js'; import { getDiff } from '../../utils.js'; @@ -9,13 +7,16 @@ const { OPENAI_KEY } = process.env; export default testSuite(({ describe }) => { if (!OPENAI_KEY) { - console.warn('⚠️ process.env.OPENAI_KEY is necessary to run these tests. Skipping...'); + console.warn( + '⚠️ process.env.OPENAI_KEY is necessary to run these tests. Skipping...' + ); return; } describe('Conventional Commits', async ({ test }) => { 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 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 getDiff('new-feature.diff'); @@ -58,9 +59,7 @@ export default testSuite(({ describe }) => { }); await test('Should use "build:" conventional commit when change relate to github action build pipeline', async () => { - const gitDiff = await getDiff( - 'github-action-build-pipeline.diff', - ); + const gitDiff = await getDiff('github-action-build-pipeline.diff'); const commitMessage = await runGenerateCommitMessage(gitDiff); @@ -128,8 +127,10 @@ export default testSuite(({ describe }) => { console.log('Generated message:', commitMessage); }); - async function runGenerateCommitMessage(gitDiff: string, - configOverrides: Partial = {}): Promise { + async function runGenerateCommitMessage( + gitDiff: string, + configOverrides: Partial = {} + ): Promise { const config = { locale: 'en', type: 'conventional', @@ -137,7 +138,16 @@ export default testSuite(({ describe }) => { 'max-length': 50, ...configOverrides, } as ValidConfig; - const commitMessages = await generateCommitMessage(OPENAI_KEY!, 'gpt-3.5-turbo', config.locale, gitDiff, config.generate, config['max-length'], config.type, 7000); + const commitMessages = await generateCommitMessage( + OPENAI_KEY!, + 'gpt-3.5-turbo', + config.locale, + gitDiff, + config.generate, + config['max-length'], + config.type, + 7000 + ); return commitMessages[0]; } diff --git a/tests/utils.ts b/tests/utils.ts index 77e190eb..e2eb670d 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -15,46 +15,32 @@ const createAicommits = (fixture: FsFixture) => { USERPROFILE: fixture.path, // Windows }; - return ( - args?: string[], - options?: Options, - ) => execaNode(aicommitsPath, args, { - cwd: fixture.path, - ...options, - extendEnv: false, - env: { - ...homeEnv, - ...options?.env, - }, + return (args?: string[], options?: Options) => + execaNode(aicommitsPath, args, { + cwd: fixture.path, + ...options, + extendEnv: false, + env: { + ...homeEnv, + ...options?.env, + }, - // Block tsx nodeOptions - nodeOptions: [], - }); + // Block tsx nodeOptions + nodeOptions: [], + }); }; export const createGit = async (cwd: string) => { - const git = ( - command: string, - args?: string[], - options?: Options, - ) => ( - execa( - 'git', - [command, ...(args || [])], - { - cwd, - ...options, - }, - ) - ); + const git = (command: string, args?: string[], options?: Options) => + execa('git', [command, ...(args || [])], { + cwd, + ...options, + }); - await git( - 'init', - [ - // In case of different default branch name - '--initial-branch=master', - ], - ); + await git('init', [ + // In case of different default branch name + '--initial-branch=master', + ]); await git('config', ['user.name', 'name']); await git('config', ['user.email', 'email']); @@ -62,9 +48,7 @@ export const createGit = async (cwd: string) => { return git; }; -export const createFixture = async ( - source?: string | FileTree, -) => { +export const createFixture = async (source?: string | FileTree) => { const fixture = await createFixtureBase(source); const aicommits = createAicommits(fixture); @@ -76,17 +60,20 @@ export const createFixture = async ( export const files = Object.freeze({ '.aicommits': `OPENAI_KEY=${process.env.OPENAI_KEY}`, - 'data.json': Array.from({ length: 10 }, (_, i) => `${i}. Lorem ipsum dolor sit amet`).join('\n'), + 'data.json': Array.from( + { length: 10 }, + (_, i) => `${i}. Lorem ipsum dolor sit amet` + ).join('\n'), }); export const assertOpenAiToken = () => { if (!process.env.OPENAI_KEY) { - throw new Error('⚠️ process.env.OPENAI_KEY is necessary to run these tests. Skipping...'); + 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 => fs.readFile( - new URL(`fixtures/${diffName}`, import.meta.url), - 'utf8', -); +export const getDiff = async (diffName: string): Promise => + fs.readFile(new URL(`fixtures/${diffName}`, import.meta.url), 'utf8');