-
Notifications
You must be signed in to change notification settings - Fork 0
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
tests done #237
tests done #237
Changes from 4 commits
7d35c64
917330a
c786d14
3d04c47
31972ff
4f1d36b
5ea22cd
2d6684c
bc64f10
07f5fd5
dbe6df3
6a94dd5
10d5b1d
fd152be
ab56064
e1e45ef
f74d9b0
2f742f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { readFile } from "../configReader"; | ||
import axios from "axios"; | ||
|
||
export const buildWodinStaticSite = async (configPath: string, destPath: string) => { | ||
const storesPath = path.resolve(configPath, "stores") | ||
const stores = fs.readdirSync(storesPath, { recursive: false }) as string[]; | ||
|
||
// bootstrapping and making sure the folder structure is correct | ||
if (!fs.existsSync(destPath)) { | ||
fs.mkdirSync(destPath); | ||
} | ||
const destStoresPath = path.resolve(destPath, "stores"); | ||
if (fs.existsSync(destStoresPath)) { | ||
fs.rmSync(destStoresPath, { recursive: true }); | ||
} | ||
fs.mkdirSync(destStoresPath); | ||
|
||
// copy in their config specific files (assets to come soon) | ||
fs.cpSync(path.resolve(configPath, "index.html"), path.resolve(destPath, "index.html")); | ||
|
||
// get runners | ||
const runnerOdeResponse = await axios.get("http://localhost:8001/support/runner-ode"); | ||
fs.writeFileSync(path.resolve(destStoresPath, "runnerOde.js"), runnerOdeResponse.data.data); | ||
const runnerDiscreteResponse = await axios.get("http://localhost:8001/support/runner-discrete"); | ||
fs.writeFileSync(path.resolve(destStoresPath, "runnerDiscrete.js"), runnerDiscreteResponse.data.data); | ||
|
||
// make folder per store with config.json (their config + default code) and | ||
// model.json (model response from odin.api) | ||
stores.forEach(async store => { | ||
const destStorePath = path.resolve(destStoresPath, store); | ||
fs.mkdirSync(destStorePath); | ||
const model = readFile(path.resolve(storesPath, store, "model.R")).split("\n"); | ||
|
||
const config = JSON.parse(readFile(path.resolve(storesPath, store, "config.json"))) as { appType: string }; | ||
fs.writeFileSync(path.resolve(destStorePath, "config.json"), JSON.stringify({ ...config, defaultCode: model })); | ||
|
||
const timeType = config.appType === "stochastic" ? "discrete" : "continuous"; | ||
const modelResponse = await axios.post("http://localhost:8001/compile", { model, requirements: { timeType } }); | ||
fs.writeFileSync(path.resolve(destStorePath, `model.json`), JSON.stringify(modelResponse.data.data)); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,6 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { readFile } from "../configReader"; | ||
import axios from "axios"; | ||
import { processArgs } from "./args"; | ||
|
||
const mkdirForce = (path: string) => { | ||
if (!fs.existsSync(path)) { | ||
fs.mkdirSync(path); | ||
} | ||
}; | ||
import { buildWodinStaticSite } from "./builder"; | ||
|
||
const { configPath, destPath } = processArgs(); | ||
|
||
const storesPath = path.resolve(configPath, "stores") | ||
const stores = fs.readdirSync(storesPath, { recursive: false }) as string[]; | ||
|
||
mkdirForce(destPath); | ||
const destStoresPath = path.resolve(destPath, "stores"); | ||
if (fs.existsSync(destStoresPath)) { | ||
fs.rmSync(destStoresPath, { recursive: true }); | ||
} | ||
fs.mkdirSync(destStoresPath); | ||
|
||
fs.cpSync(path.resolve(configPath, "index.html"), path.resolve(destPath, "index.html")); | ||
|
||
const getRunners = async () => { | ||
const runnerOdeResponse = await axios.get("http://localhost:8001/support/runner-ode"); | ||
fs.writeFileSync(path.resolve(destStoresPath, "runnerOde.js"), runnerOdeResponse.data.data); | ||
|
||
const runnerDiscreteResponse = await axios.get("http://localhost:8001/support/runner-discrete"); | ||
fs.writeFileSync(path.resolve(destStoresPath, "runnerDiscrete.js"), runnerDiscreteResponse.data.data); | ||
} | ||
getRunners(); | ||
|
||
stores.forEach(async store => { | ||
const destStorePath = path.resolve(destStoresPath, store); | ||
fs.mkdirSync(destStorePath); | ||
|
||
const model = readFile(path.resolve(storesPath, store, "model.R")).split("\n"); | ||
|
||
const config = JSON.parse(readFile(path.resolve(storesPath, store, "config.json"))) as { appType: string }; | ||
fs.writeFileSync(path.resolve(destStorePath, "config.json"), JSON.stringify({ ...config, defaultCode: model })); | ||
|
||
const modelResponse = await axios.post("http://localhost:8001/compile", { | ||
model, | ||
requirements: { timeType: config.appType === "stochastic" ? "discrete" : "continuous" } | ||
}); | ||
fs.writeFileSync(path.resolve(destStorePath, `model.json`), JSON.stringify(modelResponse.data.data)); | ||
}); | ||
buildWodinStaticSite(configPath, destPath); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { processArgs } from "../../src/static-site-builder/args"; | ||
|
||
describe("args", () => { | ||
const realArgs = process.argv; | ||
afterEach(() => { | ||
process.argv = realArgs; | ||
}); | ||
|
||
const builderPath = "/builder", | ||
cfgPath = "/configPath", | ||
destPath = "/destPath"; | ||
|
||
it("throws error if no config path or dest path arg", () => { | ||
const argv = ["node", builderPath]; | ||
expect(() => { processArgs(argv); }) | ||
.toThrow("Usage:"); | ||
|
||
const argv1 = ["node", builderPath, cfgPath]; | ||
expect(() => { processArgs(argv1); }) | ||
.toThrow("Usage:"); | ||
}); | ||
|
||
it("returns config path and dest path", () => { | ||
const argv = ["node", builderPath, cfgPath, destPath]; | ||
const args = processArgs(argv); | ||
expect(args.configPath).toBe(cfgPath); | ||
expect(args.destPath).toBe(destPath); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { tmpdirTest } from "./tempDirTestHelper" | ||
import { buildWodinStaticSite } from "../../src/static-site-builder/builder" | ||
|
||
const buildSite = async (tmpdir: string) => { | ||
await buildWodinStaticSite("../../config-static", tmpdir); | ||
} | ||
|
||
const { | ||
mockRunnerOde, mockRunnerDiscrete, | ||
mockModel, mockGet, mockPost | ||
} = vi.hoisted(() => { | ||
const mockRunnerOde = "var odinRunnerOde;"; | ||
const mockRunnerDiscrete = "var odinRunnerDiscrete;"; | ||
const mockModel = "class odinModel {};"; | ||
const mockGet = vi.fn().mockImplementation((url: string) => { | ||
if (url === "http://localhost:8001/support/runner-ode") { | ||
return { data: { data: mockRunnerOde } }; | ||
} else if (url === "http://localhost:8001/support/runner-discrete") { | ||
return { data: { data: mockRunnerDiscrete } }; | ||
} | ||
}); | ||
const mockPost = vi.fn().mockImplementation((url: string) => { | ||
if (url === "http://localhost:8001/compile") { | ||
return { data: { data: mockModel } }; | ||
} | ||
}); | ||
return { | ||
mockRunnerOde, mockRunnerDiscrete, | ||
mockModel, mockGet, mockPost | ||
}; | ||
}); | ||
|
||
vi.mock("axios", () => { | ||
return { | ||
default: { | ||
get: mockGet, | ||
post: mockPost | ||
} | ||
} | ||
}); | ||
|
||
const p = path.resolve; | ||
|
||
const expectPath = (...paths: string[]) => { | ||
expect(fs.existsSync(p(...paths))).toBe(true); | ||
}; | ||
|
||
const expectPathContent = (content: string, ...paths: string[]) => { | ||
expect(fs.readFileSync(p(...paths)).toString()).toBe(content); | ||
} | ||
|
||
const expectPathContentContains = (content: string, ...paths: string[]) => { | ||
expect(fs.readFileSync(p(...paths)).toString()).toContain(content); | ||
} | ||
|
||
describe("Wodin builder", () => { | ||
tmpdirTest("creates dest dir if it doesn't exist", async ({ tmpdir }) => { | ||
fs.rmdirSync(tmpdir); | ||
await buildSite(tmpdir); | ||
expectPath(tmpdir); | ||
}); | ||
|
||
tmpdirTest("works as expected", async ({ tmpdir }) => { | ||
const storesPath = p(tmpdir, "stores"); | ||
await buildSite(tmpdir); | ||
|
||
expectPath(tmpdir, "index.html"); | ||
|
||
expectPath(storesPath, "runnerOde.js"); | ||
expectPathContent(mockRunnerOde, storesPath, "runnerOde.js"); | ||
expectPath(storesPath, "runnerDiscrete.js"); | ||
expectPathContent(mockRunnerDiscrete, storesPath, "runnerDiscrete.js"); | ||
|
||
expectPath(storesPath, "basic", "model.json"); | ||
expectPathContentContains(mockModel, storesPath, "basic", "model.json"); | ||
expectPath(storesPath, "basic", "config.json"); | ||
}); | ||
|
||
tmpdirTest("overwrites existing stores folder", async ({ tmpdir }) => { | ||
const storesPath = p(tmpdir, "stores"); | ||
fs.mkdirSync(storesPath); | ||
fs.writeFileSync(p(storesPath, "trash.txt"), "whats up"); | ||
|
||
await buildSite(tmpdir); | ||
|
||
expect(fs.existsSync(p(storesPath, "trash.txt"))).toBe(false) | ||
}); | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { test } from "vitest"; | ||
import os from "node:os"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
|
||
type TmpDirTestFixture = { tmpdir: string; } | ||
|
||
const createTempDir = () => { | ||
const ostmpdir = os.tmpdir(); | ||
const tmpdir = path.resolve(ostmpdir, "static-site-builder-tests-"); | ||
return fs.mkdtempSync(tmpdir); | ||
}; | ||
|
||
export const tmpdirTest = test.extend<TmpDirTestFixture>({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is neat! I've just been using memfs to fake the fs, but this is actually nicer because it cleans up after itself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep its quite cool! want another excuse to use stuff like this more |
||
// eslint-disable-next-line no-empty-pattern | ||
tmpdir: async ({}, use) => { | ||
const dir = createTempDir(); | ||
await use(dir); | ||
|
||
fs.rmSync(dir, { recursive: true }) | ||
} | ||
}); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, classic test name 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yess, i shall try and improve it!