From 5c47968ac058ddc20498dcac64f8439c34996cd5 Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Tue, 3 Dec 2024 13:19:04 +0100 Subject: [PATCH 01/11] Implemented automagically loading of index.pidx #355, #357, #358 --- cmd/commands/add.go | 9 +- cmd/commands/root.go | 15 ++-- cmd/commands/root_test.go | 2 +- cmd/errors/errors.go | 4 + cmd/installer/root.go | 39 ++++++++- cmd/installer/root_pack_add_test.go | 124 +++++++++++++-------------- cmd/installer/root_pack_list_test.go | 24 +++--- cmd/installer/root_pack_rm_test.go | 18 ++-- cmd/installer/root_pdsc_add_test.go | 12 +-- cmd/installer/root_pdsc_rm_test.go | 12 +-- cmd/installer/root_test.go | 38 ++++---- cmd/utils/utils.go | 2 +- cmd/xml/pidx.go | 25 +++++- cmd/xml/pidx_test.go | 13 +++ testdata/newTimestamp.pidx | 9 ++ testdata/oldTimestamp.pidx | 9 ++ 16 files changed, 224 insertions(+), 131 deletions(-) create mode 100644 testdata/newTimestamp.pidx create mode 100644 testdata/oldTimestamp.pidx diff --git a/cmd/commands/add.go b/cmd/commands/add.go index c6c580ca..d3f5020d 100644 --- a/cmd/commands/add.go +++ b/cmd/commands/add.go @@ -69,13 +69,18 @@ Add a pack using the following "" specification or using packs provided by The file can be a local file or a file hosted somewhere else on the Internet. If it's hosted somewhere, cpackget will first download it then extract all pack files into "CMSIS_PACK_ROOT////" If "-f" is used, cpackget will call "cpackget pack add" on each URL specified in the file.`, - Args: cobra.MinimumNArgs(0), - PersistentPreRunE: configureInstaller, + Args: cobra.MinimumNArgs(0), RunE: func(cmd *cobra.Command, args []string) error { utils.SetEncodedProgress(addCmdFlags.encodedProgress) utils.SetSkipTouch(addCmdFlags.skipTouch) + createPackRoot = true + err := configureInstaller(cmd, args) + if err != nil { + return err + } + if addCmdFlags.packsListFileName != "" { log.Infof("Parsing packs urls via file %v", addCmdFlags.packsListFileName) diff --git a/cmd/commands/root.go b/cmd/commands/root.go index 61215c7c..0d1a5656 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -36,9 +36,6 @@ var AllCommands = []*cobra.Command{ // createPackRoot is a flag that determines if the pack root should be created or not var createPackRoot bool -// defaultPublicIndex is the public index to use in "default mode" -const defaultPublicIndex = "https://www.keil.com/pack/index.pidx" - var viper *viperType.Viper func configureInstallerGlobalCmd(cmd *cobra.Command, args []string) error { @@ -70,37 +67,37 @@ func configureInstaller(cmd *cobra.Command, args []string) error { } targetPackRoot := viper.GetString("pack-root") - checkConnection := viper.GetBool("check-connection") + checkConnection := viper.GetBool("check-connection") // TODO: never set if targetPackRoot == installer.GetDefaultCmsisPackRoot() { // If using the default pack root path and the public index is not found, // initialize it if !checkConnection && !utils.FileExists(filepath.Join(targetPackRoot, ".Web", "index.pidx")) { - err := installer.SetPackRoot(targetPackRoot, true) + err := installer.SetPackRoot(targetPackRoot, true, true) if err != nil { return err } // Exclude index updating commands to not double update if cmd.Name() != "init" && cmd.Name() != "index" && cmd.Name() != "update-index" { installer.UnlockPackRoot() - err = installer.UpdatePublicIndex(defaultPublicIndex, true, true, false, false, 0, 0) + err = installer.UpdatePublicIndex(installer.DefaultPublicIndex, true, true, false, false, 0, 0) if err != nil { return err } - err = installer.SetPackRoot(targetPackRoot, false) + err = installer.SetPackRoot(targetPackRoot, false, true) if err != nil { return err } installer.LockPackRoot() } } else { - err := installer.SetPackRoot(targetPackRoot, createPackRoot) + err := installer.SetPackRoot(targetPackRoot, createPackRoot, true) if err != nil { return err } } } else { - err := installer.SetPackRoot(targetPackRoot, createPackRoot) + err := installer.SetPackRoot(targetPackRoot, createPackRoot, true) if err != nil { return err } diff --git a/cmd/commands/root_test.go b/cmd/commands/root_test.go index d93dac14..ba24220a 100644 --- a/cmd/commands/root_test.go +++ b/cmd/commands/root_test.go @@ -143,7 +143,7 @@ func runTests(t *testing.T, tests []TestCase) { os.Setenv("CMSIS_PACK_ROOT", localTestingDir) if test.createPackRoot { - assert.Nil(installer.SetPackRoot(localTestingDir, test.createPackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, test.createPackRoot, false)) installer.UnlockPackRoot() } diff --git a/cmd/errors/errors.go b/cmd/errors/errors.go index 3d5d2087..3ce5d504 100644 --- a/cmd/errors/errors.go +++ b/cmd/errors/errors.go @@ -54,6 +54,7 @@ var ( ErrFailedInflatingFile = errors.New("fail to inflate file") ErrFailedCreatingDirectory = errors.New("fail to create directory") ErrFileNotFound = errors.New("file not found") + ErrFileNotFoundUseInit = errors.New("index.pdix file not found; use cpackget init command to retrieve it") ErrDirectoryNotFound = errors.New("directory not found") ErrPathAlreadyExists = errors.New("path already exists") ErrCopyingEqualPaths = errors.New("failed copying files: source is the same as destination") @@ -95,4 +96,7 @@ var ( // Error/Flag to detect when a user has requested early termination ErrTerminatedByUser = errors.New("terminated by user request") + + ErrIndexTooOld = errors.New("public index too old") + ErrOffline = errors.New("remote server is offline or cannot be reached") ) diff --git a/cmd/installer/root.go b/cmd/installer/root.go index a7b389eb..97e94dd8 100644 --- a/cmd/installer/root.go +++ b/cmd/installer/root.go @@ -24,6 +24,9 @@ import ( "golang.org/x/sync/semaphore" ) +// DefaultPublicIndex is the public index to use in "default mode" +const DefaultPublicIndex = "https://www.keil.com/pack/index.pidx" + const KeilDefaultPackRoot = "https://www.keil.com/pack/" // GetDefaultCmsisPackRoot provides a default location @@ -613,7 +616,7 @@ func UpdatePublicIndex(indexPath string, overwrite bool, sparse bool, downloadPd } else { if indexPath != "" { if !utils.FileExists(indexPath) && !utils.DirExists(indexPath) { - return errs.ErrFileNotFound + return errs.ErrFileNotFoundUseInit } fileInfo, err := os.Stat(indexPath) if err != nil { @@ -1065,7 +1068,7 @@ 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 { +func SetPackRoot(packRoot string, create, download bool) error { if len(packRoot) == 0 { return errs.ErrPackRootNotFound } @@ -1075,7 +1078,7 @@ func SetPackRoot(packRoot string, create bool) error { return errs.ErrPackRootDoesNotExist } - checkConnection := viper.GetBool("check-connection") + checkConnection := viper.GetBool("check-connection") // TODO: never set if checkConnection && !utils.GetEncodedProgress() { if packRoot == GetDefaultCmsisPackRoot() { log.Infof("Using pack root: \"%v\" (default mode - no specific CMSIS_PACK_ROOT chosen)", packRoot) @@ -1118,6 +1121,36 @@ func SetPackRoot(packRoot string, create bool) error { // Make sure utils.DownloadFile always downloads files to .Download/ utils.CacheDir = Installation.DownloadDir + // If public index already exists then first check if online, then its timestamp + // if we are online and it is too old then download a current version + if download && utils.FileExists(Installation.PublicIndex) { + err := utils.CheckConnection(DefaultPublicIndex, 0) + if err != nil && err != errs.ErrOffline { + return err + } + if err != errs.ErrOffline { + err = Installation.PublicIndexXML.CheckTime() + if err != nil && err != errs.ErrIndexTooOld { + return err + } + if err == errs.ErrIndexTooOld { + UnlockPackRoot() + err := UpdatePublicIndex(DefaultPublicIndex, true, true, false, false, 0, 0) + if err != nil { + return err + } + } + } + } + // if public index does not or not yet exist then download without check + if download && !utils.FileExists(Installation.PublicIndex) { + UnlockPackRoot() + err := UpdatePublicIndex(DefaultPublicIndex, true, true, false, false, 0, 0) + if err != nil { + return err + } + } + err := Installation.PublicIndexXML.Read() if err != nil { return err diff --git a/cmd/installer/root_pack_add_test.go b/cmd/installer/root_pack_add_test.go index 0e58cce3..9c4cf6b1 100644 --- a/cmd/installer/root_pack_add_test.go +++ b/cmd/installer/root_pack_add_test.go @@ -38,7 +38,7 @@ func TestAddPack(t *testing.T) { // Sanity tests t.Run("test installing a pack with bad name", func(t *testing.T) { localTestingDir := "test-add-pack-with-bad-name" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -55,7 +55,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack previously installed", func(t *testing.T) { localTestingDir := "test-add-pack-already-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -77,7 +77,7 @@ func TestAddPack(t *testing.T) { t.Run("test force-reinstalling a pack not yet installed", func(t *testing.T) { localTestingDir := "test-add-pack-force-reinstall-not-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -92,7 +92,7 @@ func TestAddPack(t *testing.T) { t.Run("test force-reinstalling an installed pack", func(t *testing.T) { localTestingDir := "test-add-pack-force-reinstall-already-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -109,7 +109,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing downloaded pack", func(t *testing.T) { localTestingDir := "test-add-downloaded-pack" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -131,7 +131,7 @@ func TestAddPack(t *testing.T) { t.Run("test force-reinstalling an installed pack using encoded progress", func(t *testing.T) { localTestingDir := "test-add-pack-force-reinstall-already-installed-using-encoded-progress" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) utils.SetEncodedProgress(true) @@ -149,7 +149,7 @@ func TestAddPack(t *testing.T) { t.Run("test force-reinstalling a pack with a user interruption", func(t *testing.T) { localTestingDir := "test-add-pack-force-reinstall-user-interruption" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -175,7 +175,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing local pack that does not exist", func(t *testing.T) { localTestingDir := "test-add-local-pack-that-does-not-exist" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -193,7 +193,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing remote pack that does not exist", func(t *testing.T) { localTestingDir := "test-add-remote-pack-that-does-not-exist" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -213,7 +213,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack with corrupt zip file", func(t *testing.T) { localTestingDir := "test-add-pack-with-corrupt-zip" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -231,7 +231,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack with bad URL format", func(t *testing.T) { localTestingDir := "test-add-pack-with-malformed-url" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -249,7 +249,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack with no PDSC file inside", func(t *testing.T) { localTestingDir := "test-add-pack-without-pdsc-file" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -294,7 +294,7 @@ func TestAddPack(t *testing.T) { */ t.Run("test installing a pack with tainted compressed files", func(t *testing.T) { localTestingDir := "test-add-pack-with-tainted-compressed-files" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -313,7 +313,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack with .. in pdsc name", func(t *testing.T) { localTestingDir := "test-add-pack-with-dot-dot-name" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -332,7 +332,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack with version not present in the pdsc file", func(t *testing.T) { localTestingDir := "test-add-pack-with-version-not-present-in-the-pdsc-file" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -350,7 +350,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack with version not the latest in the pdsc file", func(t *testing.T) { localTestingDir := "test-add-pack-with-version-not-the-latest-in-the-pdsc-file" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -369,7 +369,7 @@ func TestAddPack(t *testing.T) { // Test installing a combination of public/non-public local/remote packs t.Run("test installing public pack via local file", func(t *testing.T) { localTestingDir := "test-add-public-local-pack" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -382,7 +382,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing public pack via remote file", func(t *testing.T) { localTestingDir := "test-add-public-remote-pack" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -403,7 +403,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing non-public pack via local file", func(t *testing.T) { localTestingDir := "test-add-non-public-local-pack" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -416,7 +416,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing non-public pack via remote file", func(t *testing.T) { localTestingDir := "test-add-non-public-remote-pack" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -430,7 +430,7 @@ func TestAddPack(t *testing.T) { // Test that cpackget will attempt to retrieve the PDSC file of public packs and place it under .Web/ t.Run("test installing public pack retrieving pdsc file", func(t *testing.T) { localTestingDir := "test-add-public-pack-retrieving-pdsc-file" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -464,7 +464,7 @@ func TestAddPack(t *testing.T) { // Test licenses t.Run("test installing pack without license", func(t *testing.T) { localTestingDir := "test-add-pack-without-license" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -477,7 +477,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with license disagreed", func(t *testing.T) { localTestingDir := "test-add-pack-with-license-disagreed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -501,7 +501,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with license agreed", func(t *testing.T) { localTestingDir := "test-add-pack-with-license-agreed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -514,7 +514,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with rtf license agreed", func(t *testing.T) { localTestingDir := "test-add-pack-with-rtf-license-agreed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -527,7 +527,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with license agreement skipped", func(t *testing.T) { localTestingDir := "test-add-pack-with-license-skipped" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -539,7 +539,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with license extracted", func(t *testing.T) { localTestingDir := "test-add-pack-with-license-extracted" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -559,7 +559,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with license extracted but prev license exist", func(t *testing.T) { localTestingDir := "test-add-pack-with-license-extracted-but-prev-license-exist" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -587,7 +587,7 @@ func TestAddPack(t *testing.T) { // Missing license means it is specified in the PDSC file, but the actual license // file is not there localTestingDir := "test-add-pack-with-missing-license" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -611,7 +611,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with missing license extracted", func(t *testing.T) { localTestingDir := "test-add-pack-with-license-extracted" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -631,7 +631,7 @@ func TestAddPack(t *testing.T) { // Pack with the entire pack structure within another folder t.Run("test installing pack within subfolder", func(t *testing.T) { localTestingDir := "test-add-pack-within-subfolder" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -641,7 +641,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack within too many subfolders", func(t *testing.T) { localTestingDir := "test-add-pack-within-too-many-subfolder" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -659,7 +659,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with pack id pdsc file not found "+packPath, func(t *testing.T) { localTestingDir := "test-add-pack-with-pack-id-pdsc-file-not-found-" + safePackPath - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -687,7 +687,7 @@ func TestAddPack(t *testing.T) { // pdsc file, but DOES NOT serve a pack file t.Run("test installing pack with pack id version not found "+packPath, func(t *testing.T) { localTestingDir := "test-add-pack-with-pack-id-version-not-found-" + safePackPath - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -709,7 +709,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with pack id using release url"+packPath, func(t *testing.T) { localTestingDir := "test-add-pack-with-pack-id-using-release-url" + safePackPath - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -752,7 +752,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with pack id using pdsc url "+packPath, func(t *testing.T) { localTestingDir := "test-add-pack-with-pack-id-using-pdsc-url-" + safePackPath - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -794,7 +794,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing pack with pack id using pdsc url when version is not the latest in index.pidx", func(t *testing.T) { localTestingDir := "test-add-pack-with-pack-id-using-pdsc-url-version-not-the-latest" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -841,7 +841,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing non-public pack via packID", func(t *testing.T) { localTestingDir := "test-add-non-public-local-packid" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -894,7 +894,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack that got cancelled during download", func(t *testing.T) { localTestingDir := "test-add-pack-cancelled-during-download" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -930,7 +930,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack that got cancelled during extraction", func(t *testing.T) { localTestingDir := "test-add-cancelled-during-extraction" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -983,7 +983,7 @@ func TestAddPack(t *testing.T) { // 3. No installation should proceed because the pre-installed 1.2.4 pack already satisfies the >=1.2.3 condition localTestingDir := "test-installing-pack-with-minimum-version-new-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1017,7 +1017,7 @@ func TestAddPack(t *testing.T) { // 3. Then pack 1.2.4 should be installed because that's the latest available localTestingDir := "test-installing-pack-with-minimum-version-none-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1059,7 +1059,7 @@ func TestAddPack(t *testing.T) { // 3. Then pack 1.2.4 should be installed because the pre-installed 1.2.2 does not satisfy >=1.2.3 localTestingDir := "test-installing-pack-with-minimum-version-older-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1108,7 +1108,7 @@ func TestAddPack(t *testing.T) { // 4. Should fail as the minimum version is not available to install localTestingDir := "test-installing-pack-with-minimum-version-higher-latest" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1127,7 +1127,7 @@ func TestAddPack(t *testing.T) { // 3. Should install 0.1.1 because it's the latest compatible version with 0.1.0 localTestingDir := "test-installing-pack-with-minimum-compatible-version-new-major-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1175,7 +1175,7 @@ func TestAddPack(t *testing.T) { // 3. Should install 0.1.1 because it's the latest compatible version with 0.1.0 localTestingDir := "test-installing-pack-with-minimum-compatible-none-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1217,7 +1217,7 @@ func TestAddPack(t *testing.T) { // 3. Should install 0.1.1 because it's the more recent compatible version if compared to with 0.1.0 localTestingDir := "test-installing-pack-with-minimum-compatible-version-older-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1265,7 +1265,7 @@ func TestAddPack(t *testing.T) { // 3. Should not do anything localTestingDir := "test-installing-pack-with-minimum-compatible-version-same-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1300,7 +1300,7 @@ func TestAddPack(t *testing.T) { // 4. Should fail as the minimum comaptible version is not available to install localTestingDir := "test-installing-pack-with-minimum-compatible-version-higher-latest" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1319,7 +1319,7 @@ func TestAddPack(t *testing.T) { // 3. Should install 0.1.1 because it's the patch version with 0.1.0 localTestingDir := "test-installing-pack-with-patch-version-new-major-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1367,7 +1367,7 @@ func TestAddPack(t *testing.T) { // 3. Should install 0.1.1 because it's the patch version with 0.1.0 localTestingDir := "test-installing-pack-with-patch-none-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1409,7 +1409,7 @@ func TestAddPack(t *testing.T) { // 3. Should install 0.1.1 because it's the more patch version if compared to with 0.1.0 localTestingDir := "test-installing-pack-with-patch-version-older-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1457,7 +1457,7 @@ func TestAddPack(t *testing.T) { // 3. Should not do anything localTestingDir := "test-installing-pack-with-patch-version-same-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1492,7 +1492,7 @@ func TestAddPack(t *testing.T) { // 4. Should fail as the patch version is not available to install localTestingDir := "test-installing-pack-with-patch-version-higher-latest" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1511,7 +1511,7 @@ func TestAddPack(t *testing.T) { // 3. Then pack 1.2.4 should be installed because it's the latest one localTestingDir := "test-installing-pack-with-at-latest-version-none-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1553,7 +1553,7 @@ func TestAddPack(t *testing.T) { // 3. Then pack 1.2.4 should be installed because it's more up-to-date than 1.2.3 localTestingDir := "test-installing-pack-with-at-latest-version-none-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1601,7 +1601,7 @@ func TestAddPack(t *testing.T) { // 3. No installation should proceed because the pre-installed 1.2.4 pack already satisfies the @latest condition localTestingDir := "test-installing-pack-with-at-latest-version-latest-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1637,7 +1637,7 @@ func TestAddPack(t *testing.T) { // 3. Then pack 1.2.4 should be installed because that's the latest available localTestingDir := "test-installing-local-pack-with-minimum-version-new-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1683,7 +1683,7 @@ func TestAddPack(t *testing.T) { // 3. Should do nothing localTestingDir := "test-installing-local-pack-with-at-latest-version-matching-pre-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -1706,7 +1706,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack with dependencies", func(t *testing.T) { localTestingDir := "test-installing-pack-with-dependencies" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -1723,7 +1723,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack with dependencies, alpha version string", func(t *testing.T) { localTestingDir := "test-installing-pack-with-dependencies-alpha" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -1740,7 +1740,7 @@ func TestAddPack(t *testing.T) { t.Run("test installing a pack and skipping dependencies", func(t *testing.T) { localTestingDir := "test-installing-pack-with-dependencies-skip" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) diff --git a/cmd/installer/root_pack_list_test.go b/cmd/installer/root_pack_list_test.go index 46b223ad..8d5e38fe 100644 --- a/cmd/installer/root_pack_list_test.go +++ b/cmd/installer/root_pack_list_test.go @@ -30,7 +30,7 @@ var ( // Listing on empty func ExampleListInstalledPacks() { localTestingDir := "test-list-empty-pack-root" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) defer removePackRoot(localTestingDir) log.SetOutput(os.Stdout) @@ -44,7 +44,7 @@ func ExampleListInstalledPacks() { func ExampleListInstalledPacks_emptyCache() { localTestingDir := "test-list-empty-cache" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) defer removePackRoot(localTestingDir) log.SetOutput(os.Stdout) @@ -58,7 +58,7 @@ func ExampleListInstalledPacks_emptyCache() { func ExampleListInstalledPacks_emptyPublicIndex() { localTestingDir := "test-list-empty-index" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) defer removePackRoot(localTestingDir) log.SetOutput(os.Stdout) @@ -76,7 +76,7 @@ func ExampleListInstalledPacks_emptyPublicIndex() { // * 1 is neither installer or cached, it's just available in the public index func ExampleListInstalledPacks_list() { localTestingDir := "test-list-packs" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -113,7 +113,7 @@ func ExampleListInstalledPacks_list() { func ExampleListInstalledPacks_listCached() { localTestingDir := "test-list-cached-packs" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -152,7 +152,7 @@ func TestListInstalledPacks(t *testing.T) { t.Run("test listing all installed packs", func(t *testing.T) { localTestingDir := "test-list-installed-packs" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -196,7 +196,7 @@ func TestListInstalledPacks(t *testing.T) { t.Run("test listing local packs with updated version", func(t *testing.T) { localTestingDir := "test-list-installed-local-packs-with-updated-version" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -236,7 +236,7 @@ func TestListInstalledPacks(t *testing.T) { func ExampleListInstalledPacks_listMalformedInstalledPacks() { localTestingDir := "test-list-malformed-installed-packs" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -274,7 +274,7 @@ func ExampleListInstalledPacks_listMalformedInstalledPacks() { func ExampleListInstalledPacks_filter() { localTestingDir := "test-list-packs-filter" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -309,7 +309,7 @@ func ExampleListInstalledPacks_filter() { func ExampleListInstalledPacks_filterErrorPackages() { localTestingDir := "test-list-filter-error-message" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -346,7 +346,7 @@ func ExampleListInstalledPacks_filterErrorPackages() { func ExampleListInstalledPacks_filterInvalidChars() { localTestingDir := "test-list-filter-invalid-chars" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -380,7 +380,7 @@ func ExampleListInstalledPacks_filterInvalidChars() { func ExampleListInstalledPacks_filteradditionalMessages() { localTestingDir := "test-list-filter-additional-messages" - _ = installer.SetPackRoot(localTestingDir, CreatePackRoot) + _ = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) diff --git a/cmd/installer/root_pack_rm_test.go b/cmd/installer/root_pack_rm_test.go index c34a2ac5..cf56ade3 100644 --- a/cmd/installer/root_pack_rm_test.go +++ b/cmd/installer/root_pack_rm_test.go @@ -22,7 +22,7 @@ func TestRemovePack(t *testing.T) { // Sanity tests t.Run("test removing a pack with malformed name", func(t *testing.T) { localTestingDir := "test-remove-pack-with-bad-name" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -35,7 +35,7 @@ func TestRemovePack(t *testing.T) { t.Run("test removing a pack that is not installed", func(t *testing.T) { localTestingDir := "test-remove-pack-not-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -48,7 +48,7 @@ func TestRemovePack(t *testing.T) { t.Run("test remove a public pack that was added", func(t *testing.T) { localTestingDir := "test-remove-public-pack-that-was-added" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -74,7 +74,7 @@ func TestRemovePack(t *testing.T) { t.Run("test remove a non-public pack that was added", func(t *testing.T) { localTestingDir := "test-remove-nonpublic-pack-that-was-added" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -100,7 +100,7 @@ func TestRemovePack(t *testing.T) { t.Run("test remove version of a pack", func(t *testing.T) { localTestingDir := "test-remove-version-of-a-pack" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -120,7 +120,7 @@ func TestRemovePack(t *testing.T) { t.Run("test remove a pack then purge", func(t *testing.T) { localTestingDir := "test-remove-a-pack-then-purge" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -144,7 +144,7 @@ func TestRemovePack(t *testing.T) { t.Run("test purge a pack with license", func(t *testing.T) { localTestingDir := "test-purge-pack-with-license" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -176,7 +176,7 @@ func TestRemovePack(t *testing.T) { t.Run("test remove latest version", func(t *testing.T) { localTestingDir := "test-remove-latest-versions" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.WebDir = filepath.Join(testDir, "public_index") defer removePackRoot(localTestingDir) @@ -196,7 +196,7 @@ func TestRemovePack(t *testing.T) { t.Run("test remove public pack without pdsc file in .Web folder", func(t *testing.T) { localTestingDir := "test-remove-public-pack-without-pdsc-in-web-folder" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) diff --git a/cmd/installer/root_pdsc_add_test.go b/cmd/installer/root_pdsc_add_test.go index a9bf85dd..f729071e 100644 --- a/cmd/installer/root_pdsc_add_test.go +++ b/cmd/installer/root_pdsc_add_test.go @@ -22,7 +22,7 @@ func TestAddPdsc(t *testing.T) { // Sanity tests t.Run("test add pdsc with bad name", func(t *testing.T) { localTestingDir := "test-add-pdsc-with-bad-name" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -34,7 +34,7 @@ func TestAddPdsc(t *testing.T) { t.Run("test add pdsc with bad local_repository.pidx", func(t *testing.T) { localTestingDir := "test-add-pdsc-with-bad-local-repository" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() installer.Installation.LocalPidx = xml.NewPidxXML(badLocalRepositoryPidx) defer removePackRoot(localTestingDir) @@ -46,7 +46,7 @@ func TestAddPdsc(t *testing.T) { t.Run("test add a pdsc", func(t *testing.T) { localTestingDir := "test-add-a-pdsc" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -58,7 +58,7 @@ func TestAddPdsc(t *testing.T) { t.Run("test add a pdsc already installed", func(t *testing.T) { localTestingDir := "test-add-a-pdsc-already-installed" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -71,7 +71,7 @@ func TestAddPdsc(t *testing.T) { t.Run("test add new pdsc version", func(t *testing.T) { localTestingDir := "test-add-new-pdsc-version" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -84,7 +84,7 @@ func TestAddPdsc(t *testing.T) { t.Run("test add new pdsc version with same path", func(t *testing.T) { localTestingDir := "test-add-new-pdsc-version-same-path" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) diff --git a/cmd/installer/root_pdsc_rm_test.go b/cmd/installer/root_pdsc_rm_test.go index 769d1088..18d507b9 100644 --- a/cmd/installer/root_pdsc_rm_test.go +++ b/cmd/installer/root_pdsc_rm_test.go @@ -18,7 +18,7 @@ func TestRemovePdsc(t *testing.T) { t.Run("test remove pdsc with bad name", func(t *testing.T) { localTestingDir := "test-remove-pdsc-with-bad-name" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -31,7 +31,7 @@ func TestRemovePdsc(t *testing.T) { t.Run("test remove a pdsc", func(t *testing.T) { localTestingDir := "test-remove-pdsc" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -53,7 +53,7 @@ func TestRemovePdsc(t *testing.T) { t.Run("test remove multiple pdscs using basename PDSC file name", func(t *testing.T) { localTestingDir := "test-remove-multiple-pdscs-using-basename-pdsc-file-name" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -79,7 +79,7 @@ func TestRemovePdsc(t *testing.T) { t.Run("test remove a pdsc using full path", func(t *testing.T) { localTestingDir := "test-remove-pdsc-using-full-path" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -102,7 +102,7 @@ func TestRemovePdsc(t *testing.T) { t.Run("test remove one pdsc using full path and leave others untouched", func(t *testing.T) { localTestingDir := "test-remove-one-pdsc-using-full-path-and-leave-others-untouched" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) @@ -126,7 +126,7 @@ func TestRemovePdsc(t *testing.T) { t.Run("test remove a pdsc that does not exist", func(t *testing.T) { localTestingDir := "test-remove-pdsc-that-does-not-exist" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer removePackRoot(localTestingDir) diff --git a/cmd/installer/root_test.go b/cmd/installer/root_test.go index 25cfd00b..ee416e8c 100644 --- a/cmd/installer/root_test.go +++ b/cmd/installer/root_test.go @@ -414,7 +414,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test add not found remote index.pidx", func(t *testing.T) { localTestingDir := "test-add-not-found-remote-index" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -430,7 +430,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test add malformed index.pidx", func(t *testing.T) { localTestingDir := "test-add-malformed-index" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -449,7 +449,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test add remote index.pidx", func(t *testing.T) { localTestingDir := "test-add-remote-index" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -473,7 +473,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test add remaining PDSC from index.pidx", func(t *testing.T) { localTestingDir := "test-add-remaining-PDSC-from-index" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -497,7 +497,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test update-index delete pdsc when not in index.pidx", func(t *testing.T) { localTestingDir := "test-update-index-delete-pdsc-when-not-in-index" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -529,7 +529,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test add local file index.pidx", func(t *testing.T) { localTestingDir := "test-add-local-file-index" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -548,7 +548,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test do not overwrite index.pidx", func(t *testing.T) { localTestingDir := "test-do-not-overwrite-index" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -568,7 +568,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test overwrite index.pidx", func(t *testing.T) { localTestingDir := "test-overwrite-index" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -599,7 +599,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test add remote index.pidx and dowload pdsc files", func(t *testing.T) { localTestingDir := "test-add-remote-index-download-pdsc" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -629,7 +629,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test add remote index.pidx and concurrent dowload pdsc files", func(t *testing.T) { localTestingDir := "test-add-remote-index-concurrent-download-pdsc" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -667,7 +667,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test full update when sparse is false", func(t *testing.T) { localTestingDir := "test-sparse-update" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -736,7 +736,7 @@ func TestUpdatePublicIndex(t *testing.T) { t.Run("test full concurrent update when sparse is false", func(t *testing.T) { localTestingDir := "test-concurrent-sparse-update" - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() defer os.RemoveAll(localTestingDir) @@ -829,28 +829,28 @@ func TestSetPackRoot(t *testing.T) { // Sanity tests t.Run("test fail to initialize empty pack root", func(t *testing.T) { localTestingDir := "" - err := installer.SetPackRoot(localTestingDir, !CreatePackRoot) + err := installer.SetPackRoot(localTestingDir, !CreatePackRoot, false) assert.Equal(errs.ErrPackRootNotFound, err) - err = installer.SetPackRoot(localTestingDir, CreatePackRoot) + err = installer.SetPackRoot(localTestingDir, CreatePackRoot, false) 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) + err := installer.SetPackRoot(localTestingDir, !CreatePackRoot, false) assert.Equal(errs.ErrPackRootDoesNotExist, err) }) t.Run("test initialize pack root", func(t *testing.T) { localTestingDir := "valid-pack-root" defer removePackRoot(localTestingDir) - assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) checkPackRoot(t, localTestingDir) // Now just make sure it's usable, even when not forced to initialize - assert.Nil(installer.SetPackRoot(localTestingDir, !CreatePackRoot)) + assert.Nil(installer.SetPackRoot(localTestingDir, !CreatePackRoot, false)) }) // Define a few paths to try out per operating system @@ -858,12 +858,12 @@ func TestSetPackRoot(t *testing.T) { for description, path := range paths { t.Run("test "+description, func(t *testing.T) { defer os.RemoveAll(path) - assert.Nil(installer.SetPackRoot(path, CreatePackRoot)) + assert.Nil(installer.SetPackRoot(path, CreatePackRoot, false)) checkPackRoot(t, path) // Now just make sure it's usable, even when not forced to initialize - assert.Nil(installer.SetPackRoot(path, !CreatePackRoot)) + assert.Nil(installer.SetPackRoot(path, !CreatePackRoot, false)) }) } } diff --git a/cmd/utils/utils.go b/cmd/utils/utils.go index 30c741e5..adafca21 100644 --- a/cmd/utils/utils.go +++ b/cmd/utils/utils.go @@ -218,7 +218,7 @@ func CheckConnection(url string, timeOut int) error { } if connStatus == "offline" { - return errors.New("remote server is offline or cannot be reached") + return errs.ErrOffline } return nil diff --git a/cmd/xml/pidx.go b/cmd/xml/pidx.go index 16e80c66..f843206e 100644 --- a/cmd/xml/pidx.go +++ b/cmd/xml/pidx.go @@ -8,6 +8,7 @@ import ( "path" "path/filepath" "strings" + "time" errs "github.com/open-cmsis-pack/cpackget/cmd/errors" "github.com/open-cmsis-pack/cpackget/cmd/utils" @@ -25,6 +26,7 @@ type PidxXML struct { SchemaVersion string `xml:"schemaVersion,attr"` Vendor string `xml:"vendor"` URL string `xml:"url"` + TimeStamp string `xml:"timestamp"` Pindex struct { XMLName xml.Name `xml:"pindex"` @@ -173,6 +175,28 @@ func (p *PidxXML) FindPdscTags(pdsc PdscTag) []PdscTag { return foundTags } +// Check timestamp of public index +func (p *PidxXML) CheckTime() error { + log.Debugf("Checking timestamp of pidx \"%s\"", p.fileName) + + p.pdscList = make(map[string][]PdscTag) + + if !utils.FileExists(p.fileName) { + return nil + } + if err := utils.ReadXML(p.fileName, p); err != nil { + return err + } + if t, err := time.Parse(time.RFC3339Nano, p.TimeStamp); err != nil { + return err + } else { + if time.Since(t).Hours() > 24 { // index.pidx older than 1 day + return errs.ErrIndexTooOld + } + } + return nil +} + // Read reads FileName into this PidxXML struct and allocates memory for all PDSC tags. func (p *PidxXML) Read() error { log.Debugf("Reading pidx from file \"%s\"", p.fileName) @@ -192,7 +216,6 @@ func (p *PidxXML) Read() error { p.Vendor = strings.TrimSuffix(vendorName, filepath.Ext(vendorName)) return p.Write() } - if err := utils.ReadXML(p.fileName, p); err != nil { return err } diff --git a/cmd/xml/pidx_test.go b/cmd/xml/pidx_test.go index dce40d89..f9766ccf 100644 --- a/cmd/xml/pidx_test.go +++ b/cmd/xml/pidx_test.go @@ -211,6 +211,19 @@ func TestPidxXML(t *testing.T) { assert.Equal(err.Error(), "XML syntax error on line 3: unexpected EOF") }) + t.Run("test check public index file for old timestamp", func(t *testing.T) { + pidx := xml.NewPidxXML("../../testdata/OldTimestamp.pidx") + err := pidx.CheckTime() + assert.NotNil(err) + assert.Equal(err, errs.ErrIndexTooOld) + }) + + t.Run("test check public index file for new timestamp", func(t *testing.T) { + pidx := xml.NewPidxXML("../../testdata/NewTimestamp.pidx") + err := pidx.CheckTime() + assert.Nil(err) + }) + t.Run("test finding pdsc tag", func(t *testing.T) { fileName := "test-finding-pdsc-tag.pidx" defer os.Remove(fileName) diff --git a/testdata/newTimestamp.pidx b/testdata/newTimestamp.pidx new file mode 100644 index 00000000..e58a5217 --- /dev/null +++ b/testdata/newTimestamp.pidx @@ -0,0 +1,9 @@ + + +Keil +https://www.keil.com/pack/ +2044-11-26T04:01:41.5636929+00:00 + + + + diff --git a/testdata/oldTimestamp.pidx b/testdata/oldTimestamp.pidx new file mode 100644 index 00000000..d6c938b3 --- /dev/null +++ b/testdata/oldTimestamp.pidx @@ -0,0 +1,9 @@ + + +Keil +https://www.keil.com/pack/ +2024-11-20T04:01:41.5636929+00:00 + + + + From 91fde12b066595bb203bd0ec9870fc6c9c2bf8cd Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Tue, 3 Dec 2024 18:25:36 +0100 Subject: [PATCH 02/11] fixed timestamp problem, hopefully --- cmd/commands/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commands/root.go b/cmd/commands/root.go index 0d1a5656..8879fa74 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -97,7 +97,7 @@ func configureInstaller(cmd *cobra.Command, args []string) error { } } } else { - err := installer.SetPackRoot(targetPackRoot, createPackRoot, true) + err := installer.SetPackRoot(targetPackRoot, createPackRoot, cmd.Name() != "init") if err != nil { return err } From 0596d688cbc3635be352b42f5d85df1d36a4be71 Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Tue, 3 Dec 2024 23:12:34 +0100 Subject: [PATCH 03/11] tried to fix the time format error, added debug output --- cmd/xml/pidx.go | 3 +++ scripts/test-xmllint-localrepository | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/cmd/xml/pidx.go b/cmd/xml/pidx.go index f843206e..82a3ad0c 100644 --- a/cmd/xml/pidx.go +++ b/cmd/xml/pidx.go @@ -187,6 +187,9 @@ func (p *PidxXML) CheckTime() error { if err := utils.ReadXML(p.fileName, p); err != nil { return err } + if len(p.TimeStamp) == 0 { + return errs.ErrIndexTooOld // if there is no timestamp it always is too old + } if t, err := time.Parse(time.RFC3339Nano, p.TimeStamp); err != nil { return err } else { diff --git a/scripts/test-xmllint-localrepository b/scripts/test-xmllint-localrepository index c5dd8552..04373060 100755 --- a/scripts/test-xmllint-localrepository +++ b/scripts/test-xmllint-localrepository @@ -25,6 +25,10 @@ PIDX ./build/cpackget init /tmp/index.pidx ./build/cpackget add testdata/devpack/1.2.3/TheVendor.DevPack.pdsc +echo "xxxxxxxx" +cat $PACK_ROOT/.Local/local_repository.pidx +echo "xxxxxxxx" + xmllint --schema testdata/PackIndex.xsd $PACK_ROOT/.Local/local_repository.pidx --noout chmod -R +w $PACK_ROOT From 5cda46d595ae41ed741750f3187443f4f52ef1a6 Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Wed, 4 Dec 2024 00:51:31 +0100 Subject: [PATCH 04/11] When creating empty .pidx files fill the timestamp with a real value --- cmd/xml/pidx.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/xml/pidx.go b/cmd/xml/pidx.go index 82a3ad0c..fcd509b6 100644 --- a/cmd/xml/pidx.go +++ b/cmd/xml/pidx.go @@ -216,6 +216,8 @@ func (p *PidxXML) Read() error { } else { vendorName = path.Base(p.fileName) } + t := time.Now() + p.TimeStamp = t.Format(time.RFC3339Nano) p.Vendor = strings.TrimSuffix(vendorName, filepath.Ext(vendorName)) return p.Write() } From 90a759892ac89af6746861003d16715a9d0cb929 Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Wed, 4 Dec 2024 01:29:07 +0100 Subject: [PATCH 05/11] Changed file not found test to new message removed test output from xmllint test --- cmd/installer/root_pack_add_test.go | 4 ++-- scripts/test-xmllint-localrepository | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/cmd/installer/root_pack_add_test.go b/cmd/installer/root_pack_add_test.go index 9c4cf6b1..fcc58e8b 100644 --- a/cmd/installer/root_pack_add_test.go +++ b/cmd/installer/root_pack_add_test.go @@ -185,7 +185,7 @@ func TestAddPack(t *testing.T) { // Sanity check assert.NotNil(err) - assert.Equal(err, errs.ErrFileNotFound) + assert.Equal(err, errs.ErrFileNotFoundUseInit) // Make sure pack.idx never got touched assert.False(utils.FileExists(installer.Installation.PackIdx)) @@ -259,7 +259,7 @@ func TestAddPack(t *testing.T) { // Sanity check assert.NotNil(err) - assert.Equal(err, errs.ErrPdscFileNotFound) + assert.Equal(err, errs.ErrFileNotFoundUseInit) // Make sure pack.idx never got touched assert.False(utils.FileExists(installer.Installation.PackIdx)) diff --git a/scripts/test-xmllint-localrepository b/scripts/test-xmllint-localrepository index 04373060..c5dd8552 100755 --- a/scripts/test-xmllint-localrepository +++ b/scripts/test-xmllint-localrepository @@ -25,10 +25,6 @@ PIDX ./build/cpackget init /tmp/index.pidx ./build/cpackget add testdata/devpack/1.2.3/TheVendor.DevPack.pdsc -echo "xxxxxxxx" -cat $PACK_ROOT/.Local/local_repository.pidx -echo "xxxxxxxx" - xmllint --schema testdata/PackIndex.xsd $PACK_ROOT/.Local/local_repository.pidx --noout chmod -R +w $PACK_ROOT From d892e44f3cc29a84e71788e0cb74f83ad613bc46 Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Wed, 4 Dec 2024 01:34:08 +0100 Subject: [PATCH 06/11] reverted file not found test change, wrong place changed --- cmd/installer/root_pack_add_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/installer/root_pack_add_test.go b/cmd/installer/root_pack_add_test.go index fcc58e8b..9c4cf6b1 100644 --- a/cmd/installer/root_pack_add_test.go +++ b/cmd/installer/root_pack_add_test.go @@ -185,7 +185,7 @@ func TestAddPack(t *testing.T) { // Sanity check assert.NotNil(err) - assert.Equal(err, errs.ErrFileNotFoundUseInit) + assert.Equal(err, errs.ErrFileNotFound) // Make sure pack.idx never got touched assert.False(utils.FileExists(installer.Installation.PackIdx)) @@ -259,7 +259,7 @@ func TestAddPack(t *testing.T) { // Sanity check assert.NotNil(err) - assert.Equal(err, errs.ErrFileNotFoundUseInit) + assert.Equal(err, errs.ErrPdscFileNotFound) // Make sure pack.idx never got touched assert.False(utils.FileExists(installer.Installation.PackIdx)) From 3e86877712090e8c8ba5ebe7b2b7bc03b988fa0b Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Wed, 4 Dec 2024 01:44:56 +0100 Subject: [PATCH 07/11] changed file not found message at init command --- cmd/commands/init_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commands/init_test.go b/cmd/commands/init_test.go index 7b673f1c..409fe237 100644 --- a/cmd/commands/init_test.go +++ b/cmd/commands/init_test.go @@ -57,7 +57,7 @@ var initCmdTests = []TestCase{ name: "test create using local index.pidx that do not exist", args: []string{"init", notFoundPidxFilePath}, createPackRoot: true, - expectedErr: errs.ErrFileNotFound, + expectedErr: errs.ErrFileNotFoundUseInit, }, { name: "test create using directory as path", From 01a4135ba2d6b8df38672426263382e6d3a325c6 Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Wed, 4 Dec 2024 16:50:11 +0100 Subject: [PATCH 08/11] Many small changes with error message textx --- cmd/commands/checksum_test.go | 2 +- cmd/commands/connection_test.go | 6 ++- cmd/commands/init_test.go | 13 +++--- cmd/commands/root.go | 2 +- cmd/commands/update_index.go | 2 +- cmd/commands/update_index_test.go | 10 ++--- cmd/cryptography/checksum.go | 7 ++-- cmd/errors/errors.go | 12 ++++-- cmd/installer/root.go | 20 +++++---- cmd/installer/root_pack_add_test.go | 2 +- cmd/installer/root_test.go | 64 ++++++++++++++--------------- cmd/utils/utils.go | 5 +-- 12 files changed, 76 insertions(+), 69 deletions(-) diff --git a/cmd/commands/checksum_test.go b/cmd/commands/checksum_test.go index a705b7a5..0320a539 100644 --- a/cmd/commands/checksum_test.go +++ b/cmd/commands/checksum_test.go @@ -31,7 +31,7 @@ var checksumCreateCmdTests = []TestCase{ { name: "test using nonexisting hash function", args: []string{"checksum-create", "Vendor.Pack.1.2.3.pack", "-a", "sha1"}, - expectedErr: errors.New("provided hash function is not supported"), + expectedErr: errs.ErrHashNotSupported, setUpFunc: func(t *TestCase) { f, _ := os.Create("Vendor.Pack.1.2.3.pack.sha256.checksum") f.Close() diff --git a/cmd/commands/connection_test.go b/cmd/commands/connection_test.go index f044e1cb..9f6da220 100644 --- a/cmd/commands/connection_test.go +++ b/cmd/commands/connection_test.go @@ -6,6 +6,8 @@ package commands_test import ( "errors" "testing" + + "github.com/open-cmsis-pack/cpackget/cmd/installer" ) var ( @@ -36,8 +38,8 @@ var connectionCmdTests = []TestCase{ noCleanup: true, setUpFunc: func(t *TestCase) { server := NewServer() - t.args = append(t.args, server.URL()+"index.pidx") - server.AddRoute("index.pidx", []byte(` + t.args = append(t.args, server.URL()+installer.PublicIndex) + server.AddRoute(installer.PublicIndex, []byte(` TheVendor https://www.keil.com/ diff --git a/cmd/commands/init_test.go b/cmd/commands/init_test.go index 409fe237..05d502a5 100644 --- a/cmd/commands/init_test.go +++ b/cmd/commands/init_test.go @@ -10,6 +10,7 @@ import ( "testing" errs "github.com/open-cmsis-pack/cpackget/cmd/errors" + "github.com/open-cmsis-pack/cpackget/cmd/installer" ) // Tests for init command are placed here because there was something wrong @@ -17,7 +18,7 @@ import ( var ( pidxFilePath = filepath.Join(testingDir, "SamplePublicIndex.pidx") - notFoundPidxFilePath = filepath.Join("path", "to", "index.pidx") + notFoundPidxFilePath = filepath.Join("path", "to", installer.PublicIndex) ) var initCmdTests = []TestCase{ @@ -32,12 +33,12 @@ var initCmdTests = []TestCase{ expectedErr: nil, }, { - name: "test create using an index.pidx", + name: "test create using an " + installer.PublicIndex, args: []string{"init"}, setUpFunc: func(t *TestCase) { server := NewServer() - t.args = append(t.args, server.URL()+"index.pidx") - server.AddRoute("index.pidx", []byte(` + t.args = append(t.args, server.URL()+installer.PublicIndex) + server.AddRoute(installer.PublicIndex, []byte(` TheVendor https://the.vendor/ @@ -49,12 +50,12 @@ var initCmdTests = []TestCase{ }, }, { - name: "test create using local index.pidx", + name: "test create using local " + installer.PublicIndex, args: []string{"init", pidxFilePath}, createPackRoot: true, }, { - name: "test create using local index.pidx that do not exist", + name: "test create using local " + installer.PublicIndex + " that does not exist", args: []string{"init", notFoundPidxFilePath}, createPackRoot: true, expectedErr: errs.ErrFileNotFoundUseInit, diff --git a/cmd/commands/root.go b/cmd/commands/root.go index 8879fa74..9b52bae5 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -72,7 +72,7 @@ func configureInstaller(cmd *cobra.Command, args []string) error { if targetPackRoot == installer.GetDefaultCmsisPackRoot() { // If using the default pack root path and the public index is not found, // initialize it - if !checkConnection && !utils.FileExists(filepath.Join(targetPackRoot, ".Web", "index.pidx")) { + if !checkConnection && !utils.FileExists(filepath.Join(targetPackRoot, ".Web", installer.PublicIndex)) { err := installer.SetPackRoot(targetPackRoot, true, true) if err != nil { return err diff --git a/cmd/commands/update_index.go b/cmd/commands/update_index.go index 20345f9f..14d8cdfe 100644 --- a/cmd/commands/update_index.go +++ b/cmd/commands/update_index.go @@ -43,7 +43,7 @@ var UpdateIndexCmd = &cobra.Command{ } func getLongUpdateDescription() string { - return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + `/.Web/index.pidx using the URL in tag inside index.pidx. + return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + "/.web/" + installer.PublicIndex + " using the URL in tag inside " + installer.PublicIndex + `. By default it will also check if all PDSC files under .Web/ need update as well. This can be disabled via the "--sparse" flag.` } diff --git a/cmd/commands/update_index_test.go b/cmd/commands/update_index_test.go index 1ba1e148..4a02b759 100644 --- a/cmd/commands/update_index_test.go +++ b/cmd/commands/update_index_test.go @@ -16,7 +16,7 @@ var updateIndexServer Server var updateIndexCmdTests = []TestCase{ { name: "test no parameter is required", - args: []string{"update-index", "index.pidx"}, + args: []string{"update-index", installer.PublicIndex}, expectedErr: errors.New("accepts 0 arg(s), received 1"), }, { @@ -29,7 +29,7 @@ var updateIndexCmdTests = []TestCase{ args: []string{"update-index"}, createPackRoot: true, defaultMode: true, - expectedStdout: []string{"Updating public index", "Downloading index.pidx"}, + expectedStdout: []string{"Updating public index", "Downloading " + installer.PublicIndex}, setUpFunc: func(t *TestCase) { indexContent := ` @@ -43,14 +43,14 @@ var updateIndexCmdTests = []TestCase{ indexContent = fmt.Sprintf(indexContent, updateIndexServer.URL()) _ = os.WriteFile(installer.Installation.PublicIndex, []byte(indexContent), 0600) - updateIndexServer.AddRoute("index.pidx", []byte(indexContent)) + updateIndexServer.AddRoute(installer.PublicIndex, []byte(indexContent)) }, }, { name: "test updating index", args: []string{"update-index"}, createPackRoot: true, - expectedStdout: []string{"Updating public index", "Downloading index.pidx"}, + expectedStdout: []string{"Updating public index", "Downloading " + installer.PublicIndex}, setUpFunc: func(t *TestCase) { indexContent := ` @@ -64,7 +64,7 @@ var updateIndexCmdTests = []TestCase{ indexContent = fmt.Sprintf(indexContent, updateIndexServer.URL()) _ = os.WriteFile(installer.Installation.PublicIndex, []byte(indexContent), 0600) - updateIndexServer.AddRoute("index.pidx", []byte(indexContent)) + updateIndexServer.AddRoute(installer.PublicIndex, []byte(indexContent)) }, }, } diff --git a/cmd/cryptography/checksum.go b/cmd/cryptography/checksum.go index d8a9408e..d4c4d16f 100755 --- a/cmd/cryptography/checksum.go +++ b/cmd/cryptography/checksum.go @@ -1,7 +1,6 @@ package cryptography import ( - "errors" "os" "path/filepath" "strings" @@ -46,7 +45,7 @@ func WriteChecksumFile(digests map[string]string, filename string) error { // GenerateChecksum creates a .checksum file for a pack. func GenerateChecksum(sourcePack, destinationDir, hashFunction string) error { if !isValidHash(hashFunction) { - return errors.New("provided hash function is not supported") + return errs.ErrHashNotSupported } if !utils.FileExists(sourcePack) { log.Errorf("\"%s\" does not exist", sourcePack) @@ -106,7 +105,7 @@ func VerifyChecksum(packPath, checksumPath string) error { } hashFunction := filepath.Ext(strings.Split(checksumPath, ".checksum")[0])[1:] if !isValidHash(hashFunction) { - return errors.New("not a valid .checksum file (correct format is [].[].checksum). Please confirm if the hash is supported") + return errs.ErrNotValidChecksumFile } // Compute pack's digests @@ -144,7 +143,7 @@ func VerifyChecksum(packPath, checksumPath string) error { } } if failure { - return errors.New("bad pack integrity") + return errs.ErrBadIntegrity } log.Info("pack integrity verified, all checksums match.") diff --git a/cmd/errors/errors.go b/cmd/errors/errors.go index 3ce5d504..bdaeb072 100644 --- a/cmd/errors/errors.go +++ b/cmd/errors/errors.go @@ -46,6 +46,8 @@ var ( // Errors related to network ErrBadRequest = errors.New("bad request") ErrFailedDownloadingFile = errors.New("failed to download file") + ErrOffline = errors.New("remote server is offline or cannot be reached") + ErrHTTPtimeout = errors.New("HTTP get timed out") // Errors related to file system ErrFailedCreatingFile = errors.New("failed to create a local file") @@ -54,7 +56,7 @@ var ( ErrFailedInflatingFile = errors.New("fail to inflate file") ErrFailedCreatingDirectory = errors.New("fail to create directory") ErrFileNotFound = errors.New("file not found") - ErrFileNotFoundUseInit = errors.New("index.pdix file not found; use cpackget init command to retrieve it") + ErrFileNotFoundUseInit = errors.New("\"index.pidx\" file not found; use cpackget init command to retrieve it") ErrDirectoryNotFound = errors.New("directory not found") ErrPathAlreadyExists = errors.New("path already exists") ErrCopyingEqualPaths = errors.New("failed copying files: source is the same as destination") @@ -70,6 +72,9 @@ var ( ErrUnsupportedKeyAlgo = errors.New("unsupported key algorithm") ErrCannotVerifySignature = errors.New("cannot verify pack signature") ErrPossibleMaliciousPack = errors.New("bad pack integrity! signature does not match pack contents - might have been tampered") + ErrHashNotSupported = errors.New("provided hash function is not supported") + ErrNotValidChecksumFile = errors.New("not a valid .checksum file (correct format is [].[].checksum). Please confirm if the hash is supported") + ErrBadIntegrity = errors.New("bad pack integrity") // Security errors ErrInsecureZipFileName = errors.New("zip file contains insecure characters: ../") @@ -82,7 +87,7 @@ var ( // Cmdline errors ErrIncorrectCmdArgs = errors.New("incorrect setup of command line arguments") - // Errors on installation strucuture + // Errors on installation structure ErrCannotOverwritePublicIndex = errors.New("cannot replace \"index.pidx\", use the flag \"-f/--force\" to force overwritting it") ErrInvalidPublicIndexReference = errors.New("the specified index path can only either empty, a local file or an HTTP(S) URL - not a directory") ErrPackPdscCannotBeFound = errors.New("the URL is invalid or does not return the file") @@ -97,6 +102,5 @@ var ( // Error/Flag to detect when a user has requested early termination ErrTerminatedByUser = errors.New("terminated by user request") - ErrIndexTooOld = errors.New("public index too old") - ErrOffline = errors.New("remote server is offline or cannot be reached") + ErrIndexTooOld = errors.New("public index \"index.pidx\" too old") ) diff --git a/cmd/installer/root.go b/cmd/installer/root.go index 97e94dd8..ae7f0957 100644 --- a/cmd/installer/root.go +++ b/cmd/installer/root.go @@ -5,6 +5,7 @@ package installer import ( "context" + "errors" "fmt" "net/url" "os" @@ -24,11 +25,12 @@ import ( "golang.org/x/sync/semaphore" ) -// DefaultPublicIndex is the public index to use in "default mode" -const DefaultPublicIndex = "https://www.keil.com/pack/index.pidx" - +const PublicIndex = "index.pidx" const KeilDefaultPackRoot = "https://www.keil.com/pack/" +// DefaultPublicIndex is the public index to use in "default mode" +const DefaultPublicIndex = KeilDefaultPackRoot + PublicIndex + // GetDefaultCmsisPackRoot provides a default location // for the pack root if not provided. This is to enable // a "default mode", where the public index will be @@ -449,7 +451,7 @@ func DownloadPDSCFiles(skipInstalledPdscFiles bool, concurrency int, timeout int } func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int) error { - log.Info("Updating PDSC files of installed packs referenced in index.pidx") + log.Info("Updating PDSC files of installed packs referenced in " + PublicIndex) pdscFiles, err := utils.ListDir(Installation.WebDir, ".pdsc$") if err != nil { return err @@ -483,7 +485,7 @@ func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int // Warn the user if the pack is no longer present in index.pidx tags := pidxXML.FindPdscTags(searchTag) if len(tags) == 0 { - log.Warnf("The pack %s::%s is no longer present in the updated index.pidx, deleting PDSC file \"%v\"", pdscXML.Vendor, pdscXML.Name, pdscFile) + log.Warnf("The pack %s::%s is no longer present in the updated \"%s\", deleting PDSC file \"%v\"", pdscXML.Vendor, pdscXML.Name, PublicIndex, pdscFile) utils.UnsetReadOnly(pdscFile) os.Remove(pdscFile) continue @@ -596,7 +598,7 @@ func UpdatePublicIndex(indexPath string, overwrite bool, sparse bool, downloadPd // For backwards compatibility, allow indexPath to be a file, but ideally it should be empty if indexPath == "" { - indexPath = fmt.Sprintf("%s/index.pidx", strings.TrimSuffix(Installation.PublicIndexXML.URL, "/")) + indexPath = strings.TrimSuffix(Installation.PublicIndexXML.URL, "/") + "/" + PublicIndex } log.Debugf("Updating public index with \"%v\"", indexPath) @@ -1095,7 +1097,7 @@ func SetPackRoot(packRoot string, create, download bool) error { } Installation.LocalPidx = xml.NewPidxXML(filepath.Join(Installation.LocalDir, "local_repository.pidx")) Installation.PackIdx = filepath.Join(packRoot, "pack.idx") - Installation.PublicIndex = filepath.Join(Installation.WebDir, "index.pidx") + Installation.PublicIndex = filepath.Join(Installation.WebDir, PublicIndex) Installation.PublicIndexXML = xml.NewPidxXML(Installation.PublicIndex) missingDirs := []string{} @@ -1125,10 +1127,10 @@ func SetPackRoot(packRoot string, create, download bool) error { // if we are online and it is too old then download a current version if download && utils.FileExists(Installation.PublicIndex) { err := utils.CheckConnection(DefaultPublicIndex, 0) - if err != nil && err != errs.ErrOffline { + if err != nil && errors.Unwrap(err) != errs.ErrOffline { return err } - if err != errs.ErrOffline { + if errors.Unwrap(err) != errs.ErrOffline { err = Installation.PublicIndexXML.CheckTime() if err != nil && err != errs.ErrIndexTooOld { return err diff --git a/cmd/installer/root_pack_add_test.go b/cmd/installer/root_pack_add_test.go index 9c4cf6b1..187443e0 100644 --- a/cmd/installer/root_pack_add_test.go +++ b/cmd/installer/root_pack_add_test.go @@ -792,7 +792,7 @@ func TestAddPack(t *testing.T) { }) } - t.Run("test installing pack with pack id using pdsc url when version is not the latest in index.pidx", func(t *testing.T) { + t.Run("test installing pack with pack id using pdsc url when version is not the latest in "+installer.PublicIndex, func(t *testing.T) { localTestingDir := "test-add-pack-with-pack-id-using-pdsc-url-version-not-the-latest" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() diff --git a/cmd/installer/root_test.go b/cmd/installer/root_test.go index ee416e8c..3f28290e 100644 --- a/cmd/installer/root_test.go +++ b/cmd/installer/root_test.go @@ -391,7 +391,7 @@ func TestUpdatePublicIndex(t *testing.T) { var Concurrency = 0 // Re-enable this test when a flag --enforce-security is implemented - // t.Run("test add http server index.pidx", func(t *testing.T) { + // t.Run("test add http server "+installer.PublicIndex, func(t *testing.T) { // localTestingDir := "test-add-http-server-index" // assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot)) // installer.UnlockPackRoot() @@ -405,14 +405,14 @@ func TestUpdatePublicIndex(t *testing.T) { // ), // ) - // indexPath := httpServer.URL + "/index.pidx" + // indexPath := httpServer.URL + "/" + installer.PublicIndexindex.pidx // err := installer.UpdatePublicIndex(indexPath, Overwrite) // assert.NotNil(err) // assert.Equal(errs.ErrIndexPathNotSafe, err) // }) - t.Run("test add not found remote index.pidx", func(t *testing.T) { + t.Run("test add not found remote "+installer.PublicIndex, func(t *testing.T) { localTestingDir := "test-add-not-found-remote-index" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -428,7 +428,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Equal(errors.Unwrap(err), errs.ErrBadRequest) }) - t.Run("test add malformed index.pidx", func(t *testing.T) { + t.Run("test add malformed "+installer.PublicIndex, func(t *testing.T) { localTestingDir := "test-add-malformed-index" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -438,8 +438,8 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Nil(err) indexServer := NewServer() - indexServer.AddRoute("index.pidx", indexContent) - indexPath := indexServer.URL() + "index.pidx" + indexServer.AddRoute(installer.PublicIndex, indexContent) + indexPath := indexServer.URL() + installer.PublicIndex err = installer.UpdatePublicIndex(indexPath, Overwrite, Sparse, DownloadPdsc, !DownloadRemainingPdscFiles, Concurrency, Timeout) @@ -447,7 +447,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Equal(err.Error(), "XML syntax error on line 3: unexpected EOF") }) - t.Run("test add remote index.pidx", func(t *testing.T) { + t.Run("test add remote "+installer.PublicIndex, func(t *testing.T) { localTestingDir := "test-add-remote-index" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -456,8 +456,8 @@ func TestUpdatePublicIndex(t *testing.T) { indexContent, err := os.ReadFile(samplePublicIndex) assert.Nil(err) indexServer := NewServer() - indexServer.AddRoute("index.pidx", indexContent) - indexPath := indexServer.URL() + "index.pidx" + indexServer.AddRoute(installer.PublicIndex, indexContent) + indexPath := indexServer.URL() + installer.PublicIndex err = installer.UpdatePublicIndex(indexPath, Overwrite, Sparse, DownloadPdsc, !DownloadRemainingPdscFiles, Concurrency, Timeout) @@ -471,7 +471,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Equal(copied, indexContent) }) - t.Run("test add remaining PDSC from index.pidx", func(t *testing.T) { + t.Run("test add remaining PDSC from "+installer.PublicIndex, func(t *testing.T) { localTestingDir := "test-add-remaining-PDSC-from-index" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -482,8 +482,8 @@ func TestUpdatePublicIndex(t *testing.T) { indexServer := NewServer() // The psd URL needs to be updated as it's not known beforehand updatedIndex := []byte(strings.Replace(string(indexContent), "https://127.0.0.1", indexServer.URL(), -1)) - indexServer.AddRoute("index.pidx", updatedIndex) - indexPath := indexServer.URL() + "index.pidx" + indexServer.AddRoute(installer.PublicIndex, updatedIndex) + indexPath := indexServer.URL() + installer.PublicIndex pdscContent, err := os.ReadFile(publicLocalPack123Pdsc) assert.Nil(err) @@ -495,7 +495,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.True(utils.FileExists(path.Join(localTestingDir, ".Web", "TheVendor.PublicLocalPack.pdsc"))) }) - t.Run("test update-index delete pdsc when not in index.pidx", func(t *testing.T) { + t.Run("test update-index delete pdsc when not in "+installer.PublicIndex, func(t *testing.T) { localTestingDir := "test-update-index-delete-pdsc-when-not-in-index" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -504,8 +504,8 @@ func TestUpdatePublicIndex(t *testing.T) { indexContent, err := os.ReadFile(samplePublicIndex) assert.Nil(err) indexServer := NewServer() - indexServer.AddRoute("index.pidx", indexContent) - indexPath := indexServer.URL() + "index.pidx" + indexServer.AddRoute(installer.PublicIndex, indexContent) + indexPath := indexServer.URL() + installer.PublicIndex err = installer.UpdatePublicIndex(indexPath, Overwrite, Sparse, DownloadPdsc, !DownloadRemainingPdscFiles, Concurrency, Timeout) assert.Nil(err) @@ -527,7 +527,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.False(utils.FileExists(filepath.Join(localTestingDir, ".Web", "TheVendor.PackNotInIndex.pdsc"))) }) - t.Run("test add local file index.pidx", func(t *testing.T) { + t.Run("test add local file "+installer.PublicIndex, func(t *testing.T) { localTestingDir := "test-add-local-file-index" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -546,7 +546,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Equal(copied, indexContent) }) - t.Run("test do not overwrite index.pidx", func(t *testing.T) { + t.Run("test do not overwrite "+installer.PublicIndex, func(t *testing.T) { localTestingDir := "test-do-not-overwrite-index" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -557,8 +557,8 @@ func TestUpdatePublicIndex(t *testing.T) { indexContent, err := os.ReadFile(samplePublicIndex) assert.Nil(err) indexServer := NewServer() - indexServer.AddRoute("index.pidx", indexContent) - indexPath := indexServer.URL() + "index.pidx" + indexServer.AddRoute(installer.PublicIndex, indexContent) + indexPath := indexServer.URL() + installer.PublicIndex err = installer.UpdatePublicIndex(indexPath, !Overwrite, Sparse, DownloadPdsc, !DownloadRemainingPdscFiles, Concurrency, Timeout) @@ -566,7 +566,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Equal(errs.ErrCannotOverwritePublicIndex, err) }) - t.Run("test overwrite index.pidx", func(t *testing.T) { + t.Run("test overwrite "+installer.PublicIndex, func(t *testing.T) { localTestingDir := "test-overwrite-index" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -577,8 +577,8 @@ func TestUpdatePublicIndex(t *testing.T) { indexContent, err := os.ReadFile(samplePublicIndex) assert.Nil(err) indexServer := NewServer() - indexServer.AddRoute("index.pidx", indexContent) - indexPath := indexServer.URL() + "index.pidx" + indexServer.AddRoute(installer.PublicIndex, indexContent) + indexPath := indexServer.URL() + installer.PublicIndex err = installer.UpdatePublicIndex(indexPath, Overwrite, Sparse, DownloadPdsc, !DownloadRemainingPdscFiles, Concurrency, Timeout) @@ -597,7 +597,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.NotEqual(999999, installer.CheckConcurrency(999999)) }) - t.Run("test add remote index.pidx and dowload pdsc files", func(t *testing.T) { + t.Run("test add remote "+installer.PublicIndex+" and dowload pdsc files", func(t *testing.T) { localTestingDir := "test-add-remote-index-download-pdsc" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -608,8 +608,8 @@ func TestUpdatePublicIndex(t *testing.T) { indexServer := NewServer() // The psd URL needs to be updated as it's not known beforehand updatedIndex := []byte(strings.Replace(string(indexContent), "https://127.0.0.1", indexServer.URL(), -1)) - indexServer.AddRoute("index.pidx", updatedIndex) - indexPath := indexServer.URL() + "index.pidx" + indexServer.AddRoute(installer.PublicIndex, updatedIndex) + indexPath := indexServer.URL() + installer.PublicIndex pdscContent, err := os.ReadFile(publicLocalPack123Pdsc) assert.Nil(err) @@ -627,7 +627,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.True(utils.FileExists(installer.Installation.WebDir + string(filepath.Separator) + "TheVendor.PublicLocalPack.pdsc")) }) - t.Run("test add remote index.pidx and concurrent dowload pdsc files", func(t *testing.T) { + t.Run("test add remote "+installer.PublicIndex+" and concurrent dowload pdsc files", func(t *testing.T) { localTestingDir := "test-add-remote-index-concurrent-download-pdsc" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot, false)) installer.UnlockPackRoot() @@ -638,8 +638,8 @@ func TestUpdatePublicIndex(t *testing.T) { indexServer := NewServer() // The psd URL needs to be updated as it's not known beforehand updatedIndex := []byte(strings.Replace(string(indexContent), "https://127.0.0.1", indexServer.URL(), -1)) - indexServer.AddRoute("index.pidx", updatedIndex) - indexPath := indexServer.URL() + "index.pidx" + indexServer.AddRoute(installer.PublicIndex, updatedIndex) + indexPath := indexServer.URL() + installer.PublicIndex for i := 1; i < 11; i++ { pdsc := publicConcurrentLocalPdscBase + fmt.Sprint(i) + ".pdsc" @@ -697,7 +697,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Nil(err) // Create a temp index file to serve it for update - tempIndexFile := filepath.Join(localTestingDir, "index.pidx") + tempIndexFile := filepath.Join(localTestingDir, installer.PublicIndex) assert.Nil(utils.CopyFile(samplePublicIndex, tempIndexFile)) indexXML := xml.NewPidxXML(tempIndexFile) assert.Nil(indexXML.Read()) @@ -711,7 +711,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Nil(indexXML.Write()) indexContent, err := os.ReadFile(tempIndexFile) assert.Nil(err) - indexServer.AddRoute("index.pidx", indexContent) + indexServer.AddRoute(installer.PublicIndex, indexContent) // Add the path to the pack's pdsc pdscContent, err := os.ReadFile(publicLocalPack124Pdsc) @@ -760,7 +760,7 @@ func TestUpdatePublicIndex(t *testing.T) { } // Now get a new index.pidx and the 1.2.4 pdscs into the server and attempt updating with sparse=false - tempIndexFile := filepath.Join(localTestingDir, "index.pidx") + tempIndexFile := filepath.Join(localTestingDir, installer.PublicIndex) assert.Nil(utils.CopyFile(samplePublicIndex, tempIndexFile)) indexXML := xml.NewPidxXML(tempIndexFile) assert.Nil(indexXML.Read()) @@ -777,7 +777,7 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Nil(indexXML.Write()) indexContent, err := os.ReadFile(tempIndexFile) assert.Nil(err) - indexServer.AddRoute("index.pidx", indexContent) + indexServer.AddRoute(installer.PublicIndex, indexContent) // Add the path to the pack's pdsc for i := 1; i < 11; i++ { diff --git a/cmd/utils/utils.go b/cmd/utils/utils.go index adafca21..9b4acfa7 100644 --- a/cmd/utils/utils.go +++ b/cmd/utils/utils.go @@ -8,7 +8,6 @@ import ( "crypto/tls" "encoding/base64" "encoding/xml" - "errors" "fmt" "io" "io/fs" @@ -89,7 +88,7 @@ func (t *TimeoutTransport) RoundTrip(req *http.Request) (*http.Response, error) select { case <-timeout: t.Transport.CancelRequest(req) - return nil, errors.New("HTTP get timed out") + return nil, errs.ErrHTTPtimeout case r := <-resp: return r.resp, r.err } @@ -218,7 +217,7 @@ func CheckConnection(url string, timeOut int) error { } if connStatus == "offline" { - return errs.ErrOffline + return fmt.Errorf("\"%s\": %w", url, errs.ErrOffline) } return nil From 12a9a15a620cfdf078ba08b0492c7f9d37de3187 Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Wed, 4 Dec 2024 18:43:45 +0100 Subject: [PATCH 09/11] fix for offline test, "connection" also needs no index.pidx download, .Web uppercase W, suppress unused zip warning --- cmd/commands/connection_test.go | 9 +++++---- cmd/commands/root.go | 3 ++- cmd/commands/root_test.go | 7 ++++++- cmd/commands/update_index.go | 2 +- cmd/cryptography/signature.go | 1 + 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/commands/connection_test.go b/cmd/commands/connection_test.go index 9f6da220..1596b1f4 100644 --- a/cmd/commands/connection_test.go +++ b/cmd/commands/connection_test.go @@ -4,9 +4,9 @@ package commands_test import ( - "errors" "testing" + errs "github.com/open-cmsis-pack/cpackget/cmd/errors" "github.com/open-cmsis-pack/cpackget/cmd/installer" ) @@ -27,9 +27,10 @@ var connectionCmdTests = []TestCase{ expectedErr: nil, }, { - name: "test checking invalid url", - args: []string{"connection", wrongURLPath}, - expectedErr: errors.New("remote server is offline or cannot be reached"), + name: "test checking invalid url", + args: []string{"connection", wrongURLPath}, + expectedErr: errs.ErrOffline, + expErrUnwwrap: true, }, { // set up environment for next test diff --git a/cmd/commands/root.go b/cmd/commands/root.go index 9b52bae5..d158e9e9 100644 --- a/cmd/commands/root.go +++ b/cmd/commands/root.go @@ -97,7 +97,8 @@ func configureInstaller(cmd *cobra.Command, args []string) error { } } } else { - err := installer.SetPackRoot(targetPackRoot, createPackRoot, cmd.Name() != "init") + download := cmd.Name() != "init" && cmd.Name() != "connection" + err := installer.SetPackRoot(targetPackRoot, createPackRoot, download) if err != nil { return err } diff --git a/cmd/commands/root_test.go b/cmd/commands/root_test.go index ba24220a..ba353f8c 100644 --- a/cmd/commands/root_test.go +++ b/cmd/commands/root_test.go @@ -42,6 +42,7 @@ type TestCase struct { expectedStdout []string expectedStderr []string expectedErr error + expErrUnwwrap bool setUpFunc func(t *TestCase) tearDownFunc func() validationFunc func(t *testing.T) @@ -200,7 +201,11 @@ func runTests(t *testing.T, tests []TestCase) { outStr := string(outBytes) errStr := string(errBytes) - assert.Equal(test.expectedErr, cmdErr) + if test.expErrUnwwrap { + assert.Equal(test.expectedErr, errors.Unwrap(cmdErr)) + } else { + assert.Equal(test.expectedErr, cmdErr) + } for _, expectedStr := range test.expectedStdout { assert.Contains(outStr, expectedStr) } diff --git a/cmd/commands/update_index.go b/cmd/commands/update_index.go index 14d8cdfe..909e57e6 100644 --- a/cmd/commands/update_index.go +++ b/cmd/commands/update_index.go @@ -43,7 +43,7 @@ var UpdateIndexCmd = &cobra.Command{ } func getLongUpdateDescription() string { - return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + "/.web/" + installer.PublicIndex + " using the URL in tag inside " + installer.PublicIndex + `. + return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + "/.Web/" + installer.PublicIndex + " using the URL in tag inside " + installer.PublicIndex + `. By default it will also check if all PDSC files under .Web/ need update as well. This can be disabled via the "--sparse" flag.` } diff --git a/cmd/cryptography/signature.go b/cmd/cryptography/signature.go index d6b90e10..61e85865 100644 --- a/cmd/cryptography/signature.go +++ b/cmd/cryptography/signature.go @@ -460,6 +460,7 @@ func verifyPackFullSignature(zip *zip.ReadCloser, vendor, b64Cert, b64Hash strin // verifyPackCertOnlySignature validates the integrity of a pack // by performing some validations on the embed certificate. func verifyPackCertOnlySignature(zip *zip.ReadCloser, vendor, b64Cert string, skipCertValidation, skipInfo bool) error { + _ = zip rawCert, err := base64.StdEncoding.DecodeString(b64Cert) if err != nil { return err From 142d644de8b4e0aaae20eabb3452dc2ceb2b6eef Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Thu, 5 Dec 2024 18:14:41 +0100 Subject: [PATCH 10/11] removed test files, only for renaming --- testdata/newTimestamp.pidx | 9 --------- testdata/oldTimestamp.pidx | 9 --------- 2 files changed, 18 deletions(-) delete mode 100644 testdata/newTimestamp.pidx delete mode 100644 testdata/oldTimestamp.pidx diff --git a/testdata/newTimestamp.pidx b/testdata/newTimestamp.pidx deleted file mode 100644 index e58a5217..00000000 --- a/testdata/newTimestamp.pidx +++ /dev/null @@ -1,9 +0,0 @@ - - -Keil -https://www.keil.com/pack/ -2044-11-26T04:01:41.5636929+00:00 - - - - diff --git a/testdata/oldTimestamp.pidx b/testdata/oldTimestamp.pidx deleted file mode 100644 index d6c938b3..00000000 --- a/testdata/oldTimestamp.pidx +++ /dev/null @@ -1,9 +0,0 @@ - - -Keil -https://www.keil.com/pack/ -2024-11-20T04:01:41.5636929+00:00 - - - - From 9d45f7bea8bf3b043e92e7327ed89be87e2fbba4 Mon Sep 17 00:00:00 2001 From: Bernd-Gunter Nitzler Date: Thu, 5 Dec 2024 18:15:35 +0100 Subject: [PATCH 11/11] added renamed files --- testdata/NewTimestamp.pidx | 9 +++++++++ testdata/OldTimestamp.pidx | 9 +++++++++ 2 files changed, 18 insertions(+) create mode 100644 testdata/NewTimestamp.pidx create mode 100644 testdata/OldTimestamp.pidx diff --git a/testdata/NewTimestamp.pidx b/testdata/NewTimestamp.pidx new file mode 100644 index 00000000..e58a5217 --- /dev/null +++ b/testdata/NewTimestamp.pidx @@ -0,0 +1,9 @@ + + +Keil +https://www.keil.com/pack/ +2044-11-26T04:01:41.5636929+00:00 + + + + diff --git a/testdata/OldTimestamp.pidx b/testdata/OldTimestamp.pidx new file mode 100644 index 00000000..d6c938b3 --- /dev/null +++ b/testdata/OldTimestamp.pidx @@ -0,0 +1,9 @@ + + +Keil +https://www.keil.com/pack/ +2024-11-20T04:01:41.5636929+00:00 + + + +