Skip to content

Fix flaky specs #980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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] = '' }
Expand Down