Skip to content

Commit 4b9904f

Browse files
committed
fmt: introduce golangci-lint and go vet
1 parent 297ad41 commit 4b9904f

File tree

11 files changed

+521
-6
lines changed

11 files changed

+521
-6
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- dev
8+
pull_request:
9+
permissions:
10+
contents: read
11+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
12+
# pull-requests: read
13+
jobs:
14+
lint:
15+
runs-on: ubuntu-latest
16+
name: "Lint all"
17+
steps:
18+
- uses: actions/checkout@v5
19+
- uses: actions/setup-go@v6
20+
with:
21+
go-version: '1.22'
22+
cache: false
23+
- name: "Install golangci-lint v2.5.0"
24+
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.5.0
25+
- name: "Run linter by calling 'make fmt_check'"
26+
run: make fmt_check
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- dev
8+
pull_request:
9+
permissions:
10+
contents: read
11+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
12+
# pull-requests: read
13+
jobs:
14+
collectpackages:
15+
name: "Collect all packages to a list"
16+
runs-on: ubuntu-latest
17+
outputs:
18+
packages: "${{ steps.create-package-list.outputs.packages }}"
19+
steps:
20+
- uses: actions/checkout@v5
21+
- name: "Collect by calling 'make print_collected_packages'"
22+
id: create-package-list
23+
run: |
24+
pkgs=$(make print_collected_packages)
25+
# replace space by comma, add double quotes and copy to output
26+
echo "packages=[\"${pkgs// /\",\"}\"]" >> $GITHUB_OUTPUT
27+
- name: "Debug the collected packages as JSON array"
28+
run: echo "The collected packages are ${{ steps.create-package-list.outputs.packages }}"
29+
30+
golangci:
31+
needs: collectpackages
32+
runs-on: ubuntu-latest
33+
strategy:
34+
matrix:
35+
package: "${{ fromJson(needs.collectpackages.outputs.packages) }}"
36+
name: "Lint ${{ matrix.package }}"
37+
steps:
38+
- uses: actions/checkout@v5
39+
- uses: actions/setup-go@v6
40+
with:
41+
go-version: '1.22'
42+
cache: false
43+
- name: golangci-lint
44+
uses: golangci/golangci-lint-action@v8
45+
with:
46+
version: v2.5.0
47+
working-directory: ${{ matrix.package }}
48+
49+
# Optional: golangci-lint command line arguments.
50+
# Note: exclude arguments, e.g. --exclude-files="my_file", will not affect the "typecheck" linter,
51+
# at least since v1.61.0 - use build tags instead.
52+
#args: --exclude-files="platforms/digispark/digispark_adaptor.go"
53+
54+
# Optional: show only new issues if it's a pull request. The default value is `false`.
55+
# only-new-issues: true
56+
57+
# Optional: if set to true then the all caching functionality will be complete disabled,
58+
# takes precedence over all other caching options.
59+
# skip-cache: true

.golangci.yml

Lines changed: 308 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
# sub-directory used for build
2+
BUILD_DIR := build
3+
# find all packages without any dependency to underlying tiny-go packages, e.g. "machine" or "device/arm"
4+
BUILD_TAGS_CHECK := m5stack_core2,microbit,xiao_ble
5+
ALL_WITHOUT_MACHINE := $(shell go list -e -tags $(BUILD_TAGS_CHECK) -f '{{.Dir}},{{.Deps}}' ./... | awk -F, '$$2 !~ /machine/ && $$2 !~ /device\/arm/ {print $$1}')
6+
# exclude anything found in build output and "image" directory, and exclude some further folders, which contains problematic dependencies in sub-folders
7+
EXCLUDE_PACKAGES = $(CURDIR)/$(BUILD_DIR)/% $(CURDIR)/image/% $(CURDIR)/touch $(CURDIR)/waveshare-epd
8+
ALL_TO_CHECK := $(filter-out $(EXCLUDE_PACKAGES),$(ALL_WITHOUT_MACHINE))
9+
10+
.PHONY: clean fmt-check smoke-test unit-test test check fmt_check fmt_fix $(ALL_TO_CHECK)
111

212
clean:
3-
@rm -rf build
13+
@rm -rf $(BUILD_DIR)
414

515
FMT_PATHS = ./
616

@@ -9,7 +19,7 @@ fmt-check:
919

1020
XTENSA ?= 1
1121
smoke-test:
12-
@mkdir -p build
22+
@mkdir -p $(BUILD_DIR)
1323
@go run ./smoketest.go -xtensa=$(XTENSA) smoketest.sh
1424

1525

@@ -26,3 +36,34 @@ unit-test:
2636
@go test -v $(addprefix ./,$(TESTS))
2737

2838
test: clean fmt-check unit-test smoke-test
39+
40+
fmt_quick_check:
41+
@# a very fast check before build, but depends on accessibility of all imports
42+
@# switch off the "stdmethods" analyzer is needed due to finding:
43+
@# at24cx/at24cx.go:57:18: method WriteByte(eepromAddress uint16, value uint8) error should have signature WriteByte(byte) error
44+
@# at24cx/at24cx.go:67:18: method ReadByte(eepromAddress uint16) (uint8, error) should have signature ReadByte() (byte, error)
45+
@# switch off the "shift" analyzer is needed due to finding:
46+
@#tmc5160/registers.go:1939:16: m.CUR_A (16 bits) too small for shift of 16
47+
@#tmc5160/registers.go:1996:16: m.X3 (8 bits) too small for shift of 27
48+
@#tmc5160/registers.go:1996:27: m.X2 (8 bits) too small for shift of 24
49+
@#tmc5160/registers.go:1996:38: m.X1 (8 bits) too small for shift of 21
50+
@#tmc5160/registers.go:1996:49: m.W3 (8 bits) too small for shift of 18
51+
@#tmc5160/registers.go:1996:60: m.W2 (8 bits) too small for shift of 16
52+
@#tmc5160/registers.go:1996:71: m.W1 (8 bits) too small for shift of 14
53+
@#tmc5160/registers.go:1996:82: m.W0 (8 bits) too small for shift of 12
54+
go vet -tags $(BUILD_TAGS_CHECK) -stdmethods=false -shift=false $(ALL_TO_CHECK)
55+
56+
fmt_check:
57+
@# a complete format check, but depends on accessibility of all imports
58+
golangci-lint -v run $(ALL_TO_CHECK)
59+
60+
fmt_fix:
61+
@# an automatic reformat and complete format check, but depends on accessibility of all imports
62+
@#TODO: activate when ready
63+
@#gofumpt -l -w $(ALL_TO_CHECK)
64+
golangci-lint -v run $(ALL_TO_CHECK) --fix
65+
66+
print_collected_packages:
67+
@#this target is used to unify mechanism in CI with the local one, see ".github/workflows/golangci-lint.yml"
68+
@#we need additional exclude the root folder here, because will be checked recursive when used as working directory
69+
@echo $(filter-out $(CURDIR),$(ALL_TO_CHECK))

adafruit4650/device_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"testing"
1414
"time"
15+
1516
"tinygo.org/x/drivers"
1617
"tinygo.org/x/tinyfont"
1718
"tinygo.org/x/tinyfont/freemono"

cmd/convert2bin/convert2bin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func main() {
1919

2020
func run(args []string) error {
2121
if len(args) < 2 {
22-
return fmt.Errorf("usage: %s FILE")
22+
return fmt.Errorf("usage: %s FILE", args[0])
2323
}
2424

2525
b, err := ioutil.ReadFile(args[1])

hts221/hts221_generic.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
package hts221
44

5-
import "tinygo.org/x/drivers"
6-
75
// Configure sets up the HTS221 device for communication.
86
func (d *Device) Configure() {
97
// read calibration data

mak.bak

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# find all packages without any dependency to underlying tiny-go packages, e.g. "machine" or "device/arm"
2+
# also exclude anything found in build output directory and "image" folder
3+
BUILD_TAGS_CHECK := m5stack_core2,microbit,xiao_ble
4+
ALL_WITHOUT_MACHINE := $(shell go list -e -tags $(BUILD_TAGS_CHECK) -f '{{.Dir}},{{.Deps}}' ./... | awk -F, '$$1 !~ /build/ && $$1 !~ /image/ && $$2 !~ /machine/ && $$2 !~ /device\/arm/ {print $$1}')
5+
# create a list of sub-dirs, relative to the root
6+
ALL_DIRS_TO_CHECK := $(subst $(CURDIR),.,$(ALL_WITHOUT_MACHINE))
7+
8+
BUILD_DIR := build
9+
LINTER_DIR := $(BUILD_DIR)/linter/drivers
10+
11+
.PHONY: clean fmt-check smoke-test unit-test test check fmt_check fmt_fix $(ALL_DIRS_TO_CHECK)
12+
13+
clean:
14+
@rm -rf $(BUILD_DIR)
15+
16+
FMT_PATHS = ./
17+
18+
fmt-check:
19+
@unformatted=$$(gofmt -l $(FMT_PATHS)); [ -z "$$unformatted" ] && exit 0; echo "Unformatted:"; for fn in $$unformatted; do echo " $$fn"; done; exit 1
20+
21+
XTENSA ?= 1
22+
smoke-test:
23+
@mkdir -p $(BUILD_DIR)
24+
@go run ./smoketest.go -xtensa=$(XTENSA) smoketest.sh
25+
26+
27+
# rwildcard is a recursive version of $(wildcard)
28+
# https://blog.jgc.org/2011/07/gnu-make-recursive-wildcard-function.html
29+
rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
30+
# Recursively find all *_test.go files from cwd & reduce to unique dir names
31+
HAS_TESTS = $(sort $(dir $(call rwildcard,,*_test.go)))
32+
# Exclude anything we explicitly don't want to test for whatever reason
33+
EXCLUDE_TESTS = image waveshare-epd/epd2in66b
34+
TESTS = $(filter-out $(addsuffix /%,$(EXCLUDE_TESTS)),$(HAS_TESTS))
35+
36+
unit-test:
37+
@go test -v $(addprefix ./,$(TESTS))
38+
39+
test: clean fmt-check unit-test smoke-test
40+
41+
fmt_quick_check:
42+
@# a very fast check before build, but depends on accessibility of all imports
43+
@# switch off the "stdmethods" analyzer is needed due to finding:
44+
@# at24cx/at24cx.go:57:18: method WriteByte(eepromAddress uint16, value uint8) error should have signature WriteByte(byte) error
45+
@# at24cx/at24cx.go:67:18: method ReadByte(eepromAddress uint16) (uint8, error) should have signature ReadByte() (byte, error)
46+
@# switch off the "shift" analyzer is needed due to finding:
47+
@#tmc5160/registers.go:1939:16: m.CUR_A (16 bits) too small for shift of 16
48+
@#tmc5160/registers.go:1996:16: m.X3 (8 bits) too small for shift of 27
49+
@#tmc5160/registers.go:1996:27: m.X2 (8 bits) too small for shift of 24
50+
@#tmc5160/registers.go:1996:38: m.X1 (8 bits) too small for shift of 21
51+
@#tmc5160/registers.go:1996:49: m.W3 (8 bits) too small for shift of 18
52+
@#tmc5160/registers.go:1996:60: m.W2 (8 bits) too small for shift of 16
53+
@#tmc5160/registers.go:1996:71: m.W1 (8 bits) too small for shift of 14
54+
@#tmc5160/registers.go:1996:82: m.W0 (8 bits) too small for shift of 12
55+
go vet -tags $(BUILD_TAGS_CHECK) -stdmethods=false -shift=false $(ALL_DIRS_TO_CHECK)
56+
57+
fmt_check:
58+
@# a complete format check, but depends on accessibility of all imports
59+
@$(MAKE) linter_workspace
60+
cd $(LINTER_DIR) && golangci-lint -v run
61+
62+
fmt_fix:
63+
@# an automatic reformat and complete format check, but depends on accessibility of all imports
64+
@#TODO: activate when ready
65+
@#gofumpt -l -w $(ALL_DIRS_TO_CHECK) # TODO: test for all files, not only for filtered ones
66+
golangci-lint -v run $(ALL_DIRS_TO_CHECK) --fix
67+
68+
linter_workspace:
69+
@# creates the workspace for running the linter in one step, which is especially used for CI workflow, see ".github/workflows/golangci-lint.yml"
70+
@$(MAKE) FUNC=COPY $(filter-out .,$(ALL_DIRS_TO_CHECK))
71+
@# additionally copy go files from root for checking
72+
@cp -f $(CURDIR)/*.go $(LINTER_DIR)
73+
74+
$(ALL_DIRS_TO_CHECK):
75+
ifeq ($(FUNC),COPY)
76+
@mkdir -p $(LINTER_DIR)/$@
77+
@cp -f $(addprefix ./,$@)/*.* $(LINTER_DIR)/$@
78+
else
79+
@echo $@
80+
endif

pcf8523/pcf8523.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package pcf8523
55

66
import (
77
"time"
8+
89
"tinygo.org/x/drivers"
910
)
1011

pcf8523/pcf8523_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/hex"
55
"testing"
66
"time"
7+
78
"tinygo.org/x/drivers/tester"
89
)
910

0 commit comments

Comments
 (0)