Skip to content

Commit

Permalink
add test for newfeaturecommand
Browse files Browse the repository at this point in the history
Signed-off-by: danish9039 <[email protected]>
  • Loading branch information
danish9039 committed Jan 9, 2025
1 parent 2110dc8 commit 017a707
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions otelcol/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package otelcol

import (
"context"
"io"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -159,3 +161,59 @@ func Test_UseUnifiedEnvVarExpansionRules(t *testing.T) {
})
}
}

func TestNewFeaturesCommand(t *testing.T) {
t.Run("list all features", func(t *testing.T) {
cmd := newFeaturesCommand()
require.NotNil(t, cmd)

// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

err := cmd.RunE(cmd, []string{})
require.NoError(t, err)

w.Close()
out, _ := io.ReadAll(r)
os.Stdout = oldStdout

output := string(out)
assert.Contains(t, output, "ID")
assert.Contains(t, output, "Enabled")
assert.Contains(t, output, "Stage")
assert.Contains(t, output, "Description")
})
t.Run("specific feature details", func(t *testing.T) {
cmd := newFeaturesCommand()

// Register a test feature gate in the global registry
featuregate.GlobalRegistry().MustRegister("test.feature", featuregate.StageBeta,
featuregate.WithRegisterDescription("Test feature description"))

// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

err := cmd.RunE(cmd, []string{"test.feature"})
require.NoError(t, err)

w.Close()
out, _ := io.ReadAll(r)
os.Stdout = oldStdout

output := string(out)
assert.Contains(t, output, "Feature: test.feature")
assert.Contains(t, output, "Description: Test feature description")
assert.Contains(t, output, "Stage: Beta")
})

t.Run("non-existent feature", func(t *testing.T) {
cmd := newFeaturesCommand()
err := cmd.RunE(cmd, []string{"non.existent.feature"})
require.Error(t, err)
assert.Contains(t, err.Error(), "feature \"non.existent.feature\" not found")
})
}

0 comments on commit 017a707

Please sign in to comment.