From f6e716543a78f3d9e0a59f8d02ed24ebcc454ba9 Mon Sep 17 00:00:00 2001 From: Kyle Reynolds Date: Tue, 2 Apr 2024 08:03:33 -0600 Subject: [PATCH] test info: improve cleanup of temp files - fixes #7209 Co-authored-by: Kyle Reynolds --- cmd/test/info/info.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/cmd/test/info/info.go b/cmd/test/info/info.go index 40b9a9e6f..f9093b3f4 100644 --- a/cmd/test/info/info.go +++ b/cmd/test/info/info.go @@ -27,12 +27,14 @@ import ( "github.com/rclone/rclone/fs/config/flags" "github.com/rclone/rclone/fs/hash" "github.com/rclone/rclone/fs/object" + "github.com/rclone/rclone/fs/operations" "github.com/rclone/rclone/lib/random" "github.com/spf13/cobra" ) var ( writeJSON string + keepTestFiles bool checkNormalization bool checkControl bool checkLength bool @@ -56,6 +58,7 @@ func init() { flags.BoolVarP(cmdFlags, &checkStreaming, "check-streaming", "", false, "Check uploads with indeterminate file size", "") flags.BoolVarP(cmdFlags, &checkBase32768, "check-base32768", "", false, "Check can store all possible base32768 characters", "") flags.BoolVarP(cmdFlags, &all, "all", "", false, "Run all tests", "") + flags.BoolVarP(cmdFlags, &keepTestFiles, "keep-test-files", "", false, "Keep test files after execution", "") } var commandDefinition = &cobra.Command{ @@ -84,7 +87,15 @@ a bit of go code for each one. checkBase32768 = true } for i := range args { - f := cmd.NewFsDir(args[i : i+1]) + tempDirName := "rclone-test-info-" + random.String(8) + tempDirPath := path.Join(args[i], tempDirName) + f := cmd.NewFsDir([]string{tempDirPath}) + fs.Infof(f, "Created temporary directory for test files: %s", tempDirPath) + err := f.Mkdir(context.Background(), "") + if err != nil { + log.Fatalf("couldn't create temporary directory: %v", err) + } + cmd.Run(false, false, command, func() error { return readInfo(context.Background(), f) }) @@ -471,10 +482,18 @@ func (r *results) checkStreaming() { } func readInfo(ctx context.Context, f fs.Fs) error { - err := f.Mkdir(ctx, "") - if err != nil { - return fmt.Errorf("couldn't mkdir: %w", err) + // Ensure cleanup unless --keep-test-files is specified + if !keepTestFiles { + defer func() { + err := operations.Purge(ctx, f, "") + if err != nil { + fs.Errorf(f, "Failed to purge temporary directory: %v", err) + } else { + fs.Infof(f, "Removed temporary directory for test files: %s", f.Root()) + } + }() } + r := newResults(ctx, f) if checkControl { r.checkControls()