| 440 | } |
| 441 | |
| 442 | func TestGenerates(t *testing.T) { |
| 443 | t.Parallel() |
| 444 | |
| 445 | const dir = "testdata/generates" |
| 446 | |
| 447 | const ( |
| 448 | srcTask = "sub/src.txt" |
| 449 | relTask = "rel.txt" |
| 450 | absTask = "abs.txt" |
| 451 | fileWithSpaces = "my text file.txt" |
| 452 | ) |
| 453 | |
| 454 | srcFile := filepathext.SmartJoin(dir, srcTask) |
| 455 | |
| 456 | for _, task := range []string{srcTask, relTask, absTask, fileWithSpaces} { |
| 457 | path := filepathext.SmartJoin(dir, task) |
| 458 | _ = os.Remove(path) |
| 459 | if _, err := os.Stat(path); err == nil { |
| 460 | t.Errorf("File should not exist: %v", err) |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | buff := bytes.NewBuffer(nil) |
| 465 | e := task.NewExecutor( |
| 466 | task.WithDir(dir), |
| 467 | task.WithStdout(buff), |
| 468 | task.WithStderr(buff), |
| 469 | ) |
| 470 | require.NoError(t, e.Setup()) |
| 471 | |
| 472 | for _, theTask := range []string{relTask, absTask, fileWithSpaces} { |
| 473 | destFile := filepathext.SmartJoin(dir, theTask) |
| 474 | upToDate := fmt.Sprintf("task: Task \"%s\" is up to date\n", srcTask) + |
| 475 | fmt.Sprintf("task: Task \"%s\" is up to date\n", theTask) |
| 476 | |
| 477 | // Run task for the first time. |
| 478 | require.NoError(t, e.Run(t.Context(), &task.Call{Task: theTask})) |
| 479 | |
| 480 | if _, err := os.Stat(srcFile); err != nil { |
| 481 | t.Errorf("File should exist: %v", err) |
| 482 | } |
| 483 | if _, err := os.Stat(destFile); err != nil { |
| 484 | t.Errorf("File should exist: %v", err) |
| 485 | } |
| 486 | // Ensure task was not incorrectly found to be up-to-date on first run. |
| 487 | if buff.String() == upToDate { |
| 488 | t.Errorf("Wrong output message: %s", buff.String()) |
| 489 | } |
| 490 | buff.Reset() |
| 491 | |
| 492 | // Re-run task to ensure it's now found to be up-to-date. |
| 493 | require.NoError(t, e.Run(t.Context(), &task.Call{Task: theTask})) |
| 494 | if buff.String() != upToDate { |
| 495 | t.Errorf("Wrong output message: %s", buff.String()) |
| 496 | } |
| 497 | buff.Reset() |
| 498 | } |
| 499 | } |