diff --git a/gems/kettle-jem/lib/kettle/jem.rb b/gems/kettle-jem/lib/kettle/jem.rb index 7a41a3dda..49edd1ffb 100644 --- a/gems/kettle-jem/lib/kettle/jem.rb +++ b/gems/kettle-jem/lib/kettle/jem.rb @@ -9451,12 +9451,14 @@ def merge_ruby_template_source(file_type, recipe, template_content, destination_ end def merge_prism_gemfile_template_source(template_content, destination_content, recipe) + template_preference = recipe.fetch(:template_preference, {}) + add_template_only_nodes = DecisionPolicy.value_to_boolean(template_preference[:add_template_only_nodes]) Prism::Merge.merge_ruby( template_content, destination_content, "ruby", - preference: :template, - add_template_only_nodes: true, + preference: (template_preference[:preference] || "template").to_sym, + add_template_only_nodes: add_template_only_nodes.nil? ? true : add_template_only_nodes, signature_generator: Prism::Merge.ruby_dsl_signature_generator, freeze_token: ruby_merge_freeze_token(recipe) ) diff --git a/gems/kettle-jem/spec/integration/github_workflow_template_spec.rb b/gems/kettle-jem/spec/integration/github_workflow_template_spec.rb index 5f3b3c499..b7faafd4a 100644 --- a/gems/kettle-jem/spec/integration/github_workflow_template_spec.rb +++ b/gems/kettle-jem/spec/integration/github_workflow_template_spec.rb @@ -283,6 +283,37 @@ end end + it "honors file-level Gemfile preference for dependency requirements" do + tmp_root = File.expand_path("../tmp", __dir__) + FileUtils.mkdir_p(tmp_root) + Dir.mktmpdir("kettle-jem-gemfile-preference", tmp_root) do |root| + write_tree(root, { + "example.gemspec" => 'Gem::Specification.new { |spec| spec.name = "example"; spec.summary = "Example" }', + ".kettle-jem.yml" => <<~YAML, + defaults: + preference: template + templates: + root: template + apply: true + entries: + - Gemfile + files: + Gemfile: + strategy: merge + preference: destination + add_template_only_nodes: true + YAML + "Gemfile" => %(gem "kettle-family", "~> 1.3", ">= 1.3.38"\n), + "template/Gemfile.example" => %(gem "kettle-family", "~> 1.3", ">= 1.3.37"\ngem "extra-tool"\n) + }) + + described_class.apply_project(root, env: {}) + + expect(File.read(File.join(root, "Gemfile"))).to include(%(gem "kettle-family", "~> 1.3", ">= 1.3.38")) + expect(File.read(File.join(root, "Gemfile"))).to include(%(gem "extra-tool")) + end + end + it "strictly merges arbitrary top-level Gemfile dependencies from a template directory" do tmp_root = File.expand_path("../tmp", __dir__) FileUtils.mkdir_p(tmp_root)