| 
 | 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 license@arduino.cc.  | 
 | 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 | +	require.NoError(t, GetOrCreate(dir))  | 
 | 47 | +	expectedFile := dir.Join(lastUsedFileName)  | 
 | 48 | +	fileInfo, err := expectedFile.Stat()  | 
 | 49 | +	require.Nil(t, err)  | 
 | 50 | +	require.Greater(t, fileInfo.ModTime(), prevModTime)  | 
 | 51 | +}  | 
 | 52 | + | 
 | 53 | +func TestPurge(t *testing.T) {  | 
 | 54 | +	ttl := time.Minute  | 
 | 55 | + | 
 | 56 | +	dirToPurge := paths.New(t.TempDir(), "root")  | 
 | 57 | + | 
 | 58 | +	lastUsedTimesByDirPath := map[*paths.Path]time.Time{  | 
 | 59 | +		(dirToPurge.Join("old")):   time.Now().Add(-ttl - time.Hour),  | 
 | 60 | +		(dirToPurge.Join("fresh")): time.Now().Add(-ttl + time.Minute),  | 
 | 61 | +	}  | 
 | 62 | + | 
 | 63 | +	// create the metadata files  | 
 | 64 | +	for dirPath, lastUsedTime := range lastUsedTimesByDirPath {  | 
 | 65 | +		require.NoError(t, dirPath.MkdirAll())  | 
 | 66 | +		infoFilePath := dirPath.Join(lastUsedFileName).Canonical()  | 
 | 67 | +		require.NoError(t, infoFilePath.WriteFile([]byte{}))  | 
 | 68 | +		// make sure access time does not matter  | 
 | 69 | +		accesstime := time.Now()  | 
 | 70 | +		require.NoError(t, infoFilePath.Chtimes(accesstime, lastUsedTime))  | 
 | 71 | +	}  | 
 | 72 | + | 
 | 73 | +	Purge(dirToPurge, ttl)  | 
 | 74 | + | 
 | 75 | +	files, err := dirToPurge.Join("fresh").Stat()  | 
 | 76 | +	require.Nil(t, err)  | 
 | 77 | +	require.True(t, files.IsDir())  | 
 | 78 | +	require.True(t, dirToPurge.Exist())  | 
 | 79 | +}  | 
0 commit comments