From bc5818a2d2361adfd21463967d806d0481518d55 Mon Sep 17 00:00:00 2001 From: Dane Elwell Date: Wed, 18 Sep 2024 09:35:10 +0100 Subject: [PATCH] fix: Use absolute path for reading manifest Closes: #3550 --- ovf/importer/importer.go | 12 ++++----- ovf/importer/importer_test.go | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 ovf/importer/importer_test.go diff --git a/ovf/importer/importer.go b/ovf/importer/importer.go index 55382f06c..7c639c95f 100644 --- a/ovf/importer/importer.go +++ b/ovf/importer/importer.go @@ -21,7 +21,6 @@ import ( "context" "errors" "fmt" - "os" "path" "path/filepath" "strings" @@ -58,15 +57,16 @@ type Importer struct { Manifest map[string]*library.Checksum } -func (imp *Importer) ReadManifest(fpath string) error { +func (imp *Importer) manifestPath(fpath string) string { base := filepath.Base(fpath) ext := filepath.Ext(base) - mfName := strings.Replace(base, ext, ".mf", 1) + return filepath.Join(filepath.Dir(fpath), strings.Replace(base, ext, ".mf", 1)) +} - mf, _, err := imp.Archive.Open(mfName) +func (imp *Importer) ReadManifest(fpath string) error { + mf, _, err := imp.Archive.Open(imp.manifestPath(fpath)) if err != nil { - msg := fmt.Sprintf("manifest %q: %s", mf, err) - fmt.Fprintln(os.Stderr, msg) + msg := fmt.Sprintf("failed to read manifest %q: %s", mf, err) return errors.New(msg) } imp.Manifest, err = library.ReadManifest(mf) diff --git a/ovf/importer/importer_test.go b/ovf/importer/importer_test.go new file mode 100644 index 000000000..03284de73 --- /dev/null +++ b/ovf/importer/importer_test.go @@ -0,0 +1,46 @@ +package importer + +import ( + "runtime" + "testing" +) + +func TestImporter_manifestPath(t *testing.T) { + // We can only test filepath operations on the target OS + manifestTests := []struct { + goos string + name string + path string + expected string + }{ + { + goos: "linux", + name: "linux path", + path: "/home/user/foo/bar/qux.ovf", + expected: "/home/user/foo/bar/qux.mf", + }, + { + goos: "darwin", + name: "darwin path", + path: "/home/user/foo/bar/qux.ovf", + expected: "/home/user/foo/bar/qux.mf", + }, + { + goos: "windows", + name: "windows path", + path: "C:\\ProgramData\\Foo\\Bar\\Qux.ovf", + expected: "C:\\ProgramData\\Foo\\Bar\\Qux.mf", + }, + } + + imp := Importer{} + + for _, test := range manifestTests { + if test.goos == runtime.GOOS { + manifestPath := imp.manifestPath(test.path) + if manifestPath != test.expected { + t.Fatalf("'%s' failed: expected '%s', got '%s'", test.name, test.expected, manifestPath) + } + } + } +}