main_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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\nGo Test: SUCCESS\nGoFmt: SUCCESS\n", expErr: nil},
  15. {name: "fail", proj: "./testdata/toolErr/", out: "", expErr: &stepErr{step: "go build"}},
  16. {name: "failFormat", proj: "./testdata/toolFmtErr/", out: "", expErr: &stepErr{step: "go fmt"}},
  17. }
  18. for _, tc := range testCases {
  19. t.Run(tc.name, func(t *testing.T) {
  20. var out bytes.Buffer
  21. err := run(tc.proj, &out)
  22. if tc.expErr != nil {
  23. if err == nil {
  24. t.Errorf("Expected error: %q. Got `nil` instead", tc.expErr)
  25. }
  26. if !errors.Is(err, tc.expErr) {
  27. t.Errorf("Expected error: %q. Got %q.", tc.expErr, err)
  28. }
  29. return
  30. }
  31. if err != nil {
  32. t.Errorf("unexpected error: %q", err)
  33. }
  34. if out.String() != tc.out {
  35. t.Errorf("Expected output: %q. Got %q", tc.out, out.String())
  36. }
  37. })
  38. }
  39. }