-
Notifications
You must be signed in to change notification settings - Fork 524
refactor(cli): cover functions delete, list, new with effect lint (CLI-2459)
#6693
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
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
120 changes: 69 additions & 51 deletions
120
apps/cli/src/commands/functions/delete/delete.live.test.ts
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 |
|---|---|---|
| @@ -1,58 +1,76 @@ | ||
| import { randomUUID } from "node:crypto"; | ||
| import { mkdir, writeFile } from "node:fs/promises"; | ||
|
|
||
| import { BunServices } from "@effect/platform-bun"; | ||
| import { Cause, Data, Effect, Exit, FileSystem, Path } from "effect"; | ||
| import { expect } from "vitest"; | ||
|
|
||
| import { test, throwWithCleanup } from "../../../../tests/helpers/live.ts"; | ||
|
|
||
| async function cleanupFunction( | ||
| cli: (args: string[]) => Promise<{ exitCode: number; stdout: string; stderr: string }>, | ||
| slug: string, | ||
| ref: string, | ||
| ): Promise<void> { | ||
| const deleted = await cli(["functions", "delete", slug, "--project-ref", ref]); | ||
| if ( | ||
| deleted.exitCode !== 0 && | ||
| !/not found|does not exist/i.test(`${deleted.stdout}\n${deleted.stderr}`) | ||
| ) { | ||
| throw new Error(`functions delete cleanup failed:\n${deleted.stdout}\n${deleted.stderr}`); | ||
| } | ||
| import { type LiveFixtures, test, throwWithCleanup } from "../../../../tests/helpers/live.ts"; | ||
|
|
||
| type LiveCliEffect = LiveFixtures["cliEffect"]; | ||
|
|
||
| class FunctionsDeleteLiveError extends Data.TaggedError("FunctionsDeleteLiveError")<{ | ||
| readonly message: string; | ||
| }> {} | ||
|
|
||
| function cleanupFunction(cliEffect: LiveCliEffect, slug: string, ref: string) { | ||
| return Effect.gen(function* () { | ||
| const deleted = yield* cliEffect(["functions", "delete", slug, "--project-ref", ref]); | ||
| if ( | ||
| deleted.exitCode !== 0 && | ||
| !/not found|does not exist/i.test(`${deleted.stdout}\n${deleted.stderr}`) | ||
| ) { | ||
| return yield* new FunctionsDeleteLiveError({ | ||
| message: `functions delete cleanup failed:\n${deleted.stdout}\n${deleted.stderr}`, | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| test("deletes a deployed function", async ({ cli, project, workspace }) => { | ||
| const slug = `cli-e2e-delete-${randomUUID().slice(0, 8)}`; | ||
| const directory = `${workspace.path}/supabase/functions/${slug}`; | ||
| await mkdir(directory, { recursive: true }); | ||
| await writeFile(`${directory}/index.ts`, "Deno.serve(() => Response.json({ ok: true }));\n"); | ||
| await writeFile(`${directory}/deno.json`, '{\n "imports": {}\n}\n'); | ||
|
|
||
| let targetError: unknown; | ||
| let cleanupError: unknown; | ||
| try { | ||
| const deployed = await cli([ | ||
| "functions", | ||
| "deploy", | ||
| slug, | ||
| "--project-ref", | ||
| project.ref, | ||
| "--use-api", | ||
| ]); | ||
| if (deployed.exitCode !== 0) { | ||
| throw new Error( | ||
| `functions deploy setup failed (exit ${deployed.exitCode})\nstdout:\n${deployed.stdout}\nstderr:\n${deployed.stderr}`, | ||
| test("deletes a deployed function", ({ cliEffect, project, workspace }) => | ||
| Effect.runPromise( | ||
| Effect.gen(function* () { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| const slug = `cli-e2e-delete-${randomUUID().slice(0, 8)}`; | ||
| const directory = path.join(workspace.path, "supabase", "functions", slug); | ||
| yield* fs.makeDirectory(directory, { recursive: true }); | ||
| yield* fs.writeFileString( | ||
| path.join(directory, "index.ts"), | ||
| "Deno.serve(() => Response.json({ ok: true }));\n", | ||
| ); | ||
| } | ||
| yield* fs.writeFileString(path.join(directory, "deno.json"), '{\n "imports": {}\n}\n'); | ||
|
|
||
| const result = await cli(["functions", "delete", slug, "--project-ref", project.ref]); | ||
| expect(result.exitCode, result.stderr).toBe(0); | ||
| expect(result.stdout).toContain("Deleted Function"); | ||
| } catch (error) { | ||
| targetError = error; | ||
| } finally { | ||
| try { | ||
| await cleanupFunction(cli, slug, project.ref); | ||
| } catch (error) { | ||
| cleanupError = error; | ||
| } | ||
| } | ||
| throwWithCleanup(targetError, cleanupError === undefined ? [] : [cleanupError]); | ||
| }); | ||
| const target = Effect.gen(function* () { | ||
| const deployed = yield* cliEffect([ | ||
| "functions", | ||
| "deploy", | ||
| slug, | ||
| "--project-ref", | ||
| project.ref, | ||
| "--use-api", | ||
| ]); | ||
| if (deployed.exitCode !== 0) { | ||
| return yield* new FunctionsDeleteLiveError({ | ||
| message: `functions deploy setup failed (exit ${deployed.exitCode})\nstdout:\n${deployed.stdout}\nstderr:\n${deployed.stderr}`, | ||
| }); | ||
| } | ||
|
|
||
| const result = yield* cliEffect([ | ||
| "functions", | ||
| "delete", | ||
| slug, | ||
| "--project-ref", | ||
| project.ref, | ||
| ]); | ||
| expect(result.exitCode, result.stderr).toBe(0); | ||
| expect(result.stdout).toContain("Deleted Function"); | ||
| }); | ||
|
|
||
| const targetExit = yield* Effect.exit(target); | ||
| const cleanupExit = yield* Effect.exit(cleanupFunction(cliEffect, slug, project.ref)); | ||
| return { | ||
| targetError: Exit.isFailure(targetExit) ? Cause.squash(targetExit.cause) : undefined, | ||
| cleanupErrors: Exit.isFailure(cleanupExit) ? [Cause.squash(cleanupExit.cause)] : [], | ||
| }; | ||
| }).pipe(Effect.provide(BunServices.layer)), | ||
| ).then(({ targetError, cleanupErrors }) => throwWithCleanup(targetError, cleanupErrors))); | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.