Skip to content

Commit 87786c4

Browse files
Introduce test helpers
All generators will affect the "test" app located in `test/dummy`. Although the "test" app contains most files for a Rails application, it does not contain everything. This means during the setup phase of our tests, we'll need to create files to ensure the generator is working correctly. These helpers aim to improve the developer experience by creating a simple API to create and delete files in the "test" app. It should be noted that any files and directories created in the setup won't be removed automatically after each test, so we introduce additional helpers to be called during the [teardown][] callback. [teardown]: https://github.com/rails/rails/blob/00dfa109d6a5f4fb13d745f83d5cc779eba3b352/railties/lib/rails/generators/testing/setup_and_teardown.rb#L13
1 parent 06c8d27 commit 87786c4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/test_helper.rb

+30
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,33 @@
1212
ActiveSupport::TestCase.file_fixture_path = File.expand_path("fixtures", __dir__) + "/files"
1313
ActiveSupport::TestCase.fixtures :all
1414
end
15+
16+
module Suspenders::TestHelpers
17+
def app_root(path)
18+
Rails.root.join path
19+
end
20+
21+
def remove_file_if_exists(file)
22+
path = app_root file
23+
24+
FileUtils.rm path if File.exist? path
25+
end
26+
27+
def remove_dir_if_exists(dir)
28+
path = app_root dir
29+
30+
FileUtils.rm_r path if File.exist? path
31+
end
32+
33+
def mkdir(dir)
34+
path = app_root dir
35+
36+
FileUtils.mkdir path
37+
end
38+
39+
def touch(file)
40+
path = app_root file
41+
42+
FileUtils.touch path
43+
end
44+
end

0 commit comments

Comments
 (0)