Skip to content

Commit

Permalink
Add tests for windows escaping with PowerShell command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric BAIL committed Jun 4, 2023
1 parent 152f42f commit e47e481
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/fyne/internal/commands/package-windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (p *Packager) packageWindows(tags []string) error {
return nil
}

func runAsAdminWindows(args ...string) error {
func escapePowerShellArguments(args ...string) string {
cmd := "\"/c\""

for idx, arg := range args {
Expand All @@ -146,6 +146,12 @@ func runAsAdminWindows(args ...string) error {
cmd += "'"
}

return cmd
}

func runAsAdminWindows(args ...string) error {
cmd := escapePowerShellArguments(args...)

return execabs.Command("powershell.exe", "Start-Process", "cmd.exe", "-Verb", "runAs", "-ArgumentList", cmd).Run()
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/fyne/internal/commands/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,18 @@ func Test_PackageWeb(t *testing.T) {
expectedTotalCount(t, len(expectedWriteFileRuns.expected), expectedWriteFileRuns.current)
expectedTotalCount(t, len(expectedCopyFileRuns.expected), expectedCopyFileRuns.current)
}

func Test_PowerShellArguments(t *testing.T) {
tests := []struct {
expected string
args []string
}{
{"\"/c\",'mkdir \"C:\\Program Files\\toto\"'", []string{"mkdir", "C:\\Program Files\\toto"}},
{"\"/c\",'copy \"C:\\Program Files\\toto\\titi.txt\" \"C:\\Program Files\\toto\\tata.txt\"'", []string{"copy", "C:\\Program Files\\toto\\titi.txt", "C:\\Program Files\\toto\\tata.txt"}},
}

for _, test := range tests {
result := escapePowerShellArguments(test.args...)
assert.Equal(t, test.expected, result)
}
}

0 comments on commit e47e481

Please sign in to comment.