-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7cfd16
commit 9eb6237
Showing
51 changed files
with
705 additions
and
619 deletions.
There are no files selected for viewing
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect, t } from "@engine/utils/testing.ts" | ||
import { command } from "@engine/utils/deno/command.ts" | ||
import { Logger } from "@engine/utils/log.ts" | ||
import { DevNull } from "@engine/utils/log_test.ts" | ||
|
||
const stdio = new DevNull() | ||
const log = new Logger(import.meta, { level: Logger.channels.trace, tags: { foo: "bar" }, stdio }) | ||
|
||
Deno.test(t(import.meta, "`command()` can execute commands"), { permissions: { run: ["deno"] } }, async () => { | ||
await expect(command("deno --version")).to.be.eventually.containSubset({ success: true, code: 0 }) | ||
}) | ||
|
||
Deno.test(t(import.meta, "`command()` returns stdio content instead if asked"), { permissions: { run: ["deno"] } }, async () => { | ||
await expect(command("deno --version", { return: "stdout" })).to.eventually.include("deno") | ||
}) | ||
|
||
Deno.test(t(import.meta, "`command()` returns stdio content instead if asked"), { permissions: { run: ["deno"] } }, async () => { | ||
stdio.flush() | ||
await expect(command("deno --version", { log })).to.be.fulfilled | ||
expect(stdio.messages).to.not.be.empty | ||
}) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** Get environment value */ | ||
function get(key: string, options: { boolean: true }): boolean | ||
function get(key: string, options?: { boolean: false }): string | ||
function get(key: string, { boolean = false } = {}) { | ||
if (boolean) { | ||
const value = env.get(key, { boolean: false }) | ||
if (!value.length) { | ||
return false | ||
} | ||
return !/^(0|[Nn]o?|NO|[Oo]ff|OFF|[Ff]alse|FALSE)$/.test(value) | ||
} | ||
try { | ||
return Deno.env.get(key) ?? "" | ||
} catch { | ||
return "" | ||
} | ||
} | ||
|
||
/** Set environment value */ | ||
function set(key: string, value: string) { | ||
try { | ||
return Deno.env.set(key, value) | ||
} catch { | ||
return | ||
} | ||
} | ||
|
||
/** Environment */ | ||
export const env = { | ||
get, | ||
set, | ||
get deployment() { | ||
return env.get("DENO_DEPLOYMENT_ID", { boolean: true }) | ||
}, | ||
get actions() { | ||
return env.get("GITHUB_ACTIONS", { boolean: true }) | ||
}, | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { expect, nodeno, t } from "@engine/utils/testing.ts" | ||
import { env } from "@engine/utils/deno/env.ts" | ||
|
||
const uuid = crypto.randomUUID().slice(-12).toUpperCase() | ||
const test = { | ||
env: `UNIT_TEST_${uuid}`, | ||
value: uuid, | ||
} | ||
|
||
Deno.test(t(import.meta, "`env.get()` can read a env variable"), { permissions: { env: [test.env, `${test.env}_UNDEFINED`] } }, () => { | ||
env.set(test.env, test.value) | ||
expect(env.get(test.env)).to.equal(test.value) | ||
expect(env.get(`${test.env}_UNDEFINED`)).to.equal("") | ||
}) | ||
|
||
Deno.test(t(import.meta, "`env.get()` returns a env variable as a boolean if asked"), { permissions: { env: [test.env] } }, () => { | ||
for ( | ||
const { value, boolean } of [ | ||
{ value: "1", boolean: true }, | ||
{ value: "true", boolean: true }, | ||
{ value: "0", boolean: false }, | ||
{ value: "false", boolean: false }, | ||
] | ||
) { | ||
env.set(test.env, value) | ||
expect(env.get(test.env, { boolean: true })).to.equal(boolean) | ||
} | ||
}) | ||
|
||
Deno.test( | ||
t(import.meta, "`env.get()` returns empty values in non-deno environments"), | ||
{ permissions: "none" }, | ||
nodeno(() => { | ||
expect(env.get(test.env)).to.equal("") | ||
expect(env.get(test.env, { boolean: true })).to.equal(false) | ||
}, { with: { test } }), | ||
) | ||
|
||
Deno.test(t(import.meta, "`env.set()` can registers a env variable"), { permissions: { env: [test.env] } }, () => { | ||
expect(env.set(test.env, test.value)) | ||
expect(env.get(test.env)).to.equal(test.value) | ||
expect(env.set(`${test.env}_FORBIDDEN`, test.value)) | ||
expect(env.get(`${test.env}_FORBIDDEN`)).to.equal("") | ||
}) | ||
|
||
Deno.test( | ||
t(import.meta, "`env.set()` is a noop in non-deno environments"), | ||
{ permissions: "none" }, | ||
nodeno(() => { | ||
expect(env.set(test.env, test.value)) | ||
expect(env.get(test.env)).to.equal("") | ||
}, { with: { test } }), | ||
) | ||
|
||
Deno.test(t(import.meta, "`env.deployment` is a boolean"), { permissions: { env: [test.env, `${test.env}_UNDEFINED`] } }, () => { | ||
expect(env.deployment).to.be.a("boolean") | ||
}) | ||
|
||
Deno.test(t(import.meta, "`env.actions` is a boolean"), { permissions: { env: [test.env, `${test.env}_UNDEFINED`] } }, () => { | ||
expect(env.actions).to.be.a("boolean") | ||
}) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//Imports | ||
import { ensureDir } from "std/fs/ensure_dir.ts" | ||
import { expandGlob } from "std/fs/expand_glob.ts" | ||
import { throws } from "@engine/utils/errors.ts" | ||
import { dirname } from "std/path/dirname.ts" | ||
import { toFileUrl } from "std/path/to_file_url.ts" | ||
import { resolve } from "std/path/resolve.ts" | ||
import * as dir from "@engine/paths.ts" | ||
|
||
/** Read file */ | ||
export function read(path: string | URL, options: { sync: true }): string | ||
export function read(path: string | URL, options?: { sync?: false }): Promise<string> | ||
export function read(path: string | URL, { sync = false } = {}) { | ||
if ((typeof path === "string") && (path.startsWith("metrics://"))) { | ||
path = path.replace("metrics:/", dir.source) | ||
} else if ((path instanceof URL) && (path.protocol === "metrics:")) { | ||
path = path.href.replace("metrics:/", dir.source) | ||
} | ||
if (!globalThis.Deno) { | ||
if (sync) { | ||
throws("Unsupported action: synchronous read") | ||
} | ||
return fetch(toFileUrl(resolve(path as string))).then((response) => response.text()) | ||
} | ||
if ((typeof path === "string") && (path.startsWith("data:"))) { | ||
if (sync) { | ||
throws("Unsupported action: synchronous read") | ||
} | ||
return fetch(path).then((response) => response.text()) | ||
} | ||
return sync ? Deno.readTextFileSync(path) : Deno.readTextFile(path) | ||
} | ||
|
||
/** Write file */ | ||
export async function write(path: string, data: string | Uint8Array | ReadableStream<Uint8Array>) { | ||
if (!globalThis.Deno) { | ||
return | ||
} | ||
await ensureDir(dirname(path)) | ||
if (typeof data === "string") { | ||
return Deno.writeTextFile(path, data) | ||
} | ||
return Deno.writeFile(path, data) | ||
} | ||
|
||
/** List files in globpath */ | ||
export async function list(glob: string) { | ||
if (!globalThis.Deno) { | ||
throws("Unsupported action: list") | ||
} | ||
const files = [] | ||
const base = glob.match(/(?<base>.*\/)\*/)?.groups?.base | ||
const prefix = base ? new RegExp(`.*?${base}`) : null | ||
for await (const { path } of expandGlob(glob, { extended: true, globstar: true })) { | ||
let file = path.replaceAll("\\", "/") | ||
if (prefix) { | ||
file = file.replace(prefix, "") | ||
} | ||
files.push(file) | ||
} | ||
return files | ||
} |
Oops, something went wrong.