summary refs log tree commit diff
path: root/pkgs/development/ruby-modules/testing
diff options
context:
space:
mode:
authorJudson <nyarly@gmail.com>2017-05-27 15:19:34 -0700
committerJudson <nyarly@gmail.com>2017-05-27 15:19:34 -0700
commit998d011e426c2f8c51946ebbc4931a464f531db9 (patch)
treef7c29caa5789057980f0627fd4e9435b35e34000 /pkgs/development/ruby-modules/testing
parent022be78eb26de958e50e32ac40574e93ac81bee3 (diff)
downloadnixpkgs-998d011e426c2f8c51946ebbc4931a464f531db9.tar
nixpkgs-998d011e426c2f8c51946ebbc4931a464f531db9.tar.gz
nixpkgs-998d011e426c2f8c51946ebbc4931a464f531db9.tar.bz2
nixpkgs-998d011e426c2f8c51946ebbc4931a464f531db9.tar.lz
nixpkgs-998d011e426c2f8c51946ebbc4931a464f531db9.tar.xz
nixpkgs-998d011e426c2f8c51946ebbc4931a464f531db9.tar.zst
nixpkgs-998d011e426c2f8c51946ebbc4931a464f531db9.zip
Restructuring files
Diffstat (limited to 'pkgs/development/ruby-modules/testing')
-rw-r--r--pkgs/development/ruby-modules/testing/assertions.nix24
-rw-r--r--pkgs/development/ruby-modules/testing/driver.nix20
-rwxr-xr-xpkgs/development/ruby-modules/testing/runtests.sh2
-rw-r--r--pkgs/development/ruby-modules/testing/stubs.nix33
-rw-r--r--pkgs/development/ruby-modules/testing/tap-support.nix20
-rw-r--r--pkgs/development/ruby-modules/testing/testing.nix62
6 files changed, 161 insertions, 0 deletions
diff --git a/pkgs/development/ruby-modules/testing/assertions.nix b/pkgs/development/ruby-modules/testing/assertions.nix
new file mode 100644
index 00000000000..3cf67d6f3eb
--- /dev/null
+++ b/pkgs/development/ruby-modules/testing/assertions.nix
@@ -0,0 +1,24 @@
+{ test, lib, ...}:
+{
+  equal = expected: actual:
+    if actual == expected then
+      (test.passed "= ${toString expected}") else
+      (test.failed "'${toString actual}'(${builtins.typeOf actual}) != '${toString expected}'(${builtins.typeOf expected})");
+
+  beASet = actual:
+    if builtins.isAttrs actual then
+      (test.passed "is a set") else
+      (test.failed "is not a set, was ${builtins.typeOf actual}: ${toString actual}");
+
+  haveKeys = expected: actual:
+    if builtins.all
+    (ex: builtins.any (ac: ex == ac) (builtins.attrNames actual))
+    expected then
+      (test.passed "has expected keys") else
+      (test.failed "keys differ: expected [${lib.concatStringsSep ";" expected}] have [${lib.concatStringsSep ";" (builtins.attrNames actual)}]");
+
+  havePrefix = expected: actual:
+    if lib.hasPrefix expected actual then
+      (test.passed "has prefix '${expected}'") else
+      (test.failed "prefix '${expected}' not found in '${actual}'");
+}
diff --git a/pkgs/development/ruby-modules/testing/driver.nix b/pkgs/development/ruby-modules/testing/driver.nix
new file mode 100644
index 00000000000..65e7c8d4416
--- /dev/null
+++ b/pkgs/development/ruby-modules/testing/driver.nix
@@ -0,0 +1,20 @@
+/*
+Run with:
+nix-build -E 'with import <nixpkgs> { }; callPackage ./test.nix {}' --show-trace; and cat result
+
+Confusingly, the ideal result ends with something like:
+error: build of ‘/nix/store/3245f3dcl2wxjs4rci7n069zjlz8qg85-test-results.tap.drv’ failed
+*/
+{ writeText, lib, callPackage, testFiles, stdenv, ruby }@defs:
+let
+  testTools = rec {
+    test = import ./testing.nix;
+    stubs = import ./stubs.nix defs;
+    should = import ./assertions.nix { inherit test lib; };
+  };
+
+  tap = import ./tap-support.nix;
+
+  results = builtins.concatLists (map (file: callPackage file testTools) testFiles);
+in
+  writeText "test-results.tap" (tap.output results)
diff --git a/pkgs/development/ruby-modules/testing/runtests.sh b/pkgs/development/ruby-modules/testing/runtests.sh
new file mode 100755
index 00000000000..c3db8ed34af
--- /dev/null
+++ b/pkgs/development/ruby-modules/testing/runtests.sh
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+nix-build -E 'with import <nixpkgs> { }; callPackage ./test.nix {}' --show-trace && cat result
diff --git a/pkgs/development/ruby-modules/testing/stubs.nix b/pkgs/development/ruby-modules/testing/stubs.nix
new file mode 100644
index 00000000000..3585681478c
--- /dev/null
+++ b/pkgs/development/ruby-modules/testing/stubs.nix
@@ -0,0 +1,33 @@
+{ stdenv, lib, ruby, callPackage, ... }:
+let
+  real = {
+    inherit (stdenv) mkDerivation;
+  };
+  mkDerivation = {name, ...}@argSet:
+  derivation {
+    inherit name;
+    text = (builtins.toJSON (lib.filterAttrs ( n: v: builtins.any (x: x == n) ["name" "system"]) argSet));
+    builder = stdenv.shell;
+    args = [ "-c" "echo  $(<$textPath) > $out"];
+    system = stdenv.system;
+    passAsFile = ["text"];
+  };
+  fetchurl = {url?"", urls ? [],...}: "fetchurl:${if urls == [] then url else builtins.head urls}";
+
+  stdenv' = stdenv // {
+    inherit mkDerivation;
+    stubbed = true;
+  };
+  ruby' = ruby // {
+    stdenv = stdenv';
+    stubbed = true;
+  };
+in
+  {
+    ruby = ruby';
+    buildRubyGem = callPackage ../gem {
+      inherit fetchurl;
+      ruby = ruby';
+    };
+    stdenv = stdenv';
+  }
diff --git a/pkgs/development/ruby-modules/testing/tap-support.nix b/pkgs/development/ruby-modules/testing/tap-support.nix
new file mode 100644
index 00000000000..ba576683d37
--- /dev/null
+++ b/pkgs/development/ruby-modules/testing/tap-support.nix
@@ -0,0 +1,20 @@
+with builtins;
+let
+  withIndexes = list: genList (idx: (elemAt list idx) // {index = idx;}) (length list);
+
+  testLine = report: "${okStr report} ${toString report.index} ${report.description}" + testDirective report + testYaml report;
+
+  testDirective = report: "";
+
+  testYaml = report: "";
+
+  okStr = { result, ...}: if result == "pass" then "ok" else "not ok";
+in
+  {
+    output = reports: ''
+      TAP version 13
+      1..${toString (length reports)}'' + (foldl' (l: r: l + "\n" + r) "" (map testLine (withIndexes reports))) + ''
+
+      # Finished at ${toString currentTime}
+      '';
+  }
diff --git a/pkgs/development/ruby-modules/testing/testing.nix b/pkgs/development/ruby-modules/testing/testing.nix
new file mode 100644
index 00000000000..43d10fca044
--- /dev/null
+++ b/pkgs/development/ruby-modules/testing/testing.nix
@@ -0,0 +1,62 @@
+with builtins;
+let
+  /*
+  underTest = {
+    x = {
+      a = 1;
+      b = "2";
+    };
+  };
+
+  tests = [
+    (root: false)
+    {
+      x = [
+        (set: true)
+        {
+          a = (a: a > 1);
+          b = (b: b == "3");
+        }
+      ];
+    }
+  ];
+
+  results = run "Examples" underTest tests;
+  */
+
+  passed = desc: {
+    result = "pass";
+    description = desc;
+  };
+
+  failed = desc: {
+    result = "failed";
+    description = desc;
+  };
+
+  prefixName = name: res: {
+    inherit (res) result;
+    description = "${name}: ${res.description}";
+  };
+
+  run = name: under: tests: if isList tests then
+    (concatLists (map (run name under) tests))
+  else if isAttrs tests then
+    (concatLists (map (
+    subName: run (name + "." + subName) (if hasAttr subName under then getAttr subName under else "<MISSING!>") (getAttr subName tests)
+    ) (attrNames tests)))
+  else if isFunction tests then
+    let
+      res = tests under;
+    in
+      if isBool res then
+        [
+          (prefixName name (if tests under then passed "passed" else failed "failed"))
+        ]
+      else
+        [ (prefixName name res) ]
+  else [
+    failed (name ": not a function, list or set")
+  ];
+in
+  { inherit run passed failed; }