Skip to content
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

Add option to skip call to purge_before_load in parallel::load_schema and parallel::load_structure #529

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion lib/parallel_tests/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ def rails_env
ENV['RAILS_ENV'] || 'test'
end

def purge_before_load?
ENV.fetch('PURGE_BEFORE_LOAD', 'TRUE').downcase == 'false' ? false : true
end

def purge_before_load
"db:test:purge" if Gem::Version.new(Rails.version) > Gem::Version.new('4.2.0')
"db:test:purge" if purge_before_load? && Gem::Version.new(Rails.version) > Gem::Version.new('4.2.0')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cannot use Rails.version since this might be used without rails ... ActiveRecord::Version::STRING should work

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this be used without Rails? All tasks that call this method depend on Rails' rake tasks

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always though as this only needing ActiveRecord to work ... tasks come from https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake ... which is a railtie ... adding rails to the mix seems to still be unnecessary to me ... maybe ActiveRecord.version => #<Gem::Version "5.0.0.1">

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bah ... does not work on rails 3 ... maybe ActiveRecord::VERSION::STRING is best ...

end

def run_in_parallel(cmd, options={})
Expand Down
36 changes: 36 additions & 0 deletions spec/parallel_tests/tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,42 @@
end
end

describe '.purge_before_load?' do
subject { ParallelTests::Tasks.purge_before_load? }
context "PURGE_BEFORE_LOAD is set to 'false'" do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I'd test this like so: (less nesting / context switching and 1 change per test)

it "is true" do
  exoect(subject).to eq true
end

it "is false when set to false" do
  with_env(PURGE_BEFORE_LOAD: 'false') do
    expect(subject).to eq false
  end
end

it "is true when set to true" do
  with_env(PURGE_BEFORE_LOAD: 'true') do
    expect(subject).to eq true
  end
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll refactor it. However, I prefer to use expect(ENV) to set envs because it will fail if the env doesn't get used anymore because someone changed the code.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just FYI either way works :)

before { expect(ENV).to receive(:fetch).with('PURGE_BEFORE_LOAD', 'TRUE').and_return('false') }

it 'returns false' do
expect(subject).to be_falsey
end
end

context 'PURGE_BEFORE_LOAD is not set' do

it 'defaults to true' do
expect(subject).to be_truthy
end
end
end

describe '.purge_before_load' do
subject { ParallelTests::Tasks.purge_before_load }
context 'Rails version is 4.3.1' do
before do
class Rails; end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defining a class at runtime is a bad idea :D

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you stub the call to Rails.version instead?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define it global ... and then either add it there or stub it ...

allow(Rails).to receive(:version).and_return('4.3.1')
end

context "PURGE_BEFORE_LOAD is set to 'false'" do
before { expect(ENV).to receive(:fetch).with('PURGE_BEFORE_LOAD', 'TRUE').and_return('false') }

it 'returns nil' do
expect(subject).to be_nil
end
end
end
end

describe ".rails_env" do
it "should be test when nothing was set" do
expect(ParallelTests::Tasks.rails_env).to eq("test")
Expand Down