|
| 1 | +/* |
| 2 | +Copyright © 2024 Christian Hernandez <christian@chernand.io> |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/spf13/cobra" |
| 24 | +) |
| 25 | + |
| 26 | +func TestProfileLongHelp(t *testing.T) { |
| 27 | + helpText := profileLongHelp() |
| 28 | + |
| 29 | + if len(helpText) == 0 { |
| 30 | + t.Error("Profile long help should not be empty") |
| 31 | + } |
| 32 | + |
| 33 | + // Check that help contains key information |
| 34 | + expectedPhrases := []string{ |
| 35 | + "You can use \"run\" to run the specified profile", |
| 36 | + "~/.bekind/profiles/{{name}}", |
| 37 | + "config.yaml", |
| 38 | + "--view flag", |
| 39 | + } |
| 40 | + |
| 41 | + for _, phrase := range expectedPhrases { |
| 42 | + if !contains(helpText, phrase) { |
| 43 | + t.Errorf("Help text should contain '%s'", phrase) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestGetProfileNames(t *testing.T) { |
| 49 | + // Create a temporary directory structure for testing |
| 50 | + tmpDir := t.TempDir() |
| 51 | + |
| 52 | + // Create test profile directories |
| 53 | + testProfiles := []string{"profile1", "profile2", "profile3"} |
| 54 | + for _, profile := range testProfiles { |
| 55 | + err := os.MkdirAll(filepath.Join(tmpDir, profile), 0755) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Failed to create test profile directory: %v", err) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Save original ProfileDir and restore after test |
| 62 | + originalProfileDir := ProfileDir |
| 63 | + defer func() { |
| 64 | + ProfileDir = originalProfileDir |
| 65 | + }() |
| 66 | + |
| 67 | + // Set ProfileDir to our test directory |
| 68 | + ProfileDir = tmpDir |
| 69 | + |
| 70 | + // Test getProfileNames |
| 71 | + profiles, err := getProfileNames() |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("getProfileNames() returned error: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + if len(profiles) != len(testProfiles) { |
| 77 | + t.Errorf("Expected %d profiles, got %d", len(testProfiles), len(profiles)) |
| 78 | + } |
| 79 | + |
| 80 | + // Check that all expected profiles are present |
| 81 | + for _, expectedProfile := range testProfiles { |
| 82 | + found := false |
| 83 | + for _, actualProfile := range profiles { |
| 84 | + if actualProfile == expectedProfile { |
| 85 | + found = true |
| 86 | + break |
| 87 | + } |
| 88 | + } |
| 89 | + if !found { |
| 90 | + t.Errorf("Expected profile '%s' not found in results", expectedProfile) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestGetProfileNamesNonexistentDir(t *testing.T) { |
| 96 | + // Save original ProfileDir and restore after test |
| 97 | + originalProfileDir := ProfileDir |
| 98 | + defer func() { |
| 99 | + ProfileDir = originalProfileDir |
| 100 | + }() |
| 101 | + |
| 102 | + // Set ProfileDir to a nonexistent directory |
| 103 | + ProfileDir = "/nonexistent/path" |
| 104 | + |
| 105 | + // Test getProfileNames with nonexistent directory |
| 106 | + profiles, err := getProfileNames() |
| 107 | + if err == nil { |
| 108 | + t.Error("getProfileNames() should return error for nonexistent directory") |
| 109 | + } |
| 110 | + |
| 111 | + if len(profiles) != 0 { |
| 112 | + t.Errorf("Expected empty profiles slice for nonexistent directory, got %d profiles", len(profiles)) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestProfileValidArgs(t *testing.T) { |
| 117 | + // Create a temporary directory structure for testing |
| 118 | + tmpDir := t.TempDir() |
| 119 | + |
| 120 | + // Create test profile directories |
| 121 | + testProfiles := []string{"test-profile1", "test-profile2"} |
| 122 | + for _, profile := range testProfiles { |
| 123 | + err := os.MkdirAll(filepath.Join(tmpDir, profile), 0755) |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("Failed to create test profile directory: %v", err) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Save original ProfileDir and restore after test |
| 130 | + originalProfileDir := ProfileDir |
| 131 | + defer func() { |
| 132 | + ProfileDir = originalProfileDir |
| 133 | + }() |
| 134 | + |
| 135 | + // Set ProfileDir to our test directory |
| 136 | + ProfileDir = tmpDir |
| 137 | + |
| 138 | + // Test profileValidArgs with no existing args (should return profiles) |
| 139 | + completions, directive := profileValidArgs(runCmd, []string{}, "") |
| 140 | + |
| 141 | + if directive != cobra.ShellCompDirectiveNoFileComp { |
| 142 | + t.Errorf("Expected ShellCompDirectiveNoFileComp, got %v", directive) |
| 143 | + } |
| 144 | + |
| 145 | + if len(completions) != len(testProfiles) { |
| 146 | + t.Errorf("Expected %d completions, got %d", len(testProfiles), len(completions)) |
| 147 | + } |
| 148 | + |
| 149 | + // Test profileValidArgs with existing args (should return no completions) |
| 150 | + completions, directive = profileValidArgs(runCmd, []string{"profile1"}, "") |
| 151 | + |
| 152 | + if directive != cobra.ShellCompDirectiveNoFileComp { |
| 153 | + t.Errorf("Expected ShellCompDirectiveNoFileComp, got %v", directive) |
| 154 | + } |
| 155 | + |
| 156 | + if len(completions) != 0 { |
| 157 | + t.Errorf("Expected no completions when args already provided, got %d", len(completions)) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// Helper function for string contains check |
| 162 | +func contains(s, substr string) bool { |
| 163 | + return len(s) >= len(substr) && (s == substr || len(substr) == 0 || |
| 164 | + func() bool { |
| 165 | + for i := 0; i <= len(s)-len(substr); i++ { |
| 166 | + if s[i:i+len(substr)] == substr { |
| 167 | + return true |
| 168 | + } |
| 169 | + } |
| 170 | + return false |
| 171 | + }()) |
| 172 | +} |
0 commit comments