Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fail tests immediately if NoError() fails #168

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions check/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func (s *checkBaseTestSuite) BeforeTest(_, name string) {
var err error
var logName string
s.cfg, err = config.NewConfigFromString(configMap[name])
s.NoError(err)
s.Require().NoError(err)
logName, err = utils.CreateTempFileWithContent(logText, "test-*.log")
s.NoError(err)
s.Require().NoError(err)
s.cfg.WithLogfile(logName)
}

Expand All @@ -51,7 +51,7 @@ func TestCheckBaseTestSuite(t *testing.T) {

func (s *checkBaseTestSuite) TestNewCheck() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)

for _, text := range c.cfg.TestOverride.Ignore {
s.Equal(text, "Ignore Me", "Well, didn't match Ignore Me")
Expand All @@ -75,7 +75,7 @@ func (s *checkBaseTestSuite) TestNewCheck() {

func (s *checkBaseTestSuite) TestForced() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)

s.True(c.ForcedIgnore("942200-1"), "Can't find ignored value")

Expand All @@ -94,7 +94,7 @@ func (s *checkBaseTestSuite) TestForced() {

func (s *checkBaseTestSuite) TestCloudMode() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)

s.True(c.CloudMode(), "couldn't detect cloud mode")

Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *checkBaseTestSuite) TestCloudMode() {

func (s *checkBaseTestSuite) TestSetMarkers() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)

c.SetStartMarker([]byte("TesTingStArtMarKer"))
c.SetEndMarker([]byte("TestIngEnDMarkeR"))
Expand Down
6 changes: 3 additions & 3 deletions check/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func (s *checkErrorTestSuite) SetupTest() {
s.cfg = config.NewDefaultConfig()

logName, err := utils.CreateTempFileWithContent(logText, "test-*.log")
s.NoError(err)
s.Require().NoError(err)
s.cfg.WithLogfile(logName)
}
func (s *checkErrorTestSuite) TestAssertResponseErrorOK() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)
for _, e := range expectedOKTests {
c.SetExpectError(e.expected)
s.Equal(e.expected, c.AssertExpectError(e.err))
Expand All @@ -55,7 +55,7 @@ func (s *checkErrorTestSuite) TestAssertResponseErrorOK() {

func (s *checkErrorTestSuite) TestAssertResponseFail() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)

for _, e := range expectedFailTests {
c.SetExpectError(e.expected)
Expand Down
6 changes: 3 additions & 3 deletions check/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ func (s *checkLogsTestSuite) SetupTest() {
s.cfg = config.NewDefaultConfig()

s.logName, err = utils.CreateTempFileWithContent(logText, "test-*.log")
s.NoError(err)
s.Require().NoError(err)
s.cfg.WithLogfile(s.logName)
}

func (s *checkLogsTestSuite) TearDownTest() {
err := os.Remove(s.logName)
s.NoError(err)
s.Require().NoError(err)
}
func (s *checkLogsTestSuite) TestAssertLogContainsOK() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)

c.SetLogContains(`id "920300"`)
s.True(c.AssertLogContains(), "did not find expected content 'id \"920300\"'")
Expand Down
10 changes: 5 additions & 5 deletions check/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func (s *checkResponseTestSuite) SetupTest() {
var err error
s.cfg = config.NewDefaultConfig()
logName, err := utils.CreateTempFileWithContent(logText, "test-*.log")
s.NoError(err)
s.Require().NoError(err)
s.cfg.WithLogfile(logName)
}

func (s *checkResponseTestSuite) TestAssertResponseTextErrorOK() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)
for _, e := range expectedResponseOKTests {
c.SetExpectResponse(e.expected)
s.Truef(c.AssertResponseContains(e.response), "unexpected response: %v", e.response)
Expand All @@ -53,7 +53,7 @@ func (s *checkResponseTestSuite) TestAssertResponseTextErrorOK() {

func (s *checkResponseTestSuite) TestAssertResponseTextFailOK() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)
for _, e := range expectedResponseFailTests {
c.SetExpectResponse(e.expected)
s.Falsef(c.AssertResponseContains(e.response), "response shouldn't contain text %v", e.response)
Expand All @@ -62,7 +62,7 @@ func (s *checkResponseTestSuite) TestAssertResponseTextFailOK() {

func (s *checkResponseTestSuite) TestAssertResponseTextChecksFullResponseOK() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)
for _, e := range expectedResponseOKTests {
c.SetExpectResponse(e.expected)
s.Truef(c.AssertResponseContains(e.response), "unexpected response: %v", e.response)
Expand All @@ -71,7 +71,7 @@ func (s *checkResponseTestSuite) TestAssertResponseTextChecksFullResponseOK() {

func (s *checkResponseTestSuite) TestAssertResponseContainsRequired() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)
c.SetExpectResponse("")
s.False(c.AssertResponseContains(""), "response shouldn't contain text")
s.False(c.ResponseContainsRequired(), "response shouldn't contain text")
Expand Down
8 changes: 4 additions & 4 deletions check/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *checkStatusTestSuite) SetupTest() {
var err error
s.cfg = config.NewDefaultConfig()
logName, err := utils.CreateTempFileWithContent(logText, "test-*.log")
s.NoError(err)
s.Require().NoError(err)
s.cfg.WithLogfile(logName)
}

Expand All @@ -46,7 +46,7 @@ func TestCheckStatusTestSuite(t *testing.T) {

func (s *checkStatusTestSuite) TestStatusOK() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)

for _, expected := range statusOKTests {
c.SetExpectStatus(expected.expectedStatus)
Expand All @@ -56,7 +56,7 @@ func (s *checkStatusTestSuite) TestStatusOK() {

func (s *checkStatusTestSuite) TestStatusFail() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)

for _, expected := range statusFailTests {
c.SetExpectStatus(expected.expectedStatus)
Expand All @@ -66,7 +66,7 @@ func (s *checkStatusTestSuite) TestStatusFail() {

func (s *checkStatusTestSuite) TestStatusCodeRequired() {
c, err := NewCheck(s.cfg)
s.NoError(err)
s.Require().NoError(err)

c.SetExpectStatus([]int{200})
s.True(c.StatusCodeRequired(), "status code should be required")
Expand Down
12 changes: 6 additions & 6 deletions cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ type checkCmdTestSuite struct {

func (s *checkCmdTestSuite) SetupTest() {
tempDir, err := os.MkdirTemp("", "go-ftw-tests")
s.NoError(err)
s.Require().NoError(err)
s.tempDir = tempDir

err = os.MkdirAll(s.tempDir, fs.ModePerm)
s.NoError(err)
s.Require().NoError(err)
testFileContents, err := os.CreateTemp(s.tempDir, "mock-test-*.yaml")
s.NoError(err)
s.Require().NoError(err)
n, err := testFileContents.WriteString(checkFileContents)
s.NoError(err)
s.Require().NoError(err)
s.Equal(len(checkFileContents), n)

s.rootCmd = NewRootCommand()
Expand All @@ -61,7 +61,7 @@ func (s *checkCmdTestSuite) SetupTest() {

func (s *checkCmdTestSuite) TearDownTest() {
err := os.RemoveAll(s.tempDir)
s.NoError(err)
s.Require().NoError(err)
}

func TestCheckChoreTestSuite(t *testing.T) {
Expand All @@ -71,6 +71,6 @@ func TestCheckChoreTestSuite(t *testing.T) {
func (s *checkCmdTestSuite) TestCheckCommand() {
s.rootCmd.SetArgs([]string{"check", "-d", s.tempDir})
cmd, err := s.rootCmd.ExecuteContextC(context.Background())
s.NoError(err, "check command should not return an error")
s.Require().NoError(err, "check command should not return an error")
s.Equal("check", cmd.Name(), "check command should have the name 'check'")
}
2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func (s *rootCmdTestSuite) TestRootCommand() {
rootCmd := NewRootCommand()
rootCmd.SetArgs([]string{"help"})
err := Execute("v1.0.0")
s.NoError(err)
s.Require().NoError(err)
}
24 changes: 12 additions & 12 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,44 @@ func (s *runCmdTestSuite) setupMockHTTPServer() *httptest.Server {
resp := new(bytes.Buffer)
for key, value := range r.Header {
_, err := fmt.Fprintf(resp, "%s=%s,", key, value)
s.NoError(err)
s.Require().NoError(err)
}

_, err := w.Write(resp.Bytes())
s.NoError(err)
s.Require().NoError(err)
}))
return server
}

func (s *runCmdTestSuite) SetupTest() {
tempDir, err := os.MkdirTemp("", "go-ftw-tests")
s.NoError(err)
s.Require().NoError(err)
s.tempDir = tempDir

s.testHTTPServer = s.setupMockHTTPServer()
err = os.MkdirAll(s.tempDir, fs.ModePerm)
s.NoError(err)
s.Require().NoError(err)
testUrl, err := url.Parse(s.testHTTPServer.URL)
s.NoError(err)
s.Require().NoError(err)
port, err := strconv.Atoi(testUrl.Port())
s.NoError(err)
s.Require().NoError(err)
vars := map[string]int{
"Port": port,
}
testFileContents, err := os.CreateTemp(s.tempDir, "mock-test-*.yaml")
s.NoError(err)
s.Require().NoError(err)
tmpl, err := template.New("mock-test").Parse(testFileContentsTemplate)
s.NoError(err)
s.Require().NoError(err)
err = tmpl.Execute(testFileContents, vars)
s.NoError(err)
s.Require().NoError(err)

s.rootCmd = NewRootCommand()
s.rootCmd.AddCommand(NewRunCommand())
}

func (s *runCmdTestSuite) TearDownTest() {
err := os.RemoveAll(s.tempDir)
s.NoError(err)
s.Require().NoError(err)
s.testHTTPServer.Close()
}

Expand All @@ -114,7 +114,7 @@ func (s *runCmdTestSuite) TestHTTPConnectionSuccess() {
s.rootCmd.SetArgs([]string{"run", "--cloud", "-d", s.tempDir, "--wait-for-host", s.testHTTPServer.URL})
_, err := s.rootCmd.ExecuteContextC(context.Background())

s.NoError(err)
s.Require().NoError(err)
}

func (s *runCmdTestSuite) TestHTTPConnectionFail() {
Expand All @@ -133,5 +133,5 @@ func (s *runCmdTestSuite) TestHTTPRequestHeaderSuccess() {
})
_, err := s.rootCmd.ExecuteContextC(context.Background())

s.NoError(err)
s.Require().NoError(err)
}
20 changes: 10 additions & 10 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,29 @@ func (s *fileTestSuite) BeforeTest(_, name string) {
var err error
s.filename, _ = utils.CreateTempFileWithContent(testData[name], "test-*.yaml")
s.cfg, err = NewConfigFromFile(s.filename)
s.NoError(err)
s.Require().NoError(err)
s.NotNil(s.cfg)
}

func (s *fileTestSuite) TearDownTest() {
if s.filename != "" {
err := os.Remove(s.filename)
s.NoError(err)
s.Require().NoError(err)
s.filename = ""
}
}

func (s *baseTestSuite) TestBaseUnmarshalText() {
var ftwRegexp FTWRegexp
err := ftwRegexp.UnmarshalText([]byte("test"))
s.NoError(err)
s.Require().NoError(err)
s.NotNil(ftwRegexp)
s.True(ftwRegexp.MatchString("This is a test for unmarshalling"), "looks like we could not match string")
}

func (s *baseTestSuite) TestBaseNewFTWRegexpText() {
ftwRegexp, err := NewFTWRegexp("test")
s.NoError(err)
s.Require().NoError(err)
s.NotNil(ftwRegexp)
s.True(ftwRegexp.MatchString("This is a test"), "looks like we could not match string")
}
Expand All @@ -108,7 +108,7 @@ func (s *fileTestSuite) TestNewConfigBadFileConfig() {
filename, _ := utils.CreateTempFileWithContent(testData["jsonConfig"], "test-*.yaml")
defer os.Remove(filename)
cfg, err := NewConfigFromFile(filename)
s.NoError(err)
s.Require().NoError(err)
s.NotNil(cfg)
}

Expand Down Expand Up @@ -136,13 +136,13 @@ func (s *fileTestSuite) TestNewConfigDefaultConfig() {
_ = os.WriteFile(s.filename, []byte(testData["ok"]), 0644)

cfg, err := NewConfigFromFile("")
s.NoError(err)
s.Require().NoError(err)
s.NotNil(cfg)
}

func (s *fileTestSuite) TestNewConfigFromString() {
cfg, err := NewConfigFromString(testData["ok"])
s.NoError(err)
s.Require().NoError(err)
s.NotNil(cfg)
}

Expand All @@ -157,14 +157,14 @@ func (s *fileTestSuite) TestNewConfigFromEnv() {
os.Setenv("FTW_LOGFILE", "koanf")

cfg, err := NewConfigFromEnv()
s.NoError(err)
s.Require().NoError(err)
s.NotNil(cfg)
s.Equal("koanf", cfg.LogFile)
}

func (s *fileTestSuite) TestNewConfigFromEnvHasDefaults() {
cfg, err := NewConfigFromEnv()
s.NoError(err)
s.Require().NoError(err)
s.NotNil(cfg)

s.Equalf(DefaultRunMode, cfg.RunMode,
Expand All @@ -183,7 +183,7 @@ func (s *fileTestSuite) TestNewConfigFromFileHasDefaults() {

func (s *fileTestSuite) TestNewConfigFromStringHasDefaults() {
cfg, err := NewConfigFromString("")
s.NoError(err)
s.Require().NoError(err)
s.NotNil(cfg)
s.Equalf(DefaultRunMode, cfg.RunMode,
"unexpected default value '%s' for run mode", cfg.RunMode)
Expand Down
Loading