Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Testing library tlib documentation

tlib.test

Takes tests provided as an attrs in testSet, renders it to a bash script, and executes the rendered script using pkgs.runCommand.

If the command runs successfully, the test set passes. Otherwise, a detailed error message describing the failed assertion is shown in the logs.

Type

test :: (String | AttrSet) -> TestSet -> Derivation

where:

TestSet :: Test | attrsOf (TestSet | Test)

Test :: Assertion | [ Assertion ]

Assertion :: String | { cond :: String; msg :: String; }

Arguments

settings

Either a string or an attrs.

If it is a string, it will be taken as the name of the derivation built by runCommand.

If it is a set, at least one of the keys name or wrapper need to be set with a string value.

  • If you are defining tests for wrappers you should set settings.wrapper to the name of the wrapper. This way, the wrapper.meta.platforms is considered during test execution, and the test will only be run if the system it is running on is supported.
  • If settings.wrapper is not set, the test will always be run. This makes sense if you are testing core options or library functions.
  • If only settings.wrapper is set, the name will be derived from this value by suffixing it with -test.
  • If settings.name is set, it will be taken as the name of the derivation.
  • If both are set, settings.name takes precedence.

There is also an optional boolean option settings.debug (default = false). If it is set to true, the generated bash script is built as an executable script that can be inspected and run in result when running a specific test.

The test can be disabled by setting settings.enable = false.

testSet

The set of tests to run.

tlib.isDirectory

Returns an Assertion that checks whether path is an existing directory.

Type

isDirectory :: String -> Assertion

Arguments

path
The filesystem path to check.

tlib.isFile

Returns an Assertion that checks whether path is an existing regular file.

Type

isFile :: String -> Assertion

Arguments

path
The filesystem path to check.

tlib.notIsFile

Returns an Assertion that checks whether path does not exist as a regular file.

Type

notIsFile :: String -> Assertion

Arguments

path
The filesystem path that should be absent.

tlib.notIsDirectory

Returns an Assertion that checks whether path does not exist as a directory.

Type

notIsFile :: String -> Assertion

Arguments

path
The filesystem path that should be absent.

tlib.fileContains

Returns an Assertion that checks whether file contains a line matching pattern.

The check is performed with grep -Eq, so pattern is treated as an extended regular expression.

Type

fileContains :: String -> String -> Assertion

Arguments

file
Path to the file to search.
pattern
Extended regular expression to search for.

tlib.areEqual

Returns an Assertion that checks whether expected and actual are equal.

The comparison is performed in Nix at evaluation time. If the values differ, the assertion message shows both values serialised as JSON.

Type

areEqual :: Any -> Any -> Assertion

Arguments

expected
The expected value.
actual
The value to compare against expected.