From 7a042a207934ba0857f9f1c42be067a3e080e167 Mon Sep 17 00:00:00 2001 From: Jaz White Date: Thu, 5 Oct 2023 11:37:03 -0400 Subject: [PATCH] test: add alias tests that can be used as examples (#391) Adds examples for pascalcase and command --- aliases/alias.go | 4 ++-- aliases/alias_test.go | 34 ++++++++++++++++++++++++++-------- aliases/aliases-test.py | 8 ++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100755 aliases/aliases-test.py diff --git a/aliases/alias.go b/aliases/alias.go index b2b34454..c4a27ae7 100644 --- a/aliases/alias.go +++ b/aliases/alias.go @@ -120,10 +120,10 @@ func generateAliasesFromCommand(a options.Alias, flag, dir string) ([]string, er cmd.Dir = dir stdout, err := cmd.Output() if err != nil { - return nil, fmt.Errorf("filepattern '%s': failed to execute alias command: %w", a.Name, err) + return nil, fmt.Errorf("command '%s': failed to execute alias command: %w", a.Name, err) } if err := json.Unmarshal(stdout, &ret); err != nil { - return nil, fmt.Errorf("filepattern '%s': could not unmarshal json output of alias command: %w", a.Name, err) + return nil, fmt.Errorf("command '%s': could not unmarshal json output of alias command: %w", a.Name, err) } return ret, err diff --git a/aliases/alias_test.go b/aliases/alias_test.go index 9a29bb6a..53819770 100644 --- a/aliases/alias_test.go +++ b/aliases/alias_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/launchdarkly/ld-find-code-refs/v2/internal/log" o "github.com/launchdarkly/ld-find-code-refs/v2/options" @@ -40,7 +41,6 @@ func Test_GenerateAliases(t *testing.T) { flags []string aliases []o.Alias want map[string][]string - wantErr error }{ { name: "literals", @@ -73,6 +73,14 @@ func Test_GenerateAliases(t *testing.T) { }, want: map[string][]string{testFlagKey: slice("SomeFlag")}, }, + { + name: "PascalCase alias", + flags: slice("some_flag"), + aliases: []o.Alias{ + alias(o.PascalCase), + }, + want: map[string][]string{"some_flag": slice("SomeFlag")}, + }, { name: "file exact pattern", flags: slice(testFlagKey), @@ -89,19 +97,29 @@ func Test_GenerateAliases(t *testing.T) { }, want: map[string][]string{testWildFlagKey: slice("WILD_FLAG", "WILD_FLAG_SECOND_ALIAS"), testFlagKey: slice("SOME_FLAG")}, }, - // TODO - // { - // name: "command", - // flags: slice(testFlagKey), - // aliases: []o.Alias{cmd(`echo '["SOME_FLAG"]'`, 0)}, - // }, + { + name: "command", + flags: slice(testFlagKey), + aliases: []o.Alias{ + cmd(`echo ["SOME_FLAG"]`, 0), + }, + want: map[string][]string{testFlagKey: slice("SOME_FLAG")}, + }, + { + name: "command, custom pascalcase", + flags: slice("some_flag"), + aliases: []o.Alias{ + cmd(`python3 ./aliases-test.py`, 5), + }, + want: map[string][]string{"some_flag": slice("Some_Flag")}, + }, } for _, tt := range specs { t.Run(tt.name, func(t *testing.T) { aliases, err := GenerateAliases(tt.flags, tt.aliases, "") + require.NoError(t, err) assert.Equal(t, tt.want, aliases) - assert.Equal(t, tt.wantErr, err) }) } } diff --git a/aliases/aliases-test.py b/aliases/aliases-test.py new file mode 100755 index 00000000..68b7dffe --- /dev/null +++ b/aliases/aliases-test.py @@ -0,0 +1,8 @@ +#! /usr/bin/env python +import sys,json +for flag_key in sys.stdin: + flag_key = flag_key.strip() + alias = '_'.join([word.capitalize() for word in flag_key.split('_')]) + aliases = json.dumps([alias]) + print(aliases) + break