Skip to content

Commit d3d606a

Browse files
author
Konikz
committed
fix: make usrlocalsharelima.Dir() testable
This commit makes the Dir() function testable. Signed-off-by: Konikz <[email protected]>
1 parent 4c82f35 commit d3d606a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Diff for: pkg/usrlocalsharelima/usrlocalsharelima_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package usrlocalsharelima
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/lima-vm/lima/pkg/limayaml"
12+
"gotest.tools/v3/assert"
13+
)
14+
15+
func TestDir(t *testing.T) {
16+
// Create a temporary directory for testing
17+
tmpDir := t.TempDir()
18+
19+
// Create the expected directory structure
20+
shareDir := filepath.Join(tmpDir, "share", "lima")
21+
err := os.MkdirAll(filepath.Join(shareDir, "bin"), 0755)
22+
assert.NilError(t, err)
23+
24+
// Create a dummy guest agent binary
25+
gaBinary := filepath.Join(shareDir, "bin", "lima-guestagent."+limayaml.NewArch(runtime.GOARCH))
26+
err = os.WriteFile(gaBinary, []byte("dummy content"), 0755)
27+
assert.NilError(t, err)
28+
29+
// Override os.Executable for testing
30+
originalExecutable := executable
31+
defer func() {
32+
executable = originalExecutable
33+
}()
34+
executable = func() (string, error) {
35+
return filepath.Join(tmpDir, "bin", "limactl"), nil
36+
}
37+
38+
// Create bin directory
39+
err = os.MkdirAll(filepath.Join(tmpDir, "bin"), 0755)
40+
assert.NilError(t, err)
41+
42+
// Test that Dir() returns the correct path
43+
dir, err := Dir()
44+
assert.NilError(t, err)
45+
assert.Equal(t, dir, filepath.Join(shareDir, "bin"))
46+
}

0 commit comments

Comments
 (0)