mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-04 03:35:36 +08:00
test(graphql-generator): cover config failures and deletion safety
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices"
|
||||
import { assert, describe, it } from "@effect/vitest"
|
||||
import * as Effect from "effect/Effect"
|
||||
import * as FileSystem from "effect/FileSystem"
|
||||
import * as Path from "effect/Path"
|
||||
import {
|
||||
assertDeclaredInOrder,
|
||||
assertNoErrors,
|
||||
@@ -14,6 +16,7 @@ import {
|
||||
documentOf,
|
||||
fileLines,
|
||||
generateGitHub,
|
||||
generateIn,
|
||||
generateTasks,
|
||||
member,
|
||||
members,
|
||||
@@ -82,6 +85,40 @@ const generate = generateTasks({ "src/fragments.graphql": fragments, "src/tasks.
|
||||
const groupDeclaration = /^export const \w+Group\b/
|
||||
|
||||
describe("Generator per-file output", () => {
|
||||
it.effect("error diagnostics suppress deletes even when a headered orphan exists", () =>
|
||||
Effect.gen(function*() {
|
||||
const generated = yield* generateIn({
|
||||
"schema.graphql": "type Query { hello: String! }",
|
||||
"src/bad.graphql": "query Bad { missing }",
|
||||
"src/orphan.graphql.ts": "// Generated by @effect/graphql-generator from orphan.graphql. Do not edit.\n"
|
||||
}, { schema: "./schema.graphql", documents: ["src/**/*.graphql"] })
|
||||
assert.isTrue(generated.result.diagnostics.some((diagnostic) => diagnostic.severity === "error"))
|
||||
assert.deepStrictEqual(generated.result.deletes, [])
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)))
|
||||
|
||||
it.effect("deletes lists only headered orphans, not handwritten files, live outputs or the shared module", () =>
|
||||
Effect.gen(function*() {
|
||||
const orphan =
|
||||
"// Generated by @effect/graphql-generator from orphan.graphql. Do not edit.\nexport const Orphan = 1\n"
|
||||
const generated = yield* generateIn({
|
||||
"schema.graphql": "type Query { hello: String! }",
|
||||
"src/live.graphql": "query Live { hello }",
|
||||
"src/live.graphql.ts": "// Generated by @effect/graphql-generator from live.graphql. Do not edit.\n",
|
||||
"src/orphan.graphql.ts": orphan,
|
||||
"src/handwritten.graphql.ts": "export const Handwritten = true\n",
|
||||
"src/shared.graphql.ts": "// Generated by @effect/graphql-generator from schema.graphql. Do not edit.\n"
|
||||
}, {
|
||||
schema: "./schema.graphql",
|
||||
documents: ["src/**/*.graphql"],
|
||||
shared: "./src/shared.graphql.ts"
|
||||
})
|
||||
assertNoErrors(generated)
|
||||
const path = yield* Path.Path
|
||||
assert.deepStrictEqual(generated.result.deletes.map((file) => path.basename(file)), ["orphan.graphql.ts"])
|
||||
const fs = yield* FileSystem.FileSystem
|
||||
assert.strictEqual(yield* fs.readFileString(generated.result.deletes[0]!), orphan)
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)))
|
||||
|
||||
it.effect("each .graphql file generates a sibling .graphql.ts", () =>
|
||||
Effect.gen(function*() {
|
||||
const { generated } = yield* generate
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
*
|
||||
* Each test seeds a scratch project under `test/.tmp` (inside the package, so
|
||||
* a config file can import `@effect/graphql-generator/Config`) and runs the
|
||||
* command in process with `--config`. One test spawns `bin.ts` with no flags
|
||||
* so the config's `import()` runs under the real Node version.
|
||||
* command in process with `--config`. Child-process tests spawn `bin.ts`
|
||||
* so config loading and type-stripping failures run under the real Node version.
|
||||
*/
|
||||
import * as Generator from "@effect/graphql-generator/Generator"
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices"
|
||||
@@ -54,6 +54,15 @@ const header = (sourceName: string): string =>
|
||||
const tmpRoot = fileURLToPath(new URL("./.tmp", import.meta.url))
|
||||
const binPath = fileURLToPath(new URL("../src/bin.ts", import.meta.url))
|
||||
|
||||
const runBin = Effect.fnUntraced(function*(cwd: string, args: ReadonlyArray<string> = []) {
|
||||
const handle = yield* ChildProcess.make("node", [binPath, ...args], { cwd })
|
||||
return yield* Effect.all({
|
||||
exitCode: handle.exitCode,
|
||||
stdout: Stream.mkString(Stream.decodeText(handle.stdout)),
|
||||
stderr: Stream.mkString(Stream.decodeText(handle.stderr))
|
||||
}, { concurrency: "unbounded" })
|
||||
})
|
||||
|
||||
/** Writes `files` (relative path to contents) to a fresh scratch directory, removed with the scope. */
|
||||
const project = Effect.fnUntraced(function*(files: Readonly<Record<string, string>>) {
|
||||
const fs = yield* FileSystem.FileSystem
|
||||
@@ -132,6 +141,65 @@ const mtime = Effect.fnUntraced(function*(file: string) {
|
||||
})
|
||||
|
||||
describe("graphqlgen CLI", () => {
|
||||
it.effect("a missing schema is a generator filesystem failure and exits 1", () =>
|
||||
Effect.gen(function*() {
|
||||
const p = yield* baseProject(
|
||||
{ "src/a.graphql": "query A { viewer { id } }" },
|
||||
{ ...config, schema: "./missing.graphql" }
|
||||
)
|
||||
|
||||
const result = yield* runCli(["--config", p.configPath])
|
||||
assert.strictEqual(result.exitCode, 1, result.output)
|
||||
assert.include(result.stderr, "missing.graphql")
|
||||
assert.isFalse(yield* exists(p.file("src/a.graphql.ts")))
|
||||
assert.isFalse(yield* exists(p.file("missing.graphql.ts")))
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)))
|
||||
|
||||
it.effect("a scalar mapping for a type absent from the schema exits 2 naming the key", () =>
|
||||
Effect.gen(function*() {
|
||||
const p = yield* baseProject(
|
||||
{ "src/a.graphql": "query A { viewer { id } }" },
|
||||
{ ...config, scalars: { Missing: "./scalars.ts#Missing" } }
|
||||
)
|
||||
|
||||
const result = yield* runCli(["--config", p.configPath])
|
||||
assert.strictEqual(result.exitCode, 2, result.output)
|
||||
assert.include(result.stderr, "scalars.Missing")
|
||||
assert.include(result.stderr, "the schema has no type named \"Missing\"")
|
||||
assert.isFalse(yield* exists(p.file("src/a.graphql.ts")))
|
||||
assert.isFalse(yield* exists(p.file("schema.graphql.ts")))
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)))
|
||||
|
||||
it.effect("a config without a default export exits 2 with export guidance", () =>
|
||||
Effect.gen(function*() {
|
||||
const p = yield* baseProject({
|
||||
"src/a.graphql": "query A { viewer { id } }",
|
||||
"graphql.config.ts": "export const config = {}"
|
||||
})
|
||||
|
||||
const result = yield* runCli(["--config", p.configPath])
|
||||
assert.strictEqual(result.exitCode, 2, result.output)
|
||||
assert.include(result.stderr, "no default export")
|
||||
assert.include(result.stderr, "export default defineConfig")
|
||||
assert.isFalse(yield* exists(p.file("src/a.graphql.ts")))
|
||||
assert.isFalse(yield* exists(p.file("schema.graphql.ts")))
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)))
|
||||
|
||||
it.effect("bin.ts explains why a TypeScript config under node_modules cannot load and exits 2", () =>
|
||||
Effect.gen(function*() {
|
||||
const p = yield* baseProject({
|
||||
"src/a.graphql": "query A { viewer { id } }",
|
||||
"node_modules/settings/graphql.config.ts": configSource(config)
|
||||
})
|
||||
|
||||
const result = yield* runBin(p.dir, ["--config", p.file("node_modules/settings/graphql.config.ts")])
|
||||
assert.strictEqual(result.exitCode, ChildProcessSpawner.ExitCode(2), result.stderr)
|
||||
assert.include(result.stderr, "ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING")
|
||||
assert.include(result.stderr, "Move the config out of node_modules")
|
||||
assert.include(result.stderr, "Bun or Deno")
|
||||
assert.isFalse(yield* exists(p.file("src/a.graphql.ts")))
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)))
|
||||
|
||||
it.effect("a clean run writes the generated files and exits 0; a second run writes nothing", () =>
|
||||
Effect.gen(function*() {
|
||||
const p = yield* baseProject({ "src/a.graphql": "query A {\n viewer {\n id\n name\n }\n}\n" })
|
||||
@@ -289,14 +357,26 @@ describe("graphqlgen CLI", () => {
|
||||
Effect.gen(function*() {
|
||||
const p = yield* baseProject({ "src/a.graphql": "query A {\n viewer {\n id\n }\n}\n" })
|
||||
|
||||
const handle = yield* ChildProcess.make("node", [binPath], { cwd: p.dir })
|
||||
const result = yield* Effect.all({
|
||||
exitCode: handle.exitCode,
|
||||
stdout: Stream.mkString(Stream.decodeText(handle.stdout)),
|
||||
stderr: Stream.mkString(Stream.decodeText(handle.stderr))
|
||||
}, { concurrency: "unbounded" })
|
||||
const result = yield* runBin(p.dir)
|
||||
|
||||
assert.strictEqual(result.exitCode, ChildProcessSpawner.ExitCode(0), result.stderr)
|
||||
assert.include(yield* read(p.file("src/a.graphql.ts")), header("a.graphql"))
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)))
|
||||
|
||||
it.effect("bin.ts explains non-erasable TypeScript config syntax and exits 2", () =>
|
||||
Effect.gen(function*() {
|
||||
const p = yield* baseProject({
|
||||
"src/a.graphql": "query A { viewer { id } }",
|
||||
"graphql.config.ts": `enum Mode { Default }
|
||||
export default ${JSON.stringify(config)}
|
||||
`
|
||||
})
|
||||
|
||||
const result = yield* runBin(p.dir)
|
||||
assert.strictEqual(result.exitCode, ChildProcessSpawner.ExitCode(2), result.stderr)
|
||||
assert.include(result.stderr, "graphql.config.ts")
|
||||
assert.include(result.stderr, "ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX")
|
||||
assert.include(result.stderr, "enums, namespaces or parameter properties")
|
||||
assert.isFalse(yield* exists(p.file("src/a.graphql.ts")))
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user