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

DRAFT: refactor: COLOR_MODE to OnceCell #365

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

bconn98
Copy link
Collaborator

@bconn98 bconn98 commented Apr 2, 2024

Refactor COLOR_MODE to be easier for testing and for using features in the standard library come v1.71.0.

Addresses: #368

@bconn98 bconn98 requested review from estk and gadunga as code owners April 2, 2024 01:49
@codecov-commenter
Copy link

codecov-commenter commented Apr 2, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 64.20%. Comparing base (f688e38) to head (863c336).
Report is 1 commits behind head on main.

Files Patch % Lines
src/encode/writer/console.rs 93.33% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #365      +/-   ##
==========================================
+ Coverage   63.42%   64.20%   +0.78%     
==========================================
  Files          25       25              
  Lines        1572     1573       +1     
==========================================
+ Hits          997     1010      +13     
+ Misses        575      563      -12     

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

@bconn98 bconn98 linked an issue Apr 4, 2024 that may be closed by this pull request
@estk
Copy link
Owner

estk commented Jul 9, 2024

Looks like CI is failing on this one still...

src/encode/writer/console.rs Outdated Show resolved Hide resolved
Copy link
Owner

@estk estk left a comment

Choose a reason for hiding this comment

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

So, I think a better approach might be to continue to use once_cell::Lazy that way each callsite remains really lightweight. But with the twist that you access it through a getter like pub fn get_color_mode() that has some #[cfg(test)]s that allow you to supplant the normal logic.

Does that make sense?

@bconn98 bconn98 changed the title refactor: COLOR_MODE to OnceCell DRAFT: refactor: COLOR_MODE to OnceCell Jul 10, 2024
@bconn98
Copy link
Collaborator Author

bconn98 commented Jul 10, 2024

I see what you're saying but I don't think we get any test benefit with that change (which may be okay because of the integration tests). That said, I don't think I agree that using Lazy is any more lightweight. Both will be initialized once and return a reference on access if I'm reading the docs correctly.

@estk
Copy link
Owner

estk commented Jul 10, 2024

@bconn98 by lightweight i mean not having the following copy pasted at every callsite. There are other options if you prefer, you could wrap the Lazy in a struct and in cfg(test) mode internally it could simply be a Mutex that you can mutate as necessary.

            let color_mode_init = {
                let no_color = env::var("NO_COLOR");
                let clicolor_force = env::var("CLICOLOR_FORCE");
                let clicolor = env::var("CLICOLOR");
                get_color_mode(no_color, clicolor_force, clicolor)
            };

@bconn98
Copy link
Collaborator Author

bconn98 commented Jul 11, 2024

I tried a couple different versions of using mutex.

  1. Locking the whole tests
  2. Like you suggested a struct with a mutex locked getter (using deref and force)

But no avail, the Lazy will not execute a second time thereby causing tests to fail. I wouldn't want to put the actual ENV values inside of mutex's as that would invalidate the majority of the test.

@bconn98
Copy link
Collaborator Author

bconn98 commented Jul 12, 2024

I found a solution that I think is satisfactory. By Option wrapping all the args, we can dependency inject during testing or otherwise just pass Nones and pull from the env. I would like this MR to merge in the initial migration, than a follow on MR will come to bump our MSRV and use the solution that is now part of the std lib.

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.

Refactor COLOR_MODE to OnceCell
3 participants