Home Explore Blog Models CI



nixpkgs

7th chunk of `doc/build-helpers/testers.chapter.md`
f1c3de6f6cc9cecd417e9adfb49679c1bda67fb032c70a130000000100000c05
  When provided, `script` is expected to populate `actualArray`.

`expectedMap` (attribute set of string-like values, optional)

: An attribute set of string-like values.
  This attribute set *must not* be accessed or modified from within `script`.
  When provided, `script` is expected to populate `actualMap`.

### Return value {#tester-testEqualArrayOrMap-return}

The tester produces an empty output and only succeeds when `expectedArray` and `expectedMap` match `actualArray` and `actualMap`, respectively, when non-null.
The build log will contain differences encountered.

## `testEqualDerivation` {#tester-testEqualDerivation}

Checks that two packages produce the exact same build instructions.

This can be used to make sure that a certain difference of configuration, such as the presence of an overlay does not cause a cache miss.

When the derivations are equal, the return value is an empty file.
Otherwise, the build log explains the difference via `nix-diff`.

:::{.example #ex-testEqualDerivation-hello}

# Check that two packages produce the same derivation

```nix
testers.testEqualDerivation "The hello package must stay the same when enabling checks." hello (
  hello.overrideAttrs (o: {
    doCheck = true;
  })
)
```

:::

## `invalidateFetcherByDrvHash` {#tester-invalidateFetcherByDrvHash}

Use the derivation hash to invalidate the output via name, for testing.

Type: `(a@{ name, ... } -> Derivation) -> a -> Derivation`

Normally, fixed output derivations can and should be cached by their output hash only, but for testing we want to re-fetch everytime the fetcher changes.

Changes to the fetcher become apparent in the drvPath, which is a hash of how to fetch, rather than a fixed store path.
By inserting this hash into the name, we can make sure to re-run the fetcher every time the fetcher changes.

This relies on the assumption that Nix isn't clever enough to reuse its database of local store contents to optimize fetching.

You might notice that the "salted" name derives from the normal invocation, not the final derivation.
`invalidateFetcherByDrvHash` has to invoke the fetcher function twice:
once to get a derivation hash, and again to produce the final fixed output derivation.

:::{.example #ex-invalidateFetcherByDrvHash-nix}

# Prevent nix from reusing the output of a fetcher

```nix
{
  tests.fetchgit = testers.invalidateFetcherByDrvHash fetchgit {
    name = "nix-source";
    url = "https://github.com/NixOS/nix";
    rev = "9d9dbe6ed05854e03811c361a3380e09183f4f4a";
    hash = "sha256-7DszvbCNTjpzGRmpIVAWXk20P0/XTrWZ79KSOGLrUWY=";
  };
}
```

:::

## `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 change.

It accepts the following attributes:
- the derivation's `name` ;
- the `script` to be executed ;

Title: Nix Testers: `testEqualArrayOrMap` Return, `testEqualDerivation`, `invalidateFetcherByDrvHash`, and `runCommand`
Summary
This chunk describes the return value of `testers.testEqualArrayOrMap`, which produces an empty output upon success and logs differences if actual arrays/maps don't match expected ones. It then introduces `testers.testEqualDerivation`, a tester that verifies if two packages generate identical build instructions, primarily to check for cache misses due to minor configuration changes, providing an example using the `hello` package. Following this, `testers.invalidateFetcherByDrvHash` is explained, a testing utility that ensures fixed-output derivations from fetchers are re-evaluated when the fetcher's instructions change, by incorporating the derivation hash into the output name. An example demonstrates its use with `fetchgit`. Finally, `testers.runCommand` is introduced as a wrapper around `pkgs.runCommandWith`, designed to create fixed-output derivations, enable network access for commands, and automatically re-run commands when inputs change by salting the derivation's name.