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 a feature and method to override now #1244

Closed
wants to merge 2 commits into from

Conversation

pitdicker
Copy link
Collaborator

Updated version of #580. A comment in the related issue #105 (comment) has collected 28 thumb ups in three years. This may be our most popular feature request.

The only method added is Local::override_now(datetime: Option<DateTime<FixedOffset>>), and it affects Local::now() and Utc::now(). I made it a method on Local because it takes a datetime and an offset, while for Utc only a datetime would make sense.

The functionality is only available behind the test-override feature while running tests.

@codecov
Copy link

codecov bot commented Sep 1, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6f06e1a) 91.84% compared to head (481178e) 91.85%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1244   +/-   ##
=======================================
  Coverage   91.84%   91.85%           
=======================================
  Files          38       38           
  Lines       17518    17526    +8     
=======================================
+ Hits        16090    16098    +8     
  Misses       1428     1428           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pitdicker pitdicker force-pushed the test-override branch 8 times, most recently from 208eaa4 to 5272720 Compare September 1, 2023 19:19
@pitdicker
Copy link
Collaborator Author

I just wondered: cfg(test) makes this method available in unit tests, but is it also available in integration tests? Yes.

/// This method is only available behind the `test-override` feature, only works within the
/// current thread, and only in `cfg(test)`.
///
/// ```no_run
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Codecov correctly says the lines of this doctest are not covered. But I can't help it 😄.

@mgeisler
Copy link

Just a comment from a random bystander: instead of injecting this on a global level, people can opt-in code using a test double such as https://crates.io/crates/mockall_double. It does not "fix" existing code that uses chrono today, but it gives a more clean way to control the test setup.

@pitdicker
Copy link
Collaborator Author

I'm not going to push for this issue. Others can feel free to pick it up and/or advocate for it though.

@pitdicker pitdicker closed this Mar 18, 2024
@pitdicker pitdicker deleted the test-override branch March 18, 2024 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants