From 4f5e474059933f471e08d8d794bb8d472af3e7be Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 16:05:12 +0100 Subject: [PATCH] fix(cli): improve completion and Windows live testing Co-authored-by: Theodore Aptekarev Co-authored-by: gagradebnath --- internal/cmd/completion_scripts.go | 11 ++++++++--- internal/cmd/completion_test.go | 2 +- internal/config/paths.go | 4 ++-- internal/config/paths_test.go | 6 ++++++ internal/integration/live_test.go | 6 ++++++ scripts/live-test.ps1 | 12 ++++++++++++ 6 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 scripts/live-test.ps1 diff --git a/internal/cmd/completion_scripts.go b/internal/cmd/completion_scripts.go index 31426e3..3a25a93 100644 --- a/internal/cmd/completion_scripts.go +++ b/internal/cmd/completion_scripts.go @@ -37,9 +37,14 @@ complete -F _gog_complete gog func zshCompletionScript() string { return `#compdef gog -autoload -Uz bashcompinit -bashcompinit -` + bashCompletionScript() +_gog() { + local -a completions + completions=("${(@f)$(gog __complete --cword "$((CURRENT - 1))" -- "${words[@]}")}") + _describe 'values' completions +} + +compdef _gog gog +` } func fishCompletionScript() string { diff --git a/internal/cmd/completion_test.go b/internal/cmd/completion_test.go index 97447e4..9df5436 100644 --- a/internal/cmd/completion_test.go +++ b/internal/cmd/completion_test.go @@ -9,7 +9,7 @@ import ( func TestCompletionCmd(t *testing.T) { cases := map[string]string{ "bash": "complete -F _gog_complete gog", - "zsh": "bashcompinit", + "zsh": "compdef _gog gog", "fish": "complete -c gog", "powershell": "Register-ArgumentCompleter", } diff --git a/internal/config/paths.go b/internal/config/paths.go index 5b6636a..9024628 100644 --- a/internal/config/paths.go +++ b/internal/config/paths.go @@ -262,13 +262,13 @@ func ExpandPath(path string) (string, error) { return home, nil } - if strings.HasPrefix(path, "~/") { + if strings.HasPrefix(path, "~/") || strings.HasPrefix(path, "~\\") { home, err := os.UserHomeDir() if err != nil { return "", fmt.Errorf("expand home dir: %w", err) } - return filepath.Join(home, path[2:]), nil + return filepath.Join(home, strings.TrimLeft(path[2:], `/\`)), nil } return path, nil diff --git a/internal/config/paths_test.go b/internal/config/paths_test.go index 77429fe..0fafa43 100644 --- a/internal/config/paths_test.go +++ b/internal/config/paths_test.go @@ -10,6 +10,7 @@ import ( func TestPaths_CreateDirs(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg-config")) dir, err := EnsureDir() @@ -96,6 +97,11 @@ func TestExpandPath(t *testing.T) { input: "~/Downloads/file.txt", want: filepath.Join(home, "Downloads/file.txt"), }, + { + name: "tilde with backslash subpath", + input: `~\Downloads\file.txt`, + want: filepath.Join(home, `Downloads\file.txt`), + }, { name: "absolute path unchanged", input: "/usr/local/bin", diff --git a/internal/integration/live_test.go b/internal/integration/live_test.go index 9f68811..6278e80 100644 --- a/internal/integration/live_test.go +++ b/internal/integration/live_test.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "testing" "time" ) @@ -43,6 +44,11 @@ func TestLiveScript(t *testing.T) { defer cancel() cmd := exec.CommandContext(ctx, script, args...) + if runtime.GOOS == "windows" { + script = filepath.Join(root, "scripts", "live-test.ps1") + psArgs := append([]string{"-NoProfile", "-ExecutionPolicy", "Bypass", "-File", script}, args...) + cmd = exec.CommandContext(ctx, "powershell.exe", psArgs...) + } cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Env = os.Environ() diff --git a/scripts/live-test.ps1 b/scripts/live-test.ps1 new file mode 100644 index 0000000..76a6ca5 --- /dev/null +++ b/scripts/live-test.ps1 @@ -0,0 +1,12 @@ +#!/usr/bin/env pwsh +$ErrorActionPreference = "Stop" + +$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path +$bashScript = Join-Path $scriptDir "live-test.sh" + +if (Get-Command bash -ErrorAction SilentlyContinue) { + & bash $bashScript @args + exit $LASTEXITCODE +} + +throw "bash is required to run scripts/live-test.sh on Windows"