diff --git a/src/pkg/bundle/common.go b/src/pkg/bundle/common.go index fd98f9e5..988b7580 100644 --- a/src/pkg/bundle/common.go +++ b/src/pkg/bundle/common.go @@ -106,10 +106,6 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error { // validate access to packages as well as components referenced in the package for idx, pkg := range bundle.Packages { - // if package path is set, make it relative to source directory - if pkg.Path != "" { - pkg.Path = filepath.Join(b.cfg.CreateOpts.SourceDirectory, pkg.Path) - } spinner.Updatef("Validating Bundle Package: %s", pkg.Name) if pkg.Name == "" { @@ -158,7 +154,7 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error { if utils.IsRegistryURL(b.cfg.CreateOpts.Output) { return fmt.Errorf("detected local Zarf package: %s, outputting to an OCI registry is not supported when using local Zarf packages", pkg.Name) } - path := getPkgPath(pkg, bundle.Metadata.Architecture) + path := getPkgPath(pkg, bundle.Metadata.Architecture, b.cfg.CreateOpts.SourceDirectory) bundle.Packages[idx].Path = path } @@ -212,9 +208,13 @@ func (b *Bundle) ValidateBundleResources(spinner *message.Spinner) error { return nil } -func getPkgPath(pkg types.Package, arch string) string { +func getPkgPath(pkg types.Package, arch string, srcDir string) string { var fullPkgName string var path string + // Set path relative to the source directory if not absolute + if !filepath.IsAbs(pkg.Path) { + pkg.Path = filepath.Join(srcDir, pkg.Path) + } if strings.HasSuffix(pkg.Path, ".tar.zst") { // use the provided pkg tarball path = pkg.Path diff --git a/src/pkg/bundle/common_test.go b/src/pkg/bundle/common_test.go index 7cdc3669..daa5eadd 100644 --- a/src/pkg/bundle/common_test.go +++ b/src/pkg/bundle/common_test.go @@ -149,8 +149,9 @@ func Test_validateOverrides(t *testing.T) { func Test_getPkgPath(t *testing.T) { type args struct { - pkg types.Package - arch string + pkg types.Package + arch string + srcDir string } tests := []struct { name string @@ -160,39 +161,52 @@ func Test_getPkgPath(t *testing.T) { { name: "init full path", args: args{ - pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path/custom-init.tar.zst"}, - arch: "fake64", + pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path/custom-init.tar.zst"}, + arch: "fake64", + srcDir: "/mock/source", }, - want: "../fake/path/custom-init.tar.zst", + want: "/mock/fake/path/custom-init.tar.zst", }, { name: "init directory only path", args: args{ - pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path"}, - arch: "fake64", + pkg: types.Package{Name: "init", Ref: "0.0.1", Path: "../fake/path"}, + arch: "fake64", + srcDir: "/mock/source", }, - want: "../fake/path/zarf-init-fake64-0.0.1.tar.zst", + want: "/mock/fake/path/zarf-init-fake64-0.0.1.tar.zst", }, { name: "full path", args: args{ - pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst"}, - arch: "fake64", + pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst"}, + arch: "fake64", + srcDir: "/mock/source", }, - want: "./fake/zarf-package-nginx-fake64-0.0.1.tar.zst", + want: "/mock/source/fake/zarf-package-nginx-fake64-0.0.1.tar.zst", }, { name: "directory only path", args: args{ - pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "/fake"}, - arch: "fake64", + pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "fake"}, + arch: "fake64", + srcDir: "/mock/source", + }, + want: "/mock/source/fake/zarf-package-nginx-fake64-0.0.1.tar.zst", + }, + { + name: "absolute path", + args: args{ + pkg: types.Package{Name: "nginx", Ref: "0.0.1", Path: "/fake/zarf-package-nginx-fake64-0.0.1.tar.zst"}, + arch: "fake64", + srcDir: "/mock/source", }, want: "/fake/zarf-package-nginx-fake64-0.0.1.tar.zst", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - path := getPkgPath(tt.args.pkg, tt.args.arch) + path := getPkgPath(tt.args.pkg, tt.args.arch, tt.args.srcDir) require.Equal(t, tt.want, path) }) }