lockbox

password manager
Log | Files | Refs | README | LICENSE

commit c029d24e71560a75946a9f793caaa9946442e65b
parent c701de73a962bf6e6376bfa1caab30ffe03bbf1a
Author: Sean Enck <sean@ttypty.com>
Date:   Sun,  7 Sep 2025 10:45:39 -0400

restore some completion tests, cleanup generation of completion output simple lists

Diffstat:
Minternal/app/completions/core.go | 10++++++++--
Minternal/app/completions/core_test.go | 7+++++--
Minternal/app/completions/shell/bash.sh | 8++------
Minternal/app/completions/shell/zsh.sh | 6+-----
4 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/internal/app/completions/core.go b/internal/app/completions/core.go @@ -8,6 +8,7 @@ import ( "path/filepath" "slices" "sort" + "strings" "text/template" "github.com/enckse/lockbox/internal/app/commands" @@ -36,14 +37,19 @@ type ( HelpAdvancedCommand string HelpConfigCommand string ExportCommand string - Options []string - TOTPSubCommands []string + Options OptionList + TOTPSubCommands OptionList } + OptionList []string ) //go:embed shell/* var shell embed.FS +func (o OptionList) Join() string { + return strings.Join(o, " ") +} + // Generate handles creating shell completion outputs func Generate(completionType, exe string) ([]string, error) { if !slices.Contains(commands.CompletionTypes, completionType) { diff --git a/internal/app/completions/core_test.go b/internal/app/completions/core_test.go @@ -27,7 +27,7 @@ func TestCompletionReadOnly(t *testing.T) { for k := range tests { v, _ := completions.Generate(k, "lb") res := strings.Join(v, "\n") - for _, needs := range []string{`}rm`, `}insert`, `}mv`, `}unset`} { + for _, needs := range []string{` rm`, ` insert`, ` mv`, ` unset`} { has := strings.Contains(res, needs) if has { if !b { @@ -52,7 +52,7 @@ func TestFeatures(t *testing.T) { without int } for k := range tests { - for _, feature := range []counts{{"clip", 5, 1}} { + for _, feature := range []counts{{"clip", 5, 1}, {"totp", 4, 2}} { store.Clear() key := fmt.Sprintf("LOCKBOX_FEATURE_%s", strings.ToUpper(feature.cmd)) store.SetBool(key, true) @@ -65,6 +65,9 @@ func TestFeatures(t *testing.T) { func testCompletionFeature(t *testing.T, completionMode, cmd string, expect int) { e := expect + if completionMode == "bash" && cmd == "totp" { + e++ + } v, _ := completions.Generate(completionMode, "lb") if cnt := strings.Count(strings.Join(v, "\n"), cmd); cnt != e { t.Errorf("completion mismatch %s: %d != %d (%s)", completionMode, cnt, expect, cmd) diff --git a/internal/app/completions/shell/bash.sh b/internal/app/completions/shell/bash.sh @@ -4,9 +4,7 @@ _{{ $.Executable }}() { local cur opts chosen found cur=${COMP_WORDS[COMP_CWORD]} if [ "$COMP_CWORD" -eq 1 ]; then -{{- range $idx, $value := $.Options }} - opts="${opts}{{ $value }} " -{{- end}} + opts="{{ $.Options.Join }}" # shellcheck disable=SC2207 COMPREPLY=( $(compgen -W "$opts" -- "$cur") ) else @@ -37,9 +35,7 @@ _{{ $.Executable }}() { ;; "{{ $.TOTPCommand }}") opts="{{ $.TOTPListCommand }} " -{{- range $key, $value := .TOTPSubCommands }} - opts="$opts {{ $value }}" -{{- end}} + opts="$opts {{ $.TOTPSubCommands.Join }}" ;; esac else diff --git a/internal/app/completions/shell/zsh.sh b/internal/app/completions/shell/zsh.sh @@ -10,11 +10,7 @@ _{{ $.Executable }}() { len=${#words[@]} case $state in main) - args="" -{{- range $idx, $value := $.Options }} - [ -n "$args" ] && args="$args " - args="${args}{{ $value }}" -{{- end }} + args="{{ $.Options.Join }}" _arguments "1:main:($args)" ;; *)