Skip to content

Commit 82ae26b

Browse files
authored
Merge pull request rails#49803 from zzak/railties/remove_from_file-gsub
Railties TestHelper remove_from_config should remove all occurrences
2 parents 9954650 + e2169f5 commit 82ae26b

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

railties/test/isolation/abstract_unit.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def remove_from_env_config(env, str)
448448

449449
def remove_from_file(file, str)
450450
contents = File.read(file)
451-
contents.sub!(/#{str}/, "")
451+
contents.gsub!(/#{str}/, "")
452452
File.write(file, contents)
453453
end
454454

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# frozen_string_literal: true
2+
3+
require "isolation/abstract_unit"
4+
5+
module TestHelpersTests
6+
class GenerationTest < ActiveSupport::TestCase
7+
include ActiveSupport::Testing::Isolation
8+
9+
def test_build_app
10+
build_app
11+
12+
assert File.exist?("#{app_path}/config/database.yml")
13+
assert File.exist?("#{app_path}/config/routes.rb")
14+
assert File.exist?("#{app_path}/config/initializers")
15+
end
16+
17+
def test_teardown_app
18+
build_app
19+
teardown_app
20+
21+
assert_not File.exist?(app_path)
22+
end
23+
24+
def test_add_to_config
25+
build_app
26+
27+
config_file = "#{app_path}/config/application.rb"
28+
29+
assert_not_empty File.open(config_file, &:read)
30+
31+
add_to_config <<-RUBY
32+
config.zomg = 'zomg'
33+
RUBY
34+
35+
config = File.open(config_file, &:read)
36+
37+
# preserves indentation
38+
assert_match(/ config\.zomg = 'zomg'$/, config, "Expected `#{config_file}` to include `config.zomg = 'zomg'`, but did not:\n #{config}")
39+
end
40+
41+
def test_remove_from_config
42+
build_app
43+
44+
config_file = "#{app_path}/config/application.rb"
45+
46+
assert_not_empty File.open(config_file, &:read)
47+
48+
add_to_config <<-RUBY
49+
config.zomg = 'zomg'
50+
RUBY
51+
52+
remove_from_config "config.zomg = 'zomg'"
53+
54+
config = File.open(config_file, &:read)
55+
56+
assert_no_match(/config\.zomg = 'zomg'$/, config, "Expected `#{config_file}` to include `config.zomg = 'zomg'`, but did not:\n #{config}")
57+
58+
add_to_config <<-RUBY
59+
config.duplicates = :none
60+
config.duplicates = :none
61+
RUBY
62+
63+
# removes all occurrences
64+
remove_from_config "config.duplicates = :none"
65+
66+
config = File.open(config_file, &:read)
67+
68+
assert_no_match(/config\.duplicates = :none$/, config, "Expected `#{config_file}` to include `config.duplicates = :none`, but did not:\n #{config}")
69+
end
70+
end
71+
end

0 commit comments

Comments
 (0)