forked from peak/s5cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flag/stat: fix omitted stat in error log level
Create unit & e2e tests Fixes peak#359 Co-Authored-By: Muhammed Can Küçükaslan <[email protected]>
- Loading branch information
1 parent
eea87b1
commit ca3fd47
Showing
1 changed file
with
59 additions
and
0 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,59 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/peak/s5cmd/log/stat" | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestStat(t *testing.T) { | ||
levels := []string{ | ||
"trace", | ||
"debug", | ||
"info", | ||
"error", | ||
} | ||
for _, l := range levels { | ||
testStatHelper(l, t) | ||
} | ||
// testStatHelper method closes and remakes the outputCh | ||
// but this creates a useless channel at the end too | ||
// so we need to close it at the end. | ||
Close() | ||
} | ||
|
||
func testStatHelper(level string, t *testing.T) { | ||
old := os.Stdout // keep backup of the real stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
Init(level, true) | ||
op := "op" | ||
|
||
var s, e int64 = 1, 0 | ||
Stat(stat.Stats{{op, s, e}}) | ||
|
||
// Close closes the output channel so that the current test level can have its output. | ||
Close() | ||
// To be able to test the remaining tests, we should create new channel for them | ||
outputCh = make(chan output, 10000) | ||
outC := make(chan string) | ||
// copy the output in a separate goroutine so printing can't block indefinitely | ||
go func() { | ||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
outC <- buf.String() | ||
}() | ||
// back to normal state | ||
w.Close() | ||
os.Stdout = old // restoring the real stdout | ||
out := <-outC | ||
out = strings.TrimSpace(out) | ||
if out != fmt.Sprintf("{\"operation\":\"%s\",\"success\":%v,\"error\":%v}", op, s, e) { | ||
t.Errorf("Stat does not print in %v level!\n$%v$", level, out) | ||
} | ||
} |