Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gno version command #3002

Merged
merged 22 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions gnovm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ GOBUILD_FLAGS ?= -ldflags "-X github.com/gnolang/gno/gnovm/pkg/gnoenv._GNOROOT=$
# more complex than adding -coverprofile, like test.cmd.coverage.
GOTEST_COVER_PROFILE ?= cmd-profile.out

# user for gno version [branch].[N]+[hash]
VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null || echo "$(shell git rev-parse --abbrev-ref HEAD).$(shell git rev-list --count HEAD)+$(shell git rev-parse --short HEAD)")
VCS_REF ?= $(shell git rev-parse --short HEAD)

########################################
# Dev tools
.PHONY: build
build:
go build $(GOBUILD_FLAGS) -o build/gno ./cmd/gno
go build -ldflags="-X main.buildVersion=$(VERSION) -X main.commitHash=$(VCS_REF) -X github.com/gnolang/gno/gnovm/pkg/gnoenv._GNOROOT=$(GNOROOT_DIR)" -o build/gno ./cmd/gno
kazai777 marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: install
install:
go install $(GOBUILD_FLAGS) ./cmd/gno
go install -ldflags="-X main.buildVersion=$(VERSION) -X main.commitHash=$(VCS_REF) -X github.com/gnolang/gno/gnovm/pkg/gnoenv._GNOROOT=$(GNOROOT_DIR)" ./cmd/gno

.PHONY: clean
clean:
Expand Down
1 change: 1 addition & 0 deletions gnovm/cmd/gno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func newGnocliCmd(io commands.IO) *commands.Command {
newEnvCmd(io),
newBugCmd(io),
newFmtCmd(io),
newVersionCmd(io),
// graph
// vendor -- download deps from the chain in vendor/
// list -- list packages
Expand Down
kazai777 marked this conversation as resolved.
Show resolved Hide resolved
kazai777 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- stdout.golden --
gno version: master.387+e872fa
2 changes: 2 additions & 0 deletions gnovm/cmd/gno/testdata/gno_version/tagged_version.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- stdout.golden --
gno version: chain/test4.2
33 changes: 33 additions & 0 deletions gnovm/cmd/gno/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"context"

"github.com/gnolang/gno/tm2/pkg/commands"
)

var buildVersion string

// newVersionCmd creates a new version command
func newVersionCmd(io commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "version",
ShortUsage: "version",
ShortHelp: "Display installed gno version",
},
nil,
func(_ context.Context, args []string) error {
version := getGnoVersion()
io.Println("gno version:", version)
return nil
},
)
}

func getGnoVersion() string {
if buildVersion != "" {
return buildVersion
}
return "unknown version"

Check warning on line 32 in gnovm/cmd/gno/version.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/version.go#L32

Added line #L32 was not covered by tests
}
48 changes: 48 additions & 0 deletions gnovm/cmd/gno/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"os"
"strings"
"testing"
)

var testBuildVersion string

func init() {
if isTestTaggedVersion() {
testBuildVersion = "chain/test4.2"
} else {
testBuildVersion = "master.387+e872fa"
}
buildVersion = testBuildVersion
}

func isTestTaggedVersion() bool {
testDir := os.Getenv("TEST_CASE_DIR")
return strings.Contains(testDir, "tagged_version")
}

func TestVersionApp(t *testing.T) {
testCases := []testMainCase{
{
args: []string{"version"},
testDir: "testdata/gno_version/branch_commit_version.txtar",
stdoutShouldContain: "gno version: master.387+e872fa",
},
{
args: []string{"version"},
testDir: "testdata/gno_version/tagged_version.txtar",
stdoutShouldContain: "gno version: chain/test4.2",
},
}

for _, testCase := range testCases {
os.Setenv("TEST_CASE_DIR", testCase.testDir)
if isTestTaggedVersion() {
buildVersion = "chain/test4.2"
} else {
buildVersion = "master.387+e872fa"
}
testMainCaseRun(t, []testMainCase{testCase})
}
}
Loading