-
Notifications
You must be signed in to change notification settings - Fork 57
fix(cli): add requireTTY() before each unguarded interactive TUI launch (#982) #1640
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f0f0adf
fix(cli): guard import/view/export TUI launches with requireTTY (#982)
aidandaly24 5edc308
test(cli): drive real requireTTY guard; centralize in renderTUI
aidandaly24 379e02d
fix(test): type ink render mocks to satisfy tsc --noEmit
aidandaly24 cee06c4
fix(cli): guard dev browser-mode picker with requireTTY (#982)
aidandaly24 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { launchTuiDevScreenWithPicker } from '../browser-mode'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| // Drive the REAL requireTTY by toggling process.stdin.isTTY / process.stdout.isTTY | ||
| // and spying on process.exit / console.error. Only the I/O boundaries are mocked: | ||
| // the Ink renderer and the DevScreen the picker renders. This pins the guard that | ||
| // PR #1640 centralized — the browser-mode harness picker must refuse to render in a | ||
| // non-TTY context instead of throwing Ink's "Raw mode is not supported" stack trace. | ||
| const mockRender = vi.fn((..._args: unknown[]) => ({ | ||
| unmount: vi.fn(), | ||
| waitUntilExit: () => Promise.resolve(), | ||
| })); | ||
|
|
||
| vi.mock('ink', () => ({ | ||
| render: (...args: unknown[]) => mockRender(...args), | ||
| })); | ||
|
|
||
| vi.mock('../../../tui/screens/dev/DevScreen', () => ({ DevScreen: () => null })); | ||
|
|
||
| describe('dev browser-mode picker TTY guard', () => { | ||
| let exitSpy: ReturnType<typeof vi.spyOn>; | ||
| let errorSpy: ReturnType<typeof vi.spyOn>; | ||
| let stdoutWriteSpy: ReturnType<typeof vi.spyOn>; | ||
| const origStdinIsTTY = process.stdin.isTTY; | ||
| const origStdoutIsTTY = process.stdout.isTTY; | ||
|
|
||
| beforeEach(() => { | ||
| exitSpy = vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => { | ||
| throw new Error(`process.exit(${code})`); | ||
| }); | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); | ||
| stdoutWriteSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.stdin.isTTY = origStdinIsTTY; | ||
| process.stdout.isTTY = origStdoutIsTTY; | ||
| exitSpy.mockRestore(); | ||
| errorSpy.mockRestore(); | ||
| stdoutWriteSpy.mockRestore(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('exits with code 1 and never renders in a non-TTY context', async () => { | ||
| process.stdin.isTTY = false; | ||
| process.stdout.isTTY = false; | ||
|
|
||
| await expect(launchTuiDevScreenWithPicker('/tmp/project')).rejects.toThrow('process.exit(1)'); | ||
|
|
||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('requires an interactive terminal')); | ||
| expect(mockRender).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('exits when only stdin is not a TTY', async () => { | ||
| process.stdin.isTTY = false; | ||
| process.stdout.isTTY = true; | ||
|
|
||
| await expect(launchTuiDevScreenWithPicker('/tmp/project')).rejects.toThrow('process.exit(1)'); | ||
|
|
||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| expect(mockRender).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('exits when only stdout is not a TTY', async () => { | ||
| process.stdin.isTTY = true; | ||
| process.stdout.isTTY = false; | ||
|
|
||
| await expect(launchTuiDevScreenWithPicker('/tmp/project')).rejects.toThrow('process.exit(1)'); | ||
|
|
||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| expect(mockRender).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('renders the picker when both stdin and stdout are TTYs', async () => { | ||
| process.stdin.isTTY = true; | ||
| process.stdout.isTTY = true; | ||
|
|
||
| await launchTuiDevScreenWithPicker('/tmp/project'); | ||
|
|
||
| expect(exitSpy).not.toHaveBeenCalled(); | ||
| expect(mockRender).toHaveBeenCalled(); | ||
| }); | ||
| }); |
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,87 @@ | ||
| import { registerExport } from '../index'; | ||
| import { Command } from '@commander-js/extra-typings'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| // Drive the REAL requireTTY (centralized inside renderTUI) by toggling | ||
| // process.stdin.isTTY / process.stdout.isTTY and spying on process.exit / | ||
| // console.error. Only the I/O boundaries renderTUI touches are mocked: the | ||
| // Ink renderer, telemetry, and post-command notices. The guard runs first in | ||
| // renderTUI, so a non-TTY context exits before any of these are reached. | ||
| const mockRender = vi.fn((..._args: unknown[]) => ({ waitUntilExit: () => Promise.resolve() })); | ||
|
|
||
| vi.mock('ink', () => ({ | ||
| render: (...args: unknown[]) => mockRender(...args), | ||
| })); | ||
|
|
||
| vi.mock('../../../telemetry', () => ({ | ||
| TelemetryClientAccessor: { | ||
| init: vi.fn().mockResolvedValue(undefined), | ||
| get: vi.fn().mockResolvedValue(null), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../../../notices', () => ({ | ||
| printPostCommandNotices: vi.fn().mockResolvedValue(undefined), | ||
| })); | ||
|
|
||
| describe('export harness TTY guard', () => { | ||
| let program: Command; | ||
| let exitSpy: ReturnType<typeof vi.spyOn>; | ||
| let errorSpy: ReturnType<typeof vi.spyOn>; | ||
| let writeSpy: ReturnType<typeof vi.spyOn>; | ||
| const origStdinIsTTY = process.stdin.isTTY; | ||
| const origStdoutIsTTY = process.stdout.isTTY; | ||
|
|
||
| beforeEach(() => { | ||
| program = new Command(); | ||
| program.exitOverride(); | ||
| registerExport(program); | ||
|
|
||
| // Swallow the alt-screen escape sequences renderTUI writes on the TTY path. | ||
| writeSpy = vi.spyOn(process.stdout, 'write').mockReturnValue(true); | ||
| exitSpy = vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => { | ||
| throw new Error(`process.exit(${code})`); | ||
| }); | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.stdin.isTTY = origStdinIsTTY; | ||
| process.stdout.isTTY = origStdoutIsTTY; | ||
| writeSpy.mockRestore(); | ||
| exitSpy.mockRestore(); | ||
| errorSpy.mockRestore(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('exits with code 1 and never renders the TUI in a non-TTY context', async () => { | ||
| process.stdin.isTTY = false; | ||
| process.stdout.isTTY = false; | ||
|
|
||
| await expect(program.parseAsync(['export', 'harness'], { from: 'user' })).rejects.toThrow('process.exit(1)'); | ||
|
|
||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('requires an interactive terminal')); | ||
| expect(mockRender).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('exits when only stdout is not a TTY', async () => { | ||
| process.stdin.isTTY = true; | ||
| process.stdout.isTTY = false; | ||
|
|
||
| await expect(program.parseAsync(['export', 'harness'], { from: 'user' })).rejects.toThrow('process.exit(1)'); | ||
|
|
||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| expect(mockRender).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('renders the TUI when both stdin and stdout are TTYs', async () => { | ||
| process.stdin.isTTY = true; | ||
| process.stdout.isTTY = true; | ||
|
|
||
| await program.parseAsync(['export', 'harness'], { from: 'user' }); | ||
|
|
||
| expect(exitSpy).not.toHaveBeenCalled(); | ||
| expect(mockRender).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
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,81 @@ | ||
| import { registerImport } from '../command'; | ||
| import { Command } from '@commander-js/extra-typings'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| // Drive the REAL requireTTY by toggling process.stdin.isTTY / process.stdout.isTTY | ||
| // and spying on process.exit / console.error. Only the I/O boundaries are mocked: | ||
| // the Ink renderer and the ImportFlow screen. requireProject is stubbed to a no-op | ||
| // so the TTY guard (which runs after it) is what we exercise here. | ||
| const mockRender = vi.fn((..._args: unknown[]) => ({ clear: vi.fn(), unmount: vi.fn() })); | ||
|
|
||
| vi.mock('../../../tui/guards', async importOriginal => { | ||
| const actual = await importOriginal<typeof import('../../../tui/guards')>(); | ||
| return { | ||
| ...actual, | ||
| requireProject: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock('ink', () => ({ | ||
| render: (...args: unknown[]) => mockRender(...args), | ||
| })); | ||
|
|
||
| vi.mock('../../../tui/screens/import', () => ({ ImportFlow: () => null })); | ||
|
|
||
| describe('import non-source TTY guard', () => { | ||
| let program: Command; | ||
| let exitSpy: ReturnType<typeof vi.spyOn>; | ||
| let errorSpy: ReturnType<typeof vi.spyOn>; | ||
| const origStdinIsTTY = process.stdin.isTTY; | ||
| const origStdoutIsTTY = process.stdout.isTTY; | ||
|
|
||
| beforeEach(() => { | ||
| program = new Command(); | ||
| program.exitOverride(); | ||
| registerImport(program); | ||
|
|
||
| exitSpy = vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => { | ||
| throw new Error(`process.exit(${code})`); | ||
| }); | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.stdin.isTTY = origStdinIsTTY; | ||
| process.stdout.isTTY = origStdoutIsTTY; | ||
| exitSpy.mockRestore(); | ||
| errorSpy.mockRestore(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('exits with code 1 and does not render in a non-TTY context', async () => { | ||
| process.stdin.isTTY = false; | ||
| process.stdout.isTTY = false; | ||
|
|
||
| await expect(program.parseAsync(['import'], { from: 'user' })).rejects.toThrow('process.exit(1)'); | ||
|
|
||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('requires an interactive terminal')); | ||
| expect(mockRender).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('exits when only stdin is not a TTY', async () => { | ||
| process.stdin.isTTY = false; | ||
| process.stdout.isTTY = true; | ||
|
|
||
| await expect(program.parseAsync(['import'], { from: 'user' })).rejects.toThrow('process.exit(1)'); | ||
|
|
||
| expect(exitSpy).toHaveBeenCalledWith(1); | ||
| expect(mockRender).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('renders the interactive flow when both stdin and stdout are TTYs', async () => { | ||
| process.stdin.isTTY = true; | ||
| process.stdout.isTTY = true; | ||
|
|
||
| await program.parseAsync(['import'], { from: 'user' }); | ||
|
|
||
| expect(exitSpy).not.toHaveBeenCalled(); | ||
| expect(mockRender).toHaveBeenCalled(); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocking
requireTTYitself means this test isn't actually verifying the guard's behavior — it only verifies that something calledmockRequireTTYruns before something that callsmockRenderTUI. If a future refactor movedrequireTTY()afterrenderTUI(), this test would still pass becausemockRequireTTYonly throws andmockRenderTUIis a no-op that's then never reached anyway via the rejected promise.Suggest exercising the real
requireTTY:Then mock only
renderTUI(true I/O boundary) and assert that with non-TTY,process.exit(1)was called andrenderTUIwas not; with TTY,renderTUIwas called. This is the pattern used insrc/cli/commands/feedback/__tests__/consent-prompt.test.ts.