Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/usrlocalsharelima/usrlocalsharelima.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (

"github.com/lima-vm/lima/pkg/debugutil"
"github.com/lima-vm/lima/pkg/limayaml"
. "github.com/lima-vm/lima/pkg/must"
"github.com/sirupsen/logrus"
)

// Cache the executable path at package level.
var self = Must(os.Executable())

func Dir() (string, error) {
self, err := os.Executable()
if err != nil {
return "", err
}
selfSt, err := os.Stat(self)
if err != nil {
return "", err
Expand Down
49 changes: 49 additions & 0 deletions pkg/usrlocalsharelima/usrlocalsharelima_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: Copyright The Lima Authors
// SPDX-License-Identifier: Apache-2.0

package usrlocalsharelima

import (
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
)

func TestDir(t *testing.T) {
// Create a temporary directory for testing
tmpDir := t.TempDir()

// Create the expected directory structure
shareDir := filepath.Join(tmpDir, "share", "lima")
err := os.MkdirAll(shareDir, 0o755)
assert.NilError(t, err)

// Create a dummy guest agent binary with the correct name format
gaBinary := filepath.Join(shareDir, "lima-guestagent.Linux-x86_64")
err = os.WriteFile(gaBinary, []byte("dummy content"), 0o755)
assert.NilError(t, err)

// Create bin directory and limactl file
binDir := filepath.Join(tmpDir, "bin")
err = os.MkdirAll(binDir, 0o755)
assert.NilError(t, err)
limactlPath := filepath.Join(binDir, "limactl")
err = os.WriteFile(limactlPath, []byte("dummy content"), 0o755)
assert.NilError(t, err)

// Save original value of self
originalSelf := self
// Restore original value after test
defer func() {
self = originalSelf
}()
// Override self for the test
self = limactlPath

// Test that Dir() returns the correct path
dir, err := Dir()
assert.NilError(t, err)
assert.Equal(t, dir, shareDir)
}
Loading