main_test.go 902 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package main
  2. import (
  3. "bytes"
  4. "errors"
  5. "testing"
  6. )
  7. func TestRun(t *testing.T) {
  8. var testCases = []struct {
  9. name string
  10. proj string
  11. out string
  12. expErr error
  13. }{
  14. {name: "success", proj: "./testdata/tool/", out: "Go Build: SUCCESS\n", expErr: nil},
  15. {name: "fail", proj: "./testdata/toolErr/", out: "", expErr: &stepErr{step: "go build"}},
  16. }
  17. for _, tc := range testCases {
  18. t.Run(tc.name, func(t *testing.T) {
  19. var out bytes.Buffer
  20. err := run(tc.proj, &out)
  21. if tc.expErr != nil {
  22. if err == nil {
  23. t.Errorf("Expected error: %q. Got `nil` instead", tc.expErr)
  24. }
  25. if !errors.Is(err, tc.expErr) {
  26. t.Errorf("Expected error: %q. Got %q.", tc.expErr, err)
  27. }
  28. return
  29. }
  30. if err != nil {
  31. t.Errorf("unexpected error: %q", err)
  32. }
  33. if out.String() != tc.out {
  34. t.Errorf("Expected output: %q. Got %q", tc.out, out.String())
  35. }
  36. })
  37. }
  38. }