deno: add some simple tests
This commit is contained in:
parent
27bcfe5f6e
commit
159f042d38
|
@ -85,6 +85,7 @@ rustPlatform.buildRustPackage rec {
|
||||||
'';
|
'';
|
||||||
|
|
||||||
passthru.updateScript = ./update/update.ts;
|
passthru.updateScript = ./update/update.ts;
|
||||||
|
passthru.tests = callPackage ./tests { };
|
||||||
|
|
||||||
meta = with lib; {
|
meta = with lib; {
|
||||||
homepage = "https://deno.land/";
|
homepage = "https://deno.land/";
|
||||||
|
|
1
pkgs/development/web/deno/tests/basic.ts
Normal file
1
pkgs/development/web/deno/tests/basic.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
console.log(1 + 1)
|
68
pkgs/development/web/deno/tests/default.nix
Normal file
68
pkgs/development/web/deno/tests/default.nix
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
{ deno, runCommand, lib, testers }:
|
||||||
|
let
|
||||||
|
testDenoRun =
|
||||||
|
name:
|
||||||
|
{ args ? ""
|
||||||
|
, dir ? ./. + "/${name}"
|
||||||
|
, file ? "index.ts"
|
||||||
|
, expected ? ""
|
||||||
|
, expectFailure ? false
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
command = "deno run ${args} ${dir}/${file}";
|
||||||
|
in
|
||||||
|
runCommand "deno-test-${name}" { nativeBuildInputs = [ deno ]; meta.timeout = 60; } ''
|
||||||
|
HOME=$(mktemp -d)
|
||||||
|
if output=$(${command} 2>&1); then
|
||||||
|
if [[ $output =~ '${expected}' ]]; then
|
||||||
|
echo "Test '${name}' passed"
|
||||||
|
touch $out
|
||||||
|
else
|
||||||
|
echo -n ${lib.escapeShellArg command} >&2
|
||||||
|
echo " output did not match what was expected." >&2
|
||||||
|
echo "The expected was:" >&2
|
||||||
|
echo '${expected}' >&2
|
||||||
|
echo "The output was:" >&2
|
||||||
|
echo "$output" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [[ "${toString expectFailure}" == "1" ]]; then
|
||||||
|
echo "Test '${name}' failed as expected"
|
||||||
|
touch $out
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
echo -n ${lib.escapeShellArg command} >&2
|
||||||
|
echo " returned a non-zero exit code." >&2
|
||||||
|
echo "$output" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
'';
|
||||||
|
in
|
||||||
|
(lib.mapAttrs testDenoRun {
|
||||||
|
basic = {
|
||||||
|
dir = ./.;
|
||||||
|
file = "basic.ts";
|
||||||
|
expected = "2";
|
||||||
|
};
|
||||||
|
import-json = {
|
||||||
|
expected = "hello from JSON";
|
||||||
|
};
|
||||||
|
import-ts = {
|
||||||
|
expected = "hello from ts";
|
||||||
|
};
|
||||||
|
read-file = {
|
||||||
|
args = "--allow-read";
|
||||||
|
expected = "hello from a file";
|
||||||
|
};
|
||||||
|
fail-read-file = {
|
||||||
|
expectFailure = true;
|
||||||
|
dir = ./read-file;
|
||||||
|
};
|
||||||
|
}) //
|
||||||
|
{
|
||||||
|
version = testers.testVersion {
|
||||||
|
package = deno;
|
||||||
|
command = "deno --version";
|
||||||
|
};
|
||||||
|
}
|
1
pkgs/development/web/deno/tests/import-json/data.json
Normal file
1
pkgs/development/web/deno/tests/import-json/data.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{ "msg": "hello from JSON" }
|
2
pkgs/development/web/deno/tests/import-json/index.ts
Normal file
2
pkgs/development/web/deno/tests/import-json/index.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
import file from "./data.json" assert { type: "json" };
|
||||||
|
console.log(file.msg);
|
3
pkgs/development/web/deno/tests/import-ts/index.ts
Normal file
3
pkgs/development/web/deno/tests/import-ts/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import { sayHello } from "./lib.ts"
|
||||||
|
|
||||||
|
sayHello("ts")
|
3
pkgs/development/web/deno/tests/import-ts/lib.ts
Normal file
3
pkgs/development/web/deno/tests/import-ts/lib.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export function sayHello(thing: string) {
|
||||||
|
console.log(`hello from ${thing}`);
|
||||||
|
}
|
1
pkgs/development/web/deno/tests/read-file/data.txt
Normal file
1
pkgs/development/web/deno/tests/read-file/data.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
hello from a file
|
5
pkgs/development/web/deno/tests/read-file/index.ts
Normal file
5
pkgs/development/web/deno/tests/read-file/index.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// trim 'file://' prefix
|
||||||
|
const thisDir = Deno.mainModule.substring(7, Deno.mainModule.length);
|
||||||
|
const getParent = (path: string) => path.substring(0, path.lastIndexOf("/"))
|
||||||
|
const text = await Deno.readTextFile(getParent(thisDir) + "/data.txt");
|
||||||
|
console.log(text);
|
Loading…
Reference in a new issue