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

Don't try to mock context manager; use a simple class instead #39

Merged
merged 1 commit into from
Jan 25, 2025

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Jan 25, 2025

This one's related to #38. It turns out that while trying to mock a
context manager kind of works, it will do the wrong thing in edge cases
like when an exception is thrown from inside a with block, silently
swallowing it and causing a return that's completely wrong.

There may be some way to fix the mock to make it do the right thing, but
instead of getting fancier with these mocks that are already awful,
instead repair the problem by defining a plain class that implements
context manager and just use that.

@brandur brandur force-pushed the brandur-no-context-manager-mock-2 branch 2 times, most recently from 7d5d75e to b91a059 Compare January 25, 2025 22:43
@@ -63,7 +63,7 @@ jobs:
run: psql --echo-errors --quiet -c '\timing off' -c "CREATE DATABASE ${TEST_DATABASE_NAME};" ${ADMIN_DATABASE_URL}

- name: river migrate-up
run: river migrate-up --database-url "$TEST_DATABASE_URL"
run: river migrate-up --database-url "$DATABASE_URL" --max-steps 5 # temporarily include max steps so tests can pass with unique fixes
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change needed for tests to pass, but should be reverted in #38.

This one's related to #38. It turns out that while trying to mock a
context manager kind of works, it will do the wrong thing in edge cases
like when an exception is thrown from inside a `with` block, silently
swallowing it and causing a return that's completely wrong.

There may be some way to fix the mock to make it do the right thing, but
instead of getting fancier with these mocks that are already awful,
instead repair the problem by defining a plain class that implements
context manager and just use that.
@brandur brandur force-pushed the brandur-no-context-manager-mock-2 branch from b91a059 to a01790c Compare January 25, 2025 22:45
@brandur brandur requested a review from bgentry January 25, 2025 22:47
@brandur brandur mentioned this pull request Jan 25, 2025
4 tasks
@@ -63,7 +63,7 @@ jobs:
run: psql --echo-errors --quiet -c '\timing off' -c "CREATE DATABASE ${TEST_DATABASE_NAME};" ${ADMIN_DATABASE_URL}

- name: river migrate-up
run: river migrate-up --database-url "$TEST_DATABASE_URL"
run: river migrate-up --database-url "$TEST_DATABASE_URL" --max-steps 5 # temporarily include max steps so tests can pass with unique fixes
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change needed for tests to pass, but should be reverted in #38.

Copy link

@bgentry bgentry left a comment

Choose a reason for hiding this comment

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

That explains what I was running into, thanks!

@brandur
Copy link
Contributor Author

brandur commented Jan 25, 2025

thx.

@brandur brandur merged commit 580a39c into master Jan 25, 2025
4 checks passed
@brandur brandur deleted the brandur-no-context-manager-mock-2 branch January 25, 2025 22:58
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.

2 participants