-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from suecodelabs/bug/logger-fatal
fixed bug in logger.Fatal*() and added testcases
- Loading branch information
Showing
4 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2022 Sue B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package logger | ||
|
||
// Inspired by | ||
// https://stackoverflow.com/a/30690532 | ||
|
||
import "os" | ||
|
||
// Func takes a code as exit status | ||
type Func func(int) | ||
|
||
// Exit has an exit func, and will memorize the exit status code | ||
type Exit struct { | ||
exit Func | ||
status int | ||
} | ||
|
||
// Exit calls the exiter, and then returns code as status. | ||
// If e was declared, but never set (since only a test would set e), | ||
// simply calls os.Exit() | ||
func (e *Exit) Exit(code int) { | ||
if e != nil { | ||
e.status = code | ||
e.exit(code) | ||
} else { | ||
os.Exit(code) | ||
} | ||
} | ||
|
||
// Status get the exit status code as memorized | ||
// after the call to the exit func. | ||
func (e *Exit) Status() int { | ||
return e.status | ||
} | ||
|
||
// Default returns an Exit with default os.Exit() call. | ||
// That means the status will never be visible, | ||
// since os.Exit() stops everything. | ||
func Default() *Exit { | ||
return &Exit{exit: os.Exit} | ||
} | ||
|
||
// CreateExiter returns an exiter with a custom function | ||
func CreateExiter(exit Func) *Exit { | ||
return &Exit{exit: exit} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2022 Sue B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestFatalError(t *testing.T) { | ||
l := CreateDebugLogger() | ||
err := fmt.Errorf("test error") | ||
msg := "this is a test crash" | ||
l.FatalError(err, msg) | ||
assert.Equal(t, 1, l.GetExiter().status) | ||
logs := GetObservedLogs() | ||
theMsg := logs.FilterMessage(msg) | ||
assert.Equal(t, theMsg.All()[0].Message, msg) | ||
} | ||
|
||
func TestFatal(t *testing.T) { | ||
l := CreateDebugLogger() | ||
msg := "this is a test crash" | ||
l.Fatal(msg) | ||
assert.Equal(t, 1, l.GetExiter().status) | ||
logs := GetObservedLogs() | ||
theMsg := logs.FilterMessage(msg) | ||
assert.Equal(t, theMsg.All()[0].Message, msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters