Get more tests working; make Nix build work

This commit is contained in:
Andrew Dunham 2021-07-19 21:13:18 -04:00
parent 86fa81c411
commit 91d2808bfb
4 changed files with 134 additions and 71 deletions

View file

@ -7,9 +7,11 @@ lint:
test: lint test-dash test-bash test-ksh
# Not included by default
test-ash:
@cd test && ash ./lib_test.sh
@cd test && ash ./run_test.sh
@echo "Path is $$PATH"
@cd test && busybox ash ./lib_test.sh
@cd test && busybox ash ./run_test.sh
test-dash:
@cd test && dash ./lib_test.sh
@ -23,4 +25,4 @@ test-ksh:
@cd test && ksh ./lib_test.sh
@cd test && ksh ./run_test.sh
.PHONY: lint test test-dash test-bash
.PHONY: lint test test-ash test-dash test-bash test-ksh

58
apply
View file

@ -3,65 +3,79 @@
set -e
set -u
function usage() {
usage() {
echo "usage: $(basename "$0") [-v] [-p] <host> [<host>...]"
exit 1
}
function host_targets() {
host_targets() {
local host="$*"
cat < "${APPLY_ROOT}"/"$host" | tr '\n' ' '
}
function push_each() {
rootpath() {
if [[ "${#BASH_SOURCE}" -gt 0 ]]; then
cd -- "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd
else
cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd
fi
}
push_each() {
local vflag="$1"
local parallelize="$2"
shift
shift
if [[ -n $parallelize ]]; then
# shellcheck disable=SC2086
parallel -j10 --progress --colsep ' ' ./push $vflag
parallel -j10 --progress --colsep ' ' "$(rootpath)/push" $vflag
else
# shellcheck disable=SC2086
parallel -j1 -u --colsep ' ' ./push $vflag
parallel -j1 -u --colsep ' ' "$(rootpath)/push" $vflag
fi
}
if [[ -z "${APPLY_ROOT:-}" ]]; then
main() {
# The default path to config is the current working directory.
if [[ -z "${APPLY_ROOT:-}" ]]; then
APPLY_ROOT="."
fi
fi
if [[ $# -lt 1 ]]; then
if [[ $# -lt 1 ]]; then
usage
fi
fi
if [[ "$1" == "-v" ]]; then
if [[ "$1" == "-v" ]]; then
vflag='-v'
shift
else
else
vflag=''
fi
fi
if [[ "$1" == "-p" ]]; then
if [[ "$1" == "-p" ]]; then
pflag='-p'
shift
else
else
pflag=''
fi
fi
if [[ $# -lt 1 ]]; then
if [[ $# -lt 1 ]]; then
usage
fi
fi
hosts=()
while [[ $# -gt 0 ]]; do
hosts=()
while [[ $# -gt 0 ]]; do
hosts+=("$1")
shift
done
done
for host in ${hosts[*]}; do
for host in ${hosts[*]}; do
target=${host#*/}
# shellcheck disable=SC2046
echo $(host_targets "$host") root@"$target"
done | push_each "$vflag" "$pflag"
done | push_each "$vflag" "$pflag"
}
main "$@"

105
flake.nix
View file

@ -6,8 +6,77 @@
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
let pkgs = nixpkgs.legacyPackages.${system}; in
rec {
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) stdenv lib;
in rec {
defaultPackage = self.packages."${system}".apply;
packages.apply = stdenv.mkDerivation {
pname = "apply";
version = "2021-07-16";
src = ./.;
nativeBuildInputs = with pkgs; [ makeWrapper ];
# Overridden in tests
doCheck = false;
checkInputs = with pkgs; [
shellcheck
gnumake
bash
dash
ksh
# busybox as an input will break things
];
# Test with each shell
checkPhase = ''
cd "$src"
make test
(
export PATH="${lib.makeBinPath (with pkgs; [ busybox gnumake shellcheck ])}"
make test-ash
)
'';
# Don't patch shebangs on everything or it'll get the 'lib' and 'run'
# scripts, which we need to be able to copy elsewhere.
dontPatchShebangs = true;
installPhase = ''
mkdir -p $out/bin/
cp ./{apply,lib,push,run} $out/bin/
chmod +x $out/bin/*
'';
postFixup = ''
for f in $out/bin/{apply,push}; do
patchShebangs "$f"
wrapProgram "$f" \
--suffix PATH ":" "${lib.makeBinPath (with pkgs; [ coreutils parallel openssh ])}"
done
'';
meta = with lib; {
homepage = "https://github.com/andrew-d/apply";
description = "TODO";
maintainers = with maintainers; [ andrew-d ];
license = licenses.bsd3;
platforms = platforms.unix;
};
};
defaultApp = self.apps."${system}".apply;
apps.apply = {
type = "app";
program = "${self.defaultPackage."${system}"}/bin/apply";
};
devShell = pkgs.mkShell {
buildInputs = with pkgs; [
# Shells
@ -21,36 +90,8 @@
];
};
checks = let
commonDeps = with pkgs; [ gnumake shellcheck ];
in {
bash = pkgs.runCommand "lint" {
src = ./.;
nativeBuildInputs = commonDeps ++ (with pkgs; [ bash ]);
} ''
export SHELL="${pkgs.bash}/bin/bash"
make -C "$src" test-bash
touch "$out"
'';
dash = pkgs.runCommand "lint" {
src = ./.;
nativeBuildInputs = commonDeps ++ (with pkgs; [ dash ]);
} ''
export SHELL="${pkgs.dash}/bin/dash"
make -C "$src" test-dash
touch "$out"
'';
busybox = pkgs.runCommand "lint" {
src = ./.;
nativeBuildInputs = commonDeps ++ (with pkgs; [ busybox ]);
} ''
export SHELL="${pkgs.busybox}/bin/ash"
make -C "$src" test-ash
touch "$out"
'';
checks = {
apply = self.defaultPackage.${system}.overrideAttrs (super: { doCheck = true; });
};
}
);

View file

@ -96,6 +96,12 @@ testEpochseconds() {
}
testUserHomedir() {
# TODO(andrew-d): this doesn't work in the Nix sandbox; detect this and
# skip the test for now.
if [ "$HOME" = "/homeless-shelter" ]; then
return
fi
assertEquals "$HOME" "$(user_homedir "$USER")"
}