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
61 changes: 61 additions & 0 deletions data_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package main_test
import (
"os"
"regexp"
"strings"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -1373,4 +1374,64 @@ var suites = []FixtureSuite{
},
},
},
// version flag tests
{
{
Name: "version subcommand",
Config: FixtureConfig{
CliArgs: []string{"version"},
},
Expect: Results{
CliOutputRe: regexp.MustCompile(`.+`), // match and strip any content
CliOutput: "\n", // after strip, should be just newline
ExitCode: 0,
},
CheckFuncs: []CheckFunc{
func(t *testing.T, f Fixture, r Results) {
// version subcommand outputs just the version string
// should be "unknown" or "version commit date"
output := strings.TrimSpace(r.CliOutput)
if output == "" {
t.Errorf("version subcommand produced no output")
return
}
// valid outputs: "unknown" or multi-part version like "0.6.0 abc123 2025-11-09"
if output != "unknown" && !regexp.MustCompile(`^\S+`).MatchString(output) {
t.Errorf("version subcommand output format unexpected: %q", output)
}
},
},
},
{
Name: "--version flag",
Config: FixtureConfig{
CliArgs: []string{"--version"},
},
Expect: Results{
CliOutputRe: regexp.MustCompile(`.+`), // match and strip any content
CliOutput: "\n", // after strip, should be just newline
ExitCode: 0,
},
CheckFuncs: []CheckFunc{
func(t *testing.T, f Fixture, r Results) {
// --version flag outputs "otel-cli version <version>"
output := strings.TrimSpace(r.CliOutput)
if !strings.HasPrefix(output, "otel-cli version ") {
t.Errorf("--version flag should start with 'otel-cli version ', got: %q", output)
return
}
// extract version part after "otel-cli version "
version := strings.TrimPrefix(output, "otel-cli version ")
if version == "" {
t.Errorf("--version flag has no version content after prefix")
return
}
// valid version: "unknown" or multi-part like "0.6.0 abc123 2025-11-09"
if version != "unknown" && !regexp.MustCompile(`^\S+`).MatchString(version) {
t.Errorf("--version flag version format unexpected: %q", version)
}
},
},
},
},
}
1 change: 1 addition & 0 deletions otelcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func createRootCmd(config *Config) *cobra.Command {

cobra.EnableCommandSorting = false
rootCmd.Flags().SortFlags = false
rootCmd.Version = config.Version

Diag.NumArgs = len(os.Args) - 1
Diag.CliArgs = []string{}
Expand Down