Skip to content

Commit 60f2a6e

Browse files
committed
fix: Use absolute path for reading manifest
Closes: #3550
1 parent 3450f77 commit 60f2a6e

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

ovf/importer/importer.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24-
"os"
2524
"path"
2625
"path/filepath"
2726
"strings"
@@ -58,15 +57,16 @@ type Importer struct {
5857
Manifest map[string]*library.Checksum
5958
}
6059

61-
func (imp *Importer) ReadManifest(fpath string) error {
60+
func (imp *Importer) manifestPath(fpath string) string {
6261
base := filepath.Base(fpath)
6362
ext := filepath.Ext(base)
64-
mfName := strings.Replace(base, ext, ".mf", 1)
63+
return filepath.Join(filepath.Dir(fpath), strings.Replace(base, ext, ".mf", 1))
64+
}
6565

66-
mf, _, err := imp.Archive.Open(mfName)
66+
func (imp *Importer) ReadManifest(fpath string) error {
67+
mf, _, err := imp.Archive.Open(imp.manifestPath(fpath))
6768
if err != nil {
68-
msg := fmt.Sprintf("manifest %q: %s", mf, err)
69-
fmt.Fprintln(os.Stderr, msg)
69+
msg := fmt.Sprintf("failed to read manifest %q: %s", mf, err)
7070
return errors.New(msg)
7171
}
7272
imp.Manifest, err = library.ReadManifest(mf)

ovf/importer/importer_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package importer
18+
19+
import (
20+
"runtime"
21+
"testing"
22+
)
23+
24+
func TestImporter_manifestPath(t *testing.T) {
25+
// We can only test filepath operations on the target OS
26+
manifestTests := []struct {
27+
goos string
28+
name string
29+
path string
30+
expected string
31+
}{
32+
{
33+
goos: "linux",
34+
name: "linux path",
35+
path: "/home/user/foo/bar/qux.ovf",
36+
expected: "/home/user/foo/bar/qux.mf",
37+
},
38+
{
39+
goos: "darwin",
40+
name: "darwin path",
41+
path: "/home/user/foo/bar/qux.ovf",
42+
expected: "/home/user/foo/bar/qux.mf",
43+
},
44+
{
45+
goos: "windows",
46+
name: "windows path",
47+
path: "C:\\ProgramData\\Foo\\Bar\\Qux.ovf",
48+
expected: "C:\\ProgramData\\Foo\\Bar\\Qux.mf",
49+
},
50+
}
51+
52+
imp := Importer{}
53+
54+
for _, test := range manifestTests {
55+
if test.goos == runtime.GOOS {
56+
manifestPath := imp.manifestPath(test.path)
57+
if manifestPath != test.expected {
58+
t.Fatalf("'%s' failed: expected '%s', got '%s'", test.name, test.expected, manifestPath)
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)