Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions cmd/gh-actions-lock/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ $ gh actions-lock --no-fix --json=valid,findings
# All fields as JSON
$ gh actions-lock --json
`),
Version: cliVersion(),
Comment thread
kachick marked this conversation as resolved.
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
opts.workflowPaths = args
Expand Down
37 changes: 37 additions & 0 deletions cmd/gh-actions-lock/root_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package main

import (
"bytes"
"strings"
"testing"

"github.com/github/gh-actions-lock/internal/pinpool"
"github.com/github/gh-actions-lock/internal/resolve"
)

func TestNewRootCmdSuppressesCobraUsageForHandledErrors(t *testing.T) {
Expand All @@ -10,3 +15,35 @@ func TestNewRootCmdSuppressesCobraUsageForHandledErrors(t *testing.T) {
t.Fatalf("expected root command to suppress Cobra usage/errors for handled failures")
}
}

func TestNewRootCmd_Version(t *testing.T) {
var stdout, stderr bytes.Buffer
cmd := newRootCmd(func(string, *pinpool.Pool) (*resolve.Resolver, error) {
t.Fatalf("unexpected resolver call: --version must skip resolution")
return nil, nil
})
cmd.SetOut(&stdout)
cmd.SetErr(&stderr)
cmd.SetArgs([]string{"--version"})

if err := cmd.Execute(); err != nil {
t.Fatalf("unexpected error: %v", err)
}

if stderr.Len() != 0 {
t.Fatalf("expected empty stderr, got %q", stderr.String())
}

// We check the output shape ("actions-lock version <value>\n") instead of
// comparing with cliVersion(). cliVersion() reads build info that can change
// across build setups, and calling it here would only repeat the code.
const prefix = "actions-lock version "
out := stdout.String()
if !strings.HasPrefix(out, prefix) || !strings.HasSuffix(out, "\n") {
t.Fatalf("unexpected output format: %q", out)
}
version := strings.TrimSuffix(strings.TrimPrefix(out, prefix), "\n")
if version == "" {
t.Fatalf("expected non-empty version in stdout: %q", out)
}
}
Loading