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 systemwide config file support #1888

Merged

Conversation

patrickpichler
Copy link

@patrickpichler patrickpichler commented Oct 6, 2021

There is now support for a systemwide config file. The location of the
system wide config file is $(BAT_SYSTEM_CONFIG_PREFIX)/bat/config.
$(BAT_SYSTEM_CONFIG_PREFIX) has to be provided at compile time as an
environment variable. If the environment variable is not set, a default
is used. This default is C:\ProgramData for windows and /etc for every other os.

fixes #668

@patrickpichler patrickpichler force-pushed the feature/668/add-systemwide-config branch 3 times, most recently from 818e044 to 6617650 Compare October 6, 2021 18:57
@sharkdp
Copy link
Owner

sharkdp commented Oct 24, 2021

@patrickpichler Thank you very much. This looks good to me, except for one minor inline comment. Can we please add a test for this?

For CI, I guess we could either create a file in /etc, or provide the environment variable at compile time (and set it to some folder inside tests/). However, since we also want to run tests locally, we should probably follow the second path.

@patrickpichler patrickpichler force-pushed the feature/668/add-systemwide-config branch from 6617650 to 1f47b80 Compare October 25, 2021 11:01
@patrickpichler
Copy link
Author

@sharkdp I like the idea with the test, but I'm not 100% sure how to add it.
How would I set the environment variable needed at build time only for the
tests?

@sharkdp
Copy link
Owner

sharkdp commented Oct 26, 2021

@sharkdp I like the idea with the test, but I'm not 100% sure how to add it.
How would I set the environment variable needed at build time only for the
tests?

Hm.. I didn't really think about it. Maybe that's not possible, you're right. I thought it might work to just build normally but then test with BAT_SYSTEM_CONFIG_PREFIX=… cargo test. But it's likely this is not going to work.

I'd be okay with a CI-only test where it would be fine to set BAT_SYSTEM_CONFIG_PREFIX globally. At run time (i.e. unit test run time), we could then check if system_config_file() points to a predetermined folder in tests and only run the test conditionally in that situation.

I realize this doesn't sound too appealing, so I'm also glad to hear other ideas.

src/bin/bat/main.rs Outdated Show resolved Hide resolved
@patrickpichler patrickpichler force-pushed the feature/668/add-systemwide-config branch from 1f47b80 to a2e937c Compare October 26, 2021 08:17
@patrickpichler
Copy link
Author

I'd be okay with a CI-only test

If i understand the CICD.yml correctly, I would need to build the application then
twice, as otherwise a wrong BAT_SYSTEM_CONFIG_PREFIX would be published
to the Github release page.

I realize this doesn't sound too appealing, so I'm also glad to hear other ideas.

One idea would be to do something like git is doing. They allow to override the location
of the system and global config files via an environment variable. This way I could at
least test that the logic in general is working.

@sharkdp
Copy link
Owner

sharkdp commented Oct 26, 2021

I'd be okay with a CI-only test

If i understand the CICD.yml correctly, I would need to build the application then twice, as otherwise a wrong BAT_SYSTEM_CONFIG_PREFIX would be published to the Github release page.

Oh 🙈. You are absolutely right.

One idea would be to do something like git is doing. They allow to override the location
of the system and global config files via an environment variable. This way I could at
least test that the logic in general is working.

I think I would now prefer a custom test stage in the CI script where we explicitly place a system config file in /etc and then call a special unit test that is disabled/ignored by default. We do something similar in the "Run ignored-by-default unit tests with new syntaxes and themes" stage.

I hope there is no clash with the existing #[ignore]d unit tests. If there is, or if the configuration get's too complicated, we could also think of a integration-style test that's implemented in the CICD.yml file (i.e. in bash).

I realize that it sounds like a lot of effort for a minor piece of code. But we really want to make sure that we have a solid testing framework in order to reduce the maintenance work in the future. Having a solid test for this will ensure that this feature continues to work in the future.

That said, if it turns out that maintaining the test code for this could result in major maintenance work in the future, I'm also okay with merging this without a test.

@patrickpichler
Copy link
Author

I hope there is no clash with the existing #[ignore]d unit tests

Mhm I guess this will cause issues in the Run ignored-by-default unit tests with new syntaxes and themes step.

I could change it to explicitly provide the --test argument to cargo test, so that only the tests
in tests/assets.rs are executed. Then add a new integration test file for the global config, which
is also #[ignore]d by default and have an explicit step in the CICD.yml to run the tests.

How does this sound?

@sharkdp
Copy link
Owner

sharkdp commented Oct 26, 2021

Sounds great!

@patrickpichler
Copy link
Author

@sharkdp I've added some tests, plus made the required changes in the last commit 👍

@patrickpichler patrickpichler force-pushed the feature/668/add-systemwide-config branch from 25f6f03 to fccb57d Compare October 28, 2021 13:49
@patrickpichler patrickpichler marked this pull request as ready for review October 28, 2021 14:08
@patrickpichler
Copy link
Author

@sharkdp any update on this?

Comment on lines 5 to 24
fn bat_raw_command() -> Command {
let mut cmd = Command::cargo_bin("bat").unwrap();
cmd.current_dir("tests/examples");
cmd.env_remove("BAT_CACHE_PATH");
cmd.env_remove("BAT_CONFIG_DIR");
cmd.env_remove("BAT_CONFIG_PATH");
cmd.env_remove("BAT_OPTS");
cmd.env_remove("BAT_PAGER");
cmd.env_remove("BAT_STYLE");
cmd.env_remove("BAT_TABS");
cmd.env_remove("BAT_THEME");
cmd.env_remove("COLORTERM");
cmd.env_remove("NO_COLOR");
cmd.env_remove("PAGER");
cmd
}

fn bat() -> assert_cmd::Command {
assert_cmd::Command::from_std(bat_raw_command())
}
Copy link
Owner

Choose a reason for hiding this comment

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

it would be great to move this duplicated(?) code to a common test-helper module

assert_cmd::Command::from_std(bat_raw_command())
}

// This test is ignored, as it needs a special system wide config put into place
Copy link
Owner

@sharkdp sharkdp Nov 22, 2021

Choose a reason for hiding this comment

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

can we please add instruction on how to enable it? currently, it sounds like it is ignored for good :-). or maybe just add a reference to the CI config.

@sharkdp
Copy link
Owner

sharkdp commented Nov 22, 2021

Thank you for the updates!

Could you please add an entry to the CHANGELOG? See instructions here: https://github.com/sharkdp/bat/blob/master/CONTRIBUTING.md#add-an-entry-to-the-changelog

@sharkdp sharkdp added this to the v0.19 milestone Nov 22, 2021
@patrickpichler
Copy link
Author

@sharkdp I've added the CHANGELOG.md entry, plus added a short description of the feature to the README.md

Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

Thank you

@sharkdp
Copy link
Owner

sharkdp commented Nov 28, 2021

Tests are currently failing

@patrickpichler patrickpichler force-pushed the feature/668/add-systemwide-config branch from 878db6b to 6d69225 Compare November 28, 2021 10:48
tests/integration_tests.rs Outdated Show resolved Hide resolved
cmd
}

pub fn bat_raw_command() -> Command {
Copy link
Owner

Choose a reason for hiding this comment

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

I think this function (or the whole module) should be #[cfg(test)], to silence the warnings. Sorry, just saw this now.

Similar for the other functions in this file.

Copy link
Author

Choose a reason for hiding this comment

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

I've tried putting #[cfg(test)] on there, but cargo test still emits the warning when building.
Or did you mean some other warnings?

Copy link
Author

Choose a reason for hiding this comment

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

After looking at it again, I guess rust still emits a warning, as the function is only
used in unix tests. I'm not 100% sure how to fix this though, as one cannot put multiple
cfg annotations on top. Do you maybe have an idea?

Copy link
Collaborator

Choose a reason for hiding this comment

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

#[cfg(test)] is not enough (but does not hurt), because the mod is brought into scope by a test, but then never used. There are ways to restructure the code, but I don't think it is worth it, so let's just allow it, at least for now.

@patrickpichler patrickpichler force-pushed the feature/668/add-systemwide-config branch 2 times, most recently from 67d354f to 1ce36e6 Compare January 8, 2022 17:52
@Enselic Enselic removed this from the v0.19 milestone Mar 3, 2022
Patrick Pichler added 2 commits March 10, 2022 20:55
There is now support for a systemwide config file. The location of the
system wide config file is `$(BAT_SYSTEM_CONFIG_PREFIX)/bat/config`.
`$(BAT_SYSTEM_CONFIG_PREFIX)` has to be provided at compile time as an
environment variable. If the environment variable is not set, a default
is used. This default is `C:\ProgramData` for windows and `/etc` for
every other os.
There is now a new stage in the CICD workflow present, which will build
`bat` with the `BAT_SYSTEM_CONFIG_PREFIX` set to load the config file
from `/tests/examples/system_config/bat/config`, plus a basic set of
tests, to ensure the feature is working as expected. By default the
tests are set to ignored, as they need special setup before they can be
run.
@patrickpichler
Copy link
Author

@sharkdp @Enselic any update on this?
I looked once again into the warnings on running the system wide tests and I guess this
is caused as code is shared between the different test types.

The warnings also show up for other shared utility methods such as the one defined in
tests/utils/mocked_pagers.rs.

Do you have any plan what I could do here?

@patrickpichler patrickpichler force-pushed the feature/668/add-systemwide-config branch from 1ce36e6 to 6b660ef Compare March 10, 2022 20:27
@Enselic
Copy link
Collaborator

Enselic commented Sep 4, 2022

Looks like I got rid of the warnings now, and this looks like a nice
change to me. It has tests, CI is green, and @sharkdp even approved it
already once. Sorry for taking so long, @patrickpichler.

@sharkdp Any chance you can take a final look and then merge? I
haven't done a detailed review yet, but if you think it look good, I'm
sure it's good enough. And even if it is not perfect (what is,
really?) it certainly seems good enough to start off with and then
iteratively improve on over several versions of bat.

@Enselic Enselic removed the needs-work label Sep 4, 2022
@sharkdp sharkdp merged commit 546dcf6 into sharkdp:master Sep 4, 2022
@sharkdp
Copy link
Owner

sharkdp commented Sep 4, 2022

I did have another look and fully agree. Thank you @patrickpichler and @Enselic for the update.

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.

Separate system and user config files
3 participants