github.com/gopinath-langote/1build@v1.7.0/testing/fixtures/command_unset_fixtures.go (about) 1 package fixtures 2 3 import ( 4 "io/ioutil" 5 "os" 6 "testing" 7 8 "github.com/gopinath-langote/1build/testing/def" 9 "github.com/gopinath-langote/1build/testing/utils" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func featureUnsetTestsData() []Test { 14 feature := "unset" 15 16 return []Test{ 17 shouldUnsetTheExistingCommand(feature), 18 shouldUnsetTheExistingCommandFromSpecifiedFile(feature), 19 unsetShouldFailWhenConfigurationFileIsNotFound(feature), 20 unsetShouldFailWhenConfigurationFileIsInInvalidFormat(feature), 21 unsetShouldFailWhenCommandIsNotFound(feature), 22 shouldUnsetMultipleCommands(feature), 23 shouldUnsetMultipleCommandsEvenWhenCommandIsNotFound1(feature), 24 shouldUnsetMultipleCommandsEvenWhenCommandIsNotFound2(feature), 25 shouldUnsetMultipleCommandsEvenWhenCommandIsNotFound3(feature), 26 shouldUnsetTheBeforeCommand(feature), 27 shouldUnsetTheAfterCommand(feature), 28 unsetBeforeShouldFailWhenBeforeIsNotFound(feature), 29 unsetAfterShouldFailWhenAfterIsNotFound(feature), 30 unsetSingleCommandShouldFailWhenInvalidCommandNameIsEntered(feature), 31 unsetMultipleCommandShouldFailWhenInvalidCommandNameIsEntered(feature), 32 } 33 } 34 35 func shouldUnsetTheExistingCommand(feature string) Test { 36 37 defaultFileContent := ` 38 project: Sample Project 39 commands: 40 - build: go build 41 ` 42 43 expectedOutput := `project: Sample Project 44 commands: [] 45 ` 46 47 return Test{ 48 Feature: feature, 49 Name: "shouldUnsetTheExistingCommand", 50 CmdArgs: Args("unset", "build"), 51 Setup: func(dir string) error { 52 return utils.CreateConfigFile(dir, defaultFileContent) 53 }, 54 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 55 filePath := dir + "/" + def.ConfigFileName 56 assert.FileExists(t, dir+"/"+def.ConfigFileName) 57 content, _ := ioutil.ReadFile(filePath) 58 return assert.Exactly(t, expectedOutput, string(content)) 59 }, 60 } 61 } 62 63 func shouldUnsetTheExistingCommandFromSpecifiedFile(feature string) Test { 64 65 defaultFileContent := ` 66 project: Sample Project 67 commands: 68 - build: go build 69 ` 70 71 expectedOutput := `project: Sample Project 72 commands: [] 73 ` 74 75 return Test{ 76 Feature: feature, 77 Name: "shouldUnsetTheExistingCommandFromSpecifiedFile", 78 CmdArgs: func(dir string) []string { 79 return []string{"unset", "build", "-f", dir + "/some-dir/some-config.yaml"} 80 }, 81 Setup: func(dir string) error { 82 _ = os.MkdirAll(dir+"/some-dir", 0750) 83 return utils.CreateConfigFileWithName(dir+"/some-dir", "some-config.yaml", defaultFileContent) 84 }, 85 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 86 filePath := dir + "/some-dir/some-config.yaml" 87 assert.FileExists(t, filePath) 88 content, _ := ioutil.ReadFile(filePath) 89 return assert.Exactly(t, expectedOutput, string(content)) 90 }, 91 } 92 } 93 94 func unsetShouldFailWhenCommandIsNotFound(feature string) Test { 95 96 defaultFileContent := ` 97 project: Sample Project 98 commands: 99 - build: go build 100 ` 101 102 return Test{ 103 Feature: feature, 104 Name: "unsetShouldFailWhenCommandIsNotFound", 105 CmdArgs: Args("unset", "Test"), 106 Setup: func(dir string) error { 107 return utils.CreateConfigFile(dir, defaultFileContent) 108 }, 109 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 110 return assert.Contains(t, actualOutput, "Following command(s) not found: Test") 111 }, 112 } 113 } 114 115 func unsetShouldFailWhenConfigurationFileIsNotFound(feature string) Test { 116 return Test{ 117 Feature: feature, 118 Name: "unsetShouldFailWhenConfigurationFileIsNotFound", 119 CmdArgs: Args("unset", "build"), 120 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 121 return assert.Contains(t, actualOutput, "no '"+def.ConfigFileName+"' file found") 122 }, 123 } 124 } 125 126 func unsetShouldFailWhenConfigurationFileIsInInvalidFormat(feature string) Test { 127 return Test{ 128 Feature: feature, 129 Name: "unsetShouldFailWhenConfigurationFileIsInInvalidFormat", 130 CmdArgs: Args("unset", "build"), 131 Setup: func(dir string) error { 132 return utils.CreateConfigFile(dir, "invalid config content") 133 }, 134 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 135 return assert.Contains(t, actualOutput, "Unable to parse '"+def.ConfigFileName+"' config file. Make sure file is in correct format.") 136 }, 137 } 138 } 139 140 func shouldUnsetMultipleCommands(feature string) Test { 141 142 defaultFileContent := ` 143 project: Sample Project 144 before: go before 145 after: go after 146 commands: 147 - build: go build 148 - test: go test 149 ` 150 151 expectedOutput := `project: Sample Project 152 commands: [] 153 ` 154 155 return Test{ 156 Feature: feature, 157 Name: "shouldUnsetMultipleCommands", 158 CmdArgs: Args("unset", "build", "test", "before", "after"), 159 Setup: func(dir string) error { 160 return utils.CreateConfigFile(dir, defaultFileContent) 161 }, 162 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 163 filePath := dir + "/" + def.ConfigFileName 164 assert.FileExists(t, dir+"/"+def.ConfigFileName) 165 content, _ := ioutil.ReadFile(filePath) 166 return assert.Exactly(t, expectedOutput, string(content)) 167 }, 168 } 169 } 170 171 func shouldUnsetMultipleCommandsEvenWhenCommandIsNotFound1(feature string) Test { 172 173 defaultFileContent := ` 174 project: Sample Project 175 commands: 176 - build: go build 177 - test: go test 178 ` 179 180 expectedOutput := `project: Sample Project 181 commands: [] 182 ` 183 184 return Test{ 185 Feature: feature, 186 Name: "shouldUnsetMultipleCommandsEvenWhenCommandIsNotFound1", 187 CmdArgs: Args("unset", "build", "test", "missingCmd"), 188 Setup: func(dir string) error { 189 return utils.CreateConfigFile(dir, defaultFileContent) 190 }, 191 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 192 filePath := dir + "/" + def.ConfigFileName 193 assert.FileExists(t, dir+"/"+def.ConfigFileName) 194 content, _ := ioutil.ReadFile(filePath) 195 196 testResult := assert.Contains(t, actualOutput, "Following command(s) not found: missingCmd") && 197 assert.Exactly(t, expectedOutput, string(content)) 198 199 return testResult 200 }, 201 } 202 } 203 204 func shouldUnsetMultipleCommandsEvenWhenCommandIsNotFound2(feature string) Test { 205 206 defaultFileContent := ` 207 project: Sample Project 208 commands: 209 - build: go build 210 - test: go test 211 - lint: go lint 212 ` 213 214 expectedOutput := `project: Sample Project 215 commands: 216 - lint: go lint 217 ` 218 219 return Test{ 220 Feature: feature, 221 Name: "shouldUnsetMultipleCommandsEvenWhenCommandIsNotFound2", 222 CmdArgs: Args("unset", "build", "missingCmd", "test", "missingCmd2"), 223 Setup: func(dir string) error { 224 return utils.CreateConfigFile(dir, defaultFileContent) 225 }, 226 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 227 filePath := dir + "/" + def.ConfigFileName 228 assert.FileExists(t, dir+"/"+def.ConfigFileName) 229 content, _ := ioutil.ReadFile(filePath) 230 231 testResult := assert.Contains(t, actualOutput, "Following command(s) not found: missingCmd, missingCmd2") && 232 assert.Exactly(t, expectedOutput, string(content)) 233 234 return testResult 235 }, 236 } 237 } 238 239 func shouldUnsetMultipleCommandsEvenWhenCommandIsNotFound3(feature string) Test { 240 241 defaultFileContent := ` 242 project: Sample Project 243 before: go before 244 after: go after 245 ` 246 247 expectedOutput := `project: Sample Project 248 commands: [] 249 ` 250 251 return Test{ 252 Feature: feature, 253 Name: "shouldUnsetMultipleCommandsEvenWhenCommandIsNotFound3", 254 CmdArgs: Args("unset", "before", "after", "missingCmd"), 255 Setup: func(dir string) error { 256 return utils.CreateConfigFile(dir, defaultFileContent) 257 }, 258 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 259 filePath := dir + "/" + def.ConfigFileName 260 assert.FileExists(t, dir+"/"+def.ConfigFileName) 261 content, _ := ioutil.ReadFile(filePath) 262 263 testResult := assert.Contains(t, actualOutput, "Following command(s) not found: missingCmd") && 264 assert.Exactly(t, expectedOutput, string(content)) 265 266 return testResult 267 }, 268 } 269 } 270 271 func shouldUnsetTheBeforeCommand(feature string) Test { 272 273 defaultFileContent := ` 274 project: Sample Project 275 before: yo 276 commands: [] 277 ` 278 279 expectedOutput := `project: Sample Project 280 commands: [] 281 ` 282 283 return Test{ 284 Feature: feature, 285 Name: "shouldUnsetBeforeAndAfterCommand", 286 CmdArgs: Args("unset", "before"), 287 Setup: func(dir string) error { 288 return utils.CreateConfigFile(dir, defaultFileContent) 289 }, 290 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 291 filePath := dir + "/" + def.ConfigFileName 292 assert.FileExists(t, dir+"/"+def.ConfigFileName) 293 content, _ := ioutil.ReadFile(filePath) 294 return assert.Exactly(t, expectedOutput, string(content)) 295 }, 296 } 297 } 298 299 func shouldUnsetTheAfterCommand(feature string) Test { 300 301 defaultFileContent := ` 302 project: Sample Project 303 after: yo 304 commands: [] 305 ` 306 307 expectedOutput := `project: Sample Project 308 commands: [] 309 ` 310 311 return Test{ 312 Feature: feature, 313 Name: "shouldUnsetTheAfterCommand", 314 CmdArgs: Args("unset", "after"), 315 Setup: func(dir string) error { 316 return utils.CreateConfigFile(dir, defaultFileContent) 317 }, 318 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 319 filePath := dir + "/" + def.ConfigFileName 320 assert.FileExists(t, dir+"/"+def.ConfigFileName) 321 content, _ := ioutil.ReadFile(filePath) 322 return assert.Exactly(t, expectedOutput, string(content)) 323 }, 324 } 325 } 326 327 func unsetBeforeShouldFailWhenBeforeIsNotFound(feature string) Test { 328 329 defaultFileContent := ` 330 project: Sample Project 331 commands: 332 - build: go build 333 ` 334 335 return Test{ 336 Feature: feature, 337 Name: "unsetBeforeShouldFailWhenBeforeIsNotFound", 338 CmdArgs: Args("unset", "before"), 339 Setup: func(dir string) error { 340 return utils.CreateConfigFile(dir, defaultFileContent) 341 }, 342 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 343 return assert.Contains(t, actualOutput, "Following command(s) not found: before") 344 }, 345 } 346 } 347 348 func unsetAfterShouldFailWhenAfterIsNotFound(feature string) Test { 349 350 defaultFileContent := ` 351 project: Sample Project 352 commands: 353 - build: go build 354 ` 355 356 return Test{ 357 Feature: feature, 358 Name: "unsetAfterShouldFailWhenAfterIsNotFound", 359 CmdArgs: Args("unset", "after"), 360 Setup: func(dir string) error { 361 return utils.CreateConfigFile(dir, defaultFileContent) 362 }, 363 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 364 return assert.Contains(t, actualOutput, "Following command(s) not found: after") 365 }, 366 } 367 } 368 369 func unsetSingleCommandShouldFailWhenInvalidCommandNameIsEntered(feature string) Test { 370 defaultFileContent := ` 371 project: Sample Project 372 commands: 373 - build: go build 374 ` 375 invalidName := "!nv@lid-name" 376 expectedOutput := "1build unset: '" + invalidName + "' is not a valid command name. See '1build unset --help'." 377 378 return Test{ 379 Feature: feature, 380 Name: "unsetSingleCommandShouldFailWhenInvalidCommandNameIsEntered", 381 CmdArgs: Args("unset", invalidName), 382 Setup: func(dir string) error { 383 return utils.CreateConfigFile(dir, defaultFileContent) 384 }, 385 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 386 return assert.Contains(t, actualOutput, expectedOutput) 387 }, 388 } 389 } 390 391 func unsetMultipleCommandShouldFailWhenInvalidCommandNameIsEntered(feature string) Test { 392 defaultFileContent := ` 393 project: Sample Project 394 commands: 395 - build: go build 396 ` 397 invalidName := "!nv@lid-name" 398 expectedOutput := "1build unset: '" + invalidName + "' is not a valid command name. See '1build unset --help'." 399 400 return Test{ 401 Feature: feature, 402 Name: "unsetSingleCommandShouldFailWhenInvalidCommandNameIsEntered", 403 CmdArgs: Args("unset", "build", invalidName), 404 Setup: func(dir string) error { 405 return utils.CreateConfigFile(dir, defaultFileContent) 406 }, 407 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 408 return assert.Contains(t, actualOutput, expectedOutput) 409 }, 410 } 411 }