From 35bb2fa9ee3d31209a6c6c7de895b888bc35bfd3 Mon Sep 17 00:00:00 2001 From: boraberke <67373739+boraberke@users.noreply.github.com> Date: Fri, 1 Jul 2022 18:30:11 +0300 Subject: [PATCH] flag/stat: fix omitted stat in error log level (#451) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * flag/stat: fix omitted stat in error log level Create unit & e2e tests Fixes #359 Co-Authored-By: Muhammed Can Küçükaslan <32893303+muhammedcankucukaslan@users.noreply.github.com> * use keys explicitly in composite literal Co-Authored-By: Muhammed Can Küçükaslan <32893303+muhammedcankucukaslan@users.noreply.github.com> * Update CHANGELOG.md Co-Authored-By: Muhammed Can Küçükaslan <32893303+MuhammedCanKucukaslan@users.noreply.github.com> * 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 <32893303+MuhammedCanKucukaslan@users.noreply.github.com> Co-authored-by: Muhammed Can Küçükaslan <32893303+muhammedcankucukaslan@users.noreply.github.com> --- CHANGELOG.md | 1 + command/app.go | 2 +- e2e/app_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++------- log/log.go | 9 ++++++++ 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3280d55a4..705ffcd83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/command/app.go b/command/app.go index 49927ae4a..67f82a903 100644 --- a/command/app.go +++ b/command/app.go @@ -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() diff --git a/e2e/app_test.go b/e2e/app_test.go index 24cf08d39..2d43cf417 100644 --- a/e2e/app_test.go +++ b/e2e/app_test.go @@ -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) { diff --git a/log/log.go b/log/log.go index c890e6824..25139b9d6 100644 --- a/log/log.go +++ b/log/log.go @@ -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) @@ -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(),