From 1563da75308bc288081661f63c17f6718311a4e9 Mon Sep 17 00:00:00 2001 From: Nikolas Grottendieck <git@nikolasgrottendieck.com> Date: Mon, 26 Sep 2022 17:36:07 +0200 Subject: [PATCH 1/4] test: make test script universally executable - use user supplied bash (e.g. modern, user installed Bash versions on macOS) - skip doc checks only if necessary GNU utils aren't found - macOS may have them prefixed with `g` (installed via Homebrew or MacPorts) - Windows capable of running Bash scripts is most likely using Git Bash with built in GNU utils such as csplit and head - prevent accidental word-splitting by quoting variables - remove unnecessary `cat` --- test | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/test b/test index 39b1fbef..46b8f679 100755 --- a/test +++ b/test @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -euo pipefail SRC=$(find . -name '*.go' -not -path "./vendor/*") @@ -21,7 +21,29 @@ source ./build echo "Running tests" go test ./... -cover +csplit="" +head="" + if [ "$(go env GOOS)" = linux ]; then + csplit="csplit" + head="head" +elif [ "$(go env GOOS)" = darwin ]; then + # macOS has BSD versions of csplit and head that behave differently; + # check whether brew/macports supplied GNU versions exist + if hash gcsplit &> /dev/null; then + csplit="gcsplit" + fi + if hash ghead &> /dev/null; then + head="ghead" + fi +elif [ "$(go env GOOS)" = windows ]; then + # if we find a Bash on Windows we can comparatively safely assume + # Git Bash with GNU utils is being used + csplit="csplit" + head="head" +fi + +if [ -n "${csplit}" ] && [ -n "${head}" ]; then echo "Checking docs" shopt -s nullglob mkdir tmpdocs @@ -32,23 +54,23 @@ if [ "$(go env GOOS)" = linux ]; then for doc in docs/*md do - echo "Checking $doc" + echo "Checking ${doc}" # split each doc into a bunch of tmpfiles then run butane on them - sed -n '/^<!-- butane-config -->/,/^```$/ p' < ${doc} \ - | csplit - '/<!-- butane-config -->/' '{*}' -z --prefix "tmpdocs/config_$(basename ${doc%.*})_" -q + sed -n '/^<!-- butane-config -->/,/^```$/ p' < "${doc}" \ + | ${csplit} - '/<!-- butane-config -->/' '{*}' -z --prefix "tmpdocs/config_$(basename ${doc%.*})_" -q for i in tmpdocs/config_* do - echo "Checking $i" - cat "$i" | tail -n +3 | head -n -1 \ - | ${BIN_PATH}/${NAME} --strict --files-dir tmpdocs/files-dir > /dev/null \ - || (cat -n "$i" && false) + echo "Checking ${i}" + tail -n +3 "${i}" | ${head} -n -1 \ + | "${BIN_PATH}/${NAME}" --strict --files-dir tmpdocs/files-dir > /dev/null \ + || (cat -n "${i}" && false) done rm -f tmpdocs/config_* done else - # Avoid dealing with presence/behavior of csplit - echo "skipping docs check on non-Linux" + # Avoid dealing with presence/behavior of csplit and head + echo "skipping docs check because GNU csplit and head are unavailable" fi echo ok From 65b97b8bb2245af9eba251e74dbb3c84f26ee9f2 Mon Sep 17 00:00:00 2001 From: Nikolas Grottendieck <git@nikolasgrottendieck.com> Date: Mon, 26 Sep 2022 17:43:23 +0200 Subject: [PATCH 2/4] test: align formatting Consistently use 4 spaces for indention instead of mixed 4 or 8. --- test | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test b/test index 46b8f679..69f55966 100755 --- a/test +++ b/test @@ -54,19 +54,19 @@ if [ -n "${csplit}" ] && [ -n "${head}" ]; then for doc in docs/*md do - echo "Checking ${doc}" - # split each doc into a bunch of tmpfiles then run butane on them - sed -n '/^<!-- butane-config -->/,/^```$/ p' < "${doc}" \ - | ${csplit} - '/<!-- butane-config -->/' '{*}' -z --prefix "tmpdocs/config_$(basename ${doc%.*})_" -q + echo "Checking ${doc}" + # split each doc into a bunch of tmpfiles then run butane on them + sed -n '/^<!-- butane-config -->/,/^```$/ p' <"${doc}" \ + | ${csplit} - '/<!-- butane-config -->/' '{*}' -z --prefix "tmpdocs/config_$(basename ${doc%.*})_" -q - for i in tmpdocs/config_* - do - echo "Checking ${i}" - tail -n +3 "${i}" | ${head} -n -1 \ - | "${BIN_PATH}/${NAME}" --strict --files-dir tmpdocs/files-dir > /dev/null \ - || (cat -n "${i}" && false) - done - rm -f tmpdocs/config_* + for i in tmpdocs/config_* + do + echo "Checking ${i}" + tail -n +3 "${i}" | ${head} -n -1 \ + | "${BIN_PATH}/${NAME}" --strict --files-dir tmpdocs/files-dir >/dev/null \ + || (cat -n "${i}" && false) + done + rm -f tmpdocs/config_* done else # Avoid dealing with presence/behavior of csplit and head From 375f4c3be29093e0ccbbb2f2fc5a027f77deb93d Mon Sep 17 00:00:00 2001 From: Nikolas Grottendieck <git@nikolasgrottendieck.com> Date: Mon, 26 Sep 2022 17:44:01 +0200 Subject: [PATCH 3/4] ci: ensure tests are executed on macOS by providing GNU utils via homebrew --- .github/workflows/go.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a8efceb7..a3e4f911 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -29,6 +29,10 @@ jobs: go-version: ${{ matrix.go-version }} - name: Check out repository uses: actions/checkout@v3 + - name: Install GNU utils on macOS + if: runner.os == 'macOS' + shell: bash + run: brew install coreutils - name: Run tests shell: bash run: ./test From f76364372d45d85ab10196f799d1c50e5a9434b4 Mon Sep 17 00:00:00 2001 From: Nikolas Grottendieck <git@nikolasgrottendieck.com> Date: Mon, 26 Sep 2022 17:46:39 +0200 Subject: [PATCH 4/4] docs: update release notes for test & CI changes --- docs/release-notes.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/release-notes.md b/docs/release-notes.md index 5a3051ef..bcb929c1 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -18,6 +18,10 @@ nav_order: 9 ### Misc. changes +- The `test` script now executes all tests on all platforms if the necessary GNU + utilities are available +- CI jobs now include macOS specific instructions to install GNU utilities for + improved test coverage ### Docs changes