From 0c79a3c365e4008e514b591df6ee78f2de2fa22d Mon Sep 17 00:00:00 2001 From: Jesse van den Kieboom Date: Mon, 17 Jun 2024 08:11:12 +0200 Subject: [PATCH] Fix for windows long/short names --- completion_test.go | 46 +++++++++++++++++++++++++++++----------------- parser_test.go | 2 +- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/completion_test.go b/completion_test.go index aa3af90..6659b43 100644 --- a/completion_test.go +++ b/completion_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "reflect" "runtime" + "slices" "strings" "testing" ) @@ -81,6 +82,14 @@ type completionTest struct { var completionTests []completionTest +func makeLongName(option string) string { + return defaultLongOptDelimiter + option +} + +func makeShortName(option string) string { + return string(defaultShortOptDelimiter) + option +} + func init() { _, sourcefile, _, _ := runtime.Caller(0) completionTestSourcedir := filepath.Join(filepath.SplitList(path.Dir(sourcefile))...) @@ -97,15 +106,15 @@ func init() { completionTests = []completionTest{ { // Short names - []string{"-"}, - []string{"--debug", "--required", "--verbose", "--version", "-i"}, + []string{makeShortName("")}, + []string{makeLongName("debug"), makeLongName("required"), makeLongName("verbose"), makeLongName("version"), makeShortName("i")}, false, }, { // Short names full - []string{"-i"}, - []string{"-i"}, + []string{makeShortName("i")}, + []string{makeShortName("i")}, false, }, @@ -125,20 +134,20 @@ func init() { { // Long names with descriptions - []string{"--"}, + []string{makeLongName("")}, []string{ - "--debug # Enable debug", - "--required # This is required", - "--verbose # Verbose messages", - "--version # Show version", + makeLongName("debug # Enable debug"), + makeLongName("required # This is required"), + makeLongName("verbose # Verbose messages"), + makeLongName("version # Show version"), }, true, }, { // Long names partial - []string{"--ver"}, - []string{"--verbose", "--version"}, + []string{makeLongName("ver")}, + []string{makeLongName("verbose"), makeLongName("version")}, false, }, @@ -197,7 +206,7 @@ func init() { { // Flag filename - []string{"rm", "-f", path.Join(completionTestSourcedir, "completion")}, + []string{"rm", makeShortName("f"), path.Join(completionTestSourcedir, "completion")}, completionTestFilename, false, }, @@ -212,21 +221,21 @@ func init() { { // Flag concat filename []string{"rm", "-f" + path.Join(completionTestSourcedir, "completion")}, - []string{"-f" + completionTestFilename[0], "-f" + completionTestFilename[1]}, + []string{makeShortName("f") + completionTestFilename[0], makeShortName("f") + completionTestFilename[1]}, false, }, { // Flag equal concat filename []string{"rm", "-f=" + path.Join(completionTestSourcedir, "completion")}, - []string{"-f=" + completionTestFilename[0], "-f=" + completionTestFilename[1]}, + []string{makeShortName("f=") + completionTestFilename[0], makeShortName("f=") + completionTestFilename[1]}, false, }, { // Flag concat long filename []string{"rm", "--filename=" + path.Join(completionTestSourcedir, "completion")}, - []string{"--filename=" + completionTestFilename[0], "--filename=" + completionTestFilename[1]}, + []string{makeLongName("filename=") + completionTestFilename[0], makeLongName("filename=") + completionTestFilename[1]}, false, }, @@ -253,13 +262,13 @@ func init() { { // Custom completed - []string{"rename", "-c", "hello un"}, + []string{"rename", makeShortName("c"), "hello un"}, []string{"hello universe"}, false, }, { // Multiple flag filename - []string{"add-multi-flag", "-f", filepath.Join(completionTestSourcedir, "completion")}, + []string{"add-multi-flag", makeShortName("f"), filepath.Join(completionTestSourcedir, "completion")}, completionTestFilename, false, }, @@ -282,6 +291,9 @@ func TestCompletion(t *testing.T) { items[i] = v.Item } + slices.Sort(items) + slices.Sort(test.Completed) + if !reflect.DeepEqual(items, test.Completed) { t.Errorf("Args: %#v, %#v\n Expected: %#v\n Got: %#v", test.Args, test.ShowDescriptions, test.Completed, items) } diff --git a/parser_test.go b/parser_test.go index df37044..e675cfd 100644 --- a/parser_test.go +++ b/parser_test.go @@ -101,7 +101,7 @@ func TestDefaults(t *testing.T) { { msg: "non-zero value arguments, expecting overwritten arguments", args: []string{"-3=true"}, - expectedErr: "bool flag `-3' cannot have an argument", + expectedErr: "bool flag `" + makeShortName("3") + "' cannot have an argument", }, { msg: "zero value arguments, expecting overwritten arguments",