From e130ee33a11ae90585e9f9c09b189c3ccfe70632 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Thu, 21 Dec 2023 23:05:14 +0100 Subject: [PATCH] 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 --- .../scripts/fetch-tool.sh | 26 +++++---- .../scripts/run-local.sh | 57 ++++++++++--------- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/pkgs/test/nixpkgs-check-by-name/scripts/fetch-tool.sh b/pkgs/test/nixpkgs-check-by-name/scripts/fetch-tool.sh index fc4df11e747e6..19a48b6fb1fd5 100755 --- a/pkgs/test/nixpkgs-check-by-name/scripts/fetch-tool.sh +++ b/pkgs/test/nixpkgs-check-by-name/scripts/fetch-tool.sh @@ -2,18 +2,20 @@ # Fetches the prebuilt nixpkgs-check-by-name to use from # 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 - echo >&2 "Usage: $0 BASE_BRANCH OUTPUT_PATH" - echo >&2 "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 "Usage: $0 BASE_BRANCH OUTPUT_PATH" + trace "BASE_BRANCH: The base branch to use, e.g. master or release-23.11" + trace "OUTPUT_PATH: The output symlink path for the tool" exit 1 fi baseBranch=$1 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 # Use the release channel for all PRs to release-XX.YY, staging-XX.YY and staging-next-XX.YY preferredChannel=nixos-${BASH_REMATCH[2]} @@ -25,21 +27,21 @@ fi # Check that the channel exists. It doesn't exist for fresh release branches if curl -fSs "https://channels.nixos.org/$preferredChannel"; then channel=$preferredChannel - echo >&2 "$channel" + trace "$channel" else # Fall back to nixos-unstable, makes sense for fresh release branches 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 -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! nixpkgs=$(nix-instantiate --find-file nixpkgs -I nixpkgs=channel:"$channel") -echo >&2 "$nixpkgs" +trace "$nixpkgs" # 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 -realpath >&2 "$output" +realpath "$output" >&2 diff --git a/pkgs/test/nixpkgs-check-by-name/scripts/run-local.sh b/pkgs/test/nixpkgs-check-by-name/scripts/run-local.sh index 060f83469f4df..72d3e8dc3de39 100755 --- a/pkgs/test/nixpkgs-check-by-name/scripts/run-local.sh +++ b/pkgs/test/nixpkgs-check-by-name/scripts/run-local.sh @@ -1,21 +1,25 @@ #!/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() { - echo -n >&2 "Cleaning up.. " - # Run all cleanup commands in inverse order - for (( i=${#cleanup_commands[@]}-1; i>=0; i-- )); do - eval "${cleanup_commands[i]}" - done - echo >&2 "Done" + # Don't exit early if anything fails to cleanup + set +o errexit + + trace -n "Cleaning up.. " + + [[ -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 -tmp=$(mktemp -d) -cleanup_commands+=('rmdir "$tmp"') repo=https://github.com/NixOS/nixpkgs.git @@ -23,9 +27,9 @@ if (( $# != 0 )); then baseBranch=$1 shift else - echo >&2 "Usage: $0 BASE_BRANCH [REPOSITORY]" - echo >&2 "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 "Usage: $0 BASE_BRANCH [REPOSITORY]" + trace "BASE_BRANCH: The base branch to use, e.g. master or release-23.11" + trace "REPOSITORY: The repository to fetch the base branch from, defaults to $repo" exit 1 fi @@ -35,32 +39,29 @@ if (( $# != 0 )); then fi 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 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 -cleanup_commands+=('git worktree remove --force "$tmp/merged"') -echo >&2 "Done" +trace "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" 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" -cleanup_commands+=('git worktree remove --force "$tmp/base"') -echo >&2 "Done" +trace "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" -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" -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"