Skip to content

Commit

Permalink
fix: differential package create with non local sources (#2444)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
AustinAbro321 committed Apr 18, 2024
1 parent 3906604 commit b420171
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand Down
58 changes: 58 additions & 0 deletions src/pkg/packager/creator/normal_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}

0 comments on commit b420171

Please sign in to comment.