master
mk 121 lines 4.63 KB
Raw
1 # golang utilities
2 export GO111MODULE=on
3
4
5 # pre-definitions
6 GOCC ?= go
7 GOTAGS ?=
8 GOTFLAGS ?=
9
10 # Unexport GOFLAGS so we only apply it where we actually want it.
11 unexport GOFLAGS
12 # Override so we can combine with the user's go flags.
13 # Try to make building as reproducible as possible by stripping the go path.
14 override GOFLAGS += "-trimpath"
15
16 ifeq ($(tarball-is),1)
17 GOFLAGS += -mod=vendor
18 endif
19
20 # match Go's default GOPATH behaviour
21 export GOPATH ?= $(shell $(GOCC) env GOPATH)
22
23 DEPS_GO :=
24 TEST_GO :=
25 TEST_GO_BUILD :=
26 CHECK_GO :=
27
28 go-pkg-name=$(shell GOFLAGS=-buildvcs=false $(GOCC) list $(go-tags) github.com/ipfs/kubo/$(1))
29 go-main-name=$(notdir $(call go-pkg-name,$(1)))$(?exe)
30 go-curr-pkg-tgt=$(d)/$(call go-main-name,$(d))
31 go-pkgs=$(shell GOFLAGS=-buildvcs=false $(GOCC) list github.com/ipfs/kubo/...)
32
33 go-tags=$(if $(GOTAGS), -tags="$(call join-with,$(space),$(GOTAGS))")
34 go-flags-with-tags=$(GOFLAGS)$(go-tags)
35
36 define go-build-relative
37 $(GOCC) build $(go-flags-with-tags) -o "$@" "$(call go-pkg-name,$<)"
38 endef
39
40 define go-build
41 $(GOCC) build $(go-flags-with-tags) -o "$@" "$(1)"
42 endef
43
44 # Only disable colors when running in CI (non-interactive terminal)
45 GOTESTSUM_NOCOLOR := $(if $(CI),--no-color,)
46
47 # Packages excluded from coverage (test code and examples are not production code)
48 COVERPKG_EXCLUDE := /(test|docs/examples)/
49
50 # Packages excluded from unit tests: coverage exclusions + client/rpc (tested by test_cli)
51 UNIT_EXCLUDE := /(test|docs/examples)/|/client/rpc$$
52
53 # Unit tests with coverage
54 # Produces JSON for CI reporting and coverage profile for Codecov
55 test_unit: test/bin/gotestsum $$(DEPS_GO)
56 mkdir -p test/unit coverage
57 rm -f test/unit/gotest.json coverage/unit_tests.coverprofile
58 gotestsum $(GOTESTSUM_NOCOLOR) --jsonfile test/unit/gotest.json -- $(go-flags-with-tags) $(GOTFLAGS) -covermode=atomic -coverprofile=coverage/unit_tests.coverprofile -coverpkg=$$($(GOCC) list $(go-tags) ./... | grep -vE '$(COVERPKG_EXCLUDE)' | tr '\n' ',' | sed 's/,$$//') $$($(GOCC) list $(go-tags) ./... | grep -vE '$(UNIT_EXCLUDE)')
59 .PHONY: test_unit
60
61 # CLI/integration tests (requires built binary in PATH)
62 # Includes test/cli, test/integration, and client/rpc
63 # Produces JSON for CI reporting
64 # Override TEST_CLI_TIMEOUT for local development: make test_cli TEST_CLI_TIMEOUT=5m
65 TEST_CLI_TIMEOUT ?= 10m
66 test_cli: cmd/ipfs/ipfs test/bin/gotestsum $$(DEPS_GO)
67 mkdir -p test/cli
68 rm -f test/cli/cli-tests.json
69 TEST_FUSE=0 PATH="$(CURDIR)/cmd/ipfs:$(CURDIR)/test/bin:$$PATH" gotestsum $(GOTESTSUM_NOCOLOR) --jsonfile test/cli/cli-tests.json -- -v -timeout=$(TEST_CLI_TIMEOUT) ./test/cli/... ./test/integration/... ./client/rpc/...
70 .PHONY: test_cli
71
72 # FUSE tests (requires /dev/fuse and fusermount in PATH)
73 # TEST_FUSE=1 makes mount failures fatal instead of skipping
74 # Keep this shorter than the CI job timeout so a hang trips Go's panic
75 # (and prints stack traces) instead of getting silently killed by CI.
76 TEST_FUSE_TIMEOUT ?= 4m
77
78 # FUSE unit tests (./fuse/...)
79 test_fuse_unit: test/bin/gotestsum $$(DEPS_GO)
80 mkdir -p test/fuse
81 rm -f test/fuse/fuse-unit-tests.json
82 TEST_FUSE=1 gotestsum $(GOTESTSUM_NOCOLOR) --jsonfile test/fuse/fuse-unit-tests.json -- -v -timeout=$(TEST_FUSE_TIMEOUT) ./fuse/...
83 .PHONY: test_fuse_unit
84
85 # FUSE CLI integration tests (test/cli/fuse/)
86 test_fuse_cli: cmd/ipfs/ipfs test/bin/gotestsum $$(DEPS_GO)
87 mkdir -p test/fuse
88 rm -f test/fuse/fuse-cli-tests.json
89 TEST_FUSE=1 PATH="$(CURDIR)/cmd/ipfs:$(CURDIR)/test/bin:$$PATH" gotestsum $(GOTESTSUM_NOCOLOR) --jsonfile test/fuse/fuse-cli-tests.json -- -v -timeout=$(TEST_FUSE_TIMEOUT) ./test/cli/fuse/...
90 .PHONY: test_fuse_cli
91
92 # Combined: run all FUSE tests
93 test_fuse: test_fuse_unit test_fuse_cli
94 .PHONY: test_fuse
95
96 # Example tests (docs/examples/kubo-as-a-library)
97 # Tests against both published and current kubo versions
98 # Uses timeout to ensure CI gets output before job-level timeout kills everything
99 TEST_EXAMPLES_TIMEOUT ?= 2m
100 test_examples:
101 cd docs/examples/kubo-as-a-library && go test -v -timeout=$(TEST_EXAMPLES_TIMEOUT) ./... && cp go.mod go.mod.bak && cp go.sum go.sum.bak && (go mod edit -replace github.com/ipfs/kubo=./../../.. && go mod tidy && go test -v -timeout=$(TEST_EXAMPLES_TIMEOUT) ./...; ret=$$?; mv go.mod.bak go.mod; mv go.sum.bak go.sum; exit $$ret)
102 .PHONY: test_examples
103
104 # Build kubo for all platforms from .github/build-platforms.yml
105 test_go_build:
106 bin/test-go-build-platforms
107 .PHONY: test_go_build
108
109 # Check Go source formatting
110 test_go_fmt:
111 bin/test-go-fmt
112 .PHONY: test_go_fmt
113
114 # Run golangci-lint (used by CI)
115 test_go_lint: test/bin/golangci-lint
116 golangci-lint run --timeout=3m ./...
117 .PHONY: test_go_lint
118
119 TEST_GO := test_go_fmt test_unit test_cli test_examples
120 TEST += $(TEST_GO)
121 TEST_SHORT += test_go_fmt test_unit