From 1d104598dd1c33486fd142333abbdf1ca5c970a9 Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Wed, 12 Jun 2024 12:03:57 +0200 Subject: [PATCH] release-info: Test its format as part of 'make test' This commit makes use of `//go:embed` to run the content of `release-info.json` through golang `json.Valid` to check if it's valid JSON. This is better/simpler than using `jq` as we don't have to change all our container images/CI envs/... to make sure `jq` is installed, and `json.Valid` uses the same json parser as crc so this avoids possible differences of behaviour between json parsers. The `cross-lint` target must depend on `gen_release_info` otherwise it fails: ``` release_info_test.go:9:12: pattern release-info.json: no matching files found (typecheck) ``` Signed-off-by: Christophe Fergeau --- Makefile | 8 ++++---- release_info_test.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 release_info_test.go diff --git a/Makefile b/Makefile index 6d1b42bcfd..1376dd2b0f 100644 --- a/Makefile +++ b/Makefile @@ -128,8 +128,8 @@ generate_mocks: $(TOOLS_BINDIR)/mockery $(TOOLS_BINDIR)/mockery --srcpkg ./pkg/crc/api/client --name Client --output test/mocks/api --filename client.go .PHONY: test -test: - go test -race --tags "build $(BUILDTAGS)" -v -ldflags="$(VERSION_VARIABLES)" ./pkg/... ./cmd/... +test: gen_release_info + go test -race --tags "build $(BUILDTAGS)" -v -ldflags="$(VERSION_VARIABLES)" . ./pkg/... ./cmd/... .PHONY: spec test-rpmbuild @@ -277,10 +277,10 @@ fmt: $(TOOLS_BINDIR)/goimports # Run golangci-lint against code .PHONY: lint cross-lint -lint: $(TOOLS_BINDIR)/golangci-lint +lint: $(TOOLS_BINDIR)/golangci-lint gen_release_info "$(TOOLS_BINDIR)"/golangci-lint run -cross-lint: $(TOOLS_BINDIR)/golangci-lint +cross-lint: $(TOOLS_BINDIR)/golangci-lint gen_release_info GOARCH=amd64 GOOS=darwin "$(TOOLS_BINDIR)"/golangci-lint run GOARCH=arm64 GOOS=darwin "$(TOOLS_BINDIR)"/golangci-lint run GOARCH=amd64 GOOS=linux "$(TOOLS_BINDIR)"/golangci-lint run diff --git a/release_info_test.go b/release_info_test.go new file mode 100644 index 0000000000..047ea6674d --- /dev/null +++ b/release_info_test.go @@ -0,0 +1,16 @@ +package main + +import ( + _ "embed" + "encoding/json" + "testing" +) + +//go:embed release-info.json +var releaseInfo []byte + +func TestReleaseInfo(t *testing.T) { + if !json.Valid(releaseInfo) { + t.Fatal("release-info.json is not a valid json file") + } +}