Skip to content

Commit

Permalink
fix: Use absolute path for reading manifest
Browse files Browse the repository at this point in the history
Closes: vmware#3550
  • Loading branch information
Xiol committed Sep 18, 2024
1 parent 3450f77 commit f6896ec
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
14 changes: 8 additions & 6 deletions ovf/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -58,15 +57,18 @@ 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))
}

func (imp *Importer) ReadManifest(fpath string) error {
mfPath := imp.manifestPath(fpath)

mf, _, err := imp.Archive.Open(mfName)
mf, _, err := imp.Archive.Open(mfPath)
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)
Expand Down
47 changes: 47 additions & 0 deletions ovf/importer/importer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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
errExpected bool
}{
{
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)
}
}
}
}

0 comments on commit f6896ec

Please sign in to comment.