-
-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathArgs.test.ts
175 lines (160 loc) · 7.05 KB
/
Args.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as Args from "@effect/cli/Args"
import * as CliConfig from "@effect/cli/CliConfig"
import * as HelpDoc from "@effect/cli/HelpDoc"
import * as ValidationError from "@effect/cli/ValidationError"
import { FileSystem, Path } from "@effect/platform"
import { NodeContext } from "@effect/platform-node"
import * as Schema from "@effect/schema/Schema"
import * as Array from "effect/Array"
import * as Effect from "effect/Effect"
import * as Option from "effect/Option"
import { describe, expect, it } from "vitest"
const runEffect = <E, A>(
self: Effect.Effect<A, E, NodeContext.NodeContext>
): Promise<A> => Effect.provide(self, NodeContext.layer).pipe(Effect.runPromise)
describe("Args", () => {
it("validates an valid argument with a default", () =>
Effect.gen(function*(_) {
const args = Args.integer().pipe(Args.withDefault(0))
const result = yield* _(
Args.validate(args, Array.empty(), CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), 0])
}).pipe(runEffect))
it("validates an valid optional argument", () =>
Effect.gen(function*(_) {
const args = Args.integer().pipe(Args.optional)
let result = yield* _(
Args.validate(args, Array.empty(), CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), Option.none()])
result = yield* _(
Args.validate(args, ["123"], CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), Option.some(123)])
}).pipe(runEffect))
it("does not validate an invalid argument even when there is a default", () =>
Effect.gen(function*(_) {
const args = Args.integer().pipe(Args.withDefault(0))
const result = yield* _(Effect.flip(
Args.validate(args, Array.of("abc"), CliConfig.defaultConfig)
))
expect(result).toEqual(ValidationError.invalidArgument(HelpDoc.p("'abc' is not a integer")))
}).pipe(runEffect))
it("should validate an existing file that is expected to exist", () =>
Effect.gen(function*(_) {
const path = yield* _(Path.Path)
const filePath = path.join(__dirname, "Args.test.ts")
const args = Args.file({ name: "files", exists: "yes" }).pipe(Args.repeated)
const result = yield* _(
Args.validate(args, Array.of(filePath), CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), Array.of(filePath)])
}).pipe(runEffect))
it("should return an error when a file that is expected to exist is not found", () =>
Effect.gen(function*(_) {
const path = yield* _(Path.Path)
const filePath = path.join(__dirname, "NotExist.test.ts")
const args = Args.file({ name: "files", exists: "yes" }).pipe(Args.repeated)
const result = yield* _(
Effect.flip(Args.validate(args, Array.of(filePath), CliConfig.defaultConfig))
)
expect(result).toEqual(ValidationError.invalidArgument(HelpDoc.p(
`Path '${filePath}' must exist`
)))
}).pipe(runEffect))
it("should validate a non-existent file that is expected not to exist", () =>
Effect.gen(function*(_) {
const path = yield* _(Path.Path)
const filePath = path.join(__dirname, "NotExist.test.ts")
const args = Args.file({ name: "files", exists: "no" }).pipe(Args.repeated)
const result = yield* _(
Args.validate(args, Array.of(filePath), CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), Array.of(filePath)])
}).pipe(runEffect))
it("should validate a series of files", () =>
Effect.gen(function*(_) {
const path = yield* _(Path.Path)
const filePath = path.join(__dirname, "NotExist.test.ts")
const args = Args.file({ name: "files", exists: "no" }).pipe(Args.repeated)
const result = yield* _(
Args.validate(args, Array.make(filePath, filePath), CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), Array.make(filePath, filePath)])
}).pipe(runEffect))
it("validates an valid argument with a Schema", () =>
Effect.gen(function*(_) {
const args = Args.integer().pipe(Args.withSchema(Schema.Positive))
const result = yield* _(
Args.validate(args, ["123"], CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), 123])
}).pipe(runEffect))
it("does not validate an invalid argument with a Schema", () =>
Effect.gen(function*(_) {
const args = Args.integer().pipe(Args.withSchema(Schema.Positive))
const result = yield* _(Effect.flip(
Args.validate(args, Array.of("-123"), CliConfig.defaultConfig)
))
expect(result).toEqual(ValidationError.invalidArgument(HelpDoc.p(
"Positive\n" +
"└─ Predicate refinement failure\n" +
" └─ Expected Positive, actual -123"
)))
}).pipe(runEffect))
it("fileContent", () =>
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const filePath = path.join(__dirname, "fixtures/config.json")
const content = yield* _(fs.readFile(filePath))
const args = Args.fileContent({ name: "files" }).pipe(Args.repeated)
const result = yield* _(
Args.validate(args, Array.of(filePath), CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), Array.of([filePath, content])])
}).pipe(runEffect))
it("fileText", () =>
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const filePath = path.join(__dirname, "fixtures/config.json")
const content = yield* _(fs.readFileString(filePath))
const args = Args.fileText({ name: "files" }).pipe(Args.repeated)
const result = yield* _(
Args.validate(args, Array.of(filePath), CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), Array.of([filePath, content])])
}).pipe(runEffect))
it("fileParse", () =>
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const filePath = path.join(__dirname, "fixtures/config.json")
const content = yield* _(fs.readFileString(filePath), Effect.map(JSON.parse))
const args = Args.fileParse({ name: "files" }).pipe(Args.repeated)
const result = yield* _(
Args.validate(args, Array.of(filePath), CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), Array.of(content)])
}).pipe(runEffect))
it("fileSchema", () =>
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const filePath = path.join(__dirname, "fixtures/config.json")
const content = yield* _(fs.readFileString(filePath), Effect.map(JSON.parse))
const args = Args.fileSchema(
Schema.Struct({
foo: Schema.Boolean,
bar: Schema.Literal("baz")
}),
{ name: "files" }
).pipe(Args.repeated)
const result = yield* _(
Args.validate(args, Array.of(filePath), CliConfig.defaultConfig)
)
expect(result).toEqual([Array.empty(), Array.of(content)])
}).pipe(runEffect))
})