Repository: decred/dcrd
Pull request #3161 (2023-07-21 21:51:17)

build: Move lint logic to its own script.
**This is rebased on #3157**.

This separates the logic in `run_tests.sh` that runs all of the linters on all modules in the repository to a separate `lint.sh` script and updates `run_tests.sh` to call that script instead.  Rather than listing every linter in the script comments, it refers to the `.golangci.yml` configuration file that specifies them now that it exists.

This allows developers to more easily selectively run only the linters without needing to run all of the tests again.

It also cleans up the comments in `run_test.sh` to match reality while here.

jrick: > This allows developers to more easily selectively run only the linters without needing to run all of the tests again.

this works well to only perform linting while skipping testing, but not to perform tests without performing lints.

Having the lint operations in a separate script is fine, but could we change run_tests.sh to take an (optional) parameter to describe which operation to perform, and default to performing all of them?

Another alternative is an "all.sh" script that does all, and break out tests into another script like this PR currently does for linting.
davecgh: > this works well to only perform linting while skipping testing, but not to perform tests without performing lints.
> 
> Having the lint operations in a separate script is fine, but could we change run_tests.sh to take an (optional) parameter to describe which operation to perform, and default to performing all of them?
> 
> Another alternative is an "all.sh" script that does all, and break out tests into another script like this PR currently does for linting.

Yeah, that was intentional.  The lints are part of the tests.  I wasn't worried about having a separate test script, because, unlike the linters, it is pretty trivial to selectively run tests with lints with `go test ` nowadays, including for all modules.

I'm not really opposed to having separate scripts for it, but if we decide to go that route, I'd prefer to do it in a separate PR since this solves the immediate thing it sets out to solve as is.


Patch:
diff --git a/lint.sh b/lint.sh
new file mode 100755
index 00000000..b490315e
--- /dev/null
+++ b/lint.sh
@@ -0,0 +1,32 @@
+#/usr/bin/env bash
+
+set -ex
+
+# The script uses golangci-lint (github.com/golangci/golangci-lint) to run all
+# linters defined by the configuration in .golangci.yml on every module in the
+# repository.
+
+go version
+
+# loop all modules
+ROOTPKG=$(go list)
+ROOTPKGPATTERN=$(echo $ROOTPKG | sed 's,\\,\\\\,g' | sed 's,/,\\/,g')
+MODPATHS=$(go list -m all | grep "^$ROOTPKGPATTERN" | cut -d' ' -f1)
+for module in $MODPATHS; do
+  echo "==> lint ${module}"
+
+  # determine module directory
+  MODNAME=$(echo $module | sed -E -e "s/^$ROOTPKGPATTERN//" \
+    -e 's,^/,,' -e 's,/v[0-9]+$,,')
+  if [ -z "$MODNAME" ]; then
+    MODNAME=.
+  fi
+
+  # run commands in the module directory as a subshell
+  (
+    cd $MODNAME
+
+    # run linters
+    golangci-lint run
+  )
+done


Test patch:
diff --git a/.github/stablilize_testdata_timestamps.sh b/.github/stablilize_testdata_timestamps.sh
old mode 100644
new mode 100755
index 4b0379fc..87dba103
--- a/.github/stablilize_testdata_timestamps.sh
+++ b/.github/stablilize_testdata_timestamps.sh
@@ -1,4 +1,4 @@
-#!/bin/env bash
+#!/usr/bin/env bash
 
 display_usage() {
 	echo -e "Usage: $0 PATH_TO_REPOSITORY_ROOT\n"
diff --git a/run_tests.sh b/run_tests.sh
index 93ad77c2..6efeef38 100755
--- a/run_tests.sh
+++ b/run_tests.sh
@@ -1,18 +1,10 @@
-#!/bin/sh
+#!/usr/bin/env bash
 
 set -ex
 
-# The script does automatic checking on a Go package and its sub-packages,
-# including:
-# 1. gofmt         (https://golang.org/cmd/gofmt/)
-# 2. gosimple      (https://github.com/dominikh/go-simple)
-# 3. unconvert     (https://github.com/mdempsky/unconvert)
-# 4. ineffassign   (https://github.com/gordonklaus/ineffassign)
-# 5. go vet        (https://golang.org/cmd/vet)
-# 6. misspell      (https://github.com/client9/misspell)
-
-# golangci-lint (github.com/golangci/golangci-lint) is used to run each
-# static checker.
+# This script runs the tests for all packages in all Go modules in the
+# repository and then runs the linters for all Go modules in the repository by
+# invoking the separate linter script.
 
 go version
 
@@ -21,27 +13,8 @@ echo "==> test all modules"
 ROOTPKG=$(go list)
 go test -short -tags rpctest $ROOTPKG/...
 
-# loop all modules
-ROOTPKGPATTERN=$(echo $ROOTPKG | sed 's,\\,\\\\,g' | sed 's,/,\\/,g')
-MODPATHS=$(go list -m all | grep "^$ROOTPKGPATTERN" | cut -d' ' -f1)
-for module in $MODPATHS; do
-  echo "==> lint ${module}"
-
-  # determine module directory
-  MODNAME=$(echo $module | sed -E -e "s/^$ROOTPKGPATTERN//" \
-    -e 's,^/,,' -e 's,/v[0-9]+$,,')
-  if [ -z "$MODNAME" ]; then
-    MODNAME=.
-  fi
-
-  # run commands in the module directory as a subshell
-  (
-    cd $MODNAME
-
-    # run linters
-    golangci-lint run
-  )
-done
+# run linters on all modules
+. ./lint.sh
 
 echo "------------------------------------------"
 echo "Tests completed successfully!"