diff --git a/test/package/default.nix b/test/package/default.nix index 5a45b14..a0305ea 100644 --- a/test/package/default.nix +++ b/test/package/default.nix @@ -1,5 +1,6 @@ { system, inputs, self, pkgs }: - (import ./migrator { inherit system inputs self pkgs; }) // - (import ./hemar { inherit system inputs self pkgs; }) // + (import ./migrator { inherit system inputs self pkgs; }) // + (import ./hemar { inherit system inputs self pkgs; }) // (import (./. + "/sentinèlla") { inherit system inputs self pkgs; }) // - (import ./db-tool { inherit system inputs self pkgs; }) + (import ./db-tool { inherit system inputs self pkgs; }) // + (import ./linux-devshell { inherit system inputs self pkgs; }) diff --git a/test/package/linux-devshell/default.nix b/test/package/linux-devshell/default.nix new file mode 100644 index 0000000..79553d0 --- /dev/null +++ b/test/package/linux-devshell/default.nix @@ -0,0 +1,140 @@ +{ inputs, self, pkgs, system, ... }: let + lib = inputs.nixpkgs.lib; + + 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; + 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)); + + linuxDevShell = self.packages.${system}.linux-devshell; + linuxDevShellStandalone = self.packages.${system}.linux-devshell-standalone; + + archBootstrap = pkgs.fetchurl { + url = "https://geo.mirror.pkgbuild.com/iso/latest/archlinux-bootstrap-x86_64.tar.zst"; + hash = "sha256-1YnwGo2li1yIzm6W6yYYwuZeY16Ddsrz1LRTY6/A3Ww="; + }; + + mkTest = testName: testDrv: pkgs.runCommand "linux-devshell-test-${testName}" + { + nativeBuildInputs = [ pkgs.coreutils pkgs.gnugrep pkgs.gnused ]; + buildInputs = [ pkgs.dash ]; + linuxDevShell = linuxDevShell; + linuxDevShellStandalone = linuxDevShellStandalone; + } '' + ${builtins.readFile self.legacyPackages.${system}.helpers.posix-shell.log} + export HECTIC_LOG=trace + test=${testDrv} + linuxDevShell="${linuxDevShell}" + linuxDevShellStandalone="${linuxDevShellStandalone}" + ${builtins.readFile ./launch.sh} + + mkdir -p "$out" + ''; + + archTest = pkgs.runCommand "linux-devshell-test-arch-integration" + { + nativeBuildInputs = [ pkgs.coreutils pkgs.gnugrep pkgs.gnused pkgs.zstd pkgs.git ]; + buildInputs = [ pkgs.dash pkgs.proot ]; + linuxDevShellStandalone = linuxDevShellStandalone; + archBootstrap = archBootstrap; + } '' + ${builtins.readFile self.legacyPackages.${system}.helpers.posix-shell.log} + export HECTIC_LOG=trace + + log notice "test case: ''${WHITE}arch integration" + + ARCH_DIR="$(mktemp -d)" + trap 'rm -rf "$ARCH_DIR"' EXIT + + log info "Extracting Arch bootstrap..." + tar --zstd -xf "${archBootstrap}" -C "$ARCH_DIR" --strip-components=1 \ + --no-same-permissions --no-same-owner --warning=no-unknown-keyword \ + --exclude='etc/ca-certificates' --exclude='etc/ssl' || true + + log info "Preparing Arch environment..." + mkdir -p "$ARCH_DIR/root/test-repo/script" + cp "${linuxDevShellStandalone}" "$ARCH_DIR/root/test-repo/script/linux-devshell" + chmod +x "$ARCH_DIR/root/test-repo/script/linux-devshell" + + cat > "$ARCH_DIR/root/test-repo/flake.nix" <<'EOF' + { + description = "Test flake for linux-devshell"; + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + outputs = { self, nixpkgs }: { + devShells.x86_64-linux.default = nixpkgs.legacyPackages.x86_64-linux.mkShell { + shellHook = '''' + echo "=== Inside dev shell ===" + exit 0 + ''''; + }; + }; + } + EOF + + mkdir -p "$ARCH_DIR/root/test-repo/.git" + git init "$ARCH_DIR/root/test-repo" + git -C "$ARCH_DIR/root/test-repo" config user.email "test@example.com" + git -C "$ARCH_DIR/root/test-repo" config user.name "Test" + git -C "$ARCH_DIR/root/test-repo" add . + git -C "$ARCH_DIR/root/test-repo" commit -m "init" + + log info "Running linux-devshell in Arch via proot..." + proot -b /dev -r "$ARCH_DIR" -w /root/test-repo /bin/sh -c ' + export PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" + export TMPDIR=/tmp + mkdir -p /nix /build /tmp + /root/test-repo/script/linux-devshell 2>&1 + ' | tee /tmp/proot-output || true + + log info "Checking script behavior in Arch environment..." + + if ! grep -q "Nix not found" /tmp/proot-output; then + log error "Script did not detect missing Nix" + cat /tmp/proot-output + exit 1 + fi + log success "Script correctly detects missing Nix" + + if ! grep -q "Installing via nixos.org" /tmp/proot-output; then + log error "Script did not attempt Nix installation" + cat /tmp/proot-output + exit 1 + fi + log success "Script attempts Nix installation" + + if ! grep -q "Failed to download Nix installer" /tmp/proot-output; then + log error "Script did not handle download failure (no network in sandbox)" + cat /tmp/proot-output + exit 1 + fi + log success "Script handles network failure gracefully" + + log success "Script runs correctly in Arch Linux environment" + + mkdir -p "$out" + ''; +in + (lib.mapAttrs (name: drv: mkTest name drv) testDrvs) // { + arch-integration = archTest; + } diff --git a/test/package/linux-devshell/launch.sh b/test/package/linux-devshell/launch.sh new file mode 100644 index 0000000..a3ec8cc --- /dev/null +++ b/test/package/linux-devshell/launch.sh @@ -0,0 +1,3 @@ +set -eu + +. "$test/run.sh" diff --git a/test/package/linux-devshell/test/standalone-structure.sh b/test/package/linux-devshell/test/standalone-structure.sh new file mode 100644 index 0000000..7032e46 --- /dev/null +++ b/test/package/linux-devshell/test/standalone-structure.sh @@ -0,0 +1,49 @@ +#!/bin/dash +set -eu + +log notice "test case: ${WHITE}syntax check" + +log info "Checking standard package binary exists..." +[ -x "${linuxDevShell}/bin/linux-devshell" ] || { + log error "linux-devshell binary not found or not executable" + exit 1 +} +log success "Standard package binary exists" + +log info "Checking standalone script exists..." +[ -x "${linuxDevShellStandalone}" ] || { + log error "linux-devshell-standalone script not found or not executable" + exit 1 +} +log success "Standalone script exists" + +log info "Checking standalone script has portable shebang..." +head -1 "${linuxDevShellStandalone}" | grep -q '^#!/bin/sh$' || { + log error "Standalone script does not have #!/bin/sh shebang" + exit 1 +} +log success "Standalone script has portable shebang" + +log info "Checking standalone script contains log helpers..." +grep -q 'log_info()' "${linuxDevShellStandalone}" || { + log error "Standalone script missing log_info helper" + exit 1 +} +grep -q 'log_error()' "${linuxDevShellStandalone}" || { + log error "Standalone script missing log_error helper" + exit 1 +} +log success "Standalone script contains log helpers" + +log info "Checking standalone script contains main logic..." +grep -q 'detect_shell()' "${linuxDevShellStandalone}" || { + log error "Standalone script missing detect_shell function" + exit 1 +} +grep -q 'install_nix()' "${linuxDevShellStandalone}" || { + log error "Standalone script missing install_nix function" + exit 1 +} +log success "Standalone script contains main logic" + +log notice "test passed" diff --git a/test/package/linux-devshell/test/syntax-check.sh b/test/package/linux-devshell/test/syntax-check.sh new file mode 100644 index 0000000..ab29405 --- /dev/null +++ b/test/package/linux-devshell/test/syntax-check.sh @@ -0,0 +1,27 @@ +#!/bin/dash +set -eu + +log notice "test case: ${WHITE}syntax check" + +log info "Checking standard package script syntax..." +dash -n "${linuxDevShell}/bin/linux-devshell" || { + log error "Standard package script has syntax errors" + exit 1 +} +log success "Standard package script syntax is valid" + +log info "Checking standalone script syntax..." +dash -n "${linuxDevShellStandalone}" || { + log error "Standalone script has syntax errors" + exit 1 +} +log success "Standalone script syntax is valid" + +log info "Checking script contains Nix detection logic..." +grep -q 'command -v nix' "${linuxDevShellStandalone}" || { + log error "Standalone script missing Nix detection" + exit 1 +} +log success "Script contains Nix detection logic" + +log notice "test passed"