Skip to content

Commit

Permalink
fix: improve lint and test scripts (#1607)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall authored Sep 10, 2023
1 parent 0e1c90e commit b320803
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
5 changes: 3 additions & 2 deletions scripts/install_dependencies.bash
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if [ -z "$RUNNER_OS" ]; then
exit 1
fi

### Set environment variables for tracking versions
### Set variables for tracking versions
# Elvish
elvish_semver="v0.19.2"
# Fish
Expand Down Expand Up @@ -73,5 +73,6 @@ fi

### Install bats-core
printf "%s\n" "Installing bats-core"
git clone --depth 1 --branch "v$(grep -Eo "^\\s*bats\\s*.*$" ".tool-versions" | cut -d ' ' -f2-)" https://github.com/bats-core/bats-core.git "$HOME/bats-core"
bats_version=$(grep -Eo "^\\s*bats\\s*.*$" ".tool-versions" | cut -d ' ' -f2-)
git clone --depth 1 --branch "v$bats_version" https://github.com/bats-core/bats-core.git "$HOME/bats-core"
echo "$HOME/bats-core/bin" >>"$GITHUB_PATH"
50 changes: 30 additions & 20 deletions scripts/lint.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
set -euo pipefail
IFS=$'\n\t'

print.info() {
printf '[INFO] %s\n' "$*"
}

print.error() {
printf '[ERROR] %s\n' "$*" >&2
}

usage() {
printf "%s\n" "Lint script for the asdf codebase. Must be executed from the"
printf "%s\n\n" "repository root directory."
Expand All @@ -21,7 +29,7 @@ run_shfmt_stylecheck() {
shfmt_flag="--diff"
fi

printf "%s\n" "[INFO] Checking .bash with shfmt"
print.info "Checking .bash with shfmt"
shfmt --language-dialect bash --indent 2 "${shfmt_flag}" \
completions/*.bash \
bin/asdf \
Expand All @@ -35,17 +43,17 @@ run_shfmt_stylecheck() {
test/fixtures/dummy_legacy_plugin/bin/* \
test/fixtures/dummy_plugin/bin/*

printf "%s\n" "[INFO] Checking .bats with shfmt"
print.info "Checking .bats with shfmt"
shfmt --language-dialect bats --indent 2 "${shfmt_flag}" \
test/*.bats
}

run_shellcheck_linter() {
printf "%s\n" "[INFO] Checking .sh files with Shellcheck"
print.info "Checking .sh files with Shellcheck"
shellcheck --shell sh --external-sources \
asdf.sh

printf "%s\n" "[INFO] Checking .bash files with Shellcheck"
print.info "Checking .bash files with Shellcheck"
shellcheck --shell bash --external-sources \
completions/*.bash \
bin/asdf \
Expand All @@ -59,7 +67,7 @@ run_shellcheck_linter() {
test/fixtures/dummy_legacy_plugin/bin/* \
test/fixtures/dummy_plugin/bin/*

printf "%s\n" "[INFO] Checking .bats files with Shellcheck"
print.info "Checking .bats files with Shellcheck"
shellcheck --shell bats --external-source \
test/*.bats
}
Expand All @@ -74,15 +82,15 @@ run_custom_python_stylecheck() {

if [ -n "$github_actions" ] && ! command -v python3 &>/dev/null; then
# fail if CI and no python3
printf "%s\n" "[ERROR] Detected execution in GitHub Actions but python3 was not found. This is required during CI linting."
print.error "Detected execution in GitHub Actions but python3 was not found. This is required during CI linting."
exit 1
fi

if ! command -v python3 &>/dev/null; then
# skip if local and no python3
printf "%s\n" "[WARNING] python3 not found. Skipping Custom Python Script."
else
printf "%s\n" "[INFO] Checking files with Custom Python Script."
print.info "Checking files with Custom Python Script."
"${0%/*}/checkstyle.py" "${flag}"
fi

Expand All @@ -106,15 +114,15 @@ run_fish_linter() {

if [ -n "$github_actions" ] && ! command -v fish_indent &>/dev/null; then
# fail if CI and no fish_ident
printf "%s\n" "[ERROR] Detected execution in GitHub Actions but fish_indent was not found. This is required during CI linting."
print.error "Detected execution in GitHub Actions but fish_indent was not found. This is required during CI linting."
exit 1
fi

if ! command -v fish_indent &>/dev/null; then
# skip if local and no fish_ident
printf "%s\n" "[WARNING] fish_indent not found. Skipping .fish files."
else
printf "%s\n" "[INFO] Checking .fish files with fish_indent"
print.info "Checking .fish files with fish_indent"
fish_indent "${flag}" ./**/*.fish
fi
}
Expand All @@ -129,17 +137,19 @@ run_fish_linter() {
# printf "%s\n" "[WARNING] powershell linter/formatter not found, skipping for now."
#}

repo_root=$(git rev-parse --show-toplevel)
if [ "$repo_root" != "$PWD" ]; then
printf "%s\n" "[ERROR] This scripts requires execution from the repository root directory."
printf "\t%s\t%s\n" "Repo root dir:" "$repo_root"
printf "\t%s\t%s\n\n" "Current dir:" "$PWD"
printf "%s\n" "See usage details with -h or --help."
exit 1
fi
{
repo_dir=$(git rev-parse --show-toplevel)
current_dir=$(pwd -P)
if [ "$repo_dir" != "$current_dir" ]; then
print.error "This scripts requires execution from the repository root directory."
printf "\t%s\t%s\n" "Repo root dir:" "$repo_dir"
printf "\t%s\t%s\n\n" "Current dir:" "$current_dir"
exit 1
fi
}

if [ $# -eq 0 ]; then
printf "%s\n" "[ERROR] At least one option required."
print.error "At least one option required."
printf "=%.0s" {1..60}
printf "\n"
usage
Expand All @@ -159,7 +169,7 @@ case "$1" in
mode="fix"
;;
*)
printf "%s\n" "[ERROR] Invalid flag: $1"
print.error "Invalid flag: $1"
printf "=%.0s" {1..60}
printf "\n"
usage
Expand All @@ -177,4 +187,4 @@ run_fish_linter "$mode"
#run_nushell_linter "$mode"
#run_powershell_linter "$mode"

printf "%s\n" "[INFO] Success!"
print.info "Success!"
37 changes: 24 additions & 13 deletions scripts/test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,37 @@
set -euo pipefail
IFS=$'\n\t'

repo_root=$(git rev-parse --show-toplevel)
if [ "$repo_root" != "$PWD" ]; then
printf "%s\n" "[ERROR] This scripts requires execution from the repository root directory."
printf "\t%s\t%s\n" "Repo root dir:" "$repo_root"
printf "\t%s\t%s\n\n" "Current dir:" "$PWD"
exit 1
fi
print.info() {
printf '[INFO] %s\n' "$1"
}

print.error() {
printf '[ERROR] %s\n' "$1" >&2
}

{
repo_dir=$(git rev-parse --show-toplevel)
current_dir=$(pwd -P)
if [ "$repo_dir" != "$current_dir" ]; then
print.error "This scripts requires execution from the repository root directory."
printf "\t%s\t%s\n" "Repo root dir:" "$repo_dir"
printf "\t%s\t%s\n\n" "Current dir:" "$current_dir"
exit 1
fi
}

test_directory="test"
test_directory="./test"
bats_options=(--timing --print-output-on-failure)

if command -v parallel >/dev/null; then
# enable parallel jobs
bats_options=("${bats_options[@]}" --jobs 2 --no-parallelize-within-files)
# Enable parallel jobs
bats_options+=(--jobs 2 --no-parallelize-within-files)
elif [[ -n "${CI-}" ]]; then
printf "* %s\n" "GNU parallel should be installed in the CI environment. Please install and rerun the test suite."
print.error "GNU parallel should be installed in the CI environment. Please install and rerun the test suite."
exit 1
else
printf "* %s\n" "For faster test execution, install GNU parallel."
print.info "For faster test execution, install GNU parallel."
fi

printf "* %s\n" "Running Bats in directory '${test_directory}' with options:" "${bats_options[@]}"
print.info "Running Bats in directory '${test_directory}' with options:" "${bats_options[@]}"
bats "${bats_options[@]}" "${test_directory}"

0 comments on commit b320803

Please sign in to comment.