Skip to content

Commit 41d1ea4

Browse files
committed
Fix Pro generator Gemfile swap duplicate and indentation
1 parent 7be4509 commit 41d1ea4

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

react_on_rails/lib/generators/react_on_rails/pro_generator.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,16 @@ def swap_base_gem_for_pro_in_gemfile
8282
return unless File.exist?(gemfile_path)
8383

8484
gemfile_content = File.read(gemfile_path)
85+
pro_gem_line_pattern = /^\s*gem\s+["']react_on_rails_pro["'][^\n]*$/
86+
pro_gem_already_present = gemfile_content.match?(pro_gem_line_pattern)
8587
updated_content = gemfile_content.gsub(
86-
/^\s*gem\s+["']react_on_rails["'][^\n]*$/,
87-
"gem 'react_on_rails_pro', '#{recommended_pro_gem_version}'"
88+
/^(\s*)gem\s+["']react_on_rails["'][^\n]*$/
89+
) do
90+
if pro_gem_already_present
91+
""
92+
else
93+
"#{Regexp.last_match(1)}gem 'react_on_rails_pro', '#{recommended_pro_gem_version}'"
94+
end
8895
)
8996
return if updated_content == gemfile_content
9097

react_on_rails/spec/react_on_rails/generators/pro_generator_spec.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
describe "#swap_base_gem_for_pro_in_gemfile" do
5959
let(:generator) { described_class.new }
6060
let(:gemfile_path) { File.join(destination_root, "Gemfile") }
61+
let(:expected_version) { Gem::Version.new(ReactOnRails::VERSION).release.to_s }
6162

6263
before do
6364
prepare_destination
@@ -74,12 +75,44 @@
7475
generator.send(:swap_base_gem_for_pro_in_gemfile)
7576

7677
gemfile_content = File.read(gemfile_path)
77-
expected_version = Gem::Version.new(ReactOnRails::VERSION).release.to_s
7878
expect(gemfile_content).to include("gem 'react_on_rails_pro', '#{expected_version}'")
7979
expect(gemfile_content).not_to match(/gem\s+["']react_on_rails["']/)
8080
expect(generator).to have_received(:bundle_install_after_gem_swap)
8181
end
8282

83+
it "removes react_on_rails when react_on_rails_pro is already present" do
84+
simulate_existing_file("Gemfile", <<~RUBY)
85+
source "https://rubygems.org"
86+
gem "react_on_rails", "~> 16.0"
87+
gem "react_on_rails_pro", "~> 16.0"
88+
RUBY
89+
allow(generator).to receive(:bundle_install_after_gem_swap)
90+
91+
generator.send(:swap_base_gem_for_pro_in_gemfile)
92+
93+
gemfile_content = File.read(gemfile_path)
94+
pro_gem_lines = gemfile_content.scan(/^\s*gem\s+["']react_on_rails_pro["'][^\n]*$/)
95+
expect(pro_gem_lines.length).to eq(1)
96+
expect(gemfile_content).not_to match(/gem\s+["']react_on_rails["']/)
97+
expect(generator).to have_received(:bundle_install_after_gem_swap)
98+
end
99+
100+
it "preserves leading whitespace when replacing indented react_on_rails entry" do
101+
simulate_existing_file("Gemfile", <<~RUBY)
102+
source "https://rubygems.org"
103+
104+
group :production do
105+
gem "react_on_rails", "~> 16.0"
106+
end
107+
RUBY
108+
allow(generator).to receive(:bundle_install_after_gem_swap)
109+
110+
generator.send(:swap_base_gem_for_pro_in_gemfile)
111+
112+
gemfile_content = File.read(gemfile_path)
113+
expect(gemfile_content).to include(" gem 'react_on_rails_pro', '#{expected_version}'")
114+
end
115+
83116
it "does nothing when Gemfile has no react_on_rails entry" do
84117
simulate_existing_file("Gemfile", <<~RUBY)
85118
source "https://rubygems.org"

0 commit comments

Comments
 (0)