mirror of
https://github.com/rclone/rclone.git
synced 2026-06-29 18:35:06 -04:00
The Cobra generated PowerShell completion script captures rclone's output through a pipeline with Invoke-Expression. PowerShell decodes that output using [Console]::OutputEncoding, which on non-UTF-8 hosts (for example PowerShell 5.1 on a Windows install with an OEM code page such as CP852) misinterprets the UTF-8 bytes rclone emits and corrupts remote and path names containing non-ASCII characters, so tab completion produces a path that does not exist. Inject "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8" into the generated script immediately before the Invoke-Expression call. This is safe on PowerShell 7+, where UTF-8 is already the default. If the expected line is not present (for example after a Cobra template change) the script is emitted unmodified so we never produce a corrupted completion script.
166 lines
4.5 KiB
Go
166 lines
4.5 KiB
Go
package genautocomplete
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCompletionBash(t *testing.T) {
|
|
tempFile, err := os.CreateTemp("", "completion_bash")
|
|
assert.NoError(t, err)
|
|
defer func() {
|
|
_ = tempFile.Close()
|
|
_ = os.Remove(tempFile.Name())
|
|
}()
|
|
|
|
bashCommandDefinition.Run(bashCommandDefinition, []string{tempFile.Name()})
|
|
|
|
bs, err := os.ReadFile(tempFile.Name())
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, string(bs))
|
|
}
|
|
|
|
func TestCompletionBashStdout(t *testing.T) {
|
|
originalStdout := os.Stdout
|
|
tempFile, err := os.CreateTemp("", "completion_zsh")
|
|
assert.NoError(t, err)
|
|
defer func() {
|
|
_ = tempFile.Close()
|
|
_ = os.Remove(tempFile.Name())
|
|
}()
|
|
|
|
os.Stdout = tempFile
|
|
defer func() { os.Stdout = originalStdout }()
|
|
|
|
bashCommandDefinition.Run(bashCommandDefinition, []string{"-"})
|
|
|
|
output, err := os.ReadFile(tempFile.Name())
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, string(output))
|
|
}
|
|
|
|
func TestCompletionZsh(t *testing.T) {
|
|
tempFile, err := os.CreateTemp("", "completion_zsh")
|
|
assert.NoError(t, err)
|
|
defer func() {
|
|
_ = tempFile.Close()
|
|
_ = os.Remove(tempFile.Name())
|
|
}()
|
|
|
|
zshCommandDefinition.Run(zshCommandDefinition, []string{tempFile.Name()})
|
|
|
|
bs, err := os.ReadFile(tempFile.Name())
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, string(bs))
|
|
}
|
|
|
|
func TestCompletionZshStdout(t *testing.T) {
|
|
originalStdout := os.Stdout
|
|
tempFile, err := os.CreateTemp("", "completion_zsh")
|
|
assert.NoError(t, err)
|
|
defer func() {
|
|
_ = tempFile.Close()
|
|
_ = os.Remove(tempFile.Name())
|
|
}()
|
|
|
|
os.Stdout = tempFile
|
|
defer func() { os.Stdout = originalStdout }()
|
|
|
|
zshCommandDefinition.Run(zshCommandDefinition, []string{"-"})
|
|
output, err := os.ReadFile(tempFile.Name())
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, string(output))
|
|
}
|
|
|
|
func TestCompletionFish(t *testing.T) {
|
|
tempFile, err := os.CreateTemp("", "completion_fish")
|
|
assert.NoError(t, err)
|
|
defer func() {
|
|
_ = tempFile.Close()
|
|
_ = os.Remove(tempFile.Name())
|
|
}()
|
|
|
|
fishCommandDefinition.Run(fishCommandDefinition, []string{tempFile.Name()})
|
|
|
|
bs, err := os.ReadFile(tempFile.Name())
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, string(bs))
|
|
}
|
|
|
|
func TestCompletionFishStdout(t *testing.T) {
|
|
originalStdout := os.Stdout
|
|
tempFile, err := os.CreateTemp("", "completion_zsh")
|
|
assert.NoError(t, err)
|
|
defer func() {
|
|
_ = tempFile.Close()
|
|
_ = os.Remove(tempFile.Name())
|
|
}()
|
|
|
|
os.Stdout = tempFile
|
|
defer func() { os.Stdout = originalStdout }()
|
|
|
|
fishCommandDefinition.Run(fishCommandDefinition, []string{"-"})
|
|
|
|
output, err := os.ReadFile(tempFile.Name())
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, string(output))
|
|
}
|
|
|
|
func TestCompletionPowershell(t *testing.T) {
|
|
tempFile, err := os.CreateTemp("", "completion_powershell")
|
|
assert.NoError(t, err)
|
|
defer func() {
|
|
_ = tempFile.Close()
|
|
_ = os.Remove(tempFile.Name())
|
|
}()
|
|
|
|
powershellCommandDefinition.Run(powershellCommandDefinition, []string{tempFile.Name()})
|
|
|
|
bs, err := os.ReadFile(tempFile.Name())
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, string(bs))
|
|
// The generated script must force UTF-8 output decoding so that non-ASCII
|
|
// remote names are not corrupted on non-UTF-8 PowerShell hosts.
|
|
assert.Contains(t, string(bs), powerShellUTF8Fix)
|
|
}
|
|
|
|
func TestCompletionPowershellStdout(t *testing.T) {
|
|
originalStdout := os.Stdout
|
|
tempFile, err := os.CreateTemp("", "completion_powershell")
|
|
assert.NoError(t, err)
|
|
defer func() {
|
|
_ = tempFile.Close()
|
|
_ = os.Remove(tempFile.Name())
|
|
}()
|
|
|
|
os.Stdout = tempFile
|
|
defer func() { os.Stdout = originalStdout }()
|
|
|
|
powershellCommandDefinition.Run(powershellCommandDefinition, []string{"-"})
|
|
|
|
output, err := os.ReadFile(tempFile.Name())
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, string(output))
|
|
assert.Contains(t, string(output), powerShellUTF8Fix)
|
|
}
|
|
|
|
func TestPatchPowerShellCompletion(t *testing.T) {
|
|
t.Run("injects the encoding fix before the invoke line", func(t *testing.T) {
|
|
script := "before\n " + powerShellInvokeLine + "\nafter\n"
|
|
got := patchPowerShellCompletion(script)
|
|
// The fix is inserted on its own line, sharing the indentation of the
|
|
// invoke line, immediately before it.
|
|
want := "before\n " + powerShellUTF8Fix + "\n " + powerShellInvokeLine + "\nafter\n"
|
|
assert.Equal(t, want, got)
|
|
assert.Less(t, strings.Index(got, powerShellUTF8Fix), strings.Index(got, powerShellInvokeLine))
|
|
})
|
|
|
|
t.Run("leaves the script unchanged when the invoke line is absent", func(t *testing.T) {
|
|
script := "some other script\nwithout the expected line\n"
|
|
assert.Equal(t, script, patchPowerShellCompletion(script))
|
|
})
|
|
}
|