Skip to content

Commit

Permalink
flag/stat: fix omitted stat in error log level (#451)
Browse files Browse the repository at this point in the history
* flag/stat: fix omitted stat in error log level

Create unit & e2e tests
Fixes #359

Co-Authored-By: Muhammed Can Küçükaslan <[email protected]>

* use keys explicitly in composite literal

Co-Authored-By: Muhammed Can Küçükaslan <[email protected]>

* Update CHANGELOG.md

Co-Authored-By: Muhammed Can Küçükaslan <[email protected]>

* log: remove reduntant tests

Removed unit tests in log_test.go as they are already tested in TestAppDashStat under e2e/app_test.go

Removed redundant integration test which is already covered by another test in the same file.

Co-Authored-By: Muhammed Can Küçükaslan <[email protected]>

Co-authored-by: Muhammed Can Küçükaslan <[email protected]>
  • Loading branch information
boraberke and kucukaslan authored Jul 1, 2022
1 parent 7200415 commit 35bb2fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- Updated region detection call to use current session's address resolving method ([#314](https://github.com/peak/s5cmd/issues/314))
- Fixed a bug where lines with large tokens fail in `run` command. `sync` was failing when it finds multiple files to remove. ([#435](https://github.com/peak/s5cmd/issues/435), [#436](https://github.com/peak/s5cmd/issues/436))
- Print usage error if given log level(`--log`) is not valid. ([#430](https://github.com/peak/s5cmd/pull/430))
- Fixed a bug where (`--stat`) is ignored when log level is error. ([#359](https://github.com/peak/s5cmd/issues/359))

## v1.4.0 - 21 Sep 2021

Expand Down
2 changes: 1 addition & 1 deletion command/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ var app = &cli.App{
},
After: func(c *cli.Context) error {
if c.Bool("stat") {
log.Info(stat.Statistics())
log.Stat(stat.Statistics())
}

parallel.Close()
Expand Down
59 changes: 51 additions & 8 deletions e2e/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,64 @@ func TestAppRetryCount(t *testing.T) {
}
}

// Checks if the stats are written at the end of each log level output.
func TestAppDashStat(t *testing.T) {
t.Parallel()

_, s5cmd, cleanup := setup(t)
defer cleanup()
const (
bucket = "bucket"
fileContent = "this is a file content"
dst = "."
src = "file1.txt"
)

cmd := s5cmd("--stat", "ls")
result := icmd.RunCmd(cmd)
testcases := []struct {
name string
level string
}{
{
name: "--stat --log trace cp s3://bucket/object .",
level: "trace",
},
{
name: "--stat --log debug cp s3://bucket/object .",
level: "debug",
},
{
name: "--stat --log info cp s3://bucket/object .",
level: "info",
},
{
name: "--stat --log error cp s3://bucket/object .",
level: "error",
},
}

result.Assert(t, icmd.Success)
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

s3client, s5cmd, cleanup := setup(t)
defer cleanup()

createBucket(t, s3client, bucket)

out := result.Stdout()
putFile(t, s3client, bucket, src, fileContent)

tsv := fmt.Sprintf("%s\t%s\t%s\t%s\t", "Operation", "Total", "Error", "Success")
assert.Assert(t, strings.Contains(out, tsv))
srcPath := fmt.Sprintf("s3://%v/%v", bucket, src)
cmd := s5cmd("--stat", "--log", tc.level, "cp", srcPath, dst)
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Success)

out := result.Stdout()
tsv := fmt.Sprintf("%s\t%s\t%s\t%s\t", "Operation", "Total", "Error", "Success")

assert.Assert(t, strings.Contains(out, tsv))

})
}
}

func TestAppUnknownCommand(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func Info(msg Message) {
global.printf(levelInfo, msg, os.Stdout)
}

// Stat prints stat message regardless of the log level with info print formatting.
// It uses printfHelper instead of printf to ignore the log level condition.
func Stat(msg Message) {
global.printfHelper(levelInfo, msg, os.Stdout)
}

// Error prints message in error mode.
func Error(msg Message) {
global.printf(levelError, msg, os.Stderr)
Expand Down Expand Up @@ -72,7 +78,10 @@ func (l *Logger) printf(level logLevel, message Message, std *os.File) {
if level < l.level {
return
}
l.printfHelper(level, message, std)
}

func (l *Logger) printfHelper(level logLevel, message Message, std *os.File) {
if l.json {
outputCh <- output{
message: message.JSON(),
Expand Down

0 comments on commit 35bb2fa

Please sign in to comment.