Skip to content

Commit cfb5dc6

Browse files
committed
test(app): extract parser helpers to satisfy max-lines
1 parent 54efd57 commit cfb5dc6

File tree

2 files changed

+86
-75
lines changed

2 files changed

+86
-75
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { expect } from "@effect/vitest"
2+
import { Effect, Either } from "effect"
3+
4+
import type { Command } from "@effect-template/lib/core/domain"
5+
import { parseArgs } from "../../src/docker-git/cli/parser.js"
6+
7+
export type CreateCommand = Extract<Command, { _tag: "Create" }>
8+
type ProjectDirRunUpCommand = Extract<Command, { readonly projectDir: string; readonly runUp: boolean }>
9+
10+
export const expectParseErrorTag = (
11+
args: ReadonlyArray<string>,
12+
expectedTag: string
13+
) =>
14+
Effect.sync(() => {
15+
const parsed = parseArgs(args)
16+
Either.match(parsed, {
17+
onLeft: (error) => {
18+
expect(error._tag).toBe(expectedTag)
19+
},
20+
onRight: () => {
21+
throw new Error("expected parse error")
22+
}
23+
})
24+
})
25+
26+
export const parseOrThrow = (args: ReadonlyArray<string>): Command => {
27+
const parsed = parseArgs(args)
28+
return Either.match(parsed, {
29+
onLeft: (error) => {
30+
throw new Error(`unexpected error ${error._tag}`)
31+
},
32+
onRight: (command) => command
33+
})
34+
}
35+
36+
export const expectProjectDirRunUpCommand = (
37+
args: ReadonlyArray<string>,
38+
expectedTag: ProjectDirRunUpCommand["_tag"],
39+
expectedProjectDir: string,
40+
expectedRunUp: boolean
41+
) =>
42+
Effect.sync(() => {
43+
const command = parseOrThrow(args)
44+
if (command._tag !== expectedTag) {
45+
throw new Error(`expected ${expectedTag} command`)
46+
}
47+
if (!("projectDir" in command) || !("runUp" in command)) {
48+
throw new Error("expected command with projectDir and runUp")
49+
}
50+
expect(command.projectDir).toBe(expectedProjectDir)
51+
expect(command.runUp).toBe(expectedRunUp)
52+
})
53+
54+
export const expectAttachProjectDirCommand = (
55+
args: ReadonlyArray<string>,
56+
expectedProjectDir: string
57+
) =>
58+
Effect.sync(() => {
59+
const command = parseOrThrow(args)
60+
if (command._tag !== "Attach") {
61+
throw new Error("expected Attach command")
62+
}
63+
expect(command.projectDir).toBe(expectedProjectDir)
64+
})
65+
66+
export const expectCreateCommand = (
67+
args: ReadonlyArray<string>,
68+
onRight: (command: CreateCommand) => void
69+
) =>
70+
Effect.sync(() => {
71+
const command = parseOrThrow(args)
72+
if (command._tag !== "Create") {
73+
throw new Error("expected Create command")
74+
}
75+
onRight(command)
76+
})

packages/app/tests/docker-git/parser.test.ts

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,16 @@
11
import { describe, expect, it } from "@effect/vitest"
2-
import { Effect, Either } from "effect"
2+
import { Effect } from "effect"
33

4-
import { type Command, defaultTemplateConfig } from "@effect-template/lib/core/domain"
4+
import { defaultTemplateConfig } from "@effect-template/lib/core/domain"
55
import { expandContainerHome } from "@effect-template/lib/usecases/scrap-path"
6-
import { parseArgs } from "../../src/docker-git/cli/parser.js"
7-
8-
type CreateCommand = Extract<Command, { _tag: "Create" }>
9-
10-
const expectParseErrorTag = (
11-
args: ReadonlyArray<string>,
12-
expectedTag: string
13-
) =>
14-
Effect.sync(() => {
15-
const parsed = parseArgs(args)
16-
Either.match(parsed, {
17-
onLeft: (error) => {
18-
expect(error._tag).toBe(expectedTag)
19-
},
20-
onRight: () => {
21-
throw new Error("expected parse error")
22-
}
23-
})
24-
})
25-
26-
const parseOrThrow = (args: ReadonlyArray<string>): Command => {
27-
const parsed = parseArgs(args)
28-
return Either.match(parsed, {
29-
onLeft: (error) => {
30-
throw new Error(`unexpected error ${error._tag}`)
31-
},
32-
onRight: (command) => command
33-
})
34-
}
35-
36-
type ProjectDirRunUpCommand = Extract<Command, { readonly projectDir: string; readonly runUp: boolean }>
37-
38-
const expectProjectDirRunUpCommand = (
39-
args: ReadonlyArray<string>,
40-
expectedTag: ProjectDirRunUpCommand["_tag"],
41-
expectedProjectDir: string,
42-
expectedRunUp: boolean
43-
) =>
44-
Effect.sync(() => {
45-
const command = parseOrThrow(args)
46-
if (command._tag !== expectedTag) {
47-
throw new Error(`expected ${expectedTag} command`)
48-
}
49-
if (!("projectDir" in command) || !("runUp" in command)) {
50-
throw new Error("expected command with projectDir and runUp")
51-
}
52-
expect(command.projectDir).toBe(expectedProjectDir)
53-
expect(command.runUp).toBe(expectedRunUp)
54-
})
55-
56-
const expectAttachProjectDirCommand = (
57-
args: ReadonlyArray<string>,
58-
expectedProjectDir: string
59-
) =>
60-
Effect.sync(() => {
61-
const command = parseOrThrow(args)
62-
if (command._tag !== "Attach") {
63-
throw new Error("expected Attach command")
64-
}
65-
expect(command.projectDir).toBe(expectedProjectDir)
66-
})
67-
68-
const expectCreateCommand = (
69-
args: ReadonlyArray<string>,
70-
onRight: (command: CreateCommand) => void
71-
) =>
72-
Effect.sync(() => {
73-
const command = parseOrThrow(args)
74-
if (command._tag !== "Create") {
75-
throw new Error("expected Create command")
76-
}
77-
onRight(command)
78-
})
6+
import {
7+
type CreateCommand,
8+
expectAttachProjectDirCommand,
9+
expectCreateCommand,
10+
expectParseErrorTag,
11+
expectProjectDirRunUpCommand,
12+
parseOrThrow
13+
} from "./parser-helpers.js"
7914

8015
const expectCreateDefaults = (command: CreateCommand) => {
8116
expect(command.config.repoUrl).toBe("https://github.com/org/repo.git")

0 commit comments

Comments
 (0)