|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stackvista/stackstate-cli/internal/di" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestStackPackCommand_FeatureGating(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + envVarValue string |
| 16 | + expectScaffoldCommand bool |
| 17 | + description string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "scaffold command hidden by default", |
| 21 | + envVarValue: "", |
| 22 | + expectScaffoldCommand: false, |
| 23 | + description: "When environment variable is not set, scaffold command should be hidden", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "scaffold command visible when env var is set to 1", |
| 27 | + envVarValue: "1", |
| 28 | + expectScaffoldCommand: true, |
| 29 | + description: "When environment variable is set to '1', scaffold command should be visible", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "scaffold command visible when env var is set to any value", |
| 33 | + envVarValue: "true", |
| 34 | + expectScaffoldCommand: true, |
| 35 | + description: "When environment variable is set to any non-empty value, scaffold command should be visible", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "scaffold command visible when env var is set to enabled", |
| 39 | + envVarValue: "enabled", |
| 40 | + expectScaffoldCommand: true, |
| 41 | + description: "When environment variable is set to 'enabled', scaffold command should be visible", |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + // Store original environment value to restore later |
| 48 | + originalValue := os.Getenv("STS_EXPERIMENTAL_STACKPACK_SCAFFOLD") |
| 49 | + defer func() { |
| 50 | + if originalValue == "" { |
| 51 | + os.Unsetenv("STS_EXPERIMENTAL_STACKPACK_SCAFFOLD") |
| 52 | + } else { |
| 53 | + os.Setenv("STS_EXPERIMENTAL_STACKPACK_SCAFFOLD", originalValue) |
| 54 | + } |
| 55 | + }() |
| 56 | + |
| 57 | + // Set the environment variable for this test |
| 58 | + if tt.envVarValue == "" { |
| 59 | + os.Unsetenv("STS_EXPERIMENTAL_STACKPACK_SCAFFOLD") |
| 60 | + } else { |
| 61 | + err := os.Setenv("STS_EXPERIMENTAL_STACKPACK_SCAFFOLD", tt.envVarValue) |
| 62 | + require.NoError(t, err) |
| 63 | + } |
| 64 | + |
| 65 | + // Create the command |
| 66 | + cli := di.NewMockDeps(t) |
| 67 | + cmd := StackPackCommand(&cli.Deps) |
| 68 | + |
| 69 | + // Check if scaffold command exists |
| 70 | + scaffoldCmd, _, err := cmd.Find([]string{"scaffold"}) |
| 71 | + |
| 72 | + if tt.expectScaffoldCommand { |
| 73 | + assert.NoError(t, err, tt.description) |
| 74 | + assert.NotNil(t, scaffoldCmd, tt.description) |
| 75 | + assert.Equal(t, "scaffold", scaffoldCmd.Use, tt.description) |
| 76 | + } else { |
| 77 | + assert.Error(t, err, tt.description) |
| 78 | + assert.Contains(t, err.Error(), "unknown command", tt.description) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestStackPackCommand_AlwaysPresentCommands(t *testing.T) { |
| 85 | + // Ensure that other commands are always present regardless of environment variable |
| 86 | + cli := di.NewMockDeps(t) |
| 87 | + cmd := StackPackCommand(&cli.Deps) |
| 88 | + |
| 89 | + expectedCommands := []string{ |
| 90 | + "upload", |
| 91 | + "list", |
| 92 | + "list-instances", |
| 93 | + "install", |
| 94 | + "list-parameters", |
| 95 | + "uninstall", |
| 96 | + "upgrade", |
| 97 | + "confirm-manual-steps", |
| 98 | + "describe", |
| 99 | + } |
| 100 | + |
| 101 | + for _, cmdName := range expectedCommands { |
| 102 | + t.Run("command_"+cmdName+"_always_present", func(t *testing.T) { |
| 103 | + foundCmd, _, err := cmd.Find([]string{cmdName}) |
| 104 | + assert.NoError(t, err, "Command %s should always be present", cmdName) |
| 105 | + assert.NotNil(t, foundCmd, "Command %s should always be present", cmdName) |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments