Skip to content

Commit

Permalink
subtract: Remove RBS file if the subtracted definition is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Jul 21, 2023
1 parent db860d1 commit 11bf939
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,11 @@ def run_subtract(args, _)
w.write(subtracted)

if write_to_file
rbs_path.write(io.string)
if io.string.empty?
rbs_path.delete
else
rbs_path.write(io.string)
end
else
stdout.puts(io.string)
end
Expand Down
25 changes: 25 additions & 0 deletions test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,31 @@ def y: () -> untyped
end
end


def test_subtract_write_removes_definition_if_empty
Dir.mktmpdir do |dir|
dir = Pathname(dir)

minuend = dir.join('minuend.rbs')
minuend.write(<<~RBS)
class C
def x: () -> untyped
end
RBS
subtrahend = dir.join('subtrahend.rbs')
subtrahend.write(<<~RBS)
class C
def x: () -> untyped
end
RBS

stdout, stderr = run_rbs('subtract', '--write', minuend.to_s, subtrahend.to_s)
assert_empty stderr
assert_empty stdout
assert_equal minuend.exist?, false
end
end

def assert_rbs_test_no_errors cli, dir, arg_array
args = ['-I', dir.to_s, 'test', *arg_array]
assert_instance_of Process::Status, cli.run(args)
Expand Down

0 comments on commit 11bf939

Please sign in to comment.