diff --git a/CHANGELOG.md b/CHANGELOG.md index d0ae555b0..c71dc81b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- `tt clean` no longer tries to clean files multiple times. + ## [2.2.1] - 2024-04-03 ### Added diff --git a/cli/cmd/clean.go b/cli/cmd/clean.go index 1d0b6e796..e47fffb62 100644 --- a/cli/cmd/clean.go +++ b/cli/cmd/clean.go @@ -43,7 +43,7 @@ func NewCleanCmd() *cobra.Command { return cleanCmd } -func collectFiles(list []string, dirname string) ([]string, error) { +func collectFiles(files map[string]bool, dirname string) (map[string]bool, error) { err := filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -51,7 +51,7 @@ func collectFiles(list []string, dirname string) ([]string, error) { } if !info.IsDir() { - list = append(list, path) + files[path] = true } return nil }) @@ -59,28 +59,28 @@ func collectFiles(list []string, dirname string) ([]string, error) { return nil, err } - return list, nil + return files, nil } func clean(run *running.InstanceCtx) error { - removeList := []string{} + removeFiles := map[string]bool{} confirm := false var err error for _, dir := range [...]string{run.LogDir, run.WalDir, run.VinylDir, run.MemtxDir} { - removeList, err = collectFiles(removeList, dir) + removeFiles, err = collectFiles(removeFiles, dir) if err != nil { return err } } - if len(removeList) == 0 { + if len(removeFiles) == 0 { log.Infof("Already cleaned.\n") return nil } log.Infof("List of files to delete:\n") - for _, file := range removeList { + for file, _ := range removeFiles { log.Infof("%s", file) } @@ -92,7 +92,7 @@ func clean(run *running.InstanceCtx) error { } if confirm || forceRemove { - for _, file := range removeList { + for file, _ := range removeFiles { err = os.Remove(file) if err != nil { return err diff --git a/test/integration/running/test_running.py b/test/integration/running/test_running.py index ea8a3596a..7e2636805 100644 --- a/test/integration/running/test_running.py +++ b/test/integration/running/test_running.py @@ -172,7 +172,7 @@ def test_logrotate(tt_cmd, tmpdir_with_cfg): def assert_file_cleaned(filepath, cmd_out): - assert re.search(r"• " + str(filepath), cmd_out) + assert len(re.findall(r"• " + str(filepath), cmd_out)) == 1 # https://github.com/tarantool/tt/issues/735 assert os.path.exists(filepath) is False @@ -229,6 +229,7 @@ def test_clean(tt_cmd, tmpdir_with_cfg): # Check that clean is working. clean_rc, clean_out = run_command_and_get_output(clean_cmd, cwd=tmpdir) assert clean_rc == 0 + assert re.search(r"\[ERR\]", clean_out) is None assert_file_cleaned(os.path.join(log_dir, log_file), clean_out) assert_file_cleaned(os.path.join(lib_dir, initial_snap), clean_out)