Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from MrJoy/fix_file_rewriting_with_rewriters
`ruby-rewrite` fails to actually rewrite the input file when a rewriter is loaded. This fixes the issue.
- Loading branch information
Showing
6 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if(true) | ||
puts "Hello, world!" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if true | ||
puts "Hello, world!" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Rewriter < Parser::Rewriter | ||
def on_if(node) | ||
# Crude, totally-not-usable-in-the-real-world code to remove optional | ||
# parens from control keywords. | ||
# | ||
# In a perfect test scenario we'd simply make this a no-op, to demonstrate | ||
# that the bug happens when any rewriter is loaded regardless of whether it | ||
# actually changes anything but that makes assertions much harder to get | ||
# right. It's much easier to just show that the file did, or did not | ||
# get changed. | ||
if node.children[0].type == :begin | ||
replace node.children[0].loc.begin, ' ' | ||
remove node.children[0].loc.end | ||
end | ||
|
||
super | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'pathname' | ||
require 'fileutils' | ||
require 'shellwords' | ||
|
||
BASE_DIR = Pathname.new(__FILE__) + '../..' | ||
require (BASE_DIR + 'helper').expand_path | ||
|
||
class TestRunnerRewrite < Minitest::Test | ||
def setup | ||
@ruby_rewrite = BASE_DIR.expand_path + '../bin/ruby-rewrite' | ||
@test_dir = BASE_DIR + 'bug_163' | ||
@fixtures_dir = @test_dir + 'fixtures' | ||
end | ||
|
||
def test_rewriter | ||
Dir.mktmpdir(nil, BASE_DIR.expand_path.to_s) do |tmp_dir| | ||
tmp_dir = Pathname.new(tmp_dir) | ||
sample_file = tmp_dir + 'bug_163.rb' | ||
sample_file_expanded = sample_file.expand_path | ||
expected_file = @fixtures_dir + 'output.rb' | ||
|
||
FileUtils.cp(@fixtures_dir + 'input.rb', tmp_dir + 'bug_163.rb') | ||
FileUtils.cd @test_dir do | ||
exit_code = system %Q{ | ||
#{Shellwords.escape(@ruby_rewrite.to_s)} --modify \ | ||
-l rewriter.rb \ | ||
#{Shellwords.escape(sample_file_expanded.to_s)} | ||
} | ||
end | ||
|
||
assert File.read(expected_file.expand_path) == File.read(sample_file), | ||
"#{sample_file} should be identical to #{expected_file}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'tempfile' | ||
require 'minitest/test' | ||
|
||
require 'simplecov' | ||
require 'coveralls' | ||
|