Extract from devtools project

This commit is contained in:
Michaël Lemaire 2021-09-05 22:16:35 +02:00
commit f267b4c65a
12 changed files with 97 additions and 0 deletions

9
.editorconfig Normal file
View file

@ -0,0 +1,9 @@
root = true
[*.{ts,json}]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
deno.d.ts
.vscode
.local

11
README.md Normal file
View file

@ -0,0 +1,11 @@
# typescript/system
[![Build Status](https://thunderk.visualstudio.com/typescript/_apis/build/status/system?branchName=master)](https://dev.azure.com/thunderk/typescript/_build?pipelineNameFilter=system)
## About
Access to system ressources (files, network...).
The Sys object exposed is compatible with the
[Deno](https://doc.deno.land/builtin/stable) object, but its functions can be
replaced by mocks.

6
deps.testing.ts Normal file
View file

@ -0,0 +1,6 @@
export {
describe,
expect,
it,
mock,
} from "https://js.thunderk.net/testing@1.0.0/mod.ts";

0
deps.ts Normal file
View file

7
doc/about.md Normal file
View file

@ -0,0 +1,7 @@
## About
Access to system ressources (files, network...).
The Sys object exposed is compatible with the
[Deno](https://doc.deno.land/builtin/stable) object, but its functions can be
replaced by mocks.

1
doc/index Normal file
View file

@ -0,0 +1 @@
about

1
mod.ts Normal file
View file

@ -0,0 +1 @@
export { Sys } from "./src/deno.ts";

19
run Executable file
View file

@ -0,0 +1,19 @@
#!/bin/sh
# Simplified run tool for deno commands
if test $# -eq 0
then
echo "Usage: $0 [file or command]"
exit 1
elif echo $1 | grep -q '.*.ts'
then
denocmd=run
denoargs=$1
shift
else
denocmd=$1
shift
fi
denoargs="$(cat config/$denocmd.flags 2> /dev/null) $denoargs $@"
exec deno $denocmd $denoargs

20
src/deno.test.ts Normal file
View file

@ -0,0 +1,20 @@
import { Sys } from "./deno.ts";
import { describe, expect, it, mock } from "../deps.testing.ts";
describe("Sys", () => {
it("is mockable", () => {
function runSomething() {
Sys.run({ cmd: ["echo", "test"] });
}
expect(runSomething).toThrow(
'Requires run access to "echo", run again with the --allow-run flag',
);
mock(Sys, "run", undefined, (run) => {
runSomething();
expect(run).toHaveBeenCalledTimes(1);
expect(run).toHaveBeenCalledWith({ cmd: ["echo", "test"] });
});
});
});

10
src/deno.ts Normal file
View file

@ -0,0 +1,10 @@
// Mockable Deno proxy
export const Sys: typeof Deno = new Proxy({} as typeof Deno, {
get(obj: any, prop: keyof typeof Deno): any {
return obj.hasOwnProperty(prop) ? obj[prop] : Deno[prop];
},
set: function (obj: any, prop: keyof typeof Deno, value: any) {
obj[prop] = value;
return true;
},
});

10
tsconfig.json Normal file
View file

@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "esnext",
"target": "ESNext",
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"preserveConstEnums": true
}
}