From d1949b739a57845d0c4a4d4a79ef41620269edd3 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Mon, 8 Aug 2022 20:10:45 +0200 Subject: [PATCH 1/3] bats: init libraries - init bats-assert 2.0.0 - init bats-support 0.3.0 - init bats-file 0.3.0 --- .../development/interpreters/bats/default.nix | 3 + .../interpreters/bats/libraries.nix | 70 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 pkgs/development/interpreters/bats/libraries.nix diff --git a/pkgs/development/interpreters/bats/default.nix b/pkgs/development/interpreters/bats/default.nix index ef1a110f8e74..cc57d72e8058 100644 --- a/pkgs/development/interpreters/bats/default.nix +++ b/pkgs/development/interpreters/bats/default.nix @@ -13,6 +13,7 @@ , ps , bats , lsof +, callPackages , doInstallCheck ? true }: @@ -105,6 +106,8 @@ resholve.mkDerivation rec { }; }; + passthru.libraries = callPackages ./libraries.nix {}; + passthru.tests.upstream = bats.unresholved.overrideAttrs (old: { name = "${bats.name}-tests"; dontInstall = true; # just need the build directory diff --git a/pkgs/development/interpreters/bats/libraries.nix b/pkgs/development/interpreters/bats/libraries.nix new file mode 100644 index 000000000000..74324b03e6ad --- /dev/null +++ b/pkgs/development/interpreters/bats/libraries.nix @@ -0,0 +1,70 @@ +{ lib, stdenv, fetchFromGitHub }: { + bats-assert = stdenv.mkDerivation { + pname = "bats-assert"; + version = "2.0.0"; + src = fetchFromGitHub { + owner = "bats-core"; + repo = "bats-assert"; + rev = "v2.0.0"; + sha256 = "sha256-whSbAj8Xmnqclf78dYcjf1oq099ePtn4XX9TUJ9AlyQ="; + }; + dontBuild = true; + installPhase = '' + mkdir -p "$out/share/bats" + cp -r . "$out/share/bats/bats-assert" + ''; + meta = { + description = "Common assertions for Bats"; + platforms = lib.platforms.all; + homepage = "https://github.com/bats-core/bats-assert"; + license = lib.licenses.cc0; + maintainers = with lib.maintainers; [ infinisil ]; + }; + }; + + bats-file = stdenv.mkDerivation { + pname = "bats-file"; + version = "0.3.0"; + src = fetchFromGitHub { + owner = "bats-core"; + repo = "bats-file"; + rev = "v0.3.0"; + sha256 = "sha256-3xevy0QpwNZrEe+2IJq58tKyxQzYx8cz6dD2nz7fYUM="; + }; + dontBuild = true; + installPhase = '' + mkdir -p "$out/share/bats" + cp -r . "$out/share/bats/bats-file" + ''; + meta = { + description = "Common filesystem assertions for Bats"; + platforms = lib.platforms.all; + homepage = "https://github.com/bats-core/bats-file"; + license = lib.licenses.cc0; + maintainers = with lib.maintainers; [ infinisil ]; + }; + }; + + bats-support = stdenv.mkDerivation { + pname = "bats-support"; + version = "0.3.0"; + src = fetchFromGitHub { + owner = "bats-core"; + repo = "bats-support"; + rev = "v0.3.0"; + sha256 = "sha256-4N7XJS5XOKxMCXNC7ef9halhRpg79kUqDuRnKcrxoeo="; + }; + dontBuild = true; + installPhase = '' + mkdir -p "$out/share/bats" + cp -r . "$out/share/bats/bats-support" + ''; + meta = { + description = "Supporting library for Bats test helpers"; + platforms = lib.platforms.all; + homepage = "https://github.com/bats-core/bats-support"; + license = lib.licenses.cc0; + maintainers = with lib.maintainers; [ infinisil ]; + }; + }; +} From fbe194fdf6e577468bc38a5ee0bd77d130ddca49 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Mon, 8 Aug 2022 20:12:56 +0200 Subject: [PATCH 2/3] bats: Add library wrapper Adds a `bats.withLibraries (p: [ ... ])` function, which creates a `bats` wrapper where the `BATS_LIB_PATH` environment variable contains fallbacks for the given list of libraries. This allows to e.g. use the `bats-assert` library (which itself requires the `bats-support` library) with bats.withLibraries (p: [ p.bats-support p.bats-assert ]) In a `.bats` file you can then call `bats_load_library` [1] to load the libraries in the `setup()` function: setup() { bats_load_library bats-support bats_load_library bats-assert } [1]: https://bats-core.readthedocs.io/en/stable/writing-tests.html?highlight=library#bats-load-library-load-system-wide-libraries --- .../development/interpreters/bats/default.nix | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkgs/development/interpreters/bats/default.nix b/pkgs/development/interpreters/bats/default.nix index cc57d72e8058..b9899fc229dd 100644 --- a/pkgs/development/interpreters/bats/default.nix +++ b/pkgs/development/interpreters/bats/default.nix @@ -14,6 +14,8 @@ , bats , lsof , callPackages +, symlinkJoin +, makeWrapper , doInstallCheck ? true }: @@ -108,6 +110,24 @@ resholve.mkDerivation rec { passthru.libraries = callPackages ./libraries.nix {}; + passthru.withLibraries = selector: + symlinkJoin { + name = "bats-with-libraries-${bats.version}"; + + paths = [ + bats + ] ++ selector bats.libraries; + + nativeBuildInputs = [ + makeWrapper + ]; + + postBuild = '' + wrapProgram "$out/bin/bats" \ + --suffix BATS_LIB_PATH : "$out/share/bats" + ''; + }; + passthru.tests.upstream = bats.unresholved.overrideAttrs (old: { name = "${bats.name}-tests"; dontInstall = true; # just need the build directory From d42f6128c7075a5babf21e4716f0d071724359e1 Mon Sep 17 00:00:00 2001 From: "Travis A. Everett" Date: Tue, 9 Aug 2022 10:19:07 -0500 Subject: [PATCH 3/3] bats: Add library test Co-Authored-By: Silvan Mosberger --- .../development/interpreters/bats/default.nix | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkgs/development/interpreters/bats/default.nix b/pkgs/development/interpreters/bats/default.nix index b9899fc229dd..89539850163e 100644 --- a/pkgs/development/interpreters/bats/default.nix +++ b/pkgs/development/interpreters/bats/default.nix @@ -16,6 +16,7 @@ , callPackages , symlinkJoin , makeWrapper +, runCommand , doInstallCheck ? true }: @@ -128,6 +129,32 @@ resholve.mkDerivation rec { ''; }; + passthru.tests.libraries = runCommand "${bats.name}-with-libraries-test" { + testScript = '' + setup() { + bats_load_library bats-support + bats_load_library bats-assert + + bats_require_minimum_version 1.5.0 + } + + @test echo_hi { + run -0 echo hi + assert_output "hi" + } + + @test cp_failure { + run ! cp + assert_line --index 0 "cp: missing file operand" + assert_line --index 1 "Try 'cp --help' for more information." + } + ''; + passAsFile = [ "testScript" ]; + } '' + ${bats.withLibraries (p: [ p.bats-support p.bats-assert ])}/bin/bats "$testScriptPath" + touch "$out" + ''; + passthru.tests.upstream = bats.unresholved.overrideAttrs (old: { name = "${bats.name}-tests"; dontInstall = true; # just need the build directory