forked from TrueCloudLab/restic
cache: Restructure New to remove redundant operations
New and its helpers used to create the cache directories several times over. They now only do so once. The added test ensures that the cache is produced in a consistent state when parts are deleted.
This commit is contained in:
parent
51dc80be5b
commit
da419be43c
2 changed files with 68 additions and 28 deletions
46
internal/cache/cache_test.go
vendored
Normal file
46
internal/cache/cache_test.go
vendored
Normal file
|
@ -0,0 +1,46 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
parent := rtest.TempDir(t)
|
||||
basedir := filepath.Join(parent, "cache")
|
||||
id := restic.NewRandomID().String()
|
||||
tagFile := filepath.Join(basedir, "CACHEDIR.TAG")
|
||||
versionFile := filepath.Join(basedir, id, "version")
|
||||
|
||||
const (
|
||||
stepCreate = iota
|
||||
stepComplete
|
||||
stepRmTag
|
||||
stepRmVersion
|
||||
stepEnd
|
||||
)
|
||||
|
||||
for step := stepCreate; step < stepEnd; step++ {
|
||||
switch step {
|
||||
case stepRmTag:
|
||||
rtest.OK(t, os.Remove(tagFile))
|
||||
case stepRmVersion:
|
||||
rtest.OK(t, os.Remove(versionFile))
|
||||
}
|
||||
|
||||
c, err := New(id, basedir)
|
||||
rtest.OK(t, err)
|
||||
rtest.Equals(t, basedir, c.Base)
|
||||
rtest.Equals(t, step == stepCreate, c.Created)
|
||||
|
||||
for _, name := range []string{tagFile, versionFile} {
|
||||
info, err := os.Lstat(name)
|
||||
rtest.OK(t, err)
|
||||
rtest.Assert(t, info.Mode().IsRegular(), "")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue