pkgs/test/nixpkgs-check-by-name/scripts: Various improvements

- trace function, avoids littering `echo >&2` all throughout
- Avoid `eval`, remove unneeded shellcheck

Co-Authored-By: Victor Engmark <victor@engmark.name>
This commit is contained in:
Silvan Mosberger 2023-12-21 23:05:14 +01:00
parent f882df781c
commit e130ee33a1
2 changed files with 43 additions and 40 deletions

View File

@ -2,18 +2,20 @@
# Fetches the prebuilt nixpkgs-check-by-name to use from # Fetches the prebuilt nixpkgs-check-by-name to use from
# the NixOS channel corresponding to the given base branch # the NixOS channel corresponding to the given base branch
set -euo pipefail set -o pipefail -o errexit -o nounset
trace() { echo >&2 "$@"; }
if (( $# < 2 )); then if (( $# < 2 )); then
echo >&2 "Usage: $0 BASE_BRANCH OUTPUT_PATH" trace "Usage: $0 BASE_BRANCH OUTPUT_PATH"
echo >&2 "BASE_BRANCH: The base branch to use, e.g. master or release-23.11" trace "BASE_BRANCH: The base branch to use, e.g. master or release-23.11"
echo >&2 "OUTPUT_PATH: The output symlink path for the tool" trace "OUTPUT_PATH: The output symlink path for the tool"
exit 1 exit 1
fi fi
baseBranch=$1 baseBranch=$1
output=$2 output=$2
echo >&2 -n "Determining the channel to use for PR base branch $baseBranch.. " trace -n "Determining the channel to use for PR base branch $baseBranch.. "
if [[ "$baseBranch" =~ ^(release|staging|staging-next)-([0-9][0-9]\.[0-9][0-9])$ ]]; then if [[ "$baseBranch" =~ ^(release|staging|staging-next)-([0-9][0-9]\.[0-9][0-9])$ ]]; then
# Use the release channel for all PRs to release-XX.YY, staging-XX.YY and staging-next-XX.YY # Use the release channel for all PRs to release-XX.YY, staging-XX.YY and staging-next-XX.YY
preferredChannel=nixos-${BASH_REMATCH[2]} preferredChannel=nixos-${BASH_REMATCH[2]}
@ -25,21 +27,21 @@ fi
# Check that the channel exists. It doesn't exist for fresh release branches # Check that the channel exists. It doesn't exist for fresh release branches
if curl -fSs "https://channels.nixos.org/$preferredChannel"; then if curl -fSs "https://channels.nixos.org/$preferredChannel"; then
channel=$preferredChannel channel=$preferredChannel
echo >&2 "$channel" trace "$channel"
else else
# Fall back to nixos-unstable, makes sense for fresh release branches # Fall back to nixos-unstable, makes sense for fresh release branches
channel=nixos-unstable channel=nixos-unstable
echo >&2 -e "\e[33mWarning: Preferred channel $preferredChannel could not be fetched, using fallback: $channel\e[0m" trace -e "\e[33mWarning: Preferred channel $preferredChannel could not be fetched, using fallback: $channel\e[0m"
fi fi
echo >&2 -n "Fetching latest version of channel $channel.. " trace -n "Fetching latest version of channel $channel.. "
# This is probably the easiest way to get Nix to output the path to a downloaded channel! # This is probably the easiest way to get Nix to output the path to a downloaded channel!
nixpkgs=$(nix-instantiate --find-file nixpkgs -I nixpkgs=channel:"$channel") nixpkgs=$(nix-instantiate --find-file nixpkgs -I nixpkgs=channel:"$channel")
echo >&2 "$nixpkgs" trace "$nixpkgs"
# This file only exists in channels # This file only exists in channels
echo >&2 -e "Git revision of channel $channel is \e[34m$(<"$nixpkgs/.git-revision")\e[0m" trace -e "Git revision of channel $channel is \e[34m$(<"$nixpkgs/.git-revision")\e[0m"
echo >&2 -n "Fetching the prebuilt version of nixpkgs-check-by-name.. " trace -n "Fetching the prebuilt version of nixpkgs-check-by-name.. "
nix-build -o "$output" "$nixpkgs" -A tests.nixpkgs-check-by-name -j 0 >/dev/null nix-build -o "$output" "$nixpkgs" -A tests.nixpkgs-check-by-name -j 0 >/dev/null
realpath >&2 "$output" realpath "$output" >&2

View File

@ -1,21 +1,25 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck disable=SC2016
set -euo pipefail set -o pipefail -o errexit -o nounset
cleanup_commands=() trace() { echo >&2 "$@"; }
tmp=$(mktemp -d)
cleanup() { cleanup() {
echo -n >&2 "Cleaning up.. " # Don't exit early if anything fails to cleanup
# Run all cleanup commands in inverse order set +o errexit
for (( i=${#cleanup_commands[@]}-1; i>=0; i-- )); do
eval "${cleanup_commands[i]}" trace -n "Cleaning up.. "
done
echo >&2 "Done" [[ -e "$tmp/base" ]] && git worktree remove --force "$tmp/base"
[[ -e "$tmp/merged" ]] && git worktree remove --force "$tmp/merged"
rm -rf "$tmp"
trace "Done"
} }
trap cleanup exit trap cleanup exit
tmp=$(mktemp -d)
cleanup_commands+=('rmdir "$tmp"')
repo=https://github.com/NixOS/nixpkgs.git repo=https://github.com/NixOS/nixpkgs.git
@ -23,9 +27,9 @@ if (( $# != 0 )); then
baseBranch=$1 baseBranch=$1
shift shift
else else
echo >&2 "Usage: $0 BASE_BRANCH [REPOSITORY]" trace "Usage: $0 BASE_BRANCH [REPOSITORY]"
echo >&2 "BASE_BRANCH: The base branch to use, e.g. master or release-23.11" trace "BASE_BRANCH: The base branch to use, e.g. master or release-23.11"
echo >&2 "REPOSITORY: The repository to fetch the base branch from, defaults to $repo" trace "REPOSITORY: The repository to fetch the base branch from, defaults to $repo"
exit 1 exit 1
fi fi
@ -35,32 +39,29 @@ if (( $# != 0 )); then
fi fi
if [[ -n "$(git status --porcelain)" ]]; then if [[ -n "$(git status --porcelain)" ]]; then
echo >&2 -e "\e[33mWarning: Dirty tree, uncommitted changes won't be taken into account\e[0m" trace -e "\e[33mWarning: Dirty tree, uncommitted changes won't be taken into account\e[0m"
fi fi
headSha=$(git rev-parse HEAD) headSha=$(git rev-parse HEAD)
echo >&2 -e "Using HEAD commit \e[34m$headSha\e[0m" trace -e "Using HEAD commit \e[34m$headSha\e[0m"
echo >&2 -n "Creating Git worktree for the HEAD commit in $tmp/merged.. " trace -n "Creating Git worktree for the HEAD commit in $tmp/merged.. "
git worktree add --detach -q "$tmp/merged" HEAD git worktree add --detach -q "$tmp/merged" HEAD
cleanup_commands+=('git worktree remove --force "$tmp/merged"') trace "Done"
echo >&2 "Done"
echo >&2 -n "Fetching base branch $baseBranch to compare against.. " trace -n "Fetching base branch $baseBranch to compare against.. "
git fetch -q "$repo" refs/heads/"$baseBranch" git fetch -q "$repo" refs/heads/"$baseBranch"
baseSha=$(git rev-parse FETCH_HEAD) baseSha=$(git rev-parse FETCH_HEAD)
echo >&2 -e "\e[34m$baseSha\e[0m" trace -e "\e[34m$baseSha\e[0m"
echo >&2 -n "Creating Git worktree for the base branch in $tmp/base.. " trace -n "Creating Git worktree for the base branch in $tmp/base.. "
git worktree add -q "$tmp/base" "$baseSha" git worktree add -q "$tmp/base" "$baseSha"
cleanup_commands+=('git worktree remove --force "$tmp/base"') trace "Done"
echo >&2 "Done"
echo >&2 -n "Merging base branch into the HEAD commit in $tmp/merged.. " trace -n "Merging base branch into the HEAD commit in $tmp/merged.. "
git -C "$tmp/merged" merge -q --no-edit "$baseSha" git -C "$tmp/merged" merge -q --no-edit "$baseSha"
echo >&2 -e "\e[34m$(git -C "$tmp/merged" rev-parse HEAD)\e[0m" trace -e "\e[34m$(git -C "$tmp/merged" rev-parse HEAD)\e[0m"
"$tmp/merged/pkgs/test/nixpkgs-check-by-name/scripts/fetch-tool.sh" "$baseBranch" "$tmp/tool" "$tmp/merged/pkgs/test/nixpkgs-check-by-name/scripts/fetch-tool.sh" "$baseBranch" "$tmp/tool"
cleanup_commands+=('rm "$tmp/tool"')
echo >&2 "Running nixpkgs-check-by-name.." trace "Running nixpkgs-check-by-name.."
"$tmp/tool/bin/nixpkgs-check-by-name" --base "$tmp/base" "$tmp/merged" "$tmp/tool/bin/nixpkgs-check-by-name" --base "$tmp/base" "$tmp/merged"