From b18963529909e548c2b785e41b8dcbd06ffee22b Mon Sep 17 00:00:00 2001 From: Chris C Date: Mon, 11 Sep 2023 16:07:00 -0600 Subject: [PATCH] Refactor the generate todo file tests. - Refactored the `test_generate_todo` and `test_generate_todo_existing` methods in the `StandardrbTest` class to use a new helper function called `assert_generate_todo`. --- .../standardrb/generate_todo/.standard.yml | 1 + test/standardrb_test.rb | 60 ++++++++----------- 2 files changed, 25 insertions(+), 36 deletions(-) create mode 100644 test/fixture/standardrb/generate_todo/.standard.yml diff --git a/test/fixture/standardrb/generate_todo/.standard.yml b/test/fixture/standardrb/generate_todo/.standard.yml new file mode 100644 index 00000000..a347c552 --- /dev/null +++ b/test/fixture/standardrb/generate_todo/.standard.yml @@ -0,0 +1 @@ +ruby_version: 2.6 \ No newline at end of file diff --git a/test/standardrb_test.rb b/test/standardrb_test.rb index a4932afa..d1803d82 100644 --- a/test/standardrb_test.rb +++ b/test/standardrb_test.rb @@ -2,14 +2,13 @@ require "open3" class StandardrbTest < UnitTest + FIXTURE_ROOT_FOLDER = "test/fixture" TEST_ROOT_FOLDER = "tmp/tests/standardrb" - def before_setup - super - - FileUtils.rm_rf(TEST_ROOT_FOLDER) - FileUtils.mkdir_p(TEST_ROOT_FOLDER) - end + # Initialize the test folder once for all the tests + # in this file. + FileUtils.rm_rf(TEST_ROOT_FOLDER) + FileUtils.mkdir_p(TEST_ROOT_FOLDER) def test_project_a_failure_output stdout, status = run_standardrb("test/fixture/project/a") @@ -24,38 +23,11 @@ def test_project_a_failure_output end def test_generate_todo - FileUtils.rm_rf("tmp/standardrb_generate_todo_test") - FileUtils.mkdir_p("tmp/standardrb_generate_todo_test") - - FileUtils.cp("test/fixture/standardrb/generate_todo/useless_assignment.rb", "tmp/standardrb_generate_todo_test/") - - stdout, status = run_standardrb("tmp/standardrb_generate_todo_test", %w[--generate-todo --config ../../config/base.yml]) - - # The generate todo returns the Rubocop result which is non-zero - # if any linting errors where found. - refute status.success? - assert stdout.empty? - - assert File.exist?("tmp/standardrb_generate_todo_test/.standard_todo.yml") - assert_equal File.read("test/fixture/standardrb/generate_todo/.standard_todo_expected.yml"), File.read("tmp/standardrb_generate_todo_test/.standard_todo.yml") + assert_generate_todo("standardrb/generate_todo", "generate_todo", %w[--generate-todo --config ./.standard.yml]) end def test_generate_todo_existing - config_file = "./.standard.yml" - test_folder = File.join(TEST_ROOT_FOLDER, "generate_todo_existing") - fixture_folder = "test/fixture/standardrb/generate_todo_existing" - - FileUtils.cp_r(File.join(fixture_folder, "."), test_folder) - - stdout, status = run_standardrb(test_folder, ["--generate-todo", "--config", config_file]) - - # The generate todo returns the Rubocop result which is non-zero - # if any linting errors where found. - refute status.success? - assert_equal "", stdout - - assert File.exist?(File.join(test_folder, ".standard_todo.yml")) - assert_equal File.read(File.join(test_folder, ".standard_todo_expected.yml")), File.read(File.join(test_folder, ".standard_todo.yml")) + assert_generate_todo("standardrb/generate_todo_existing", "generate_todo_existing", %w[--generate-todo --config ./.standard.yml]) end def test_project_a_forwards_rubocop_options @@ -109,7 +81,6 @@ def test_plugins_options private def run_standardrb(cwd, args = []) - # REM: Refactor to chdir block? og_pwd = Dir.pwd Dir.chdir(cwd) Open3.capture2(File.join(__dir__, "../exe/standardrb"), *args) @@ -120,4 +91,21 @@ def run_standardrb(cwd, args = []) def assert_same_lines(expected, actual) assert_equal expected.split("\n").sort, actual.split("\n").sort end + + def assert_generate_todo(fixture_subfolder, test_subfolder, arguments) + fixture_folder = File.join(FIXTURE_ROOT_FOLDER, fixture_subfolder) + test_folder = File.join(TEST_ROOT_FOLDER, test_subfolder) + + FileUtils.cp_r(File.join(fixture_folder, "."), test_folder) + + stdout, status = run_standardrb(test_folder, arguments) + + # The generate todo returns the Rubocop result which is non-zero + # if any linting errors where found. + refute status.success? + assert stdout.empty? + + assert File.exist?(File.join(test_folder, ".standard_todo.yml")) + assert_equal File.read(File.join(test_folder, ".standard_todo_expected.yml")), File.read(File.join(test_folder, ".standard_todo.yml")) + end end