File tree Expand file tree Collapse file tree 3 files changed +35
-2
lines changed
Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ def insert_libraries_configuration
4747 end
4848
4949 def setup_custom_configuration
50- return "" if options [ :custom ] . blank?
50+ return if options [ :custom ] . blank?
5151
5252 insert_custom_configuration
5353 create_custom_directory
@@ -58,7 +58,7 @@ def setup_custom_configuration
5858 INITIALIZER = "config/initializers/rails_icons.rb"
5959
6060 def insert_custom_configuration
61- unless File . read ( INITIALIZER ) . include? ( "config.libraries.merge!" )
61+ unless file_contains? ( INITIALIZER , "config.libraries.merge!" )
6262 custom_default_configuration = <<~RB . indent ( 2 )
6363 config.libraries.merge!(
6464
Original file line number Diff line number Diff line change @@ -11,10 +11,34 @@ def initialize(*arguments)
1111 private
1212
1313 def validate!
14+ return if custom_library?
15+
1416 raise RailsIcons ::LibraryNotFound . new ( "" ) if options . libraries . empty?
1517 raise RailsIcons ::LibraryNotFound . new ( invalid_libraries . join ( ", " ) ) if invalid_libraries . any?
1618 end
1719
1820 def invalid_libraries = options . libraries . map ( &:to_sym ) . map ( &:downcase ) . reject { RailsIcons . libraries . key? ( _1 ) }
21+
22+ def custom_library? = options . custom . present?
23+
24+ # Uses `gsub_file` as a read-only operation to check file content. This
25+ # approach is preferred over `File.read` because `gsub_file` is properly stubbed
26+ # in generator tests, while `File.read` would fail in the test environment.
27+ #
28+ def file_contains? ( path , content )
29+ return false unless File . exist? ( path )
30+
31+ result = false
32+
33+ gsub_file ( path , /.*/ ) do |file_content |
34+ result = file_content . include? ( content )
35+
36+ file_content
37+ end
38+
39+ result
40+ rescue Thor ::Error
41+ false
42+ end
1943 end
2044end
Original file line number Diff line number Diff line change @@ -74,6 +74,15 @@ class InitializerGeneratorTest < Rails::Generators::TestCase
7474 end
7575 end
7676
77+ test "generator creates the initializer with custom library" do
78+ run_generator %w[ --custom=simple_icons ]
79+
80+ assert_file "config/initializers/rails_icons.rb" do |file |
81+ assert_match "simple_icons" , file
82+ assert_match "config.libraries.merge!" , file
83+ end
84+ end
85+
7786 test "generator raise RailsIcons::LibraryNotFound when no library is specified" do
7887 assert_raises ( RailsIcons ::LibraryNotFound ) do
7988 run_generator
You can’t perform that action at this time.
0 commit comments