-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: delegate ol changelog to @doist/cli-core/commands
#69
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { Command } from 'commander' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { registerChangelogCommand } from '../commands/changelog.js' | ||
| import { BaseCliError } from '../lib/errors.js' | ||
| import { formatError, formatErrorJson } from '../lib/output.js' | ||
|
|
||
| describe('changelog command end-to-end', () => { | ||
| it('rejects with BaseCliError(INVALID_TYPE) when --count is not a number', async () => { | ||
| const program = new Command() | ||
| program.exitOverride() | ||
| registerChangelogCommand(program) | ||
|
|
||
| await expect( | ||
| program.parseAsync(['node', 'ol', 'changelog', '-n', 'abc']), | ||
| ).rejects.toBeInstanceOf(BaseCliError) | ||
|
|
||
| const err = await program | ||
| .parseAsync(['node', 'ol', 'changelog', '-n', 'abc']) | ||
| .catch((e: Error) => e) | ||
| expect(err).toBeInstanceOf(BaseCliError) | ||
| expect((err as BaseCliError).code).toBe('INVALID_TYPE') | ||
| }) | ||
|
|
||
| it('formats the rejected BaseCliError through formatError (human)', async () => { | ||
| const program = new Command() | ||
| program.exitOverride() | ||
| registerChangelogCommand(program) | ||
|
|
||
| const err = await program | ||
| .parseAsync(['node', 'ol', 'changelog', '-n', 'abc']) | ||
| .catch((e: Error) => e) | ||
| expect(err).toBeInstanceOf(BaseCliError) | ||
| const out = formatError(err as BaseCliError) | ||
| expect(out).toContain('Error: INVALID_TYPE') | ||
| expect(out).toContain('Count must be a positive integer') | ||
| }) | ||
|
|
||
| it('formats the rejected BaseCliError through formatErrorJson', async () => { | ||
| const program = new Command() | ||
| program.exitOverride() | ||
| registerChangelogCommand(program) | ||
|
|
||
| const err = await program | ||
| .parseAsync(['node', 'ol', 'changelog', '-n', 'abc']) | ||
| .catch((e: Error) => e) | ||
| const parsed = JSON.parse(formatErrorJson(err as BaseCliError)) | ||
| expect(parsed.error.code).toBe('INVALID_TYPE') | ||
| expect(parsed.error.message).toBe('Count must be a positive integer') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,161 +1,42 @@ | ||
| import { basename } from 'node:path' | ||
| import { Command } from 'commander' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('node:fs/promises') | ||
| const { registerCoreChangelogCommand } = vi.hoisted(() => ({ | ||
| registerCoreChangelogCommand: vi.fn(), | ||
| })) | ||
|
|
||
| import { readFile } from 'node:fs/promises' | ||
| import { registerChangelogCommand } from '../commands/changelog.js' | ||
|
|
||
| const mockReadFile = vi.mocked(readFile) | ||
|
|
||
| const SAMPLE_CHANGELOG = `# Changelog | ||
|
|
||
| ## [1.5.0](https://example.com) (2026-03-15) | ||
|
|
||
| ### Features | ||
|
|
||
| * feature five | ||
|
|
||
| ## [1.4.0](https://example.com) (2026-03-14) | ||
|
|
||
| ### Features | ||
|
|
||
| * feature four | ||
|
|
||
| ## [1.3.0](https://example.com) (2026-03-13) | ||
|
|
||
| ### Bug Fixes | ||
|
|
||
| * fix three | ||
|
|
||
| ## [1.2.0](https://example.com) (2026-03-12) | ||
| vi.mock('@doist/cli-core/commands', () => ({ | ||
| registerChangelogCommand: registerCoreChangelogCommand, | ||
| })) | ||
|
|
||
| ### Features | ||
|
|
||
| * feature two | ||
|
|
||
| ## [1.1.0](https://example.com) (2026-03-11) | ||
|
|
||
| ### Features | ||
|
|
||
| * feature one | ||
|
|
||
| ## [1.0.0](https://example.com) (2026-03-10) | ||
|
|
||
| ### Features | ||
|
|
||
| * initial release | ||
| ` | ||
|
|
||
| function createProgram() { | ||
| const program = new Command() | ||
| program.exitOverride() | ||
| registerChangelogCommand(program) | ||
| return program | ||
| } | ||
|
|
||
| describe('changelog command', () => { | ||
| let consoleSpy: ReturnType<typeof vi.spyOn> | ||
| let consoleErrorSpy: ReturnType<typeof vi.spyOn> | ||
| import packageJson from '../../package.json' with { type: 'json' } | ||
| import { registerChangelogCommand } from '../commands/changelog.js' | ||
|
|
||
| describe('changelog wrapper', () => { | ||
| beforeEach(() => { | ||
| consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) | ||
| consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
| process.exitCode = undefined | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| process.exitCode = undefined | ||
| registerCoreChangelogCommand.mockReset() | ||
| }) | ||
|
|
||
| it('shows last 5 versions by default', async () => { | ||
| mockReadFile.mockResolvedValue(SAMPLE_CHANGELOG) | ||
|
|
||
| const program = createProgram() | ||
| await program.parseAsync(['node', 'ol', 'changelog']) | ||
|
|
||
| expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('1.5.0')) | ||
| expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('1.1.0')) | ||
| // Should show "view full changelog" link since there are 6 versions | ||
| expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('View full changelog')) | ||
| }) | ||
|
|
||
| it('includes latest version when changelog has no preamble', async () => { | ||
| const noPreambleChangelog = `## [2.0.0](https://example.com) (2026-03-20) | ||
|
|
||
| ### Features | ||
|
|
||
| * new major version | ||
|
|
||
| ## [1.5.0](https://example.com) (2026-03-15) | ||
|
|
||
| ### Features | ||
|
|
||
| * feature five | ||
| ` | ||
| mockReadFile.mockResolvedValue(noPreambleChangelog) | ||
|
|
||
| const program = createProgram() | ||
| await program.parseAsync(['node', 'ol', 'changelog']) | ||
|
|
||
| const output = consoleSpy.mock.calls[0][0] as string | ||
| expect(output).toContain('2.0.0') | ||
| expect(output).toContain('1.5.0') | ||
| }) | ||
|
|
||
| it('respects --count option', async () => { | ||
| mockReadFile.mockResolvedValue(SAMPLE_CHANGELOG) | ||
|
|
||
| const program = createProgram() | ||
| await program.parseAsync(['node', 'ol', 'changelog', '-n', '2']) | ||
|
|
||
| const output = consoleSpy.mock.calls[0][0] as string | ||
| expect(output).toContain('1.5.0') | ||
| expect(output).toContain('1.4.0') | ||
| expect(output).not.toContain('1.3.0') | ||
| }) | ||
|
|
||
| it('handles fewer entries than requested', async () => { | ||
| const shortChangelog = `# Changelog | ||
|
|
||
| ## [1.1.0](https://example.com) (2026-03-11) | ||
|
|
||
| ### Features | ||
|
|
||
| * only version | ||
| ` | ||
| mockReadFile.mockResolvedValue(shortChangelog) | ||
|
|
||
| const program = createProgram() | ||
| await program.parseAsync(['node', 'ol', 'changelog']) | ||
|
|
||
| expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('1.1.0')) | ||
| // Should NOT show "view full changelog" link since all versions are shown | ||
| expect(consoleSpy).not.toHaveBeenCalledWith(expect.stringContaining('View full changelog')) | ||
| }) | ||
|
|
||
| it('handles missing changelog file', async () => { | ||
| mockReadFile.mockRejectedValue(new Error('ENOENT')) | ||
|
|
||
| const program = createProgram() | ||
| await program.parseAsync(['node', 'ol', 'changelog']) | ||
| it('delegates to @doist/cli-core/commands with the expected config', () => { | ||
| const program = new Command() | ||
| registerChangelogCommand(program) | ||
|
|
||
| expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
| expect.anything(), | ||
| expect.stringContaining('Could not read changelog file'), | ||
| ) | ||
| expect(process.exitCode).toBe(1) | ||
| expect(registerCoreChangelogCommand).toHaveBeenCalledTimes(1) | ||
| const [passedProgram, config] = registerCoreChangelogCommand.mock.calls[0] | ||
| expect(passedProgram).toBe(program) | ||
| expect(basename(config.path)).toBe('CHANGELOG.md') | ||
| expect(config.repoUrl).toBe('https://github.com/Doist/outline-cli') | ||
| expect(config.version).toBe(packageJson.version) | ||
| expect(config.bulletMarkers).toEqual(['*', '-']) | ||
| }) | ||
|
|
||
| it('handles invalid count', async () => { | ||
| const program = createProgram() | ||
| await program.parseAsync(['node', 'ol', 'changelog', '-n', 'abc']) | ||
| it('derives repoUrl from package.json (strips .git / git+ prefix)', () => { | ||
| const program = new Command() | ||
| registerChangelogCommand(program) | ||
|
|
||
| expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
| expect.anything(), | ||
| expect.stringContaining('Count must be a positive number'), | ||
| ) | ||
| expect(process.exitCode).toBe(1) | ||
| const [, config] = registerCoreChangelogCommand.mock.calls[0] | ||
| expect(config.repoUrl).not.toMatch(/\.git$/) | ||
| expect(config.repoUrl).not.toMatch(/^git\+/) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.