Skip to content

Commit

Permalink
refactor: extend paths helper use
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Bianconi committed Jan 13, 2023
1 parent 241d798 commit df1ec17
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 42 deletions.
40 changes: 14 additions & 26 deletions buildcache/build_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package buildcache

import (
"os"
"testing"
"time"

Expand All @@ -25,36 +24,29 @@ import (
)

func Test_UpdateLastUsedFileNotExisting(t *testing.T) {

testBuildDir := paths.New(t.TempDir(), "sketches", "sketch-xxx")
err := os.MkdirAll(testBuildDir.String(), 0770)
require.Nil(t, err)
require.NoError(t, testBuildDir.MkdirAll())
timeBeforeUpdating := time.Unix(0, 0)
requireCorrectUpdate(t, testBuildDir, timeBeforeUpdating)
}

func Test_UpdateLastUsedFileExisting(t *testing.T) {

testBuildDir := paths.New(t.TempDir(), "sketches", "sketch-xxx")
err := os.MkdirAll(testBuildDir.String(), 0770)
require.Nil(t, err)
require.NoError(t, testBuildDir.MkdirAll())

// create the file
preExistingFile := testBuildDir.Join(lastUsedFileName).String()
err = paths.New(preExistingFile).WriteFile([]byte{})
require.Nil(t, err)
preExistingFile := testBuildDir.Join(lastUsedFileName)
require.NoError(t, preExistingFile.WriteFile([]byte{}))
timeBeforeUpdating := time.Now().Add(-time.Second)
os.Chtimes(preExistingFile, time.Now(), timeBeforeUpdating)
preExistingFile.Chtimes(time.Now(), timeBeforeUpdating)
requireCorrectUpdate(t, testBuildDir, timeBeforeUpdating)
}

func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time) {
err := Used(dir)
require.Nil(t, err)
require.NoError(t, Used(dir))
expectedFile := dir.Join(lastUsedFileName)
fileInfo, err := os.Stat(expectedFile.String())
fileInfo, err := expectedFile.Stat()
require.Nil(t, err)

require.GreaterOrEqual(t, fileInfo.ModTime(), prevModTime)
}

Expand All @@ -70,22 +62,18 @@ func TestPurge(t *testing.T) {

// create the metadata files
for dirPath, lastUsedTime := range lastUsedTimesByDirPath {
err := os.MkdirAll(dirPath.Canonical().String(), 0770)
require.Nil(t, err)
infoFilePath := dirPath.Join(lastUsedFileName).Canonical().String()
err = paths.New(infoFilePath).WriteFile([]byte{})
require.Nil(t, err)
require.NoError(t, dirPath.MkdirAll())
infoFilePath := dirPath.Join(lastUsedFileName).Canonical()
require.NoError(t, infoFilePath.WriteFile([]byte{}))
// make sure access time does not matter
accesstime := time.Now()
err = os.Chtimes(infoFilePath, accesstime, lastUsedTime)
require.Nil(t, err)
require.NoError(t, infoFilePath.Chtimes(accesstime, lastUsedTime))
}

Purge(dirToPurge, ttl)

fileinfo, err := os.Stat(dirToPurge.Join("fresh").String())
files, err := dirToPurge.Join("fresh").Stat()
require.Nil(t, err)
require.True(t, fileinfo.IsDir())
_, err = os.Stat(dirToPurge.Join("old").String())
require.ErrorIs(t, err, os.ErrNotExist)
require.True(t, files.IsDir())
require.True(t, dirToPurge.Exist())
}
18 changes: 10 additions & 8 deletions buildcache/directory_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package buildcache

import (
"os"
"time"

"github.com/arduino/go-paths-helper"
Expand All @@ -43,7 +42,7 @@ func (dc *directoryCache) isExpired(key string, ttl time.Duration) bool {
}

func (dc *directoryCache) modTime(key string) (time.Time, error) {
fileInfo, err := os.Stat(dc.basePath().Join(key, lastUsedFileName).String())
fileInfo, err := dc.basePath().Join(key, lastUsedFileName).Stat()
if err != nil {
return time.Unix(0, 0), err
}
Expand All @@ -59,7 +58,10 @@ func (dc *directoryCache) GetOrCreate(key string, value time.Time) (time.Time, e
}

subDir := dc.basePath().Join(key)
os.MkdirAll(subDir.String(), 0770)
err = subDir.MkdirAll()
if err != nil || existing {
return modTime, err
}
err = subDir.Join(lastUsedFileName).WriteFile([]byte{})
if err != nil || existing {
return modTime, err
Expand All @@ -68,16 +70,16 @@ func (dc *directoryCache) GetOrCreate(key string, value time.Time) (time.Time, e
}

func (dc *directoryCache) Purge() error {
files, err := os.ReadDir(dc.basePath().String())
files, err := dc.basePath().ReadDir()
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() && dc.isExpired(file.Name(), dc.ttl) {
subDir := dc.basePath().Join(file.Name())
modTime, _ := dc.modTime(file.Name())
if file.IsDir() && dc.isExpired(file.Base(), dc.ttl) {
subDir := dc.basePath().Join(file.Base())
modTime, _ := dc.modTime(file.Base())
logrus.Tracef(`Purging cache directory "%s". Expired by %s\n`, subDir, (time.Since(modTime) - dc.ttl))
err := os.RemoveAll(subDir.String())
err := subDir.RemoveAll()

if err != nil {
logrus.Tracef(`Error while pruning cache directory "%s".\n%s\n`, subDir, errors.WithStack(err))
Expand Down
12 changes: 4 additions & 8 deletions internal/integrationtest/compile_1/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,12 @@ func compileWithCachePurgeNeeded(t *testing.T, env *integrationtest.Environment,

// purge case: hold file too old
oldDir1 := baseDir.Join("test_old_sketch_1")
err := os.MkdirAll(oldDir1.String(), 0770)
require.Nil(t, err)
err = oldDir1.Join(".last-used").WriteFile([]byte{})
require.Nil(t, err)
err = os.Chtimes(oldDir1.Join(".last-used").String(), time.Now(), time.Unix(0, 0))
require.Nil(t, err)
require.NoError(t, os.MkdirAll(oldDir1.String(), 0770))
require.NoError(t, oldDir1.Join(".last-used").WriteFile([]byte{}))
require.NoError(t, os.Chtimes(oldDir1.Join(".last-used").String(), time.Now(), time.Unix(0, 0)))
// purge case: hold file not existing
oldDir2 := baseDir.Join("test_old_sketch_2")
err = os.MkdirAll(oldDir2.String(), 0770)
require.Nil(t, err)
require.NoError(t, os.MkdirAll(oldDir2.String(), 0770))

defer os.RemoveAll(oldDir1.String())
defer os.RemoveAll(oldDir2.String())
Expand Down

0 comments on commit df1ec17

Please sign in to comment.