diff --git a/changelog/unreleased/issue-4437 b/changelog/unreleased/issue-4437 index 199c04cbf..ee0981c50 100644 --- a/changelog/unreleased/issue-4437 +++ b/changelog/unreleased/issue-4437 @@ -7,3 +7,4 @@ The `check` command now attempts to create the cache directory before initializ https://github.com/restic/restic/issues/4437 https://github.com/restic/restic/pull/4805 +https://github.com/restic/restic/pull/4883 diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index 642a46c00..cccb1986e 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -176,12 +176,14 @@ func prepareCheckCache(opts CheckOptions, gopts *GlobalOptions, printer progress cachedir = cache.EnvDir() } - // use a cache in a temporary directory - err := os.MkdirAll(cachedir, 0755) - if err != nil { - Warnf("unable to create cache directory %s, disabling cache: %v\n", cachedir, err) - gopts.NoCache = true - return cleanup + if cachedir != "" { + // use a cache in a temporary directory + err := os.MkdirAll(cachedir, 0755) + if err != nil { + Warnf("unable to create cache directory %s, disabling cache: %v\n", cachedir, err) + gopts.NoCache = true + return cleanup + } } tempdir, err := os.MkdirTemp(cachedir, "restic-check-cache-") if err != nil { diff --git a/cmd/restic/cmd_check_test.go b/cmd/restic/cmd_check_test.go index 9516ce420..18d607a14 100644 --- a/cmd/restic/cmd_check_test.go +++ b/cmd/restic/cmd_check_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" "github.com/restic/restic/internal/ui/progress" @@ -229,3 +230,17 @@ func TestPrepareCheckCache(t *testing.T) { }) } } + +func TestPrepareDefaultCheckCache(t *testing.T) { + gopts := GlobalOptions{CacheDir: ""} + cleanup := prepareCheckCache(CheckOptions{}, &gopts, &progress.NoopPrinter{}) + _, err := os.ReadDir(gopts.CacheDir) + rtest.OK(t, err) + + // Call the cleanup function to remove the temporary cache directory + cleanup() + + // Verify that the cache directory has been removed + _, err = os.ReadDir(gopts.CacheDir) + rtest.Assert(t, errors.Is(err, os.ErrNotExist), "Expected cache directory to be removed, but it still exists") +}