|
1 | 1 | /*
|
2 |
| - * © 2025 Snyk Limited |
| 2 | + * 2025 Snyk Limited |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -103,6 +103,132 @@ func Test_GetOrCreateFolderConfig_shouldReturnExistingFolderConfig(t *testing.T)
|
103 | 103 | require.Equal(t, expected, actual)
|
104 | 104 | }
|
105 | 105 |
|
| 106 | +func Test_mergeFolderConfigs(t *testing.T) { |
| 107 | + t.Run("different folder paths should return first", func(t *testing.T) { |
| 108 | + first := &types.FolderConfig{ |
| 109 | + FolderPath: "/path1", |
| 110 | + AdditionalParameters: []string{"--param1=value1"}, |
| 111 | + LocalBranches: []string{"branch1"}, |
| 112 | + BaseBranch: "main", |
| 113 | + } |
| 114 | + |
| 115 | + second := &types.FolderConfig{ |
| 116 | + FolderPath: "/path2", |
| 117 | + AdditionalParameters: []string{"--param2=value2"}, |
| 118 | + LocalBranches: []string{"branch2"}, |
| 119 | + BaseBranch: "develop", |
| 120 | + } |
| 121 | + |
| 122 | + result := mergeFolderConfigs(first, second) |
| 123 | + |
| 124 | + require.Equal(t, first, result) |
| 125 | + require.Equal(t, 1, len(result.AdditionalParameters)) |
| 126 | + require.Equal(t, "--param1=value1", result.AdditionalParameters[0]) |
| 127 | + require.Equal(t, 1, len(result.LocalBranches)) |
| 128 | + require.Equal(t, "main", result.BaseBranch) |
| 129 | + }) |
| 130 | + t.Run("same folder paths with complete merging", func(t *testing.T) { |
| 131 | + scanCommandConfig1 := types.ScanCommandConfig{ |
| 132 | + PreScanCommand: "/cmd1", |
| 133 | + } |
| 134 | + |
| 135 | + scanCommandConfig2 := types.ScanCommandConfig{ |
| 136 | + PreScanCommand: "/cmd2", |
| 137 | + } |
| 138 | + |
| 139 | + first := &types.FolderConfig{ |
| 140 | + FolderPath: "/path1", |
| 141 | + AdditionalParameters: []string{"--param1=value1"}, |
| 142 | + LocalBranches: nil, |
| 143 | + BaseBranch: "", |
| 144 | + ScanCommandConfig: map[product.Product]types.ScanCommandConfig{ |
| 145 | + product.ProductOpenSource: scanCommandConfig1, |
| 146 | + }, |
| 147 | + ReferenceFolderPath: "", |
| 148 | + } |
| 149 | + |
| 150 | + second := &types.FolderConfig{ |
| 151 | + FolderPath: "/path1", |
| 152 | + AdditionalParameters: []string{"--param2=value2"}, |
| 153 | + LocalBranches: []string{"branch2"}, |
| 154 | + BaseBranch: "develop", |
| 155 | + ScanCommandConfig: map[product.Product]types.ScanCommandConfig{ |
| 156 | + product.ProductOpenSource: scanCommandConfig2, |
| 157 | + }, |
| 158 | + ReferenceFolderPath: "/ref/path", |
| 159 | + } |
| 160 | + |
| 161 | + result := mergeFolderConfigs(first, second) |
| 162 | + |
| 163 | + // Check that it's still the first object (modified) |
| 164 | + require.Equal(t, first, result) |
| 165 | + |
| 166 | + // Check additional parameters are merged |
| 167 | + require.Equal(t, 2, len(result.AdditionalParameters)) |
| 168 | + require.Contains(t, result.AdditionalParameters, "--param1=value1") |
| 169 | + require.Contains(t, result.AdditionalParameters, "--param2=value2") |
| 170 | + |
| 171 | + // Check other fields are taken from second |
| 172 | + require.Equal(t, second.LocalBranches, result.LocalBranches) |
| 173 | + require.Equal(t, second.BaseBranch, result.BaseBranch) |
| 174 | + require.Equal(t, second.ScanCommandConfig, result.ScanCommandConfig) |
| 175 | + require.Equal(t, second.ReferenceFolderPath, result.ReferenceFolderPath) |
| 176 | + }) |
| 177 | + t.Run("parameter deduplication", func(t *testing.T) { |
| 178 | + first := &types.FolderConfig{ |
| 179 | + FolderPath: "/path1", |
| 180 | + AdditionalParameters: []string{"--param1=value1", "--param2=valueA"}, |
| 181 | + } |
| 182 | + |
| 183 | + second := &types.FolderConfig{ |
| 184 | + FolderPath: "/path1", |
| 185 | + AdditionalParameters: []string{"--param2=valueB", "--param3=value3"}, |
| 186 | + } |
| 187 | + |
| 188 | + result := mergeFolderConfigs(first, second) |
| 189 | + |
| 190 | + // Should have 3 parameters (param2 from second should be ignored) |
| 191 | + require.Equal(t, 3, len(result.AdditionalParameters)) |
| 192 | + require.Contains(t, result.AdditionalParameters, "--param1=value1") |
| 193 | + require.Contains(t, result.AdditionalParameters, "--param2=valueA") // first takes precedence |
| 194 | + require.Contains(t, result.AdditionalParameters, "--param3=value3") |
| 195 | + }) |
| 196 | + t.Run("partial merging", func(t *testing.T) { |
| 197 | + scanCommandConfig1 := types.ScanCommandConfig{ |
| 198 | + PreScanCommand: "/cmd1", |
| 199 | + } |
| 200 | + |
| 201 | + first := &types.FolderConfig{ |
| 202 | + FolderPath: "/path1", |
| 203 | + AdditionalParameters: []string{"--param1=value1"}, |
| 204 | + LocalBranches: []string{"branch1"}, |
| 205 | + BaseBranch: "main", |
| 206 | + ScanCommandConfig: map[product.Product]types.ScanCommandConfig{ |
| 207 | + product.ProductOpenSource: scanCommandConfig1, |
| 208 | + }, |
| 209 | + ReferenceFolderPath: "/ref/path1", |
| 210 | + } |
| 211 | + |
| 212 | + second := &types.FolderConfig{ |
| 213 | + FolderPath: "/path1", |
| 214 | + AdditionalParameters: []string{"--param2=value2"}, |
| 215 | + LocalBranches: nil, // nil, should not replace |
| 216 | + BaseBranch: "", // empty, should not replace |
| 217 | + ScanCommandConfig: nil, // nil, should not replace |
| 218 | + ReferenceFolderPath: "", // empty, should not replace |
| 219 | + } |
| 220 | + |
| 221 | + result := mergeFolderConfigs(first, second) |
| 222 | + |
| 223 | + // Check that fields from second didn't overwrite first when they were nil/empty |
| 224 | + require.Equal(t, 2, len(result.AdditionalParameters)) // Additional params still merge |
| 225 | + require.Equal(t, first.LocalBranches, result.LocalBranches) |
| 226 | + require.Equal(t, first.BaseBranch, result.BaseBranch) |
| 227 | + require.Equal(t, first.ScanCommandConfig, result.ScanCommandConfig) |
| 228 | + require.Equal(t, first.ReferenceFolderPath, result.ReferenceFolderPath) |
| 229 | + }) |
| 230 | +} |
| 231 | + |
106 | 232 | func SetupConfigurationWithStorage(t *testing.T) (configuration.Configuration, string) {
|
107 | 233 | t.Helper()
|
108 | 234 | conf := configuration.NewWithOpts(configuration.WithAutomaticEnv())
|
|
0 commit comments