From 11bf9399165f4b3ba3883d87d4aa259ba44d5ddf Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 21 Jul 2023 20:45:26 +0900 Subject: [PATCH] subtract: Remove RBS file if the subtracted definition is empty --- lib/rbs/cli.rb | 6 +++++- test/rbs/cli_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/rbs/cli.rb b/lib/rbs/cli.rb index d1892a860..394bab576 100644 --- a/lib/rbs/cli.rb +++ b/lib/rbs/cli.rb @@ -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 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)