From b4201718aa96f34f37dac7afa5ccfe59affedbd0 Mon Sep 17 00:00:00 2001 From: Austin Abro <37223396+AustinAbro321@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:25:28 -0400 Subject: [PATCH] fix: differential package create with non local sources (#2444) ## Description Currently there is a bug wherein differential will only work if the source is a local file ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow) followed --- src/pkg/packager/creator/normal.go | 12 +++-- src/pkg/packager/creator/normal_test.go | 58 +++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/pkg/packager/creator/normal_test.go diff --git a/src/pkg/packager/creator/normal.go b/src/pkg/packager/creator/normal.go index 5693d32d98..e9904852f2 100644 --- a/src/pkg/packager/creator/normal.go +++ b/src/pkg/packager/creator/normal.go @@ -48,12 +48,16 @@ type PackageCreator struct { cfg *types.PackagerConfig } -// NewPackageCreator returns a new PackageCreator. -func NewPackageCreator(createOpts types.ZarfCreateOptions, cfg *types.PackagerConfig, cwd string) *PackageCreator { - if createOpts.DifferentialPackagePath != "" && !filepath.IsAbs(createOpts.DifferentialPackagePath) { - createOpts.DifferentialPackagePath = filepath.Join(cwd, createOpts.DifferentialPackagePath) +func updateRelativeDifferentialPackagePath(path string, cwd string) string { + if path != "" && !filepath.IsAbs(path) && !helpers.IsURL(path) { + return filepath.Join(cwd, path) } + return path +} +// NewPackageCreator returns a new PackageCreator. +func NewPackageCreator(createOpts types.ZarfCreateOptions, cfg *types.PackagerConfig, cwd string) *PackageCreator { + createOpts.DifferentialPackagePath = updateRelativeDifferentialPackagePath(createOpts.DifferentialPackagePath, cwd) return &PackageCreator{createOpts, cfg} } diff --git a/src/pkg/packager/creator/normal_test.go b/src/pkg/packager/creator/normal_test.go new file mode 100644 index 0000000000..9a4830cb1f --- /dev/null +++ b/src/pkg/packager/creator/normal_test.go @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +// Package creator contains functions for creating Zarf packages. +package creator + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDifferentialPackagePathSetCorrectly(t *testing.T) { + type testCase struct { + name string + path string + cwd string + expected string + } + + absolutePackagePath, err := filepath.Abs(filepath.Join("home", "cool-guy", "zarf-package", "my-package.tar.zst")) + require.NoError(t, err) + + testCases := []testCase{ + { + name: "relative path", + path: "my-package.tar.zst", + cwd: filepath.Join("home", "cool-guy", "zarf-package"), + expected: filepath.Join("home", "cool-guy", "zarf-package", "my-package.tar.zst"), + }, + { + name: "absolute path", + path: absolutePackagePath, + cwd: filepath.Join("home", "should-not-matter"), + expected: absolutePackagePath, + }, + { + name: "oci path", + path: "oci://my-cool-registry.com:555/my-package.tar.zst", + cwd: filepath.Join("home", "should-not-matter"), + expected: "oci://my-cool-registry.com:555/my-package.tar.zst", + }, + { + name: "https path", + path: "https://neat-url.com/zarf-init-amd64-v1.0.0.tar.zst", + cwd: filepath.Join("home", "should-not-matter"), + expected: "https://neat-url.com/zarf-init-amd64-v1.0.0.tar.zst", + }, + } + for _, testCase := range testCases { + tc := testCase + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + require.Equal(t, tc.expected, updateRelativeDifferentialPackagePath(tc.path, tc.cwd)) + }) + } +}