Skip to content

Commit 72b15fb

Browse files
committed
Fix issue in cext_preprocessor.rb if the replacement is empty
* Like in json_patches.rb * See #2386 * Rename test file to use _ and not - by convention. (cherry picked from commit a8b7abc)
1 parent 2a770e3 commit 72b15fb

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib/truffle/truffle/cext_preprocessor.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ def self.patch(file, contents, directory)
9696
# preprocessor macro which _must_ end with a newline and
9797
# so requires that we preserve the trailing whitespace.
9898
patched_file[:patches].each do |patch|
99-
last_line = patch[:replacement].rstrip.lines.last.lstrip
99+
replacement = patch[:replacement].rstrip
100+
last_line = replacement.lines.last || replacement # .lines returns an empty Array if String#empty?
101+
last_line = last_line.lstrip
100102
contents = contents.gsub(patch[:match],
101103
if last_line && last_line.start_with?('#')
102104
patch[:replacement]

test/truffle/cexts/test-preprocess.rb renamed to test/truffle/cexts/test_preprocess.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,22 @@ def test_patch(file, directory, input, expected)
5656
}
5757
EOF
5858

59+
json_original = <<-EOF
60+
# else
61+
rb_str_resize(*result, RSTRING_LEN(*result));
62+
# endif
63+
EOF
64+
65+
json_patched = <<-EOF
66+
# else
67+
68+
# endif
69+
EOF
70+
5971
test_patch 'xml_sax_parser.c', 'ext/nokogiri', original, modified
6072
# Should not patch other files or other gems
6173
test_patch 'other_file.c', 'ext/nokogiri', original, original
6274
test_patch 'xml_sax_parser.c', 'ext/other_gem', original, original
75+
76+
# Tests an empty replacement
77+
test_patch 'parser.c', 'ext/json/ext/parser', json_original, json_patched

tool/jt.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ def retag(*args)
12781278
case test_name
12791279
when 'tools'
12801280
# Test tools
1281-
run_ruby 'test/truffle/cexts/test-preprocess.rb'
1281+
run_ruby 'test/truffle/cexts/test_preprocess.rb'
12821282

12831283
when 'minimum', 'method', 'module', 'globals', 'backtraces', 'xopenssl'
12841284
# Test that we can compile and run some very basic C extensions

0 commit comments

Comments
 (0)