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

Improve railties' tests #30595

Merged
merged 3 commits into from
Oct 9, 2017
Merged

Conversation

bogdanvlviv
Copy link
Contributor

@bogdanvlviv bogdanvlviv commented Sep 13, 2017

  • Remove redundant execution of Dir.chdir(app_path) { } in railties' tests
  • Improve RakeTest#test_not_protected_when_previous_migration_was_not_production
    • Allow failure for db:test:prepare to ensure the test woun't raise error:
      RuntimeError: rails command failed (1): bin/rails db:test:prepare 2>&1
    • Prevent redundant execution of bin/rails test inside the test

/cc @matthewd

@rails-bot
Copy link

r? @eileencodes

(@rails-bot has picked a reviewer for you, use r? to override)

Copy link
Member

@matthewd matthewd left a comment

Choose a reason for hiding this comment

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

There are several very different changes going on in a single commit here, and instead of describing any of them, the title of the commit is just copied from the other PR.

@bogdanvlviv bogdanvlviv force-pushed the use-railties-rails-command branch 3 times, most recently from 135782d to 97c9ebe Compare September 14, 2017 09:33
@bogdanvlviv bogdanvlviv changed the title Run in-app rails commands via fork+load where possible. Follow #30520 Improve railties' tests Sep 14, 2017
@bogdanvlviv
Copy link
Contributor Author

@matthewd Thanks for the correcting, I just split up changes to commits with explanations.

@@ -40,7 +40,7 @@ def test_not_protected_when_previous_migration_was_not_production
with_rails_env "test" do
rails "generate", "model", "product", "name:string"
rails "db:create", "db:migrate"
output = Dir.chdir(app_path) { rails("db:test:prepare", "test") }
output = rails("db:test:prepare", allow_failure: true)
Copy link
Member

Choose a reason for hiding this comment

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

Is this currently raising? What failure do we need to allow? 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If for some reason db:test:prepare fails, the code below

refute_match(/ActiveRecord::ProtectedEnvironmentError/, output)

won't be executed

instead we will see error

RuntimeError: rails command failed (1): bin/rails db:test:prepare 2>&1

Copy link
Member

Choose a reason for hiding this comment

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

Why would that be a bad thing?

If the command fails, I want the test to fail, not pass just because the failure doesn't contain that string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because if leave it as it is and db:test:prepare fails for some reason(for example: adding bad changes in a PR)

this line will never be executed

refute_match(/ActiveRecord::ProtectedEnvironmentError/, output)

so if db:test:prepare raise ActiveRecord::ProtectedEnvironmentError or any antother error, test status will be as error but not as failure or passed.

In this way, this test doesn't check what we expect.

Copy link
Member

Choose a reason for hiding this comment

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

If there is an error in that command, then an error has occurred, and so 'error' would be the correct result for the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matthewd I just removed the changes

@@ -40,7 +40,7 @@ def test_not_protected_when_previous_migration_was_not_production
with_rails_env "test" do
rails "generate", "model", "product", "name:string"
rails "db:create", "db:migrate"
output = Dir.chdir(app_path) { rails("db:test:prepare", "test") }
output = rails("db:test:prepare")
Copy link
Member

Choose a reason for hiding this comment

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

Again, I think this is important to ensure test actually works, because our only assertion is a loose non-match.

It's really not good to make changes to tests just because you don't think something needs to be there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matthewd do we have to return back execution of bin/rails test that was removed by https://github.com/rails/rails/pull/30520/files#diff-e1c35224a4df7f73d90a9a90e45aab2bL32?

Copy link
Member

Choose a reason for hiding this comment

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

No. That one is never reached, because the prepare fails. And the following assertion is positive ("this must be there"), not negative ("this specific string should not be").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I deleted the changes

@bogdanvlviv
Copy link
Contributor Author

@matthewd This PR is ready for review.

@eileencodes eileencodes assigned matthewd and unassigned eileencodes Oct 8, 2017
assert system("bin/rails do_nothing RAILS_ENV=production"),
"should not be pre-required for rake even eager_load=true"
end
assert rails("do_nothing" "RAILS_ENV=production"),
Copy link
Member

Choose a reason for hiding this comment

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

Asserting on a value that's always true isn't very useful.

.. and this is missing a comma, so it's also not running the command it intends to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matthewd Should I just leave rails("do_nothing", "RAILS_ENV=production") without assert?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm.. maybe we should make do_nothing do a puts, then assert on that. That way it would've caught the missing comma.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm.. maybe we should make do_nothing do a puts, then assert on that. That way it would've caught the missing comma.

Good idea! Done.

@matthewd matthewd merged commit ac1ee51 into rails:master Oct 9, 2017
@bogdanvlviv bogdanvlviv deleted the use-railties-rails-command branch October 9, 2017 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants