|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package buildcache |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/arduino/go-paths-helper" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func Test_UpdateLastUsedFileNotExisting(t *testing.T) { |
| 27 | + testBuildDir := paths.New(t.TempDir(), "sketches", "xxx") |
| 28 | + require.NoError(t, testBuildDir.MkdirAll()) |
| 29 | + timeBeforeUpdating := time.Unix(0, 0) |
| 30 | + requireCorrectUpdate(t, testBuildDir, timeBeforeUpdating) |
| 31 | +} |
| 32 | + |
| 33 | +func Test_UpdateLastUsedFileExisting(t *testing.T) { |
| 34 | + testBuildDir := paths.New(t.TempDir(), "sketches", "xxx") |
| 35 | + require.NoError(t, testBuildDir.MkdirAll()) |
| 36 | + |
| 37 | + // create the file |
| 38 | + preExistingFile := testBuildDir.Join(lastUsedFileName) |
| 39 | + require.NoError(t, preExistingFile.WriteFile([]byte{})) |
| 40 | + timeBeforeUpdating := time.Now().Add(-time.Second) |
| 41 | + preExistingFile.Chtimes(time.Now(), timeBeforeUpdating) |
| 42 | + requireCorrectUpdate(t, testBuildDir, timeBeforeUpdating) |
| 43 | +} |
| 44 | + |
| 45 | +func requireCorrectUpdate(t *testing.T, dir *paths.Path, prevModTime time.Time) { |
| 46 | + _, err := New(dir.Parent()).GetOrCreate(dir.Base()) |
| 47 | + require.NoError(t, err) |
| 48 | + expectedFile := dir.Join(lastUsedFileName) |
| 49 | + fileInfo, err := expectedFile.Stat() |
| 50 | + require.Nil(t, err) |
| 51 | + require.Greater(t, fileInfo.ModTime(), prevModTime) |
| 52 | +} |
| 53 | + |
| 54 | +func TestPurge(t *testing.T) { |
| 55 | + ttl := time.Minute |
| 56 | + |
| 57 | + dirToPurge := paths.New(t.TempDir(), "root") |
| 58 | + |
| 59 | + lastUsedTimesByDirPath := map[*paths.Path]time.Time{ |
| 60 | + (dirToPurge.Join("old")): time.Now().Add(-ttl - time.Hour), |
| 61 | + (dirToPurge.Join("fresh")): time.Now().Add(-ttl + time.Minute), |
| 62 | + } |
| 63 | + |
| 64 | + // create the metadata files |
| 65 | + for dirPath, lastUsedTime := range lastUsedTimesByDirPath { |
| 66 | + require.NoError(t, dirPath.MkdirAll()) |
| 67 | + infoFilePath := dirPath.Join(lastUsedFileName).Canonical() |
| 68 | + require.NoError(t, infoFilePath.WriteFile([]byte{})) |
| 69 | + // make sure access time does not matter |
| 70 | + accesstime := time.Now() |
| 71 | + require.NoError(t, infoFilePath.Chtimes(accesstime, lastUsedTime)) |
| 72 | + } |
| 73 | + |
| 74 | + New(dirToPurge).Purge(ttl) |
| 75 | + |
| 76 | + require.False(t, dirToPurge.Join("old").Exist()) |
| 77 | + require.True(t, dirToPurge.Join("fresh").Exist()) |
| 78 | +} |
0 commit comments