Skip to content

Update unit tests for non-UTC windows environments #11265

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 7 commits into from
Jul 19, 2022
Merged

Conversation

finnagin
Copy link
Contributor

@finnagin finnagin commented Jul 15, 2022

Some of the unit tests seem to fail when run on some windows environments that are not configured with the UTC timezone. I believe this may be due to time.tzset() not being implemented in windows and so this code does not get run in conftest.py during the tests causing the time zone to not be updated.

I've tested this on two different machines, one running Windows 10 and the other running Windows 11. In both cases I used python 3.10, set the time zone to something other than UTC, and run the tests using nox (version 2022.1.7) with the following command: nox -s test-3.10 -- -m unit -n auto. This resulted the the following failing tests:

FAILED tests/unit/test_base_command.py::test_log_command_success - AssertionError: assert '2019-01-16T22:00:37,040 fa...
FAILED tests/unit/test_base_command.py::test_log_file_command_error - assert False
FAILED tests/unit/test_base_command.py::test_log_command_error - assert False
FAILED tests/unit/test_logging.py::TestIndentingFormatter::test_format_with_timestamp[INFO-2019-01-17T06:00:37,040 hello\n2019-01-17T06:00:37,040 world]
FAILED tests/unit/test_logging.py::TestIndentingFormatter::test_format_with_timestamp[WARNING-2019-01-17T06:00:37,040 WARNING: hello\n2019-01-17T06:00:37,040 world]

All tests seem to be failing due to the time stamp in the log file being wrong. For example, the error I get from test_log_command_success when my time zone is set to PST is:

>           assert f.read().rstrip() == "2019-01-17T06:00:37,040 fake"
E           AssertionError: assert '2019-01-16T22:00:37,040 fake' == '2019-01-17T06:00:37,040 fake'
E             - 2019-01-17T06:00:37,040 fake
E             ?          ^ ^^
E             + 2019-01-16T22:00:37,040 fake
E             ?          ^ ^^

Which is off by the 8 hour difference between PST and UTC. When I changed my system's time zone to UTC in both cases the tests then pass.

My proposed fix is to add the value time.timezone to the fixed time values in both test_base_command.py and test_logging.py. In the cases where the timezone was correctly set to UTC this value will be 0 and thus not change fixed time value. However, in the cases where the timezone was not correctly set to UTC this value will be non-zero and update the test's fixed time value to match the time as if it were in UTC.

fixes #10816
closes #10834
closes #10835

@sbidoul
Copy link
Member

sbidoul commented Jul 16, 2022

Note there is already an issue (#10816) and a couple of PRs (#10834, #10835) that seem to address the same topic.

@notatallshaw
Copy link
Member

Note there is already an issue (#10816) and a couple of PRs (#10834, #10835) that seem to address the same topic.

I'd be more than happy to close my issues and PRs based on this PR landing.

@finnagin
Copy link
Contributor Author

@sbidoul I'm happy if either fix ends up getting meged.

@sbidoul
Copy link
Member

sbidoul commented Jul 18, 2022

This PR has a smaller diff so it's more attractive to lazy reviewers :)

@finnagin maybe we can drop the utc fixture completely ? Would you mind trying that ?

@finnagin
Copy link
Contributor Author

@sbidoul Sure!

@finnagin
Copy link
Contributor Author

@sbidoul I've removed the utc fixture and the references to it in the tests. Looks to still be working.

I also went ahead and ran it locally as well as in a different branch with a modified github actions workflow to try a different timezone in the Linux, MacOS, & Windows environments and it passed the tests there as well.

Copy link
Member

@sbidoul sbidoul left a comment

Choose a reason for hiding this comment

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

Thanks!

@uranusjr uranusjr merged commit b1a01ef into pypa:main Jul 19, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some unit tests are sensitive to Timezone of host machine
4 participants