From 82f8f0cbdd6a67804ea5aed5b435f08f04df15ab Mon Sep 17 00:00:00 2001 From: Marc Heiligers Date: Wed, 1 Apr 2026 14:23:32 -0700 Subject: [PATCH] No auto_detect when forced_traits are passed --- lib/warbler/config.rb | 8 ++++---- lib/warbler/traits.rb | 10 ++++++---- spec/warbler/traits_spec.rb | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lib/warbler/config.rb b/lib/warbler/config.rb index 01dad970..9d2df9ed 100644 --- a/lib/warbler/config.rb +++ b/lib/warbler/config.rb @@ -30,8 +30,8 @@ class Config # Traits: an array of trait classes corresponding to # characteristics of the project that are either auto-detected or - # configured. - attr_accessor :traits + # forced enabled during `Config.new(forced_traits: [...]) do |config|` + attr_reader :traits # Directory where the war file will be written. Can be used to direct # Warbler to place your war file directly in your application server's @@ -183,8 +183,8 @@ class Config attr_reader :warbler_templates attr_reader :warbler_scripts - def initialize(warbler_home = WARBLER_HOME) - super() + def initialize(warbler_home = WARBLER_HOME, forced_traits: nil) + super(forced_traits) @warbler_home = warbler_home @warbler_templates = "#{WARBLER_HOME}/lib/warbler/templates" diff --git a/lib/warbler/traits.rb b/lib/warbler/traits.rb index 17d3499a..5c1471f7 100644 --- a/lib/warbler/traits.rb +++ b/lib/warbler/traits.rb @@ -15,10 +15,12 @@ module Warbler # the kind of project and how it should be packed into the jar or # war file. module Traits - attr_accessor :traits - - def initialize - @traits = auto_detect_traits + def initialize(forced_traits) + @traits = if forced_traits.nil? || forced_traits.empty? + auto_detect_traits + else + TraitsDependencyArray.new(forced_traits) + end end def auto_detect_traits diff --git a/spec/warbler/traits_spec.rb b/spec/warbler/traits_spec.rb index 0e3ff8e3..da5d6d73 100644 --- a/spec/warbler/traits_spec.rb +++ b/spec/warbler/traits_spec.rb @@ -18,4 +18,36 @@ end end end + + describe "#auto_detect_traits" do + context "in a Rack project with Bundler" do + run_in_directory 'spec/sample_bundler' + + it "auto-detects traits" do + config = Warbler::Config.new + expect(config.traits).to eq([Warbler::Traits::War, Warbler::Traits::Rack, Warbler::Traits::Bundler]) + end + end + end + + describe "forced_traits" do + context "with no forced traits" do + run_in_directory 'spec/sample_jar' + + it "behaves identically to auto-detection" do + default_config = Warbler::Config.new + forced_config = Warbler::Config.new(forced_traits: []) + expect(forced_config.traits).to eq(default_config.traits) + end + end + + context "in a Rack project with Bundler with Jar forced" do + run_in_directory 'spec/sample_bundler' + + it "only has the Jar trait" do + config = Warbler::Config.new(forced_traits: [Warbler::Traits::Jar]) + expect(config.traits).to eq([Warbler::Traits::Jar]) + end + end + end end