diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index f712b1ad..b5f167d6 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -153,7 +153,11 @@ def mock_column(name, type, options = {}) end before :each do - AnnotateModels.send(:parse_options, options) + AnnotateModels.parse_options(options) + end + + after :each do + AnnotateModels.parse_options({ skip_subdirectory_model_load: false }) end describe '@root_dir' do @@ -1647,6 +1651,10 @@ def mock_column(name, type, options = {}) Annotate::Helpers.true?(ENV['show_complete_foreign_keys']) end + after :each do + ENV.delete('show_complete_foreign_keys') + end + context 'when default value of "show_complete_foreign_keys" is not set' do it 'returns false' do is_expected.to be(false) @@ -1658,14 +1666,14 @@ def mock_column(name, type, options = {}) Annotate.set_defaults('show_complete_foreign_keys' => 'true') end + after do + Annotate.instance_variable_set('@has_set_defaults', false) + end + it 'returns true' do is_expected.to be(true) end end - - after :each do - ENV.delete('show_complete_foreign_keys') - end end describe '.get_patterns' do @@ -1815,8 +1823,14 @@ def mock_column(name, type, options = {}) end describe '.get_model_class' do - before :all do - AnnotateModels.model_dir = Dir.mktmpdir('annotate_models') + before :each do + @model_dir = Dir.mktmpdir('annotate_models') + AnnotateModels.model_dir = @model_dir + create(filename, file_content) + end + + after :each do + FileUtils.remove_dir(@model_dir, true) end # TODO: use 'files' gem instead @@ -1829,10 +1843,6 @@ def create(filename, file_content) end end - before :each do - create(filename, file_content) - end - let :klass do AnnotateModels.get_model_class(File.join(AnnotateModels.model_dir[0], filename)) end @@ -2113,7 +2123,9 @@ class Foo < ActiveRecord::Base let :file_content_2 do <<-EOS - class Bar::Foo < ActiveRecord::Base + module Bar + class Foo < ActiveRecord::Base + end end EOS end @@ -2146,7 +2158,9 @@ class Foo < ActiveRecord::Base let :file_content_2 do <<-EOS - class Bar::Foo < ActiveRecord::Base + module Bar + class Foo < ActiveRecord::Base + end end EOS end @@ -2163,6 +2177,7 @@ class Bar::Foo < ActiveRecord::Base it 'attempts to load the model path without expanding if skip_subdirectory_model_load is false' do allow(AnnotateModels).to receive(:skip_subdirectory_model_load).and_return(false) full_path = File.join(AnnotateModels.model_dir[0], filename_2) + Kernel.load(full_path) expect(File).to_not receive(:expand_path).with(full_path) AnnotateModels.get_model_class(full_path) end @@ -2171,6 +2186,7 @@ class Bar::Foo < ActiveRecord::Base $LOAD_PATH.unshift(AnnotateModels.model_dir[0]) allow(AnnotateModels).to receive(:skip_subdirectory_model_load).and_return(true) full_path = File.join(AnnotateModels.model_dir[0], filename_2) + Kernel.load(full_path) expect(File).to receive(:expand_path).with(full_path).and_call_original AnnotateModels.get_model_class(full_path) end @@ -2218,6 +2234,10 @@ class Foo < ActiveRecord::Base AnnotateModels.remove_annotation_of_file(path) end + after :each do + FileUtils.remove_dir(tmpdir, true) + end + let :tmpdir do Dir.mktmpdir('annotate_models') end @@ -2502,7 +2522,7 @@ class Foo < ActiveRecord::Base end describe 'annotating a file' do - before do + before :each do @model_dir = Dir.mktmpdir('annotate_models') (@model_file_name, @file_content) = write_model 'user.rb', <<~EOS class User < ActiveRecord::Base @@ -2519,6 +2539,10 @@ class User < ActiveRecord::Base Annotate::Helpers.reset_options(Annotate::Constants::ALL_ANNOTATE_OPTIONS) end + after :each do + FileUtils.remove_dir(@model_dir, true) + end + def write_model(file_name, file_content) fname = File.join(@model_dir, file_name) FileUtils.mkdir_p(File.dirname(fname)) @@ -2531,7 +2555,7 @@ def annotate_one_file(options = {}) Annotate.set_defaults(options) options = Annotate.setup_options(options) AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, options) - + ensure # Wipe settings so the next call will pick up new values... Annotate.instance_variable_set('@has_set_defaults', false) Annotate::Constants::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' }