Skip to content

fix: ensure usrlocalsharelima.Dir() works when called from tests #3401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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