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

fix: respect the default for a text prompt #3466

Merged
merged 6 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions .changeset/cool-dogs-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/cli": patch
---

Respect the `Prompt.TextOptions.default` for a prompt created with `Prompt.text`
6 changes: 4 additions & 2 deletions packages/cli/src/internal/prompt/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,11 @@ function handleProcess(options: Options) {
}
case "enter":
case "return": {
return Effect.match(options.validate(state.value), {
const value = state.value.length > 0 ? state.value : options.default
return Effect.match(options.validate(value), {
onFailure: (error) =>
Action.NextFrame({
state: { ...state, error: Option.some(error) }
state: { ...state, value, error: Option.some(error) }
}),
onSuccess: (value) => Action.Submit({ value })
})
Expand Down Expand Up @@ -264,6 +265,7 @@ function basePrompt(
validate: Effect.succeed,
...options
}

return InternalPrompt.custom(initialState, {
render: handleRender(opts),
process: handleProcess(opts),
Expand Down
43 changes: 43 additions & 0 deletions packages/cli/test/Prompt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as Prompt from "@effect/cli/Prompt"
import * as MockTerminal from "@effect/cli/test/services/MockTerminal"
import type { Terminal } from "@effect/platform/Terminal"
import * as Effect from "effect/Effect"
import * as Fiber from "effect/Fiber"
import { describe, expect, it } from "vitest"


Check failure on line 8 in packages/cli/test/Prompt.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra line break(s)

const runEffect = <E, A>(
self: Effect.Effect<A, E, Terminal>
): Promise<A> => Effect.provide(self, MockTerminal.layer).pipe(Effect.runPromise)

describe("Prompt", () => {
describe("text", () => {
it("should use the prompt value when no default is provided", () =>
Effect.gen(function*() {
const prompt = Prompt.text({
message: "This does not have a default",

Check failure on line 19 in packages/cli/test/Prompt.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra code ","
})

const fiber = yield* Effect.fork(prompt)
yield* MockTerminal.inputKey("enter")
const result = yield* Fiber.join(fiber)

expect(result).toBe('')

Check failure on line 26 in packages/cli/test/Prompt.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Require code "\"\"" instead of "''"
}).pipe(runEffect))

it("should use the default value when the default is provided", () =>
Effect.gen(function*() {
const prompt = Prompt.text({
message: "This should have a default",
default: "default-value"
})

Check failure on line 35 in packages/cli/test/Prompt.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Extra whitespace(s)
const fiber = yield* Effect.fork(prompt)
yield* MockTerminal.inputKey("enter")
const result = yield* Fiber.join(fiber)

expect(result).toBe('default-value')

Check failure on line 40 in packages/cli/test/Prompt.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Require code "\"default-value\"" instead of "'default-value'"
}).pipe(runEffect))
})
})
Loading