Skip to content

Commit

Permalink
Merge pull request NixOS#333236 from nbraud/testers/runCommand
Browse files Browse the repository at this point in the history
testers.runCommand: init
  • Loading branch information
philiptaron authored Aug 17, 2024
2 parents 4af6be8 + 18dd486 commit cd7b95e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 3 deletions.
35 changes: 35 additions & 0 deletions doc/build-helpers/testers.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,41 @@ once to get a derivation hash, and again to produce the final fixed output deriv

:::

## `runCommand` {#tester-runCommand}

`runCommand :: { name, script, stdenv ? stdenvNoCC, hash ? "...", ... } -> Derivation`

This is a wrapper around `pkgs.runCommandWith`, which
- produces a fixed-output derivation, enabling the command(s) to access the network ;
- salts the derivation's name based on its inputs, ensuring the command is re-run whenever the inputs changes.

It accepts the following attributes:
- the derivation's `name` ;
- the `script` to be executed ;
- `stdenv`, the environment to use, defaulting to `stdenvNoCC` ;
- the derivation's output `hash`, defaulting to the empty file's.
The derivation's `outputHashMode` is set by default to recursive, so the `script` can output a directory as well.

All other attributes are passed through to [`mkDerivation`](#sec-using-stdenv),
including `nativeBuildInputs` to specify dependencies available to the `script`.

:::{.example #ex-tester-runCommand-nix}

# Run a command with network access

```nix
testers.runCommand {
name = "access-the-internet";
command = ''
curl -o /dev/null https://example.com
touch $out
'';
nativeBuildInputs = with pkgs; [ cacert curl ];
}
```

:::

## `runNixOSTest` {#tester-runNixOSTest}

A helper function that behaves exactly like the NixOS `runTest`, except it also assigns this Nixpkgs package set as the `pkgs` of the test and makes the `nixpkgs.*` options read-only.
Expand Down
41 changes: 40 additions & 1 deletion pkgs/build-support/testers/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
{ pkgs, pkgsLinux, buildPackages, diffoscopeMinimal, lib, callPackage, runCommand, stdenv, substituteAll, testers }:
{
lib,
buildPackages,
callPackage,
pkgs,
pkgsLinux,

diffoscopeMinimal,
runCommand,
runCommandWith,
stdenv,
stdenvNoCC,
substituteAll,
testers,
}:
# Documentation is in doc/build-helpers/testers.chapter.md
{
# See https://nixos.org/manual/nixpkgs/unstable/#tester-lycheeLinkCheck
Expand Down Expand Up @@ -87,6 +101,31 @@
else salted;
in checked;

# See https://nixos.org/manual/nixpkgs/unstable/#tester-runCommand
runCommand = testers.invalidateFetcherByDrvHash (
{
hash ? pkgs.emptyFile.outputHash,
name,
script,
stdenv ? stdenvNoCC,
...
}@args:

runCommandWith {
inherit name stdenv;

derivationArgs = {
outputHash = hash;
outputHashMode = "recursive";
} // lib.removeAttrs args [
"hash"
"name"
"script"
"stdenv"
];
} script
);

# See https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest
# or doc/build-helpers/testers.chapter.md
runNixOSTest =
Expand Down
23 changes: 23 additions & 0 deletions pkgs/build-support/testers/test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ lib.recurseIntoAttrs {

shellcheck = pkgs.callPackage ../shellcheck/tests.nix { };

runCommand = lib.recurseIntoAttrs {
bork = pkgs.python3Packages.bork.tests.pytest-network;

dns-resolution = testers.runCommand {
name = "runCommand-dns-resolution-test";
nativeBuildInputs = [ pkgs.ldns ];
script = ''
drill example.com
touch $out
'';
};

nonDefault-hash = testers.runCommand {
name = "runCommand-nonDefaultHash-test";
script = ''
mkdir $out
touch $out/empty
echo aaaaaaaaaaicjnrkeflncmrlk > $out/keymash
'';
hash = "sha256-eMy+6bkG+KS75u7Zt4PM3APhtdVd60NxmBRN5GKJrHs=";
};
};

runNixOSTest-example = pkgs-with-overlay.testers.runNixOSTest ({ lib, ... }: {
name = "runNixOSTest-test";
nodes.machine = { pkgs, ... }: {
Expand Down
3 changes: 1 addition & 2 deletions pkgs/build-support/trivial-builders/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,8 @@ rec {
/* An immutable file in the store with a length of 0 bytes. */
emptyFile = runCommand "empty-file"
{
outputHashAlgo = "sha256";
outputHash = "sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY=";
outputHashMode = "recursive";
outputHash = "0ip26j2h11n1kgkz36rl4akv694yz65hr72q4kv4b3lxcbi65b3p";
preferLocalBuild = true;
} "touch $out";

Expand Down
3 changes: 3 additions & 0 deletions pkgs/development/python-modules/bork/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
lib,
buildPythonPackage,
callPackage,
fetchFromGitHub,
pytestCheckHook,
pythonOlder,
Expand Down Expand Up @@ -61,6 +62,8 @@ buildPythonPackage rec {
"test_repo"
];

passthru.tests = callPackage ./tests.nix { };

meta = with lib; {
description = "Python build and release management tool";
mainProgram = "bork";
Expand Down
28 changes: 28 additions & 0 deletions pkgs/development/python-modules/bork/tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
testers,

bork,
cacert,
git,
pytest,
}:
{
# a.k.a. `tests.testers.runCommand.bork`
pytest-network = testers.runCommand {
name = "bork-pytest-network";
nativeBuildInputs = [
bork
cacert
git
pytest
];
script = ''
# Copy the source tree over, and make it writeable
cp -r ${bork.src} bork/
find -type d -exec chmod 0755 '{}' '+'
pytest -v -m network bork/
touch $out
'';
};
}

0 comments on commit cd7b95e

Please sign in to comment.