Skip to content

Commit e9eeed8

Browse files
committed
feat: add ignoreFiles option to suppress file warnings in test directories
Adds a new TestSuite configuration option `ignoreFiles` that accepts file patterns (e.g., "*.md", "README*") to ignore when collecting test step files. Files matching these patterns are silently skipped without generating warnings, allowing documentation and other non-test files to coexist in test case directories. Fixes #650. Signed-off-by: Martin André <[email protected]>
1 parent 9ef9f00 commit e9eeed8

File tree

7 files changed

+65
-3
lines changed

7 files changed

+65
-3
lines changed

docs/testing/reference.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ kindContainers:
1313
testDirs:
1414
- tests/e2e/
1515
timeout: 120
16+
ignoreFiles:
17+
- "*.md"
18+
- "README*"
19+
- ".gitignore"
1620
```
1721
1822
Supported settings:
@@ -39,6 +43,7 @@ reportGranularity | string | What granularity to report failures at. O
3943
reportName | string | The name of report to create. This field is not used unless reportFormat is set. | "kuttl-test"
4044
namespace | string | The namespace to use for tests. This namespace will be created if it does not exist and removed if it was created (unless `skipDelete` is set). If no namespace is set, one will be auto-generated. |
4145
suppress | list of strings | Suppresses log collection of the specified types. Currently only `events` is supported. |
46+
ignoreFiles | list of strings | File patterns (e.g., `*.md`, `README*`) to ignore when collecting test steps. Files matching these patterns will not generate warnings about not matching the expected test file pattern. | []
4247

4348
## TestStep
4449

internal/harness/harness.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (h *Harness) LoadTests(dir string) ([]*testcase.Case, error) {
8888
testcase.WithNamespace(h.TestSuite.Namespace),
8989
testcase.WithTimeout(timeout),
9090
testcase.WithLogSuppressions(h.TestSuite.Suppress),
91+
testcase.WithIgnoreFiles(h.TestSuite.IgnoreFiles),
9192
testcase.WithRunLabels(h.RunLabels),
9293
testcase.WithClients(h.Client, h.DiscoveryClient),
9394
testcase.WithTemplateVars(h.TemplateVars)))

internal/testcase/case.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ func WithLogSuppressions(suppressions []string) CaseOption {
7171
}
7272
}
7373

74+
// WithIgnoreFiles sets the list of file patterns to ignore.
75+
func WithIgnoreFiles(patterns []string) CaseOption {
76+
return func(c *Case) {
77+
c.ignoreFiles = patterns
78+
}
79+
}
80+
7481
// WithRunLabels sets the run labels.
7582
func WithRunLabels(runLabels labels.Set) CaseOption {
7683
return func(c *Case) {
@@ -118,6 +125,8 @@ type Case struct {
118125
logger testutils.Logger
119126
// List of log types which should be suppressed.
120127
suppressions []string
128+
// List of file patterns to ignore when collecting test steps.
129+
ignoreFiles []string
121130
// Caution: the Vars element of this struct may be shared with other Case objects.
122131
templateEnv template.Env
123132
}
@@ -347,7 +356,7 @@ func (c *Case) getEagerClients() ([]clientWithKubeConfig, error) {
347356

348357
// LoadTestSteps loads all the test steps for a test case.
349358
func (c *Case) LoadTestSteps() error {
350-
testStepFiles, err := files.CollectTestStepFiles(c.dir, c.logger)
359+
testStepFiles, err := files.CollectTestStepFiles(c.dir, c.logger, c.ignoreFiles)
351360
if err != nil {
352361
return err
353362
}

internal/utils/files/files.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// CollectTestStepFiles collects a map of test steps and their associated files
1212
// from a directory.
13-
func CollectTestStepFiles(dir string, logger testutils.Logger) (map[int64][]string, error) {
13+
func CollectTestStepFiles(dir string, logger testutils.Logger, ignorePatterns []string) (map[int64][]string, error) {
1414
testStepFiles := map[int64][]string{}
1515

1616
files, err := os.ReadDir(dir)
@@ -19,6 +19,23 @@ func CollectTestStepFiles(dir string, logger testutils.Logger) (map[int64][]stri
1919
}
2020

2121
for _, file := range files {
22+
// Check if file matches any ignore pattern
23+
shouldIgnore := false
24+
for _, pattern := range ignorePatterns {
25+
matched, err := filepath.Match(pattern, file.Name())
26+
if err != nil {
27+
logger.Logf("Invalid ignore pattern %q: %v", pattern, err)
28+
continue
29+
}
30+
if matched {
31+
shouldIgnore = true
32+
break
33+
}
34+
}
35+
if shouldIgnore {
36+
continue
37+
}
38+
2239
f := kfile.Parse(file.Name())
2340
if f.Type == kfile.TypeUnknown {
2441
logger.Logf("Ignoring %q: %v.", file.Name(), f.Error)

internal/utils/files/files_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,30 @@ func TestCollectTestStepFiles(t *testing.T) {
4848
},
4949
} {
5050
t.Run(tt.path, func(t *testing.T) {
51-
testStepFiles, err := CollectTestStepFiles(tt.path, testutils.NewTestLogger(t, tt.path))
51+
testStepFiles, err := CollectTestStepFiles(tt.path, testutils.NewTestLogger(t, tt.path), nil)
5252
require.NoError(t, err)
5353
assert.Equal(t, tt.expected, testStepFiles)
5454
})
5555
}
5656
}
57+
58+
func TestCollectTestStepFilesWithIgnorePatterns(t *testing.T) {
59+
// Test with ignore patterns - should silently skip README.md
60+
ignorePatterns := []string{"README*", "*.md"}
61+
logger := testutils.NewTestLogger(t, "test_data/with-overrides-with-ignore")
62+
testStepFiles, err := CollectTestStepFiles("test_data/with-overrides", logger, ignorePatterns)
63+
require.NoError(t, err)
64+
65+
// Verify we still get the expected test step files
66+
assert.NotEmpty(t, testStepFiles)
67+
assert.Contains(t, testStepFiles, int64(0))
68+
assert.Contains(t, testStepFiles, int64(1))
69+
assert.Contains(t, testStepFiles, int64(2))
70+
assert.Contains(t, testStepFiles, int64(3))
71+
72+
// Test that multiple patterns work
73+
multiPatterns := []string{"*.md", "*.txt", "*.rst"}
74+
testStepFilesMulti, err := CollectTestStepFiles("test_data/with-overrides", logger, multiPatterns)
75+
require.NoError(t, err)
76+
assert.Equal(t, testStepFiles, testStepFilesMulti)
77+
}

pkg/apis/testharness/v1beta1/test_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ type TestSuite struct {
9797
// Suppress is used to suppress logs
9898
Suppress []string `json:"suppress"`
9999

100+
// IgnoreFiles is a list of file patterns (e.g., "*.md", "README*") to ignore when collecting test steps.
101+
// Files matching these patterns will not generate warnings about not matching the expected test file pattern.
102+
IgnoreFiles []string `json:"ignoreFiles"`
103+
100104
Config *RestConfig `json:"config,omitempty"`
101105
}
102106

pkg/apis/testharness/v1beta1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)