Skip to content

Commit

Permalink
fix: support special chars in assert_contains expected expression (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored May 10, 2023
1 parent d13884f commit b5e9238
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/testing.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,31 @@ def assert_contains(name, actual, expected, size = None, timeout = None, **kwarg
"""

test_sh = "_{}_test.sh".format(name)
expected_file = "_{}_expected.txt".format(name)

write_file(
name = "_%s_expected" % name,
out = expected_file,
content = [expected],
)

write_file(
name = "_" + name,
out = test_sh,
content = [
"#!/usr/bin/env bash",
"set -o errexit",
"grep --fixed-strings '{}' $1".format(expected),
"grep --fixed-strings -f $1 $2",
],
)

native.sh_test(
name = name,
srcs = [test_sh],
args = ["$(rootpath %s)" % actual],
args = ["$(rootpath %s)" % expected_file, "$(rootpath %s)" % actual],
size = size,
timeout = default_timeout(size, timeout),
data = [actual],
data = [actual, expected_file],
**kwargs
)

Expand Down
21 changes: 21 additions & 0 deletions lib/tests/assert_contains/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("//lib:testing.bzl", "assert_contains")

# Special characters within expected string: https://github.com/aspect-build/rules_js/issues/1045

assert_contains(
name = "regexy-args-1",
actual = ":case1.txt",
expected = "--arg1='/{{[{]?(.*?)[}]?}}/'",
)

assert_contains(
name = "regexy-args-2",
actual = ":case1.txt",
expected = "--arg2='/{%(.*?)%}/'",
)

assert_contains(
name = "backtick",
actual = ":case1.txt",
expected = "`ff`",
)
3 changes: 3 additions & 0 deletions lib/tests/assert_contains/case1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--arg1='/{{[{]?(.*?)[}]?}}/'
--arg2='/{%(.*?)%}/'
`ff`

0 comments on commit b5e9238

Please sign in to comment.