@@ -153,7 +153,11 @@ def mock_column(name, type, options = {})
153153 end
154154
155155 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 } )
157161 end
158162
159163 describe '@root_dir' do
@@ -1647,6 +1651,10 @@ def mock_column(name, type, options = {})
16471651 Annotate ::Helpers . true? ( ENV [ 'show_complete_foreign_keys' ] )
16481652 end
16491653
1654+ after :each do
1655+ ENV . delete ( 'show_complete_foreign_keys' )
1656+ end
1657+
16501658 context 'when default value of "show_complete_foreign_keys" is not set' do
16511659 it 'returns false' do
16521660 is_expected . to be ( false )
@@ -1658,14 +1666,14 @@ def mock_column(name, type, options = {})
16581666 Annotate . set_defaults ( 'show_complete_foreign_keys' => 'true' )
16591667 end
16601668
1669+ after do
1670+ Annotate . instance_variable_set ( '@has_set_defaults' , false )
1671+ end
1672+
16611673 it 'returns true' do
16621674 is_expected . to be ( true )
16631675 end
16641676 end
1665-
1666- after :each do
1667- ENV . delete ( 'show_complete_foreign_keys' )
1668- end
16691677 end
16701678
16711679 describe '.get_patterns' do
@@ -1815,8 +1823,14 @@ def mock_column(name, type, options = {})
18151823 end
18161824
18171825 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 )
18201834 end
18211835
18221836 # TODO: use 'files' gem instead
@@ -1829,10 +1843,6 @@ def create(filename, file_content)
18291843 end
18301844 end
18311845
1832- before :each do
1833- create ( filename , file_content )
1834- end
1835-
18361846 let :klass do
18371847 AnnotateModels . get_model_class ( File . join ( AnnotateModels . model_dir [ 0 ] , filename ) )
18381848 end
@@ -2113,7 +2123,9 @@ class Foo < ActiveRecord::Base
21132123
21142124 let :file_content_2 do
21152125 <<-EOS
2116- class Bar::Foo < ActiveRecord::Base
2126+ module Bar
2127+ class Foo < ActiveRecord::Base
2128+ end
21172129 end
21182130 EOS
21192131 end
@@ -2146,7 +2158,9 @@ class Foo < ActiveRecord::Base
21462158
21472159 let :file_content_2 do
21482160 <<-EOS
2149- class Bar::Foo < ActiveRecord::Base
2161+ module Bar
2162+ class Foo < ActiveRecord::Base
2163+ end
21502164 end
21512165 EOS
21522166 end
@@ -2163,6 +2177,7 @@ class Bar::Foo < ActiveRecord::Base
21632177 it 'attempts to load the model path without expanding if skip_subdirectory_model_load is false' do
21642178 allow ( AnnotateModels ) . to receive ( :skip_subdirectory_model_load ) . and_return ( false )
21652179 full_path = File . join ( AnnotateModels . model_dir [ 0 ] , filename_2 )
2180+ Kernel . load ( full_path )
21662181 expect ( File ) . to_not receive ( :expand_path ) . with ( full_path )
21672182 AnnotateModels . get_model_class ( full_path )
21682183 end
@@ -2171,6 +2186,7 @@ class Bar::Foo < ActiveRecord::Base
21712186 $LOAD_PATH. unshift ( AnnotateModels . model_dir [ 0 ] )
21722187 allow ( AnnotateModels ) . to receive ( :skip_subdirectory_model_load ) . and_return ( true )
21732188 full_path = File . join ( AnnotateModels . model_dir [ 0 ] , filename_2 )
2189+ Kernel . load ( full_path )
21742190 expect ( File ) . to receive ( :expand_path ) . with ( full_path ) . and_call_original
21752191 AnnotateModels . get_model_class ( full_path )
21762192 end
@@ -2218,6 +2234,10 @@ class Foo < ActiveRecord::Base
22182234 AnnotateModels . remove_annotation_of_file ( path )
22192235 end
22202236
2237+ after :each do
2238+ FileUtils . remove_dir ( tmpdir , true )
2239+ end
2240+
22212241 let :tmpdir do
22222242 Dir . mktmpdir ( 'annotate_models' )
22232243 end
@@ -2502,7 +2522,7 @@ class Foo < ActiveRecord::Base
25022522 end
25032523
25042524 describe 'annotating a file' do
2505- before do
2525+ before :each do
25062526 @model_dir = Dir . mktmpdir ( 'annotate_models' )
25072527 ( @model_file_name , @file_content ) = write_model 'user.rb' , <<~EOS
25082528 class User < ActiveRecord::Base
@@ -2519,6 +2539,10 @@ class User < ActiveRecord::Base
25192539 Annotate ::Helpers . reset_options ( Annotate ::Constants ::ALL_ANNOTATE_OPTIONS )
25202540 end
25212541
2542+ after :each do
2543+ FileUtils . remove_dir ( @model_dir , true )
2544+ end
2545+
25222546 def write_model ( file_name , file_content )
25232547 fname = File . join ( @model_dir , file_name )
25242548 FileUtils . mkdir_p ( File . dirname ( fname ) )
@@ -2531,7 +2555,7 @@ def annotate_one_file(options = {})
25312555 Annotate . set_defaults ( options )
25322556 options = Annotate . setup_options ( options )
25332557 AnnotateModels . annotate_one_file ( @model_file_name , @schema_info , :position_in_class , options )
2534-
2558+ ensure
25352559 # Wipe settings so the next call will pick up new values...
25362560 Annotate . instance_variable_set ( '@has_set_defaults' , false )
25372561 Annotate ::Constants ::POSITION_OPTIONS . each { |key | ENV [ key . to_s ] = '' }
0 commit comments