github.com/saucelabs/saucectl@v0.175.1/internal/cypress/v1/config_test.go (about) 1 package v1 2 3 import ( 4 "errors" 5 "reflect" 6 "testing" 7 8 "github.com/saucelabs/saucectl/internal/config" 9 "github.com/saucelabs/saucectl/internal/saucereport" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestFilterSuites(t *testing.T) { 14 testCase := []struct { 15 name string 16 config *Project 17 suiteName string 18 expConfig Project 19 expErr string 20 }{ 21 { 22 name: "filtered suite exists in config", 23 config: &Project{Suites: []Suite{ 24 { 25 Name: "suite1", 26 }, 27 { 28 Name: "suite2", 29 }, 30 }}, 31 suiteName: "suite1", 32 expConfig: Project{Suites: []Suite{ 33 { 34 Name: "suite1", 35 }, 36 }}, 37 }, 38 { 39 name: "filtered suite does not exist in config", 40 config: &Project{Suites: []Suite{ 41 { 42 Name: "suite1", 43 }, 44 { 45 Name: "suite2", 46 }, 47 }}, 48 suiteName: "suite3", 49 expConfig: Project{Suites: []Suite{ 50 { 51 Name: "suite1", 52 }, 53 { 54 Name: "suite2", 55 }, 56 }}, 57 expErr: "no suite named 'suite3' found", 58 }, 59 } 60 61 for _, tc := range testCase { 62 t.Run(tc.name, func(t *testing.T) { 63 err := tc.config.FilterSuites(tc.suiteName) 64 if err != nil { 65 assert.Equal(t, tc.expErr, err.Error()) 66 } 67 assert.True(t, reflect.DeepEqual(*tc.config, tc.expConfig)) 68 }) 69 } 70 } 71 72 func TestCypressV1_Validate(t *testing.T) { 73 testCase := []struct { 74 name string 75 project *Project 76 expErr bool 77 }{ 78 { 79 name: "empty cypress version", 80 project: &Project{ 81 Cypress: Cypress{}, 82 }, 83 expErr: true, 84 }, 85 { 86 name: "empty invalid docker config", 87 project: &Project{ 88 Cypress: Cypress{Version: "v1.1.1"}, 89 }, 90 expErr: true, 91 }, 92 { 93 name: "invalid rootDir", 94 project: &Project{ 95 Cypress: Cypress{Version: "v1.1.1"}, 96 RootDir: "./invalid_file", 97 }, 98 expErr: true, 99 }, 100 { 101 name: "invalid region", 102 project: &Project{ 103 Cypress: Cypress{Version: "v1.1.1"}, 104 RootDir: "../", 105 Sauce: config.SauceConfig{Region: ""}, 106 }, 107 expErr: true, 108 }, 109 { 110 name: "empty suite", 111 project: &Project{ 112 Cypress: Cypress{Version: "v1.1.1"}, 113 RootDir: "../", 114 Sauce: config.SauceConfig{Region: "us-west-1"}, 115 Suites: []Suite{}, 116 }, 117 expErr: true, 118 }, 119 { 120 name: "dup suite", 121 project: &Project{ 122 Cypress: Cypress{Version: "v1.1.1"}, 123 RootDir: "../", 124 Sauce: config.SauceConfig{Region: "us-west-1"}, 125 Suites: []Suite{ 126 Suite{ 127 Name: "suite1", 128 Browser: "chrome", 129 Config: SuiteConfig{ 130 SpecPattern: []string{"a"}, 131 }, 132 }, 133 Suite{ 134 Name: "suite1", 135 Browser: "chrome", 136 Config: SuiteConfig{ 137 SpecPattern: []string{"a"}, 138 }, 139 }, 140 }, 141 }, 142 expErr: true, 143 }, 144 { 145 name: "symbol in suite name", 146 project: &Project{ 147 Cypress: Cypress{Version: "v1.1.1"}, 148 RootDir: "../", 149 Sauce: config.SauceConfig{Region: "us-west-1"}, 150 Suites: []Suite{ 151 Suite{ 152 Name: string('\u212A'), 153 Browser: "chrome", 154 Config: SuiteConfig{ 155 SpecPattern: []string{"a"}, 156 }, 157 }, 158 }, 159 }, 160 expErr: true, 161 }, 162 { 163 name: "empty browser name", 164 project: &Project{ 165 Cypress: Cypress{Version: "v1.1.1"}, 166 RootDir: "../", 167 Sauce: config.SauceConfig{Region: "us-west-1"}, 168 Suites: []Suite{ 169 Suite{ 170 Name: "suite 1", 171 Config: SuiteConfig{ 172 SpecPattern: []string{"a"}, 173 }, 174 }, 175 Suite{ 176 Name: "suite 2", 177 Config: SuiteConfig{ 178 SpecPattern: []string{"a"}, 179 }, 180 }, 181 }, 182 }, 183 expErr: true, 184 }, 185 { 186 name: "empty testingType", 187 project: &Project{ 188 Cypress: Cypress{Version: "v1.1.1"}, 189 RootDir: "../", 190 Sauce: config.SauceConfig{Region: "us-west-1"}, 191 Suites: []Suite{ 192 Suite{ 193 Name: "suite 1", 194 Browser: "chrome", 195 Config: SuiteConfig{ 196 TestingType: "e2e", 197 SpecPattern: []string{"a"}, 198 }, 199 }, 200 Suite{ 201 Name: "suite 2", 202 Browser: "chrome", 203 Config: SuiteConfig{ 204 TestingType: "e2e", 205 SpecPattern: []string{"a"}, 206 }, 207 }, 208 }, 209 }, 210 expErr: true, 211 }, 212 213 { 214 name: "empty specPattern", 215 project: &Project{ 216 Cypress: Cypress{Version: "v1.1.1"}, 217 RootDir: "../", 218 Sauce: config.SauceConfig{Region: "us-west-1"}, 219 Suites: []Suite{ 220 Suite{ 221 Name: "suite 1", 222 Browser: "chrome", 223 Config: SuiteConfig{ 224 TestingType: "e2e", 225 SpecPattern: []string{"a"}, 226 }, 227 }, 228 Suite{ 229 Name: "suite 2", 230 Browser: "chrome", 231 Config: SuiteConfig{ 232 TestingType: "e2e", 233 }, 234 }, 235 }, 236 }, 237 expErr: true, 238 }, 239 } 240 for _, tc := range testCase { 241 t.Run(tc.name, func(t *testing.T) { 242 err := tc.project.Validate() 243 if err != nil { 244 assert.True(t, tc.expErr) 245 } 246 }) 247 } 248 } 249 250 func TestCyress_CleanPackages(t *testing.T) { 251 testCase := []struct { 252 name string 253 project Project 254 expResult map[string]string 255 }{ 256 { 257 name: "clean cypress package in npm packages", 258 project: Project{Npm: config.Npm{ 259 Packages: map[string]string{ 260 "cypress": "10.1.0", 261 }, 262 }}, 263 expResult: map[string]string{}, 264 }, 265 { 266 name: "no need to clean npm packages", 267 project: Project{Npm: config.Npm{ 268 Packages: map[string]string{ 269 "lodash": "*", 270 }, 271 }}, 272 expResult: map[string]string{ 273 "lodash": "*", 274 }, 275 }, 276 } 277 for _, tc := range testCase { 278 t.Run(tc.name, func(t *testing.T) { 279 tc.project.CleanPackages() 280 assert.Equal(t, tc.expResult, tc.project.Npm.Packages) 281 }) 282 } 283 } 284 285 func TestCypressV1_FilterFailedTests(t *testing.T) { 286 testcases := []struct { 287 name string 288 suiteName string 289 report saucereport.SauceReport 290 project *Project 291 expResult string 292 expErr error 293 }{ 294 { 295 name: "it should set failed tests to specified suite", 296 suiteName: "my suite", 297 report: saucereport.SauceReport{ 298 Status: saucereport.StatusFailed, 299 Suites: []saucereport.Suite{ 300 { 301 Name: "my suite", 302 Status: saucereport.StatusFailed, 303 Tests: []saucereport.Test{ 304 { 305 Status: saucereport.StatusFailed, 306 Name: "failed test1", 307 }, 308 { 309 Status: saucereport.StatusFailed, 310 Name: "failed test2", 311 }, 312 }, 313 }, 314 }, 315 }, 316 project: &Project{ 317 Suites: []Suite{ 318 { 319 Name: "my suite", 320 }, 321 }, 322 }, 323 expResult: "failed test1;failed test2", 324 expErr: nil, 325 }, 326 { 327 name: "it should keep the original settings when suiteName doesn't exist in the project", 328 suiteName: "my suite2", 329 report: saucereport.SauceReport{ 330 Status: saucereport.StatusFailed, 331 Suites: []saucereport.Suite{ 332 { 333 Name: "my suite", 334 Status: saucereport.StatusFailed, 335 Tests: []saucereport.Test{ 336 { 337 Status: saucereport.StatusFailed, 338 Name: "failed test1", 339 }, 340 { 341 Status: saucereport.StatusFailed, 342 Name: "failed test2", 343 }, 344 }, 345 }, 346 }, 347 }, 348 project: &Project{ 349 Suites: []Suite{ 350 { 351 Name: "my suite", 352 }, 353 }, 354 }, 355 expResult: "", 356 expErr: errors.New("suite(my suite2) not found"), 357 }, 358 { 359 name: "it should keep the original settings when no failed test in SauceReport", 360 suiteName: "my suite", 361 report: saucereport.SauceReport{ 362 Status: saucereport.StatusPassed, 363 Suites: []saucereport.Suite{ 364 { 365 Name: "my suite", 366 Status: saucereport.StatusPassed, 367 Tests: []saucereport.Test{ 368 { 369 Status: saucereport.StatusPassed, 370 Name: "passed test1", 371 }, 372 { 373 Status: saucereport.StatusSkipped, 374 Name: "skipped test2", 375 }, 376 }, 377 }, 378 }, 379 }, 380 project: &Project{ 381 Suites: []Suite{ 382 { 383 Name: "my suite", 384 }, 385 }, 386 }, 387 expResult: "", 388 expErr: nil, 389 }, 390 } 391 for _, tc := range testcases { 392 t.Run(tc.name, func(t *testing.T) { 393 err := tc.project.FilterFailedTests(tc.suiteName, tc.report) 394 assert.Equal(t, tc.expErr, err) 395 assert.Equal(t, tc.expResult, tc.project.Suites[0].Config.Env["grep"]) 396 }) 397 } 398 }