Skip to content

Commit

Permalink
Refactor the generate todo file tests.
Browse files Browse the repository at this point in the history
- 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`.
  • Loading branch information
mrbiggred committed Sep 11, 2023
1 parent cd1d59f commit b189635
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
1 change: 1 addition & 0 deletions test/fixture/standardrb/generate_todo/.standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby_version: 2.6
60 changes: 24 additions & 36 deletions test/standardrb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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

0 comments on commit b189635

Please sign in to comment.