Skip to content

Commit

Permalink
enable test framework for versionCommand and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgopack4 committed Sep 19, 2024
1 parent 9d1e979 commit 841d98d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ configuration is provided, ocb will generate a default Collector.
return nil, err
}
// version of this binary
cmd.AddCommand(versionCommand())
cmd.AddCommand(versionCommand(binVersion))

return cmd, nil
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/builder/internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
)

type debugReadBuildInfoFunc func() (info *debug.BuildInfo, ok bool)
type binVersionFunc func(fn debugReadBuildInfoFunc) (string, error)

// binVersion returns the version of the binary.
// If the version is not set, it attempts to read the build information.
Expand All @@ -30,14 +31,14 @@ func binVersion(fn debugReadBuildInfoFunc) (string, error) {
return info.Main.Version, nil
}

func versionCommand() *cobra.Command {
func versionCommand(fn binVersionFunc) *cobra.Command {
var err error
return &cobra.Command{
Use: "version",
Short: "Version of ocb",
Long: "Prints the version of the ocb binary",
RunE: func(cmd *cobra.Command, _ []string) error {
version, err = binVersion(debug.ReadBuildInfo)
version, err = fn(debug.ReadBuildInfo)
if err != nil {
return err
}
Expand Down
70 changes: 69 additions & 1 deletion cmd/builder/internal/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
package internal

import (
"bytes"
"context"
"fmt"
"os"
"runtime/debug"
"testing"

"github.com/spf13/cobra"
)

// Mock debug.ReadBuildInfo function
Expand All @@ -15,7 +20,7 @@ var readBuildInfo = debug.ReadBuildInfo
func TestBinVersion(t *testing.T) {
// Test case: version is set
version = "v1.0.0"
v, err := binVersion(debug.ReadBuildInfo)
v, err := binVersion(readBuildInfo)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
Expand Down Expand Up @@ -53,3 +58,66 @@ func TestBinVersion(t *testing.T) {
t.Fatalf("expected empty version, got %v", v)
}
}

var validBinVersionFunc binVersionFunc = func(fn debugReadBuildInfoFunc) (string, error) {
return "v1.0.0", nil
}

var invalidBinVersionFunc binVersionFunc = func(fn debugReadBuildInfoFunc) (string, error) {
return "", fmt.Errorf("failed to get version")
}

func TestVersionCommand(t *testing.T) {
tests := []struct {
name string
binVersion binVersionFunc
expectedOutput string
expectedError bool
}{
{
name: "valid version",
binVersion: validBinVersionFunc,
expectedOutput: "ocb version v1.0.0\n",
expectedError: false,
},
{
name: "error in binVersion",
binVersion: invalidBinVersionFunc,
expectedOutput: "",
expectedError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set a mock parent command name
parentCmd := &cobra.Command{
Use: "ocb <command>",
}
// Create the command
var cmd = versionCommand(tt.binVersion)
parentCmd.AddCommand(cmd)
// Capture the output
output := bytes.NewBufferString("")
err_output := bytes.NewBufferString("")
cmd.SetOut(output)
cmd.SetErr(err_output)
// Create a new context with a fake value
type contextKey string
ctx := context.WithValue(context.Background(), contextKey("key"), "value")
// Set fake CLI arguments
fakeArgs := []string{"cmd", "version"}
os.Args = fakeArgs
// Execute the command
err := cmd.ExecuteContext(ctx)
// Check for expected error
if (err != nil) != tt.expectedError {
t.Fatalf("expected error: %v, got: %v", tt.expectedError, err)
}
// Check for expected output
if output.String() != tt.expectedOutput {
t.Fatalf("expected output: %v, got: %v", tt.expectedOutput, output.String())
}
})
}
}

0 comments on commit 841d98d

Please sign in to comment.