<commit_before>#!/bin/sh
#
# This script is used to test that the module system is working as expected.
# By default it test the version of nixpkgs which is defined in the NIX_PATH.

# https://stackoverflow.com/a/246128/6605742
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

cd "$DIR"/modules

pass=0
fail=0

evalConfig() {
    local attr=$1
    shift;
    local script="import ./default.nix { modules = [ $@ ];}"
    nix-instantiate --timeout 1 -E "$script" -A "$attr" --eval-only --show-trace --read-write-mode
}

reportFailure() {
    local attr=$1
    shift;
    local script="import ./default.nix { modules = [ $@ ];}"
    echo 2>&1 "$ nix-instantiate -E '$script' -A '$attr' --eval-only"
    evalConfig "$attr" "$@"
    fail=$((fail + 1))
}

checkConfigOutput() {
    local outputContains=$1
    shift;
    if evalConfig "$@" 2>/dev/null | grep --silent "$outputContains" ; then
        pass=$((pass + 1))
        return 0;
    else
        echo 2>&1 "error: Expected result matching '$outputContains', while evaluating"
        reportFailure "$@"
        return 1
    fi
}

checkConfigError() {
    local errorContains=$1
    local err=""
    shift;
    if err==$(evalConfig "$@" 2>&1 >/dev/null); then
        echo 2>&1 "error: Expected error code, got exit code 0, while evaluating"
        reportFailure "$@"
        return 1
    else
        if echo "$err" | grep -zP --silent "$errorContains" ; then
            pass=$((pass + 1))
            return 0;
        else
            echo 2>&1 "error: Expected error matching '$errorContains', while evaluating"
            reportFailure "$@"
            return 1
        fi
    fi
}

# Check boolean option.
checkConfigOutput "false" config.enable ./declare-enable.nix
checkConfigError 'The option .* does not exist. Definition values:\n- In .*: true' config.enable ./define-enable.nix<commit_msg>lib/tests: Implement generalized checkConfigCodeOutErr for module tests<commit_after>#!/bin/sh
#
# This script is used to test that the module system is working as expected.
# By default it test the version of nixpkgs which is defined in the NIX_PATH.

# https://stackoverflow.com/a/246128/6605742
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

cd "$DIR"/modules

pass=0
fail=0

evalConfig() {
    local attr=$1
    shift;
    local script="import ./default.nix { modules = [ $@ ];}"
    nix-instantiate --timeout 1 -E "$script" -A "$attr" --eval-only --show-trace --read-write-mode
}

reportFailure() {
    local attr=$1
    shift;
    local script="import ./default.nix { modules = [ $@ ];}"
    echo 2>&1 "$ nix-instantiate -E '$script' -A '$attr' --eval-only"
    evalConfig "$attr" "$@"
    fail=$((fail + 1))
}

checkConfigCodeOutErr() {
    local expectedExit=$1
    shift;
    local outputContains=$1
    shift;
    local errorContains=$1
    shift;
    out=$(mktemp)
    err=$(mktemp)
    evalConfig "$@" 1>"$out" 2>"$err"
    if [[ "$?" -ne "$expectedExit" ]]; then
        echo 2>&1 "error: Expected exit code $expectedExit while evaluating"
        reportFailure "$@"
        return 1
    fi

    if [[ -n "$outputContains" ]] && ! grep -zP --silent "$outputContains" "$out"; then
        echo 2>&1 "error: Expected output matching '$outputContains', while evaluating"
        reportFailure "$@"
        return 1
    fi

    if [[ -n "$errorContains" ]] && ! grep -zP --silent "$errorContains" "$err"; then
        echo 2>&1 "error: Expected error matching '$errorContains', while evaluating"
        reportFailure "$@"
        return 1
    fi

    pass=$((pass + 1))

    # Clean up temp files
    rm "$out" "$err"
}

checkConfigOutput() {
    local outputContains=$1
    shift;
    checkConfigCodeOutErr 0 "$outputContains" "" "$@"
}

checkConfigError() {
    local errorContains=$1
    shift;
    checkConfigCodeOutErr 1 "" "$errorContains" "$@"
}

# Check boolean option.
checkConfigOutput "false" config.enable ./declare-enable.nix
checkConfigError 'The option .* does not exist. Definition values:\n- In .*: true' config.enable ./define-enable.nix
{'fraction_non_alphanumeric': 0.1527880127732744, 'fraction_numerical': 0.02112503070498649, 'mean_word_length': 2.7949673811742777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '73314', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1243, 'n_words': 383}