|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package chezmoi |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/alecthomas/assert/v2" |
| 11 | +) |
| 12 | + |
| 13 | +func TestFindExecutable(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + files []string |
| 16 | + paths []string |
| 17 | + expected string |
| 18 | + }{ |
| 19 | + { |
| 20 | + files: []string{"powershell.exe"}, |
| 21 | + paths: []string{ |
| 22 | + "c:\\windows\\system32", |
| 23 | + "c:\\windows\\system64", |
| 24 | + "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0", |
| 25 | + }, |
| 26 | + expected: "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", |
| 27 | + }, |
| 28 | + { |
| 29 | + files: []string{"powershell"}, |
| 30 | + paths: []string{ |
| 31 | + "c:\\windows\\system32", |
| 32 | + "c:\\windows\\system64", |
| 33 | + "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0", |
| 34 | + }, |
| 35 | + expected: "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", |
| 36 | + }, |
| 37 | + { |
| 38 | + files: []string{"weakshell.exe"}, |
| 39 | + paths: []string{ |
| 40 | + "c:\\windows\\system32", |
| 41 | + "c:\\windows\\system64", |
| 42 | + "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0", |
| 43 | + }, |
| 44 | + expected: "", |
| 45 | + }, |
| 46 | + { |
| 47 | + files: []string{"weakshell"}, |
| 48 | + paths: []string{ |
| 49 | + "c:\\windows\\system32", |
| 50 | + "c:\\windows\\system64", |
| 51 | + "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0", |
| 52 | + }, |
| 53 | + expected: "", |
| 54 | + }, |
| 55 | + { |
| 56 | + files: []string{"weakshell.exe", "powershell.exe"}, |
| 57 | + paths: []string{ |
| 58 | + "c:\\windows\\system32", |
| 59 | + "c:\\windows\\system64", |
| 60 | + "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0", |
| 61 | + }, |
| 62 | + expected: "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", |
| 63 | + }, |
| 64 | + { |
| 65 | + files: []string{"weakshell", "powershell"}, |
| 66 | + paths: []string{ |
| 67 | + "c:\\windows\\system32", |
| 68 | + "c:\\windows\\system64", |
| 69 | + "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0", |
| 70 | + }, |
| 71 | + expected: "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", |
| 72 | + }, |
| 73 | + { |
| 74 | + files: []string{"weakshell.exe", "chezmoishell.exe"}, |
| 75 | + paths: []string{ |
| 76 | + "c:\\windows\\system32", |
| 77 | + "c:\\windows\\system64", |
| 78 | + "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0", |
| 79 | + }, |
| 80 | + expected: "", |
| 81 | + }, |
| 82 | + { |
| 83 | + files: []string{"weakshell", "chezmoishell"}, |
| 84 | + paths: []string{ |
| 85 | + "c:\\windows\\system32", |
| 86 | + "c:\\windows\\system64", |
| 87 | + "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0", |
| 88 | + }, |
| 89 | + expected: "", |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, test := range tests { |
| 94 | + name := fmt.Sprintf("FindExecutable %v in %#v as %v", test.files, test.paths, test.expected) |
| 95 | + t.Run(name, func(t *testing.T) { |
| 96 | + actual, err := FindExecutable(test.files, test.paths) |
| 97 | + assert.NoError(t, err) |
| 98 | + assert.Equal(t, strings.ToLower(test.expected), strings.ToLower(actual)) |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments