diff --git a/cmd/errors/errors.go b/cmd/errors/errors.go index e386f82..8ba249f 100644 --- a/cmd/errors/errors.go +++ b/cmd/errors/errors.go @@ -36,6 +36,7 @@ var ( ErrExtractEula = errors.New("user wants to extract embedded license only") ErrLicenseNotFound = errors.New("embedded license not found") ErrPackRootNotFound = errors.New("no CMSIS Pack Root directory specified. Either the environment CMSIS_PACK_ROOT needs to be set or the path specified using the command line option -R/--pack-root string") + ErrPackRootDoesNotExist = errors.New("the specified CMSIS Pack Root directory does NOT exist! Please take a moment to review if the value is correct or create a new one via `cpackget init` command") ErrPdscFileTooDeepInPack = errors.New("pdsc file is too deep in pack file") // Errors related to network diff --git a/cmd/installer/root.go b/cmd/installer/root.go index 6ceab4c..0db2831 100644 --- a/cmd/installer/root.go +++ b/cmd/installer/root.go @@ -247,6 +247,7 @@ func ListInstalledPacks(listCached, listPublic bool) error { return strings.ToLower(matches[i]) < strings.ToLower(matches[j]) }) for _, pdscFilePath := range matches { + log.Debug(pdscFilePath) // Transform pdscFilePath into packName pdscFilePath = strings.Replace(pdscFilePath, Installation.PackRoot, "", -1) @@ -344,10 +345,16 @@ var Installation *PacksInstallationType // SetPackRoot sets the working directory of the packs installation // if create == true, cpackget will try to create needed resources func SetPackRoot(packRoot string, create bool) error { + if len(packRoot) == 0 { + log.Infof("Using pack root: \"%v\"", packRoot) + return errs.ErrPackRootNotFound + } + + packRoot = filepath.Clean(packRoot) log.Infof("Using pack root: \"%v\"", packRoot) - if len(packRoot) == 0 || (!utils.DirExists(packRoot) && !create) { - return errs.ErrPackRootNotFound + if !utils.DirExists(packRoot) && !create { + return errs.ErrPackRootDoesNotExist } Installation = &PacksInstallationType{ diff --git a/cmd/installer/root_test.go b/cmd/installer/root_test.go index ffbfa06..b537c57 100644 --- a/cmd/installer/root_test.go +++ b/cmd/installer/root_test.go @@ -435,10 +435,20 @@ func TestUpdatePublicIndex(t *testing.T) { }) } +func checkPackRoot(t *testing.T, path string) { + assert := assert.New(t) + + assert.True(utils.DirExists(path)) + assert.True(utils.DirExists(installer.Installation.DownloadDir)) + assert.True(utils.DirExists(installer.Installation.WebDir)) + assert.True(utils.DirExists(installer.Installation.LocalDir)) +} + func TestSetPackRoot(t *testing.T) { assert := assert.New(t) + // Sanity tests t.Run("test fail to initialize empty pack root", func(t *testing.T) { localTestingDir := "" err := installer.SetPackRoot(localTestingDir, !CreatePackRoot) @@ -448,19 +458,36 @@ func TestSetPackRoot(t *testing.T) { assert.Equal(errs.ErrPackRootNotFound, err) }) + t.Run("test fail to use non-existing directory", func(t *testing.T) { + localTestingDir := "non-existing-dir" + err := installer.SetPackRoot(localTestingDir, !CreatePackRoot) + assert.Equal(errs.ErrPackRootDoesNotExist, err) + }) + t.Run("test initialize pack root", func(t *testing.T) { localTestingDir := "valid-pack-root" defer os.RemoveAll(localTestingDir) assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) - assert.True(utils.DirExists(localTestingDir)) - assert.True(utils.DirExists(installer.Installation.DownloadDir)) - assert.True(utils.DirExists(installer.Installation.WebDir)) - assert.True(utils.DirExists(installer.Installation.LocalDir)) + checkPackRoot(t, localTestingDir) // Now just make sure it's usable, even when not forced to initialize assert.Nil(installer.SetPackRoot(localTestingDir, !CreatePackRoot)) }) + + // Define a few paths to try out per operating system + paths := generatePaths(t) + for description, path := range paths { + t.Run("test "+description, func(t *testing.T) { + defer os.RemoveAll(path) + assert.Nil(installer.SetPackRoot(path, CreatePackRoot)) + + checkPackRoot(t, path) + + // Now just make sure it's usable, even when not forced to initialize + assert.Nil(installer.SetPackRoot(path, !CreatePackRoot)) + }) + } } func init() { diff --git a/cmd/installer/root_unix_test.go b/cmd/installer/root_unix_test.go new file mode 100644 index 0000000..a27356b --- /dev/null +++ b/cmd/installer/root_unix_test.go @@ -0,0 +1,31 @@ +//go:build !windows +// +build !windows + +/* SPDX-License-Identifier: Apache-2.0 */ +/* Copyright Contributors to the cpackget project. */ + +package installer_test + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" +) + +func generatePaths(t *testing.T) map[string]string { + cwd, err := os.Getwd() + assert.Nil(t, err) + + dirName := "valid-pack-root" + absPath := filepath.Join(cwd, dirName) + + // Define a few paths to try out + return map[string]string{ + "regular absolute path": absPath, + "absolute path with ..": filepath.Join(absPath, "..", dirName), + "multiple leading slashes": "////" + absPath, + "multiple trailing slashes": absPath + "////", + } +} diff --git a/cmd/installer/root_windows_test.go b/cmd/installer/root_windows_test.go new file mode 100644 index 0000000..83531b2 --- /dev/null +++ b/cmd/installer/root_windows_test.go @@ -0,0 +1,34 @@ +//go:build windows +// +build windows + +/* SPDX-License-Identifier: Apache-2.0 */ +/* Copyright Contributors to the cpackget project. */ + +package installer_test + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func generatePaths(t *testing.T) map[string]string { + cwd, err := os.Getwd() + assert.Nil(t, err) + + dirName := "valid-pack-root" + absPath := filepath.Join(cwd, dirName) + + // Define a few paths to try out + return map[string]string{ + "regular absolute path": absPath, + "absolute path with ..": filepath.Join(absPath, "..", dirName), + "absolute path with :/": strings.Replace(absPath, ":\\", "/", 1), + "all forward slashes": strings.ReplaceAll(absPath, "\\", "/"), + "multiple leading slashes": strings.Replace(absPath, ":\\", ":\\\\\\\\", 1), + "multiple trailing slashes": absPath + "\\\\\\\\", + } +}