Skip to content

Commit b8728ea

Browse files
Fix: Escape strings printed by assertEqual
1 parent 4b914fb commit b8728ea

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

stdlib/std.jsonnet

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,10 +831,14 @@ limitations under the License.
831831
std.map(map_func, std.filter(filter_func, arr)),
832832

833833
assertEqual(a, b)::
834+
// If the values are strings, escape them for printing.
835+
// If not, they'll be JSON-stringified anyway by the later string concatenation.
836+
local astr = if std.type(a) == 'string' then std.escapeStringJson(a) else a;
837+
local bstr = if std.type(b) == 'string' then std.escapeStringJson(b) else b;
834838
if a == b then
835839
true
836840
else
837-
error 'Assertion failed. ' + a + ' != ' + b,
841+
error 'Assertion failed. ' + astr + ' != ' + bstr,
838842

839843
abs(n)::
840844
if !std.isNumber(n) then
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Copyright 2024 Google Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
std.assertEqual({ a: 1 }, { b: 1 })
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RUNTIME ERROR: Assertion failed. {"a": 1} != {"b": 1}
2+
std.jsonnet:<stdlib_position_redacted> function <anonymous>
3+
error.assert_equal_obj.jsonnet:17:1-36
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Copyright 2024 Google Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
std.assertEqual('one\ntwo', 'three\nfour\n')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RUNTIME ERROR: Assertion failed. "one\ntwo" != "three\nfour\n"
2+
std.jsonnet:<stdlib_position_redacted> function <anonymous>
3+
error.assert_equal_str.jsonnet:17:1-45

0 commit comments

Comments
 (0)