Skip to content

Commit

Permalink
Merge pull request #40 from chaws/fix-paths
Browse files Browse the repository at this point in the history
installer: normalize paths
  • Loading branch information
chaws authored Mar 18, 2022
2 parents 7d9df8c + c466610 commit b04c08f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down
35 changes: 31 additions & 4 deletions cmd/installer/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand Down
31 changes: 31 additions & 0 deletions cmd/installer/root_unix_test.go
Original file line number Diff line number Diff line change
@@ -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 + "////",
}
}
34 changes: 34 additions & 0 deletions cmd/installer/root_windows_test.go
Original file line number Diff line number Diff line change
@@ -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 + "\\\\\\\\",
}
}

0 comments on commit b04c08f

Please sign in to comment.