From f267b4c65ac5fa8f1e96802bda90afbbe4c89e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 5 Sep 2021 22:16:35 +0200 Subject: [PATCH] Extract from devtools project --- .editorconfig | 9 +++++++++ .gitignore | 3 +++ README.md | 11 +++++++++++ deps.testing.ts | 6 ++++++ deps.ts | 0 doc/about.md | 7 +++++++ doc/index | 1 + mod.ts | 1 + run | 19 +++++++++++++++++++ src/deno.test.ts | 20 ++++++++++++++++++++ src/deno.ts | 10 ++++++++++ tsconfig.json | 10 ++++++++++ 12 files changed, 97 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 README.md create mode 100644 deps.testing.ts create mode 100644 deps.ts create mode 100644 doc/about.md create mode 100644 doc/index create mode 100644 mod.ts create mode 100755 run create mode 100644 src/deno.test.ts create mode 100644 src/deno.ts create mode 100644 tsconfig.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..83c1115 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d69a4c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +deno.d.ts +.vscode +.local diff --git a/README.md b/README.md new file mode 100644 index 0000000..aa179d2 --- /dev/null +++ b/README.md @@ -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. diff --git a/deps.testing.ts b/deps.testing.ts new file mode 100644 index 0000000..0a622cf --- /dev/null +++ b/deps.testing.ts @@ -0,0 +1,6 @@ +export { + describe, + expect, + it, + mock, +} from "https://js.thunderk.net/testing@1.0.0/mod.ts"; diff --git a/deps.ts b/deps.ts new file mode 100644 index 0000000..e69de29 diff --git a/doc/about.md b/doc/about.md new file mode 100644 index 0000000..bce2932 --- /dev/null +++ b/doc/about.md @@ -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. diff --git a/doc/index b/doc/index new file mode 100644 index 0000000..93caaca --- /dev/null +++ b/doc/index @@ -0,0 +1 @@ +about \ No newline at end of file diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..2ced4e1 --- /dev/null +++ b/mod.ts @@ -0,0 +1 @@ +export { Sys } from "./src/deno.ts"; diff --git a/run b/run new file mode 100755 index 0000000..74d1c6d --- /dev/null +++ b/run @@ -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 diff --git a/src/deno.test.ts b/src/deno.test.ts new file mode 100644 index 0000000..15be5a4 --- /dev/null +++ b/src/deno.test.ts @@ -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"] }); + }); + }); +}); diff --git a/src/deno.ts b/src/deno.ts new file mode 100644 index 0000000..0fcedef --- /dev/null +++ b/src/deno.ts @@ -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; + }, +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e28737f --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "module": "esnext", + "target": "ESNext", + "strict": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "preserveConstEnums": true + } +}