feat: +checks flake output

This commit is contained in:
2025-11-03 19:02:42 +00:00
parent e3167ce6e5
commit 43dc59a04f
8 changed files with 154 additions and 0 deletions

3
test/default.nix Normal file
View File

@@ -0,0 +1,3 @@
{ system, inputs, self, pkgs, flake }:
(import ./package { inherit system inputs self pkgs; })

View File

View File

@@ -0,0 +1,17 @@
{ self, inputs, system, ... }: let
mkSys = system: opts:
(inputs.nixpkgs.lib.nixosSystem {
inherit system;
modules = [
self.nixosModules.hectic
{ hectic.hardware.lenovo-ideapad-15arh7 = opts; }
];
});
cases = {
#enable = { enable = true; };
#disabled = { enable = false; };
#customFoo = { enable = true; foo = "bar"; };
};
in inputs.nixpkgs.lib.mapAttrs
(name: opts: (mkSys system opts).config.system.build.toplevel) cases

1
test/package/default.nix Normal file
View File

@@ -0,0 +1 @@
{ system, inputs, self, pkgs }: (import ./migrator { inherit system inputs self pkgs; })

View File

@@ -0,0 +1,44 @@
{ inputs, self, pkgs, system, ... }: let
lib = inputs.nixpkgs.lib;
# turn anything under ./test into a derivation that exposes $out/run.sh
mkTestDrv = name: type:
if type == "directory" then
pkgs.runCommand "test-${name}" {} ''
if ! [ -f ${./test + "/${name}" + /run.sh} ]; then
echo no run.sh in test/${name}
exit 1
fi
mkdir -p "$out"
cp -r ${./test + "/${name}"}/* "$out/"
chmod +x "$out/run.sh"
''
else if lib.hasSuffix ".sh" name then
pkgs.runCommand "test-${lib.removeSuffix ".sh" name}" {} ''
mkdir -p "$out"
install -Dm755 ${./test + "/${name}"} "$out/run.sh"
''
else
null;
testDir = builtins.readDir ./test;
# attrset: testName -> drv with run.sh
testDrvs =
lib.mapAttrs' (n: v:
lib.nameValuePair (lib.removeSuffix ".sh" n) v
) (lib.filterAttrs (_: v: v != null)
(lib.mapAttrs (n: t: mkTestDrv n t) testDir));
migrator = self.packages.${system}.migrator;
mkPgTest = testName: testDrv: pkgs.runCommand "migrator-test-${testName}"
{
nativeBuildInputs = [ pkgs.coreutils pkgs.gnugrep pkgs.gnused ];
buildInputs = [ migrator pkgs.postgresql ];
} ''
${builtins.readFile self.legacyPackages.${system}.helpers.posix-shell.log}
test=${testDrv}
${builtins.readFile ./lauch.sh}
'';
in builtins.trace testDir (lib.mapAttrs (name: drv: mkPgTest name drv) testDrvs)

View File

@@ -0,0 +1,68 @@
#!/bin/dash
# $out - nix derivation output
# $test - test and assertion file
test_derivation="$(basename "$test")"
test_name="${test_derivation#*-*-}"
set -eu
root_dir="$(dirname $0)"
HECTIC_LOG=
# save path to pg_ctl in case $PATH will change
PG_CTL="$(command -v pg_ctl)"
cleanup() {
"$PG_CTL" -D "$data" -m fast -w stop
}
log info 'start test pipeline'
# temp dirs
wd="$PWD"
data="$wd/data"
sockdir="$wd/sock"
db="testdb"
mkdir -p "$data" "$sockdir"
# initdb
initdb -D "$data" --no-locale -E UTF8 >/dev/null
# trust local auth for the test
{
echo "unix_socket_directories = '$sockdir'"
echo "listen_addresses = ''"
} >> "$data/postgresql.conf"
sed -i "1ilocal all all trust" "$data/pg_hba.conf"
# start cluster
pg_ctl -D "$data" -o "-F" -w start
trap cleanup EXIT
user="$(id -un)"
# bootstrap DB
createdb -h "$sockdir" -U "$user" "$db"
psql -h "$sockdir" -d testdb -v ON_ERROR_STOP=1 -c 'select 1;' >/dev/null
export PGHOST="$sockdir"
export PGPORT=5432
export PGUSER="$user"
export PGDATABASE="$db"
DATABASE_URL="postgresql://${PGUSER}@/${PGDATABASE}?host=${PGHOST}&port=${PGPORT}"
export DATABASE_URL
log info "run test ${WHITE}${test_name}${NC}"
# run test
. "${test}/run.sh"
log info "finish test pipeline"
# success marker for Nix
# shellcheck disable=SC2154
mkdir -p "$out"

View File

@@ -0,0 +1,21 @@
# remove psql from $PATH
dir=$(dirname -- "$(command -v psql)")
PATH=$(printf '%s' "$PATH" | awk -v RS=: -v ORS=: -v d="$dir" '$0!=d{print}')
PATH=${PATH%:}
# clear lookup cache
hash -r 2>/dev/null || true
# temporary disable break on errors, coz in this test we check error-handling scenario
set +e
# try to run migrator without installed psql
migrator 2>/dev/null
migrator_error_code=$?
set -e
log debug "migrator error code: $migrator_error_code"
if [ "$migrator_error_code" -eq 127 ]; then
log notice "test passed"
else
log error "test failed"
fi

View File