|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package machine |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// CreateNewItemWithPowerShell creates a new item using PowerShell. |
| 16 | +// It's an helper to easily create junctions on Windows (as well as other file types). |
| 17 | +// It constructs a PowerShell command to create a new item at the specified path with the given item type. |
| 18 | +// If a target is provided, it includes it in the command. |
| 19 | +// |
| 20 | +// Parameters: |
| 21 | +// - t: The testing.T instance. |
| 22 | +// - path: The path where the new item will be created. |
| 23 | +// - itemType: The type of the item to be created (e.g., "File", "SymbolicLink", "Junction"). |
| 24 | +// - target: The target for the new item, if applicable. |
| 25 | +func CreateNewItemWithPowerShell(t *testing.T, path string, itemType string, target string) { |
| 26 | + var pwshCmd string |
| 27 | + pwshCmd = "New-Item -Path " + path + " -ItemType " + itemType |
| 28 | + if target != "" { |
| 29 | + pwshCmd += " -Target " + target |
| 30 | + } |
| 31 | + cmd := exec.Command("pwsh", "-Command", pwshCmd) |
| 32 | + cmd.Stdout = os.Stdout |
| 33 | + cmd.Stderr = os.Stderr |
| 34 | + err := cmd.Run() |
| 35 | + require.NoError(t, err) |
| 36 | +} |
| 37 | + |
| 38 | +// TestEvalSymlinksOrClean tests the EvalSymlinksOrClean function. |
| 39 | +// In particular it verifies that EvalSymlinksOrClean behaves as |
| 40 | +// filepath.EvalSymlink before Go 1.23 - with the exception of |
| 41 | +// files under a mount point (juntion) that aren't resolved |
| 42 | +// anymore. |
| 43 | +// The old behavior of filepath.EvalSymlinks can be tested with |
| 44 | +// the directive "//go:debug winsymlink=0" and replacing EvalSymlinksOrClean() |
| 45 | +// with filepath.EvalSymlink(). |
| 46 | +func TestEvalSymlinksOrClean(t *testing.T) { |
| 47 | + // Create a temporary directory to store the normal file |
| 48 | + normalFileDir := t.TempDir() |
| 49 | + |
| 50 | + // Create a temporary directory to store the (hard/sym)link files |
| 51 | + linkFilesDir := t.TempDir() |
| 52 | + |
| 53 | + // Create a temporary directory where the mount point will be created |
| 54 | + mountPointDir := t.TempDir() |
| 55 | + |
| 56 | + // Create a normal file |
| 57 | + normalFile := filepath.Join(normalFileDir, "testFile") |
| 58 | + CreateNewItemWithPowerShell(t, normalFile, "File", "") |
| 59 | + |
| 60 | + // Create a symlink file |
| 61 | + symlinkFile := filepath.Join(linkFilesDir, "testSymbolicLink") |
| 62 | + CreateNewItemWithPowerShell(t, symlinkFile, "SymbolicLink", normalFile) |
| 63 | + |
| 64 | + // Create a hardlink file |
| 65 | + hardlinkFile := filepath.Join(linkFilesDir, "testHardLink") |
| 66 | + CreateNewItemWithPowerShell(t, hardlinkFile, "HardLink", normalFile) |
| 67 | + |
| 68 | + // Create a mount point file |
| 69 | + mountPoint := filepath.Join(mountPointDir, "testJunction") |
| 70 | + mountPointFile := filepath.Join(mountPoint, "testFile") |
| 71 | + CreateNewItemWithPowerShell(t, mountPoint, "Junction", normalFileDir) |
| 72 | + |
| 73 | + // Replaces the backslashes with forward slashes in the normal file path |
| 74 | + normalFileWithBadSeparators := filepath.ToSlash(normalFile) |
| 75 | + |
| 76 | + tests := []struct { |
| 77 | + name string |
| 78 | + filePath string |
| 79 | + want string |
| 80 | + }{ |
| 81 | + { |
| 82 | + name: "Normal file", |
| 83 | + filePath: normalFile, |
| 84 | + want: normalFile, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "File under a mount point (juntion)", |
| 88 | + filePath: mountPointFile, |
| 89 | + want: mountPointFile, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "Symbolic link", |
| 93 | + filePath: symlinkFile, |
| 94 | + want: normalFile, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "Hard link", |
| 98 | + filePath: hardlinkFile, |
| 99 | + want: hardlinkFile, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "Bad separators in path", |
| 103 | + filePath: normalFileWithBadSeparators, |
| 104 | + want: normalFile, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tt := range tests { |
| 109 | + t.Run(tt.name, func(t *testing.T) { |
| 110 | + got, err := EvalSymlinksOrClean(tt.filePath) |
| 111 | + require.NoError(t, err) |
| 112 | + assert.Equal(t, tt.want, got) |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments