Skip to content

Commit

Permalink
test: add alias tests that can be used as examples (#391)
Browse files Browse the repository at this point in the history
Adds examples for pascalcase and command
  • Loading branch information
jazanne authored Oct 5, 2023
1 parent 2ad4e60 commit 7a042a2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions aliases/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 26 additions & 8 deletions aliases/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -40,7 +41,6 @@ func Test_GenerateAliases(t *testing.T) {
flags []string
aliases []o.Alias
want map[string][]string
wantErr error
}{
{
name: "literals",
Expand Down Expand Up @@ -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),
Expand All @@ -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)
})
}
}
Expand Down
8 changes: 8 additions & 0 deletions aliases/aliases-test.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7a042a2

Please sign in to comment.