Unverified Commit 76a18043 by Lovro Bikić Committed by GitHub

Fix flaky specs (#980)

parent ae0b79c0
...@@ -153,7 +153,11 @@ describe AnnotateModels do ...@@ -153,7 +153,11 @@ describe AnnotateModels do
end end
before :each do 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 end
describe '@root_dir' do describe '@root_dir' do
...@@ -1647,6 +1651,10 @@ describe AnnotateModels do ...@@ -1647,6 +1651,10 @@ describe AnnotateModels do
Annotate::Helpers.true?(ENV['show_complete_foreign_keys']) Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])
end end
after :each do
ENV.delete('show_complete_foreign_keys')
end
context 'when default value of "show_complete_foreign_keys" is not set' do context 'when default value of "show_complete_foreign_keys" is not set' do
it 'returns false' do it 'returns false' do
is_expected.to be(false) is_expected.to be(false)
...@@ -1658,14 +1666,14 @@ describe AnnotateModels do ...@@ -1658,14 +1666,14 @@ describe AnnotateModels do
Annotate.set_defaults('show_complete_foreign_keys' => 'true') Annotate.set_defaults('show_complete_foreign_keys' => 'true')
end end
after do
Annotate.instance_variable_set('@has_set_defaults', false)
end
it 'returns true' do it 'returns true' do
is_expected.to be(true) is_expected.to be(true)
end end
end end
after :each do
ENV.delete('show_complete_foreign_keys')
end
end end
describe '.get_patterns' do describe '.get_patterns' do
...@@ -1815,8 +1823,14 @@ describe AnnotateModels do ...@@ -1815,8 +1823,14 @@ describe AnnotateModels do
end end
describe '.get_model_class' do describe '.get_model_class' do
before :all do before :each do
AnnotateModels.model_dir = Dir.mktmpdir('annotate_models') @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 end
# TODO: use 'files' gem instead # TODO: use 'files' gem instead
...@@ -1829,10 +1843,6 @@ describe AnnotateModels do ...@@ -1829,10 +1843,6 @@ describe AnnotateModels do
end end
end end
before :each do
create(filename, file_content)
end
let :klass do let :klass do
AnnotateModels.get_model_class(File.join(AnnotateModels.model_dir[0], filename)) AnnotateModels.get_model_class(File.join(AnnotateModels.model_dir[0], filename))
end end
...@@ -2113,7 +2123,9 @@ describe AnnotateModels do ...@@ -2113,7 +2123,9 @@ describe AnnotateModels do
let :file_content_2 do let :file_content_2 do
<<-EOS <<-EOS
class Bar::Foo < ActiveRecord::Base module Bar
class Foo < ActiveRecord::Base
end
end end
EOS EOS
end end
...@@ -2146,7 +2158,9 @@ describe AnnotateModels do ...@@ -2146,7 +2158,9 @@ describe AnnotateModels do
let :file_content_2 do let :file_content_2 do
<<-EOS <<-EOS
class Bar::Foo < ActiveRecord::Base module Bar
class Foo < ActiveRecord::Base
end
end end
EOS EOS
end end
...@@ -2163,6 +2177,7 @@ describe AnnotateModels do ...@@ -2163,6 +2177,7 @@ describe AnnotateModels do
it 'attempts to load the model path without expanding if skip_subdirectory_model_load is false' do 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) allow(AnnotateModels).to receive(:skip_subdirectory_model_load).and_return(false)
full_path = File.join(AnnotateModels.model_dir[0], filename_2) full_path = File.join(AnnotateModels.model_dir[0], filename_2)
Kernel.load(full_path)
expect(File).to_not receive(:expand_path).with(full_path) expect(File).to_not receive(:expand_path).with(full_path)
AnnotateModels.get_model_class(full_path) AnnotateModels.get_model_class(full_path)
end end
...@@ -2171,6 +2186,7 @@ describe AnnotateModels do ...@@ -2171,6 +2186,7 @@ describe AnnotateModels do
$LOAD_PATH.unshift(AnnotateModels.model_dir[0]) $LOAD_PATH.unshift(AnnotateModels.model_dir[0])
allow(AnnotateModels).to receive(:skip_subdirectory_model_load).and_return(true) allow(AnnotateModels).to receive(:skip_subdirectory_model_load).and_return(true)
full_path = File.join(AnnotateModels.model_dir[0], filename_2) 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 expect(File).to receive(:expand_path).with(full_path).and_call_original
AnnotateModels.get_model_class(full_path) AnnotateModels.get_model_class(full_path)
end end
...@@ -2218,6 +2234,10 @@ describe AnnotateModels do ...@@ -2218,6 +2234,10 @@ describe AnnotateModels do
AnnotateModels.remove_annotation_of_file(path) AnnotateModels.remove_annotation_of_file(path)
end end
after :each do
FileUtils.remove_dir(tmpdir, true)
end
let :tmpdir do let :tmpdir do
Dir.mktmpdir('annotate_models') Dir.mktmpdir('annotate_models')
end end
...@@ -2502,7 +2522,7 @@ describe AnnotateModels do ...@@ -2502,7 +2522,7 @@ describe AnnotateModels do
end end
describe 'annotating a file' do describe 'annotating a file' do
before do before :each do
@model_dir = Dir.mktmpdir('annotate_models') @model_dir = Dir.mktmpdir('annotate_models')
(@model_file_name, @file_content) = write_model 'user.rb', <<~EOS (@model_file_name, @file_content) = write_model 'user.rb', <<~EOS
class User < ActiveRecord::Base class User < ActiveRecord::Base
...@@ -2519,6 +2539,10 @@ describe AnnotateModels do ...@@ -2519,6 +2539,10 @@ describe AnnotateModels do
Annotate::Helpers.reset_options(Annotate::Constants::ALL_ANNOTATE_OPTIONS) Annotate::Helpers.reset_options(Annotate::Constants::ALL_ANNOTATE_OPTIONS)
end end
after :each do
FileUtils.remove_dir(@model_dir, true)
end
def write_model(file_name, file_content) def write_model(file_name, file_content)
fname = File.join(@model_dir, file_name) fname = File.join(@model_dir, file_name)
FileUtils.mkdir_p(File.dirname(fname)) FileUtils.mkdir_p(File.dirname(fname))
...@@ -2531,7 +2555,7 @@ describe AnnotateModels do ...@@ -2531,7 +2555,7 @@ describe AnnotateModels do
Annotate.set_defaults(options) Annotate.set_defaults(options)
options = Annotate.setup_options(options) options = Annotate.setup_options(options)
AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, 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... # Wipe settings so the next call will pick up new values...
Annotate.instance_variable_set('@has_set_defaults', false) Annotate.instance_variable_set('@has_set_defaults', false)
Annotate::Constants::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' } Annotate::Constants::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment