@@ -153,7 +153,11 @@ def mock_column(name, type, options = {})
153
153
end
154
154
155
155
before :each do
156
- AnnotateModels . send ( :parse_options , options )
156
+ AnnotateModels . parse_options ( options )
157
+ end
158
+
159
+ after :each do
160
+ AnnotateModels . parse_options ( { skip_subdirectory_model_load : false } )
157
161
end
158
162
159
163
describe '@root_dir' do
@@ -1647,6 +1651,10 @@ def mock_column(name, type, options = {})
1647
1651
Annotate ::Helpers . true? ( ENV [ 'show_complete_foreign_keys' ] )
1648
1652
end
1649
1653
1654
+ after :each do
1655
+ ENV . delete ( 'show_complete_foreign_keys' )
1656
+ end
1657
+
1650
1658
context 'when default value of "show_complete_foreign_keys" is not set' do
1651
1659
it 'returns false' do
1652
1660
is_expected . to be ( false )
@@ -1658,14 +1666,14 @@ def mock_column(name, type, options = {})
1658
1666
Annotate . set_defaults ( 'show_complete_foreign_keys' => 'true' )
1659
1667
end
1660
1668
1669
+ after do
1670
+ Annotate . instance_variable_set ( '@has_set_defaults' , false )
1671
+ end
1672
+
1661
1673
it 'returns true' do
1662
1674
is_expected . to be ( true )
1663
1675
end
1664
1676
end
1665
-
1666
- after :each do
1667
- ENV . delete ( 'show_complete_foreign_keys' )
1668
- end
1669
1677
end
1670
1678
1671
1679
describe '.get_patterns' do
@@ -1815,8 +1823,14 @@ def mock_column(name, type, options = {})
1815
1823
end
1816
1824
1817
1825
describe '.get_model_class' do
1818
- before :all do
1819
- AnnotateModels . model_dir = Dir . mktmpdir ( 'annotate_models' )
1826
+ before :each do
1827
+ @model_dir = Dir . mktmpdir ( 'annotate_models' )
1828
+ AnnotateModels . model_dir = @model_dir
1829
+ create ( filename , file_content )
1830
+ end
1831
+
1832
+ after :each do
1833
+ FileUtils . remove_dir ( @model_dir , true )
1820
1834
end
1821
1835
1822
1836
# TODO: use 'files' gem instead
@@ -1829,10 +1843,6 @@ def create(filename, file_content)
1829
1843
end
1830
1844
end
1831
1845
1832
- before :each do
1833
- create ( filename , file_content )
1834
- end
1835
-
1836
1846
let :klass do
1837
1847
AnnotateModels . get_model_class ( File . join ( AnnotateModels . model_dir [ 0 ] , filename ) )
1838
1848
end
@@ -2113,7 +2123,9 @@ class Foo < ActiveRecord::Base
2113
2123
2114
2124
let :file_content_2 do
2115
2125
<<-EOS
2116
- class Bar::Foo < ActiveRecord::Base
2126
+ module Bar
2127
+ class Foo < ActiveRecord::Base
2128
+ end
2117
2129
end
2118
2130
EOS
2119
2131
end
@@ -2146,7 +2158,9 @@ class Foo < ActiveRecord::Base
2146
2158
2147
2159
let :file_content_2 do
2148
2160
<<-EOS
2149
- class Bar::Foo < ActiveRecord::Base
2161
+ module Bar
2162
+ class Foo < ActiveRecord::Base
2163
+ end
2150
2164
end
2151
2165
EOS
2152
2166
end
@@ -2163,6 +2177,7 @@ class Bar::Foo < ActiveRecord::Base
2163
2177
it 'attempts to load the model path without expanding if skip_subdirectory_model_load is false' do
2164
2178
allow ( AnnotateModels ) . to receive ( :skip_subdirectory_model_load ) . and_return ( false )
2165
2179
full_path = File . join ( AnnotateModels . model_dir [ 0 ] , filename_2 )
2180
+ Kernel . load ( full_path )
2166
2181
expect ( File ) . to_not receive ( :expand_path ) . with ( full_path )
2167
2182
AnnotateModels . get_model_class ( full_path )
2168
2183
end
@@ -2171,6 +2186,7 @@ class Bar::Foo < ActiveRecord::Base
2171
2186
$LOAD_PATH. unshift ( AnnotateModels . model_dir [ 0 ] )
2172
2187
allow ( AnnotateModels ) . to receive ( :skip_subdirectory_model_load ) . and_return ( true )
2173
2188
full_path = File . join ( AnnotateModels . model_dir [ 0 ] , filename_2 )
2189
+ Kernel . load ( full_path )
2174
2190
expect ( File ) . to receive ( :expand_path ) . with ( full_path ) . and_call_original
2175
2191
AnnotateModels . get_model_class ( full_path )
2176
2192
end
@@ -2502,7 +2518,7 @@ class Foo < ActiveRecord::Base
2502
2518
end
2503
2519
2504
2520
describe 'annotating a file' do
2505
- before do
2521
+ before :each do
2506
2522
@model_dir = Dir . mktmpdir ( 'annotate_models' )
2507
2523
( @model_file_name , @file_content ) = write_model 'user.rb' , <<~EOS
2508
2524
class User < ActiveRecord::Base
@@ -2519,6 +2535,10 @@ class User < ActiveRecord::Base
2519
2535
Annotate ::Helpers . reset_options ( Annotate ::Constants ::ALL_ANNOTATE_OPTIONS )
2520
2536
end
2521
2537
2538
+ after :each do
2539
+ FileUtils . remove_dir ( @model_dir , true )
2540
+ end
2541
+
2522
2542
def write_model ( file_name , file_content )
2523
2543
fname = File . join ( @model_dir , file_name )
2524
2544
FileUtils . mkdir_p ( File . dirname ( fname ) )
@@ -2531,7 +2551,7 @@ def annotate_one_file(options = {})
2531
2551
Annotate . set_defaults ( options )
2532
2552
options = Annotate . setup_options ( options )
2533
2553
AnnotateModels . annotate_one_file ( @model_file_name , @schema_info , :position_in_class , options )
2534
-
2554
+ ensure
2535
2555
# Wipe settings so the next call will pick up new values...
2536
2556
Annotate . instance_variable_set ( '@has_set_defaults' , false )
2537
2557
Annotate ::Constants ::POSITION_OPTIONS . each { |key | ENV [ key . to_s ] = '' }
0 commit comments