Skip to content

Commit

Permalink
chore: test templating e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Mar 7, 2024
1 parent 9e27e0f commit aada0b9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions deno.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"packages": {
"specifiers": {
"npm:@rivet-gg/esbuild-plugin-polyfill-node@^0.4.0": "npm:@rivet-gg/esbuild-plugin-polyfill-node@0.4.0_esbuild@0.20.0",
"npm:@types/node": "npm:@types/node@18.16.19",
"npm:ajv-formats@^2.1.1": "npm:ajv-formats@2.1.1_ajv@8.12.0",
"npm:ajv@^8.12.0": "npm:ajv@8.12.0",
"npm:dedent@^1.5.1": "npm:dedent@1.5.1",
Expand Down
9 changes: 8 additions & 1 deletion src/build/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { shutdownAllPools } from "../utils/worker_pool.ts";
import { migrateDev } from "../migrate/dev.ts";
import { compileModuleTypeHelper } from "./gen.ts";
import { migrateDeploy } from "../migrate/deploy.ts";
import { ensurePostgresRunning } from "../utils/postgres_daemon.ts";

/**
* Which format to use for building.
Expand Down Expand Up @@ -187,6 +188,9 @@ async function waitForBuildPromises(buildState: BuildState): Promise<void> {
export async function build(project: Project, opts: BuildOpts) {
const buildCachePath = resolve(project.path, "_gen", "cache.json");

// Required for `migrateDev` and `migrateDeploy`
await ensurePostgresRunning(project);

// Read hashes from file
let oldCache: BuildCachePersist;
if (await exists(buildCachePath)) {
Expand Down Expand Up @@ -242,7 +246,7 @@ async function buildSteps(
for (const module of project.modules.values()) {
if (module.db) {
buildStep(buildState, {
name: `Migrate (${module.name})`,
name: `Migrate`,
module,
files: [resolve(module.path, "db", "schema.prisma")],
async build() {
Expand All @@ -255,6 +259,9 @@ async function buildSteps(
}
},
});

// Run one migration at a time since Prisma is interactive
await waitForBuildPromises(buildState);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/template/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import dedent from "npm:dedent@^1.5.1";
export { dedent };
40 changes: 31 additions & 9 deletions src/template/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { resolve } from "../deps.ts";
import { ModuleConfig } from "../config/module.ts";
import { resolve, stringify } from "../deps.ts";
import { Project } from "../project/mod.ts";
import { dedent } from "./deps.ts";

export async function templateModule(project: Project, moduleName: string) {
if (project.modules.has(moduleName)) {
Expand All @@ -12,16 +14,36 @@ export async function templateModule(project: Project, moduleName: string) {
await Deno.mkdir(resolve(modulePath, "scripts"));
await Deno.mkdir(resolve(modulePath, "tests"));
await Deno.mkdir(resolve(modulePath, "db"));
await Deno.mkdir(resolve(modulePath, "db", "migrations"));
await Deno.mkdir(resolve(modulePath, "schema"));

// Write default config
const moduleYaml = `scripts: {}
errors: {}
`;
await Deno.writeTextFile(resolve(modulePath, "module.yaml"), moduleYaml);
const defaultModule: ModuleConfig = {
scripts: {},
errors: {},
};
await Deno.writeTextFile(
resolve(modulePath, "module.yaml"),
stringify(defaultModule),
);

// TODO: Write default schema
// Write default migration
const defaultSchema = dedent`
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
`;
await Deno.writeTextFile(
resolve(modulePath, "db", "schema.prisma"),
defaultSchema,
);

// TODO: Add to backend.yaml
// Add to backend.yaml
const newConfig = structuredClone(project.config);
newConfig.modules[moduleName] = {
registry: "local",
};
await Deno.writeTextFile(
resolve(project.path, "backend.yaml"),
stringify(newConfig),
);
}
22 changes: 22 additions & 0 deletions src/template/template_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DbDriver } from "../build/mod.ts";
import { build, Format, Runtime } from "../build/mod.ts";
import { loadProject } from "../project/mod.ts";
import { templateModule } from "./module.ts";
import { templateProject } from "./project.ts";
import { templateScript } from "./script.ts";

Deno.test("e2e", async () => {
const path = await Deno.makeTempDir();

await templateProject(path);

await templateModule(await loadProject({ path }), "module_a");

await templateScript(await loadProject({ path }), "module_a", "script_a");

await build(await loadProject({ path }), {
format: Format.Native,
runtime: Runtime.Deno,
dbDriver: DbDriver.NodePostgres,
});
});

0 comments on commit aada0b9

Please sign in to comment.