Skip to content

Commit

Permalink
Merge pull request #4247 from snyk/chore/HMMR-594-clean-cache-directory
Browse files Browse the repository at this point in the history
chore: clean old cliv1 versions in cache directory
  • Loading branch information
Avishagp committed Dec 16, 2022
2 parents a813d61 + 1556f2c commit edd913e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cliv2/internal/cliv2/cliv2.go
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"
"os/exec"
"path"
"strings"

"github.com/snyk/cli/cliv2/internal/constants"
Expand Down Expand Up @@ -60,6 +61,8 @@ func NewCLIv2(cacheDirectory string, debugLogger *log.Logger) (*CLI, error) {
stderr: os.Stderr,
}

cli.ClearCache()

err = cli.ExtractV1Binary()
if err != nil {
fmt.Println(err)
Expand All @@ -69,6 +72,33 @@ func NewCLIv2(cacheDirectory string, debugLogger *log.Logger) (*CLI, error) {
return &cli, nil
}

func (c *CLI) ClearCache() error {
// Get files in directory
fileInfo, err := os.ReadDir(c.CacheDirectory)
if err != nil {
return err
}

// Get current version binary's path
v1BinaryPath := path.Dir(c.v1BinaryLocation)
var maxVersionToDelete = 5
for i, file := range fileInfo {
currentPath := path.Join(c.CacheDirectory, file.Name())
if currentPath != v1BinaryPath {
err = os.RemoveAll(currentPath)
if err != nil {
c.DebugLogger.Println("Error deleting an old version directory: ", currentPath)
}
}
// Stop the loop after 5 deletions to not create too much overhead
if i == maxVersionToDelete {
break
}
}

return nil
}

func (c *CLI) ExtractV1Binary() error {
cliV1ExpectedSHA256 := cliv1.ExpectedSHA256()

Expand Down
66 changes: 66 additions & 0 deletions cliv2/internal/cliv2/cliv2_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"log"
"os"
"path"
"sort"
"testing"

Expand Down Expand Up @@ -222,3 +223,68 @@ func Test_executeUnknownCommand(t *testing.T) {

os.RemoveAll(cacheDir)
}

func Test_clearCache(t *testing.T) {
cacheDir := "cacheDir"
logger := log.New(ioutil.Discard, "", 0)

// create instance under test
cli, _ := cliv2.NewCLIv2(cacheDir, logger)
// create folders and files in cache dir
versionWithV := path.Join(cli.CacheDirectory, "v1.914.0")
versionNoV := path.Join(cli.CacheDirectory, "1.1048.0-dev.2401acbc")
randomFile := path.Join(versionNoV, "filename")
currentVersion := cli.GetBinaryLocation()

os.Mkdir(versionWithV, 0755)
os.Mkdir(versionNoV, 0755)
os.WriteFile(randomFile, []byte("Writing some strings"), 0666)

// clear cache
err := cli.ClearCache()
assert.Nil(t, err)

// check if directories that need to be deleted don't exist
assert.NoDirExists(t, versionWithV)
assert.NoDirExists(t, versionNoV)
assert.NoFileExists(t, randomFile)
// check if directories that need to exist still exist
assert.FileExists(t, currentVersion)
}

func Test_clearCacheBigCache(t *testing.T) {
cacheDir := "cacheDir"
logger := log.New(ioutil.Discard, "", 0)

// create instance under test
cli, _ := cliv2.NewCLIv2(cacheDir, logger)
// create folders and files in cache dir
dir1 := path.Join(cli.CacheDirectory, "dir1")
dir2 := path.Join(cli.CacheDirectory, "dir2")
dir3 := path.Join(cli.CacheDirectory, "dir3")
dir4 := path.Join(cli.CacheDirectory, "dir4")
dir5 := path.Join(cli.CacheDirectory, "dir5")
dir6 := path.Join(cli.CacheDirectory, "dir6")
currentVersion := cli.GetBinaryLocation()

os.Mkdir(dir1, 0755)
os.Mkdir(dir2, 0755)
os.Mkdir(dir3, 0755)
os.Mkdir(dir4, 0755)
os.Mkdir(dir5, 0755)
os.Mkdir(dir6, 0755)

// clear cache
err := cli.ClearCache()
assert.Nil(t, err)

// check if directories that need to be deleted don't exist
assert.NoDirExists(t, dir1)
assert.NoDirExists(t, dir2)
assert.NoDirExists(t, dir3)
assert.NoDirExists(t, dir4)
assert.NoDirExists(t, dir5)
// check if directories that need to exist still exist
assert.DirExists(t, dir6)
assert.FileExists(t, currentVersion)
}

0 comments on commit edd913e

Please sign in to comment.