|
| 1 | +package projects |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestExtensionValidation(t *testing.T) { |
| 9 | + baseProject := Project{ |
| 10 | + Name: "Test Project", |
| 11 | + Description: "A test project", |
| 12 | + SchemaVersion: "1.1.0", |
| 13 | + MaturityLog: []MaturityEntry{ |
| 14 | + { |
| 15 | + Phase: "incubating", |
| 16 | + Date: time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC), |
| 17 | + Issue: "https://github.com/cncf/toc/issues/123", |
| 18 | + }, |
| 19 | + }, |
| 20 | + Repositories: []string{"https://github.com/test/repo"}, |
| 21 | + } |
| 22 | + |
| 23 | + t.Run("valid extension", func(t *testing.T) { |
| 24 | + project := baseProject |
| 25 | + project.Extensions = map[string]Extension{ |
| 26 | + "my-tool": { |
| 27 | + Metadata: &ExtensionMetadata{ |
| 28 | + Author: "Test Author", |
| 29 | + Homepage: "https://example.com", |
| 30 | + Repository: "https://github.com/test/tool", |
| 31 | + License: "Apache-2.0", |
| 32 | + Version: "1.0.0", |
| 33 | + }, |
| 34 | + Config: map[string]interface{}{ |
| 35 | + "enabled": true, |
| 36 | + "setting": "value", |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + errors := validateProjectStruct(project) |
| 42 | + if len(errors) != 0 { |
| 43 | + t.Errorf("Expected no errors for valid extension, got: %v", errors) |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("extension without schema version", func(t *testing.T) { |
| 48 | + project := baseProject |
| 49 | + project.SchemaVersion = "" |
| 50 | + project.Extensions = map[string]Extension{ |
| 51 | + "my-tool": {Config: map[string]interface{}{"key": "value"}}, |
| 52 | + } |
| 53 | + |
| 54 | + errors := validateProjectStruct(project) |
| 55 | + found := false |
| 56 | + for _, err := range errors { |
| 57 | + if err == "extensions require schema_version >= 1.1.0" { |
| 58 | + found = true |
| 59 | + break |
| 60 | + } |
| 61 | + } |
| 62 | + if !found { |
| 63 | + t.Errorf("Expected schema version error, got: %v", errors) |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("extension with old schema version", func(t *testing.T) { |
| 68 | + project := baseProject |
| 69 | + project.SchemaVersion = "1.0.0" |
| 70 | + project.Extensions = map[string]Extension{ |
| 71 | + "my-tool": {Config: map[string]interface{}{"key": "value"}}, |
| 72 | + } |
| 73 | + |
| 74 | + errors := validateProjectStruct(project) |
| 75 | + found := false |
| 76 | + for _, err := range errors { |
| 77 | + if err == "extensions require schema_version >= 1.1.0" { |
| 78 | + found = true |
| 79 | + break |
| 80 | + } |
| 81 | + } |
| 82 | + if !found { |
| 83 | + t.Errorf("Expected schema version error, got: %v", errors) |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("reserved extension name", func(t *testing.T) { |
| 88 | + project := baseProject |
| 89 | + project.Extensions = map[string]Extension{ |
| 90 | + "name": {Config: map[string]interface{}{"key": "value"}}, |
| 91 | + } |
| 92 | + |
| 93 | + errors := validateProjectStruct(project) |
| 94 | + found := false |
| 95 | + for _, err := range errors { |
| 96 | + if err == "extensions.name: 'name' is a reserved name" { |
| 97 | + found = true |
| 98 | + break |
| 99 | + } |
| 100 | + } |
| 101 | + if !found { |
| 102 | + t.Errorf("Expected reserved name error, got: %v", errors) |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("invalid extension name format", func(t *testing.T) { |
| 107 | + project := baseProject |
| 108 | + project.Extensions = map[string]Extension{ |
| 109 | + "my tool!": {Config: map[string]interface{}{"key": "value"}}, |
| 110 | + } |
| 111 | + |
| 112 | + errors := validateProjectStruct(project) |
| 113 | + found := false |
| 114 | + for _, err := range errors { |
| 115 | + if err == "extensions.my tool!: invalid name format (use alphanumeric, hyphens, underscores, dots)" { |
| 116 | + found = true |
| 117 | + break |
| 118 | + } |
| 119 | + } |
| 120 | + if !found { |
| 121 | + t.Errorf("Expected invalid name format error, got: %v", errors) |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + t.Run("invalid metadata URL", func(t *testing.T) { |
| 126 | + project := baseProject |
| 127 | + project.Extensions = map[string]Extension{ |
| 128 | + "my-tool": { |
| 129 | + Metadata: &ExtensionMetadata{ |
| 130 | + Homepage: "not-a-url", |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + errors := validateProjectStruct(project) |
| 136 | + found := false |
| 137 | + for _, err := range errors { |
| 138 | + if err == "extensions.my-tool.metadata.homepage is not a valid URL" { |
| 139 | + found = true |
| 140 | + break |
| 141 | + } |
| 142 | + } |
| 143 | + if !found { |
| 144 | + t.Errorf("Expected invalid URL error, got: %v", errors) |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
| 148 | + |
| 149 | +func TestIsValidExtensionName(t *testing.T) { |
| 150 | + testCases := []struct { |
| 151 | + name string |
| 152 | + valid bool |
| 153 | + }{ |
| 154 | + {"my-tool", true}, |
| 155 | + {"my_tool", true}, |
| 156 | + {"my.tool", true}, |
| 157 | + {"MyTool123", true}, |
| 158 | + {"tool", true}, |
| 159 | + {"my tool", false}, |
| 160 | + {"my-tool!", false}, |
| 161 | + {"", false}, |
| 162 | + {"tool@name", false}, |
| 163 | + {"tool#name", false}, |
| 164 | + } |
| 165 | + |
| 166 | + for _, tc := range testCases { |
| 167 | + result := isValidExtensionName(tc.name) |
| 168 | + if result != tc.valid { |
| 169 | + t.Errorf("isValidExtensionName(%q) = %v, expected %v", tc.name, result, tc.valid) |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func TestIsVersionAtLeast(t *testing.T) { |
| 175 | + testCases := []struct { |
| 176 | + version string |
| 177 | + minVersion string |
| 178 | + expected bool |
| 179 | + }{ |
| 180 | + {"1.1.0", "1.1.0", true}, |
| 181 | + {"1.2.0", "1.1.0", true}, |
| 182 | + {"2.0.0", "1.1.0", true}, |
| 183 | + {"1.0.0", "1.1.0", false}, |
| 184 | + {"0.9.0", "1.1.0", false}, |
| 185 | + } |
| 186 | + |
| 187 | + for _, tc := range testCases { |
| 188 | + result := isVersionAtLeast(tc.version, tc.minVersion) |
| 189 | + if result != tc.expected { |
| 190 | + t.Errorf("isVersionAtLeast(%q, %q) = %v, expected %v", |
| 191 | + tc.version, tc.minVersion, result, tc.expected) |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +func TestBackwardCompatibility(t *testing.T) { |
| 197 | + // Test that projects without extensions still validate correctly |
| 198 | + project := Project{ |
| 199 | + Name: "Legacy Project", |
| 200 | + Description: "A project without extensions", |
| 201 | + SchemaVersion: "1.0.0", |
| 202 | + MaturityLog: []MaturityEntry{ |
| 203 | + { |
| 204 | + Phase: "incubating", |
| 205 | + Date: time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC), |
| 206 | + Issue: "https://github.com/cncf/toc/issues/123", |
| 207 | + }, |
| 208 | + }, |
| 209 | + Repositories: []string{"https://github.com/test/repo"}, |
| 210 | + } |
| 211 | + |
| 212 | + errors := validateProjectStruct(project) |
| 213 | + if len(errors) != 0 { |
| 214 | + t.Errorf("Expected no errors for legacy project, got: %v", errors) |
| 215 | + } |
| 216 | +} |
0 commit comments