Skip to content

Commit

Permalink
acc: Support custom replacements (#2231)
Browse files Browse the repository at this point in the history
## Changes
- Ability to extend a list of replacements via test.toml
- Modify selftest to both demo this feature and to get rid of sed on
Windows.

## Tests
Acceptance tests. I'm also using it
#2213 for things like pid.
  • Loading branch information
denik authored Jan 27, 2025
1 parent 82b0dd3 commit 1cb32ec
Showing 5 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
@@ -190,6 +190,7 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont
}

repls.SetPathWithParents(tmpDir, "$TMPDIR")
repls.Repls = append(repls.Repls, config.Repls...)

scriptContents := readMergedScriptContents(t, dir)
testutil.WriteFile(t, filepath.Join(tmpDir, EntryPointScript), scriptContents)
5 changes: 5 additions & 0 deletions acceptance/config_test.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import (
"testing"

"github.com/BurntSushi/toml"
"github.com/databricks/cli/libs/testdiff"
"github.com/stretchr/testify/require"
)

@@ -24,6 +25,10 @@ type TestConfig struct {
// Which OSes the test is enabled on. Each string is compared against runtime.GOOS.
// If absent, default to true.
GOOS map[string]bool

// List of additional replacements to apply on this test.
// Old is a regexp, New is a replacement expression.
Repls []testdiff.Replacement
}

// FindConfig finds the closest config file.
5 changes: 5 additions & 0 deletions acceptance/selftest/output.txt
Original file line number Diff line number Diff line change
@@ -28,3 +28,8 @@ $TMPDIR/subdir/a/b/c

=== Custom output files - everything starting with out is captured and compared
>>> echo HELLO

=== Custom regex can be specified in [[Repl]] section
1234
CUSTOM_NUMBER_REGEX
123456
7 changes: 6 additions & 1 deletion acceptance/selftest/script
Original file line number Diff line number Diff line change
@@ -15,7 +15,12 @@ trace python3 -c 'import os; print(os.getcwd())'

printf "\n=== Capturing subdir"
trace mkdir -p subdir/a/b/c
trace withdir subdir/a/b/c python3 -c 'import os; print(os.getcwd())' | sed 's/\\/\//g'
trace withdir subdir/a/b/c python3 -c 'import os; print(os.getcwd())'

printf "\n=== Custom output files - everything starting with out is captured and compared"
trace echo HELLO > out.hello.txt

printf "\n=== Custom regex can be specified in [[Repl]] section\n"
echo 1234
echo 12345
echo 123456
9 changes: 9 additions & 0 deletions acceptance/selftest/test.toml
Original file line number Diff line number Diff line change
@@ -9,3 +9,12 @@

# Disable on Linux
#linux = false

[[Repls]]
Old = '\b[0-9]{5}\b'
New = "CUSTOM_NUMBER_REGEX"

[[Repls]]
# Fix path with reverse slashes in the output for Windows.
Old = '\$TMPDIR\\subdir\\a\\b\\c'
New = '$$TMPDIR/subdir/a/b/c'

0 comments on commit 1cb32ec

Please sign in to comment.