Skip to content

Commit c52806d

Browse files
committed
chore: linter issues
1 parent 402e3bc commit c52806d

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

cmd/model/test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package model
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"os"
2223
"path"
@@ -30,6 +31,12 @@ import (
3031
"github.com/openfga/cli/internal/storetest"
3132
)
3233

34+
var (
35+
errNoTestFilesSpecified = errors.New("no test files specified")
36+
errNoTestFilesFound = errors.New("no test files found")
37+
errTestFileDoesNotExist = errors.New("test file does not exist")
38+
)
39+
3340
// modelTestCmd represents the test command.
3441
var modelTestCmd = &cobra.Command{
3542
Use: "test",
@@ -144,17 +151,21 @@ func expandTestFilePatterns(patterns []string, posArgs []string) ([]string, erro
144151
// This handles shell expansion: when the shell expands ./example/*.fga.yaml to
145152
// ./example/file1.yaml ./example/file2.yaml, the first file goes to the --tests flag
146153
// and the rest end up as positional arguments
147-
allPatterns := append(patterns, posArgs...)
154+
allPatterns := make([]string, 0, len(patterns)+len(posArgs))
155+
allPatterns = append(allPatterns, patterns...)
156+
allPatterns = append(allPatterns, posArgs...)
148157

149158
if len(allPatterns) == 0 {
150-
return nil, fmt.Errorf("no test files specified")
159+
return nil, errNoTestFilesSpecified
151160
}
152161

153162
fileNames := []string{}
163+
154164
for _, pattern := range allPatterns {
155165
// First, check if it's a literal file that exists
156166
if _, err := os.Stat(pattern); err == nil {
157167
fileNames = append(fileNames, pattern)
168+
158169
continue
159170
}
160171

@@ -168,12 +179,12 @@ func expandTestFilePatterns(patterns []string, posArgs []string) ([]string, erro
168179
fileNames = append(fileNames, matches...)
169180
} else {
170181
// If glob didn't match and file doesn't exist, report error
171-
return nil, fmt.Errorf("test file %s does not exist", pattern)
182+
return nil, fmt.Errorf("%w: %s", errTestFileDoesNotExist, pattern)
172183
}
173184
}
174185

175186
if len(fileNames) == 0 {
176-
return nil, fmt.Errorf("no test files found")
187+
return nil, errNoTestFilesFound
177188
}
178189

179190
return fileNames, nil
@@ -182,7 +193,8 @@ func expandTestFilePatterns(patterns []string, posArgs []string) ([]string, erro
182193
func init() {
183194
modelTestCmd.Flags().String("store-id", "", "Store ID")
184195
modelTestCmd.Flags().String("model-id", "", "Model ID")
185-
modelTestCmd.Flags().StringArray("tests", []string{}, "Path or glob of YAML test files. Can be specified multiple times or use glob patterns")
196+
modelTestCmd.Flags().StringArray("tests", []string{},
197+
"Path or glob of YAML test files. Can be specified multiple times or use glob patterns")
186198
modelTestCmd.Flags().Bool("verbose", false, "Print verbose JSON output")
187199
modelTestCmd.Flags().Bool("suppress-summary", false, "Suppress the plain text summary output")
188200

cmd/model/test_test.go

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package model
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -25,8 +26,8 @@ func TestExpandTestFilePatterns_MultipleFilesWithFlag(t *testing.T) {
2526
)
2627
require.NoError(t, err)
2728
assert.Len(t, files, 2)
28-
assert.True(t, containsPath(files, "example/model.fga.yaml"))
29-
assert.True(t, containsPath(files, "example/store_abac.fga.yaml"))
29+
assert.True(t, anyContains(files, "example/model.fga.yaml"))
30+
assert.True(t, anyContains(files, "example/store_abac.fga.yaml"))
3031
}
3132

3233
func TestExpandTestFilePatterns_GlobPattern(t *testing.T) {
@@ -35,8 +36,8 @@ func TestExpandTestFilePatterns_GlobPattern(t *testing.T) {
3536
files, err := expandTestFilePatterns([]string{"../../example/*.fga.yaml"}, []string{})
3637
require.NoError(t, err)
3738
assert.GreaterOrEqual(t, len(files), 2, "should match at least model.fga.yaml and store_abac.fga.yaml")
38-
assert.True(t, containsPath(files, "example/model.fga.yaml"))
39-
assert.True(t, containsPath(files, "example/store_abac.fga.yaml"))
39+
assert.True(t, anyContains(files, "example/model.fga.yaml"))
40+
assert.True(t, anyContains(files, "example/store_abac.fga.yaml"))
4041
}
4142

4243
func TestExpandTestFilePatterns_ShellExpandedFiles(t *testing.T) {
@@ -50,8 +51,8 @@ func TestExpandTestFilePatterns_ShellExpandedFiles(t *testing.T) {
5051
)
5152
require.NoError(t, err)
5253
assert.Len(t, files, 2)
53-
assert.True(t, containsPath(files, "example/model.fga.yaml"))
54-
assert.True(t, containsPath(files, "example/store_abac.fga.yaml"))
54+
assert.True(t, anyContains(files, "example/model.fga.yaml"))
55+
assert.True(t, anyContains(files, "example/store_abac.fga.yaml"))
5556
}
5657

5758
func TestExpandTestFilePatterns_MixedGlobAndLiteral(t *testing.T) {
@@ -64,8 +65,8 @@ func TestExpandTestFilePatterns_MixedGlobAndLiteral(t *testing.T) {
6465
require.NoError(t, err)
6566
// Should include model.fga.yaml and store_abac.fga.yaml
6667
assert.GreaterOrEqual(t, len(files), 2, "should have at least 2 files")
67-
assert.True(t, containsPath(files, "example/model.fga.yaml"))
68-
assert.True(t, containsPath(files, "example/store_abac.fga.yaml"))
68+
assert.True(t, anyContains(files, "example/model.fga.yaml"))
69+
assert.True(t, anyContains(files, "example/store_abac.fga.yaml"))
6970
}
7071

7172
func TestExpandTestFilePatterns_SubdirectoryGlob(t *testing.T) {
@@ -74,7 +75,7 @@ func TestExpandTestFilePatterns_SubdirectoryGlob(t *testing.T) {
7475
files, err := expandTestFilePatterns([]string{"../../example/subdir/*.fga.yaml"}, []string{})
7576
require.NoError(t, err)
7677
assert.Len(t, files, 1)
77-
assert.True(t, containsPath(files, "example/subdir/simple.fga.yaml"))
78+
assert.True(t, anyContains(files, "example/subdir/simple.fga.yaml"))
7879
}
7980

8081
func TestExpandTestFilePatterns_NonExistentFile(t *testing.T) {
@@ -114,9 +115,9 @@ func TestExpandTestFilePatterns_ShellExpandedThreeFiles(t *testing.T) {
114115
)
115116
require.NoError(t, err)
116117
assert.Len(t, files, 3)
117-
assert.True(t, containsPath(files, "example/model.fga.yaml"))
118-
assert.True(t, containsPath(files, "example/store_abac.fga.yaml"))
119-
assert.True(t, containsPath(files, "example/subdir/simple.fga.yaml"))
118+
assert.True(t, anyContains(files, "example/model.fga.yaml"))
119+
assert.True(t, anyContains(files, "example/store_abac.fga.yaml"))
120+
assert.True(t, anyContains(files, "example/subdir/simple.fga.yaml"))
120121
}
121122

122123
func TestExpandTestFilePatterns_GlobPatternNotExpandedByShell(t *testing.T) {
@@ -139,8 +140,8 @@ func TestExpandTestFilePatterns_CombineGlobAndShellExpanded(t *testing.T) {
139140
)
140141
require.NoError(t, err)
141142
assert.Len(t, files, 2)
142-
assert.True(t, containsPath(files, "example/model.fga.yaml"))
143-
assert.True(t, containsPath(files, "example/subdir/simple.fga.yaml"))
143+
assert.True(t, anyContains(files, "example/model.fga.yaml"))
144+
assert.True(t, anyContains(files, "example/subdir/simple.fga.yaml"))
144145
}
145146

146147
func TestExpandTestFilePatterns_InvalidGlobPattern(t *testing.T) {
@@ -153,25 +154,13 @@ func TestExpandTestFilePatterns_InvalidGlobPattern(t *testing.T) {
153154
assert.Contains(t, err.Error(), "invalid tests pattern")
154155
}
155156

156-
// containsPath checks if any file in the slice contains the given path substring
157-
func containsPath(files []string, pathSubstring string) bool {
158-
for _, file := range files {
159-
if containsSubstring(file, pathSubstring) {
157+
// anyContains checks if any string in the slice contains the given substring.
158+
func anyContains(slice []string, substr string) bool {
159+
for _, s := range slice {
160+
if strings.Contains(s, substr) {
160161
return true
161162
}
162163
}
163-
return false
164-
}
165-
166-
func containsSubstring(s, substr string) bool {
167-
return len(s) >= len(substr) && (s[len(s)-len(substr):] == substr || contains(s, substr))
168-
}
169164

170-
func contains(s, substr string) bool {
171-
for i := 0; i <= len(s)-len(substr); i++ {
172-
if s[i:i+len(substr)] == substr {
173-
return true
174-
}
175-
}
176165
return false
177166
}

0 commit comments

Comments
 (0)