Skip to content

Commit

Permalink
Make tests fail if they would non-hermetically read a config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Jan 5, 2025
1 parent 8e7b922 commit 59b7ead
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ env:
# Tell `anyhow` *not* to collect backtraces for errors.
# <https://users.rust-lang.org/t/anyhow-backtraces/107437>
RUST_LIB_BACKTRACE: 0
# This will cause a test to fail if it has a dependence on a system/user config file.
AIC_DO_NOT_USE_CONFIG_FILES_IN_TESTS: 1

jobs:
build:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/mutants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on: [workflow_dispatch]

env:
CARGO_TERM_COLOR: always
# This will cause a test to fail if it has a dependence on a system/user config file.
AIC_DO_NOT_USE_CONFIG_FILES_IN_TESTS: 1

jobs:
mutants:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/unstable-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ env:
# artifacts, so they won't be useful for anything (other than maybe the exhaustive
# builds with different features).
CARGO_INCREMENTAL: 0
# This will cause a test to fail if it has a dependence on a system/user config file.
AIC_DO_NOT_USE_CONFIG_FILES_IN_TESTS: 1

jobs:
build:
Expand Down
6 changes: 6 additions & 0 deletions all-is-cubes-desktop/src/config_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ use all_is_cubes_render::camera::GraphicsOptions;
pub fn load_config() -> Result<GraphicsOptions, anyhow::Error> {
// TODO: make testable
// TODO: allow users of this library function to pick their own config dir

if std::env::var("AIC_DO_NOT_USE_CONFIG_FILES_IN_TESTS").is_ok() {
panic!("tests should be hermetic and not touch user config files \
(environment variable AIC_DO_NOT_USE_CONFIG_FILES_IN_TESTS set but --no-config-files not passed)");
}

let project_dirs = ProjectDirs::from("org.switchb", "", "all-is-cubes")
.ok_or_else(|| anyhow::anyhow!("could not find configuration directory"))?;
create_dir_all(project_dirs.config_dir())?;
Expand Down
4 changes: 4 additions & 0 deletions all-is-cubes-desktop/tests/end-to-end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
fn trycmd_tests() {
let tc = trycmd::TestCases::new();

// Extra protection against depending on config now or in the future.
// This will cause a test to fail if it has a dependence on a system/user config file.
tc.env("AIC_DO_NOT_USE_CONFIG_FILES_IN_TESTS", "1");

// Prevent `anyhow` from printing backtraces if it otherwise would due to env having
// "RUST_BACKTRACE=1", because they'd cause our error output tests to fail.
// Docs: https://docs.rs/anyhow/1.0.75/anyhow/index.html#details
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes-desktop/tests/gltf-validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn gltf_round_trip_test(args: &[&str]) {
let gltf_path = temp_dir.path().join("output.gltf");

let output = process::Command::new(env!("CARGO_BIN_EXE_all-is-cubes"))
.env("AIC_DO_NOT_USE_CONFIG_FILES_IN_TESTS", "1")
.arg("--no-config-files")
.arg("--graphics=record")
.arg("--output")
Expand Down

0 comments on commit 59b7ead

Please sign in to comment.