|
| 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 | + "gotest.tools/v3/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestDir(t *testing.T) { |
| 15 | + // Create a temporary directory for testing |
| 16 | + tmpDir := t.TempDir() |
| 17 | + |
| 18 | + // Create the expected directory structure |
| 19 | + shareDir := filepath.Join(tmpDir, "share", "lima") |
| 20 | + err := os.MkdirAll(shareDir, 0o755) |
| 21 | + assert.NilError(t, err) |
| 22 | + |
| 23 | + // Create a dummy guest agent binary with the correct name format |
| 24 | + gaBinary := filepath.Join(shareDir, "lima-guestagent.Linux-x86_64") |
| 25 | + err = os.WriteFile(gaBinary, []byte("dummy content"), 0o755) |
| 26 | + assert.NilError(t, err) |
| 27 | + |
| 28 | + // Create bin directory and limactl file |
| 29 | + binDir := filepath.Join(tmpDir, "bin") |
| 30 | + err = os.MkdirAll(binDir, 0o755) |
| 31 | + assert.NilError(t, err) |
| 32 | + limactlPath := filepath.Join(binDir, "limactl") |
| 33 | + err = os.WriteFile(limactlPath, []byte("dummy content"), 0o755) |
| 34 | + assert.NilError(t, err) |
| 35 | + |
| 36 | + // Save original value of self |
| 37 | + originalSelf := self |
| 38 | + // Restore original value after test |
| 39 | + defer func() { |
| 40 | + self = originalSelf |
| 41 | + }() |
| 42 | + // Override self for the test |
| 43 | + self = limactlPath |
| 44 | + |
| 45 | + // Test that Dir() returns the correct path |
| 46 | + dir, err := Dir() |
| 47 | + assert.NilError(t, err) |
| 48 | + assert.Equal(t, dir, shareDir) |
| 49 | +} |
0 commit comments