Skip to content

Commit

Permalink
release-info: Test its format as part of 'make test'
Browse files Browse the repository at this point in the history
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 <cfergeau@redhat.com>
  • Loading branch information
cfergeau authored and praveenkumar committed Jun 13, 2024
1 parent 4b59cba commit 1d10459
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions release_info_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 1d10459

Please sign in to comment.