diff --git a/lib/rbs/cli.rb b/lib/rbs/cli.rb index bb32fe430..956e47a54 100644 --- a/lib/rbs/cli.rb +++ b/lib/rbs/cli.rb @@ -1248,7 +1248,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 diff --git a/test/rbs/cli_test.rb b/test/rbs/cli_test.rb index 2c28a4f52..7bb1fe02c 100644 --- a/test/rbs/cli_test.rb +++ b/test/rbs/cli_test.rb @@ -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)