diff --git a/go.mod b/go.mod index 390b90b..55986db 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/mrz1836/go-logger go 1.15 + +require github.com/stretchr/testify v1.6.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..afe7890 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/log_entries_test.go b/log_entries_test.go index cdb756f..3b07ea6 100644 --- a/log_entries_test.go +++ b/log_entries_test.go @@ -6,6 +6,8 @@ import ( "os/exec" "testing" "time" + + "github.com/stretchr/testify/assert" ) const testToken = "token" @@ -13,46 +15,33 @@ const testToken = "token" // TestNewLogEntriesClient will test the NewLogEntriesClient() method func TestNewLogEntriesClient(t *testing.T) { client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } - - if client.port != LogEntriesPort { - t.Fatalf("[%s] expect [%s] result", LogEntriesPort, client.port) - } - - if client.endpoint != LogEntriesURL { - t.Fatalf("[%s] expect [%s] result", LogEntriesURL, client.endpoint) - } + assert.NoError(t, err) + assert.NotNil(t, client) - if client.token != testToken { - t.Fatalf("[%s] expect [%s] result", testToken, client.token) - } + assert.Equal(t, LogEntriesPort, client.port) + assert.Equal(t, LogEntriesURL, client.endpoint) + assert.Equal(t, testToken, client.token) - _, err = NewLogEntriesClient("token", LogEntriesURL, "101010") - if err == nil { - t.Fatalf("error should have occurred") - } + client, err = NewLogEntriesClient("token", LogEntriesURL, "101010") + assert.Error(t, err) + assert.NotNil(t, client) - _, err = NewLogEntriesClient("token", "http://badurl.com", LogEntriesPort) - if err == nil { - t.Fatalf("error should have occurred") - } + client, err = NewLogEntriesClient("token", "http://badurl.com", LogEntriesPort) + assert.Error(t, err) + assert.NotNil(t, client) // Double open - _, err = NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + client, err = NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) + assert.NoError(t, err) + assert.NotNil(t, client) } // TestMsgQueue_Enqueue will test the Enqueue() method func TestMsgQueue_Enqueue(t *testing.T) { client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + assert.NoError(t, err) + assert.NotNil(t, client) var buff bytes.Buffer buff.WriteString(testToken) @@ -60,14 +49,10 @@ func TestMsgQueue_Enqueue(t *testing.T) { buff.WriteString("test") client.messages.Enqueue(&buff) - if len(client.messages.messagesToSend) == 0 { - t.Fatalf("missing messages to send: %v", client.messages.messagesToSend) - } + assert.Equal(t, 1, len(client.messages.messagesToSend)) for x := range client.messages.messagesToSend { - if x.String() != testToken+" test" { - t.Fatalf("[%s] expect [%s] result", testToken+" test", x.String()) - } + assert.Equal(t, testToken+" test", x.String()) close(client.messages.messagesToSend) } } @@ -76,9 +61,8 @@ func TestMsgQueue_Enqueue(t *testing.T) { func TestMsgQueue_PushFront(t *testing.T) { client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + assert.NoError(t, err) + assert.NotNil(t, client) var buff bytes.Buffer buff.WriteString(testToken) @@ -92,9 +76,7 @@ func TestMsgQueue_PushFront(t *testing.T) { buff2.WriteString("first") client.messages.PushFront(&buff2) - if len(client.messages.messagesToSend) == 0 { - t.Fatalf("missing messages to send: %v", client.messages.messagesToSend) - } + assert.Equal(t, 2, len(client.messages.messagesToSend)) go func() { close(client.messages.messagesToSend) @@ -105,18 +87,15 @@ func TestMsgQueue_PushFront(t *testing.T) { finalString += x.String() } - if finalString != testToken+" first"+testToken+" test" { - t.Fatalf("[%s] expect [%s] result", testToken+" first"+testToken+" test", finalString) - } + assert.Equal(t, testToken+" first"+testToken+" test", finalString) } // TestLogClient_ProcessQueue will test the ProcessQueue() method func TestLogClient_ProcessQueue(t *testing.T) { client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + assert.NoError(t, err) + assert.NotNil(t, client) go client.ProcessQueue() @@ -128,39 +107,31 @@ func TestLogClient_ProcessQueue(t *testing.T) { time.Sleep(3 * time.Second) - if len(client.messages.messagesToSend) > 0 { - t.Fatal("no messages should be in queue", len(client.messages.messagesToSend)) - } + assert.Equal(t, 0, len(client.messages.messagesToSend)) } // TestLogClient_Println will test the Println() method func TestLogClient_Println(t *testing.T) { client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + assert.NoError(t, err) + assert.NotNil(t, client) client.Println("test", "this") - if len(client.messages.messagesToSend) == 0 { - t.Fatal("expected message to send") - } + assert.Equal(t, 1, len(client.messages.messagesToSend)) } // TestLogClient_Printf will test the Printf() method func TestLogClient_Printf(t *testing.T) { client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + assert.NoError(t, err) + assert.NotNil(t, client) client.Printf("test %d", 1) - if len(client.messages.messagesToSend) == 0 { - t.Fatal("expected message to send") - } + assert.Equal(t, 1, len(client.messages.messagesToSend)) go func() { close(client.messages.messagesToSend) @@ -171,18 +142,15 @@ func TestLogClient_Printf(t *testing.T) { finalString += x.String() } - if finalString != testToken+" test 1" { - t.Fatalf("[%s] expect [%s] result", testToken+" test 1", finalString) - } + assert.Equal(t, testToken+" test 1", finalString) } // TestLogClient_Fatalf will test the Fatalf() method func TestLogClient_Fatalf(t *testing.T) { client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + assert.NoError(t, err) + assert.NotNil(t, client) if os.Getenv("EXIT_FUNCTION") == "1" { client.Fatalf("test %d", 1) @@ -201,9 +169,8 @@ func TestLogClient_Fatalf(t *testing.T) { func TestLogClient_Fatalln(t *testing.T) { client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + assert.NoError(t, err) + assert.NotNil(t, client) if os.Getenv("EXIT_FUNCTION") == "1" { client.Fatalln("test exit") diff --git a/logger.go b/logger.go index 553d030..68d1aef 100644 --- a/logger.go +++ b/logger.go @@ -99,6 +99,11 @@ func SetImplementation(impl Logger) { implementation = impl } +// GetImplementation gets the current logger implementation +func GetImplementation() Logger { + return implementation +} + // FileTag tag file func FileTag(level int) string { comps := FileTagComponents(level + 1) diff --git a/logger_test.go b/logger_test.go index 574573b..52ef878 100644 --- a/logger_test.go +++ b/logger_test.go @@ -7,9 +7,10 @@ import ( "log" "os" "os/exec" - "strings" "sync" "testing" + + "github.com/stretchr/testify/assert" ) // captureOutput captures the output of log, fmt or os.Stderr.WriteString @@ -50,33 +51,23 @@ func TestLogLevel_String(t *testing.T) { var level LogLevel // Test for debug - if level.String() != "debug" { - t.Fatalf("expected string to be: %s, got: %s", "debug", level.String()) - } + assert.Equal(t, "debug", level.String()) // Test for info level = 1 - if level.String() != "info" { - t.Fatalf("expected string to be: %s, got: %s", "info", level.String()) - } + assert.Equal(t, "info", level.String()) // Test for warn level = 2 - if level.String() != "warn" { - t.Fatalf("expected string to be: %s, got: %s", "warn", level.String()) - } + assert.Equal(t, "warn", level.String()) // Test for error level = 3 - if level.String() != "error" { - t.Fatalf("expected string to be: %s, got: %s", "error", level.String()) - } + assert.Equal(t, "error", level.String()) // Test for empty level = 4 - if level.String() != "" { - t.Fatalf("expected string to be: %s, got: %s", "", level.String()) - } + assert.Equal(t, "", level.String()) } // ExampleLogLevel_String example using level.String() @@ -99,9 +90,7 @@ func TestFileTag(t *testing.T) { // File tag fileTag := FileTag(1) - if !strings.Contains(fileTag, "go-logger/logger_test.go:go-logger.TestFileTag:") { - t.Fatalf("expected file tag: %s, got: %s", "go-logger/logger_test.go:go-logger.TestFileTag:", fileTag) - } + assert.Contains(t, fileTag, "go-logger/logger_test.go:go-logger.TestFileTag:") } // ExampleFileTag example using FileTag() @@ -124,42 +113,28 @@ func TestFileTagComponents(t *testing.T) { // Test the level: 2 fileTagComps := FileTagComponents(2) - if len(fileTagComps) == 0 || len(fileTagComps) != 3 { - t.Fatal("expected file tag components to have 3 components") - } + assert.NotEqual(t, 0, len(fileTagComps)) + assert.Equal(t, 3, len(fileTagComps)) // Test the part - if fileTagComps[0] != "testing/testing.go" { - t.Fatalf("expected component: %s, got: %s", "testing/testing.go", fileTagComps[0]) - } + assert.Equal(t, "testing/testing.go", fileTagComps[0]) // Test the part - if fileTagComps[1] != "testing.tRunner" { - t.Fatalf("expected component: %s, got: %s", "testing.tRunner", fileTagComps[1]) - } + assert.Equal(t, "testing.tRunner", fileTagComps[1]) // Test the part // todo: this number changes frequently, maybe this is not the best test? - /* - if fileTagComps[2] != "1127" { - t.Fatalf("expected component: %s, got: %s", "991", fileTagComps[2]) - } - */ + assert.NotEqual(t, 0, fileTagComps[2]) // Test the level: 1 fileTagComps = FileTagComponents(1) - if len(fileTagComps) == 0 || len(fileTagComps) != 3 { - t.Fatal("expected file tag components to have 3 components") - } + assert.NotEqual(t, 0, len(fileTagComps)) + assert.Equal(t, 3, len(fileTagComps)) // Test the part - if fileTagComps[0] != "go-logger/logger_test.go" { - t.Fatalf("expected component: %s, got: %s", "go-logger/logger_test.go", fileTagComps[0]) - } + assert.Equal(t, "go-logger/logger_test.go", fileTagComps[0]) // Test the part - if fileTagComps[1] != "go-logger.TestFileTagComponents" { - t.Fatalf("expected component: %s, got: %s", "go-logger.TestFileTagComponents", fileTagComps[1]) - } + assert.Equal(t, "go-logger.TestFileTagComponents", fileTagComps[1]) } // ExampleFileTagComponents example using FileTagComponents() @@ -182,13 +157,8 @@ func TestPrintln(t *testing.T) { Println("test this method") }) - if !strings.Contains(captured, "go-logger/logger_test.go:go-logger.TestPrintln") { - t.Fatalf("expected string: %s got: %s", "go-logger/logger_test.go:go-logger.TestPrintln", captured) - } - - if !strings.Contains(captured, "test this method") { - t.Fatalf("expected string: %s got: %s", "test this method", captured) - } + assert.Contains(t, captured, "go-logger/logger_test.go:go-logger.TestPrintln") + assert.Contains(t, captured, "test this method") } // TestNoFilePrintln test the print line method @@ -196,10 +166,7 @@ func TestNoFilePrintln(t *testing.T) { captured := captureOutput(func() { NoFilePrintln("test this method") }) - - if !strings.Contains(captured, "test this method") { - t.Fatalf("expected string: %s got: %s", "test this method", captured) - } + assert.Contains(t, captured, "test this method") } // BenchmarkPrintln benchmarks the Println() method @@ -215,13 +182,8 @@ func TestPrintf(t *testing.T) { Printf("test this method: %s", "TestPrintf") }) - if !strings.Contains(captured, "go-logger/logger_test.go:go-logger.TestPrintf") { - t.Fatalf("expected string: %s got: %s", "go-logger/logger_test.go:go-logger.TestPrintf", captured) - } - - if !strings.Contains(captured, "test this method: TestPrintf") { - t.Fatalf("expected string: %s got: %s", "test this method: TestPrintf", captured) - } + assert.Contains(t, captured, "go-logger/logger_test.go:go-logger.TestPrintf") + assert.Contains(t, captured, "test this method: TestPrintf") } // TestNoFilePrintf test the print fmt method @@ -230,9 +192,7 @@ func TestNoFilePrintf(t *testing.T) { NoFilePrintf("test this method: %s", "TestPrintf") }) - if !strings.Contains(captured, "test this method: TestPrintf") { - t.Fatalf("expected string: %s got: %s", "test this method: TestPrintf", captured) - } + assert.Contains(t, captured, "test this method: TestPrintf") } // BenchmarkPrintf benchmarks the Printf() method @@ -248,13 +208,8 @@ func TestErrorln(t *testing.T) { Errorln(2, "test this method") }) - if !strings.Contains(captured, "go-logger/logger_test.go:go-logger.TestErrorln") { - t.Fatalf("expected string: %s got: %s", "go-logger/logger_test.go:go-logger.TestErrorln", captured) - } - - if !strings.Contains(captured, "test this method") { - t.Fatalf("expected string: %s got: %s", "test this method", captured) - } + assert.Contains(t, captured, "go-logger/logger_test.go:go-logger.TestErrorln") + assert.Contains(t, captured, "test this method") } // BenchmarkErrorln benchmarks the Errorln() method @@ -270,13 +225,8 @@ func TestErrorfmt(t *testing.T) { Errorfmt(2, "test this method: %s", "Errorfmt") }) - if !strings.Contains(captured, "go-logger/logger_test.go:go-logger.TestErrorfmt") { - t.Fatalf("expected string: %s got: %s", "go-logger/logger_test.go:go-logger.TestErrorfmt", captured) - } - - if !strings.Contains(captured, "test this method: Errorfmt") { - t.Fatalf("expected string: %s got: %s", "test this method: Errorfmt", captured) - } + assert.Contains(t, captured, "go-logger/logger_test.go:go-logger.TestErrorfmt") + assert.Contains(t, captured, "test this method: Errorfmt") } // BenchmarkErrorfmt benchmarks the Errorfmt() method @@ -295,29 +245,19 @@ func TestData(t *testing.T) { // 2019/06/17 12:59:32 type="warn" file="go-logger/logger_test.go" method="go-logger.TestData.func1" line="188" message="test this method" another="value" // Check for warn - if !strings.Contains(captured, `type="warn"`) { - t.Fatalf("expected string: %s, got: %s", `type="warn"`, captured) - } + assert.Contains(t, captured, `type="warn"`) // Check for file - if !strings.Contains(captured, `file="go-logger/logger_test.go"`) { - t.Fatalf("expected string: %s, got: %s", `file="go-logger/logger_test.go"`, captured) - } + assert.Contains(t, captured, `file="go-logger/logger_test.go"`) // Check for method - if !strings.Contains(captured, `method="go-logger.TestData.func1"`) { - t.Fatalf("expected string: %s, got: %s", `method="go-logger.TestData.func1"`, captured) - } + assert.Contains(t, captured, `method="go-logger.TestData.func1"`) // Check for message - if !strings.Contains(captured, `message="test this method"`) { - t.Fatalf("expected string: %s, got: %s", `message="test this method"`, captured) - } + assert.Contains(t, captured, `message="test this method"`) // Check for additional values - if !strings.Contains(captured, `another="value"`) { - t.Fatalf("expected string: %s, got: %s", `another="value"`, captured) - } + assert.Contains(t, captured, `another="value"`) } // TestNoFileData test the NoFileData() method @@ -327,19 +267,13 @@ func TestNoFileData(t *testing.T) { }) // Check for warn - if !strings.Contains(captured, `type="warn"`) { - t.Fatalf("expected string: %s, got: %s", `type="warn"`, captured) - } + assert.Contains(t, captured, `type="warn"`) // Check for message - if !strings.Contains(captured, `message="test this method"`) { - t.Fatalf("expected string: %s, got: %s", `message="test this method"`, captured) - } + assert.Contains(t, captured, `message="test this method"`) // Check for additional values - if !strings.Contains(captured, `another="value"`) { - t.Fatalf("expected string: %s, got: %s", `another="value"`, captured) - } + assert.Contains(t, captured, `another="value"`) } // BenchmarkData benchmarks the Data() method @@ -357,9 +291,7 @@ func TestLogPkg_Printf(t *testing.T) { implementation.Printf("test this method: %s", "TestPrintf") }) - if !strings.Contains(captured, "test this method: TestPrintf") { - t.Fatalf("expected string: %s got: %s", "test this method: TestPrintf", captured) - } + assert.Contains(t, captured, "test this method: TestPrintf") } // BenchmarkLogPkg_Printf benchmarks the LogPkg_Printf() method @@ -378,9 +310,7 @@ func TestLogPkg_Println(t *testing.T) { implementation.Println("test this method: TestPrintln") }) - if !strings.Contains(captured, "test this method: TestPrintln") { - t.Fatalf("expected string: %s got: %s", "test this method: TestPrintln", captured) - } + assert.Contains(t, captured, "test this method: TestPrintln") } // BenchmarkLogPkg_Println benchmarks the LogPkg_Println() method @@ -396,12 +326,14 @@ func TestFatalf(t *testing.T) { token := "token" client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + assert.NoError(t, err) + assert.NotNil(t, client) SetImplementation(client) + theImplementation := GetImplementation() + assert.NotNil(t, theImplementation) + if os.Getenv("EXIT_FUNCTION") == "1" { Fatalf("test %d", 1) return @@ -420,9 +352,8 @@ func TestFatalln(t *testing.T) { token := "token" client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort) - if err != nil { - t.Fatalf("error should have not occurred: %s", err.Error()) - } + assert.NoError(t, err) + assert.NotNil(t, client) SetImplementation(client) diff --git a/parameters_test.go b/parameters_test.go index 7272669..72cabb8 100644 --- a/parameters_test.go +++ b/parameters_test.go @@ -1,19 +1,17 @@ package logger -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/assert" +) // TestMakeParameter test making an error struct and MakeParameter() method func TestMakeParameter(t *testing.T) { param := MakeParameter("myKey", "myValue") - if param.Key() != "myKey" { - t.Fatalf("expected value: %s, got: %s", "myKey", param.Key()) - } - if param.Value() != "myValue" { - t.Fatalf("expected value: %s, got: %s", "myValue", param.Value()) - } - if param.String() != `{"key":"myKey","value":"myValue"}` { - t.Fatalf("expected value: %s, got: %s", `{"key":"myKey","value":"myValue"}`, param.String()) - } + assert.Equal(t, "myKey", param.Key()) + assert.Equal(t, "myValue", param.Value()) + assert.Equal(t, `{"key":"myKey","value":"myValue"}`, param.String()) } // BenchmarkMakeParameter benchmarks the MakeParameter() method