From 96c86715a221cb2238981c05661f07dfeccb6b90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20N=C3=BCtzi?= Date: Sun, 21 Apr 2024 14:13:02 +0200 Subject: [PATCH] fix: Make disabled tests run again (#163) - Enable disabled tests :anchor: - Split windows tests and disable some (todo, because they hang?) :anchor: - Remove obsolete code in test :anchor: --- .circleci/config.yml | 10 ++- githooks/cmd/installer/installer.go | 26 +++++-- githooks/scripts/build.sh | 2 - scripts/install.sh | 15 ++-- tests/exec-tests.sh | 2 + tests/general.sh | 2 +- tests/setup-githooks.sh | 30 ++++++-- ...sabled-step-500.sh => disable-step-500.sh} | 6 +- tests/steps/disabled-step-137.sh | 43 ------------ tests/steps/step-137.sh | 36 ++++++++++ tests/steps/step-147.sh | 68 +++++++++++++++++++ .../{disabled-step-501.sh => step-501.sh} | 31 ++++++--- tests/test-windows.sh | 4 +- 13 files changed, 196 insertions(+), 79 deletions(-) rename tests/steps/{disabled-step-500.sh => disable-step-500.sh} (76%) delete mode 100755 tests/steps/disabled-step-137.sh create mode 100755 tests/steps/step-137.sh create mode 100755 tests/steps/step-147.sh rename tests/steps/{disabled-step-501.sh => step-501.sh} (64%) diff --git a/.circleci/config.yml b/.circleci/config.yml index 66fddb28..837f32b4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,14 +23,19 @@ jobs: description: "The test script name" default: "test-alpine" type: string + seq: + description: "The sequence of tests" + default: "" + type: string executor: + size: medium name: win/server-2022 - version: 2023.04.1 + version: 2023.11.1 steps: - checkout - run: no_output_timeout: 30m - command: "& 'C:/Program Files/Git/bin/sh.exe' tests/<>.sh" + command: "& 'C:/Program Files/Git/bin/bash.exe' -c 'tests/<>.sh --seq <>'" workflows: version: 2 @@ -70,4 +75,5 @@ workflows: matrix: parameters: test: ["test-windows"] + seq: ["{001..040}", "{041..080}", "{081..120}", "{120..200}"] filters: *filters diff --git a/githooks/cmd/installer/installer.go b/githooks/cmd/installer/installer.go index 29d74d24..9b78fad6 100644 --- a/githooks/cmd/installer/installer.go +++ b/githooks/cmd/installer/installer.go @@ -95,10 +95,15 @@ func defineArguments(cmd *cobra.Command, vi *viper.Viper) { "non-interactive", false, "Run the installation non-interactively\n"+ "without showing prompts.") - cmd.PersistentFlags().Bool( - "update", false, - "Install and update directly to the latest\n"+ - "possible tag on the clone branch.") + + if !cm.PackageManagerEnabled { + // Only allow this when not using a package manager. + cmd.PersistentFlags().Bool( + "update", false, + "Install and update directly to the latest\n"+ + "possible tag on the clone branch.") + } + cmd.PersistentFlags().Bool( "skip-install-into-existing", false, "Skip installation into existing repositories\n"+ @@ -171,6 +176,9 @@ func defineArguments(cmd *cobra.Command, vi *viper.Viper) { "use-pre-release", false, "When fetching the latest installer, also consider pre-release versions.") + // The following binds all flags to `viper` config YAML over `--config`. + // So anything specified in `--config` gets transferred to the flags, that we only can use the + // variables associated in the flags. cm.AssertNoErrorPanic( vi.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))) cm.AssertNoErrorPanic( @@ -179,8 +187,12 @@ func defineArguments(cmd *cobra.Command, vi *viper.Viper) { vi.BindPFlag("dryRun", cmd.PersistentFlags().Lookup("dry-run"))) cm.AssertNoErrorPanic( vi.BindPFlag("nonInteractive", cmd.PersistentFlags().Lookup("non-interactive"))) - cm.AssertNoErrorPanic( - vi.BindPFlag("update", cmd.PersistentFlags().Lookup("update"))) + + if !cm.PackageManagerEnabled { + cm.AssertNoErrorPanic( + vi.BindPFlag("update", cmd.PersistentFlags().Lookup("update"))) + } + cm.AssertNoErrorPanic( vi.BindPFlag("skipInstallIntoExisting", cmd.PersistentFlags().Lookup("skip-install-into-existing"))) cm.AssertNoErrorPanic( @@ -207,6 +219,7 @@ func defineArguments(cmd *cobra.Command, vi *viper.Viper) { vi.BindPFlag("hooksDir", cmd.PersistentFlags().Lookup("hooks-dir"))) cm.AssertNoErrorPanic( vi.BindPFlag("hooksDirUseTemplateDir", cmd.PersistentFlags().Lookup("hooks-dir-use-template-dir"))) + if !cm.PackageManagerEnabled { cm.AssertNoErrorPanic( vi.BindPFlag("gitConfigNoAbsPath", cmd.PersistentFlags().Lookup("git-config-no-abs-path"))) @@ -780,7 +793,6 @@ func setupGithooksExecutables(log cm.ILogContext, installDir string, noAbsPath b if cm.PackageManagerEnabled || noAbsPath { cli = hooks.GetCLIExecutable("").Cmd - log.InfoF("%v", cli) runner = hooks.GetRunnerExecutable("") dialog = hooks.GetDialogExecutable("") diff --git a/githooks/scripts/build.sh b/githooks/scripts/build.sh index 3e455579..502bbfd0 100755 --- a/githooks/scripts/build.sh +++ b/githooks/scripts/build.sh @@ -31,8 +31,6 @@ parse_args() { BUILD_TAGS="$p" elif [ "$p" = "--coverage" ]; then BUILD_COVERAGE="true" - elif [ "$p" = "--benchmark" ]; then - DEBUG_TAG="${DEBUG_TAG}${DEBUG_TAG+,}benchmark,download_mock" elif [ "$p" = "--prod" ]; then DEBUG_TAG="" LD_FLAGS+=("-ldflags" "-s -w") # strip debug information diff --git a/scripts/install.sh b/scripts/install.sh index 5c8364c2..772ec79e 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -271,11 +271,6 @@ trap clean_up EXIT githooks="$tempDir/githooks" mkdir -p "$githooks" -cliExe="githooks-cli" -if [ "$os" = "windows" ]; then - cliExe="$cliExe.exe" -fi - cd "$tempDir" echo -e "Downloading '$checksumFileURL'..." @@ -304,6 +299,16 @@ case "$url" in ;; esac +cliExe="cli" +if [ ! -f "githooks/cli" ]; then + # Version 3 has new names + cliExe="githooks-cli" +fi + +if [ "$os" = "windows" ]; then + cliExe="$cliExe.exe" +fi + if [ "$unInstall" = "true" ]; then "githooks/$cliExe" uninstaller "${installerArgs[@]}" else diff --git a/tests/exec-tests.sh b/tests/exec-tests.sh index ebae2a7a..4722abee 100755 --- a/tests/exec-tests.sh +++ b/tests/exec-tests.sh @@ -44,6 +44,7 @@ FROM githooks:$IMAGE_TYPE-base ARG DOCKER_GROUP_ID ENV DOCKER_RUNNING=true +ENV GH_SCRIPTS="/var/lib/githooks-scripts" ENV GH_TESTS="/var/lib/githooks-tests" ENV GH_TEST_TMP="/tmp/githooks" ENV GH_TEST_REPO="/var/lib/githooks" @@ -63,6 +64,7 @@ RUN bash "\$GH_TESTS/setup-githooks.sh" # Add all tests ADD tests "\$GH_TESTS" +ADD scripts "\$GH_SCRIPTS" # Git-Core folder must be existing. RUN [ -d "\$GH_TEST_GIT_CORE/templates/hooks" ] diff --git a/tests/general.sh b/tests/general.sh index ae13ea6a..d0b35c2a 100755 --- a/tests/general.sh +++ b/tests/general.sh @@ -23,7 +23,7 @@ function check_paths_are_equal() { local a a=$(echo "$1" | wrap_windows_paths) local b - b=$(echo "$1" | wrap_windows_paths) + b=$(echo "$2" | wrap_windows_paths) shift 2 [ "$a" = "$b" ] || { diff --git a/tests/setup-githooks.sh b/tests/setup-githooks.sh index e1f19eba..89b23d8c 100755 --- a/tests/setup-githooks.sh +++ b/tests/setup-githooks.sh @@ -24,20 +24,36 @@ echo "Make test Git repo to clone from ..." && git add . >/dev/null 2>&1 && git commit -a -m "Before build" >/dev/null 2>&1 || exit 1 -# Make a build which exists on the server on branch "test-download" +# Make a build which exists on the server on branch test-download. cd "$GH_TEST_REPO/githooks" && git checkout -b "test-download" && git commit -a --allow-empty \ - -m "Build version 2.1.0 for download test over Github" >/dev/null 2>&1 && - git tag "v2.1.0" && + -m "Build version 3.0.0 for download test over Github" >/dev/null 2>&1 && + git tag "v3.0.0" && ./scripts/clean.sh && - ./scripts/build.sh "${BUILD_ADD_ARGS[@]}" --prod --benchmark && + ./scripts/build.sh "${BUILD_ADD_ARGS[@]}" --prod && ./bin/githooks-cli "${CLI_ADD_ARGS[@]}" --version || exit 1 -echo "Commit build v2.1.0 to repo (for test download) ..." && +echo "Commit build v3.0.0 to repo (for test download) ..." && cd "$GH_TEST_REPO" && git add . >/dev/null 2>&1 && - git commit -a --allow-empty -m "Version 2.1.0" >/dev/null 2>&1 && - git tag -f "v2.1.0" || exit 1 + git commit -a --allow-empty -m "Version 3.0.0" >/dev/null 2>&1 && + git tag -f "v3.0.0" || exit 1 + +# Make a build is production, +# has no direct update capability in installer +# Should not download the binary +cd "$GH_TEST_REPO/githooks" && + git checkout -b "test-package-manager" && + git commit -a --allow-empty \ + -m "Build version 3.1.0 for benchmark/package-manager test" >/dev/null 2>&1 && + git tag "v3.1.0" && + ./scripts/clean.sh && + ./scripts/build.sh "${BUILD_ADD_ARGS[@]}" --prod --build-tags "package_manager_enabled,download_mock,benchmark" && + echo "Commit build v3.1.0 to repo (for benchmark/package-manager test) ..." && + cd "$GH_TEST_REPO" && + git add . >/dev/null 2>&1 && + git commit -a --allow-empty -m "Version 3.1.0" >/dev/null 2>&1 && + git tag -f "v3.1.0" || exit 1 # Setup server repository from which we install updates # branch: main diff --git a/tests/steps/disabled-step-500.sh b/tests/steps/disable-step-500.sh similarity index 76% rename from tests/steps/disabled-step-500.sh rename to tests/steps/disable-step-500.sh index 15608e7e..d516c050 100755 --- a/tests/steps/disabled-step-500.sh +++ b/tests/steps/disable-step-500.sh @@ -9,12 +9,14 @@ TEST_DIR=$(cd "$(dirname "$0")/.." && pwd) init_step -git -C "$GH_TEST_REPO" reset --hard v2.1.0 >/dev/null 2>&1 || exit 1 +git -C "$GH_TEST_REPO" reset --hard v3.0.0 >/dev/null 2>&1 || exit 1 # We do not want to inject special flags into the downloaded executable because it has not been build with coverage # therefore -> use `GH_DEPLOY_SOURCE_IS_PROD` in `executables-coverage.go` GH_DEPLOY_SOURCE_IS_PROD=true \ - "$GH_TEST_BIN/githooks-cli" installer --clone-url "https://github.com/gabyx/Githooks.git" --clone-branch "main" || + "$GH_TEST_BIN/githooks-cli" installer \ + --clone-url "https://github.com/gabyx/Githooks.git" \ + --clone-branch "main" \ exit 1 "$GH_INSTALL_BIN_DIR/githooks-cli" uninstaller diff --git a/tests/steps/disabled-step-137.sh b/tests/steps/disabled-step-137.sh deleted file mode 100755 index e5995454..00000000 --- a/tests/steps/disabled-step-137.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env bash -# Test: -# Run install.sh script from the main branch. -set -u - -TEST_DIR=$(cd "$(dirname "$0")/.." && pwd) -# shellcheck disable=SC1091 - -. "$TEST_DIR/general.sh" - -init_step - -if [ -n "${GH_COVERAGE_DIR:-}" ]; then - echo "Test cannot run for coverage." - exit 249 -fi - -ref="${CIRCLE_SHA1:-main}" -# Install with script. -curl -sL "https://raw.githubusercontent.com/gabyx/Githooks/$ref/scripts/install.sh" | bash -s -- -- \ - --centralized - -mkdir -p "$GH_TEST_TMP/test137" && - cd "$GH_TEST_TMP/test137" && - git init && - install_hooks_if_not_centralized || exit 1 - -if [ -z "$(git config core.hooksPath)" ]; then - echo "Git core.hooskPath is not set but should." - exit 1 -fi - -if [ -n "$(git config init.templateDir)" ]; then - echo "Git init.templateDir is set but should not." - exit 1 -fi - -if grep -Rq 'github.com/gabyx/githooks' .git/hooks; then - echo "Hooks should not have been installed." - exit 1 -fi - -"$GH_INSTALL_BIN_DIR/githooks-cli" uninstaller || exit 1 diff --git a/tests/steps/step-137.sh b/tests/steps/step-137.sh new file mode 100755 index 00000000..8356ba82 --- /dev/null +++ b/tests/steps/step-137.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# Test: +# Run install.sh script. +# shellcheck disable=SC2015 + +set -u + +TEST_DIR=$(cd "$(dirname "$0")/.." && pwd) +# shellcheck disable=SC1091 + +. "$TEST_DIR/general.sh" + +init_step + +if [ -n "${GH_COVERAGE_DIR:-}" ]; then + echo "Test cannot run for coverage." + exit 249 +fi + +# Install with current script the version 2.10.0 on the `main` branch. +"$GH_SCRIPTS/install.sh" --version 2.10.0 || { + echo "Could not download install.sh from 'main' and install." + exit 1 +} + +# Enable this once pre-release is out on main. +# # Update to version 3 and greater, which should fail due to guard. +# OUT=$("$GH_INSTALL_BIN_DIR/cli" update --use-pre-release --yes) +# # shellcheck disable=SC2181 +# if [ $? -eq 0 ] || ! echo "$OUT" | grep -iE "Too much changed. Please uninstall this version"; then +# echo "Install should fail because update from v2 to v3 is guarded." +# exit 1 +# fi + +# Uninstall right away again. +"$GH_INSTALL_BIN_DIR"/cli uninstaller || exit 1 diff --git a/tests/steps/step-147.sh b/tests/steps/step-147.sh new file mode 100755 index 00000000..32dba4ee --- /dev/null +++ b/tests/steps/step-147.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +# Test: +# Test package-manager enabled build with run-wrappers not using githooks.runner + +TEST_DIR=$(cd "$(dirname "$0")/.." && pwd) +# shellcheck disable=SC1091 +. "$TEST_DIR/general.sh" + +if [ -n "$GH_ON_WINDOWS" ]; then + echo "On windows somehow this test gets stuck (?)." + exit 249 +fi + +init_step + +# Use the 3.1.0 prod build with package-manager enabled and download mocked. +git -C "$GH_TEST_REPO" reset --hard v3.1.0 >/dev/null 2>&1 || exit 1 + +# run the default install +"$GH_TEST_BIN/githooks-cli" installer \ + "${EXTRA_INSTALL_ARGS[@]}" \ + --clone-url "file://$GH_TEST_REPO" \ + --clone-branch "test-package-manager" || exit 1 + +# Test run-wrappers with pure binaries in the path. +# Put binaries into the path to find them. +export PATH="$GH_TEST_BIN:$PATH" +if [ -z "$GH_COVERAGE_DIR" ]; then + # Coverage build will not run this because its wrapped... + githooks-cli --version || { + echo "! Binaries not in path." + exit 1 + } +fi + +# Overwrite runner, it should not be needed. +git config --global --unset githooks.runner + +# We need this CLI at the right place for further tests. +[ ! -f ~/.githooks/bin/githooks-cli ] || { + echo "! githooks-cli should not exist in default location" + exit 1 +} +# Install it into the default location for the test functions... +mkdir ~/.githooks/bin && + cp "$(which githooks-cli)" ~/.githooks/bin/ || exit 1 + +# Test the the runner works. +mkdir -p "$GH_TEST_TMP/test147" && + cd "$GH_TEST_TMP/test147" && + mkdir -p .githooks/pre-commit && + echo "echo 'Hook-1' >> '$GH_TEST_TMP/test'" >.githooks/pre-commit/test1 && + git init && + install_hooks_if_not_centralized || exit 1 + +if ! is_centralized_tests; then + check_local_install +else + check_centralized_install +fi + +git hooks trust +git commit --allow-empty -m "Test hook" || exit 1 + +if [ ! -f "$GH_TEST_TMP/test" ]; then + echo "! Hook did not run." + exit 1 +fi diff --git a/tests/steps/disabled-step-501.sh b/tests/steps/step-501.sh similarity index 64% rename from tests/steps/disabled-step-501.sh rename to tests/steps/step-501.sh index 1d0ba79d..c708b939 100755 --- a/tests/steps/disabled-step-501.sh +++ b/tests/steps/step-501.sh @@ -15,20 +15,33 @@ fi accept_all_trust_prompts || exit 1 -# Misuse the 2.1.0 prod build with `benchmark` build to test. -# but forbid update during commits otherwise the runner is overwritten. -git -C "$GH_TEST_REPO" reset --hard v2.1.0 >/dev/null 2>&1 || exit 1 +# Misuse the 3.1.0 prod build with package-manager enabled and download mocked. +git -C "$GH_TEST_REPO" reset --hard v3.1.0 >/dev/null 2>&1 || exit 1 # run the default install -"$GH_TEST_BIN/githooks-cli" installer || exit 1 - -# Overwrite runner. -git config --global githooks.updateCheckEnabled false -git config --global githooks.runner "$GH_TEST_BIN/githooks-runner" +"$GH_TEST_BIN/githooks-cli" installer \ + "${EXTRA_INSTALL_ARGS[@]}" \ + --clone-url "file://$GH_TEST_REPO" \ + --clone-branch "test-package-manager" || exit 1 + +# Test run-wrappers with pure binaries in the path. +# Put binaries into the path to find them. +export PATH="$GH_TEST_BIN:$PATH" +# Install CLI it into the default location for the test functions... +mkdir ~/.githooks/bin && + cp "$(which githooks-cli)" ~/.githooks/bin/ || exit 1 mkdir -p "$GH_TEST_TMP/test501" && cd "$GH_TEST_TMP/test501" && - git init || exit 1 + git init && + install_hooks_if_not_centralized || + exit 1 + +if ! is_centralized_tests; then + check_local_install +else + check_centralized_install +fi function run_commits() { for i in {1..30}; do diff --git a/tests/test-windows.sh b/tests/test-windows.sh index a76b47c1..876047fd 100755 --- a/tests/test-windows.sh +++ b/tests/test-windows.sh @@ -59,6 +59,7 @@ RUN $url = ('https://go.dev/dl/go{0}.windows-amd64.zip' -f $env:GOLANG_VERSION); Write-Host 'Complete.'; ENV DOCKER_RUNNING=true +ENV GH_SCRIPTS="c:/githooks-tests/scripts" ENV GH_TESTS="c:/githooks-tests/tests" ENV GH_TEST_TMP="c:/githooks-tests/tmp" ENV GH_TEST_REPO="c:/githooks-tests/githooks" @@ -72,9 +73,10 @@ ADD .githooks/README.md "$GH_TEST_REPO/.githooks/README.md" ADD examples "$GH_TEST_REPO/examples" ADD tests/setup-githooks.sh "$GH_TESTS/" -RUN & "'C:/Program Files/Git/bin/sh.exe'" "C:/githooks-tests/tests/setup-githooks.sh" +RUN & "'C:/Program Files/Git/bin/bash.exe'" "C:/githooks-tests/tests/setup-githooks.sh" ADD tests "$GH_TESTS" +ADD scripts "$GH_SCRIPTS" WORKDIR C:/githooks-tests/tests