evobench_tools/run/
env_vars.rs

1use crate::serde_types::allowed_env_var::AllowEnvVar;
2
3pub const EVOBENCH_ENV_VARS: &[&str] = &[
4    "EVOBENCH_LOG",
5    "BENCH_OUTPUT_LOG",
6    "COMMIT_ID",
7    "COMMIT_TAGS",
8    "DATASET_DIR",
9];
10
11pub fn is_evobench_env_var(s: &str) -> bool {
12    EVOBENCH_ENV_VARS.contains(&s)
13}
14
15/// A parameter for `AllowedEnvVar` that checks that the variable is
16/// not going to conflict with one of the built-in evobench env vars
17/// (in the future perhaps also check for things like USER?)
18#[derive(Debug)]
19pub struct AllowableCustomEnvVar;
20impl AllowEnvVar for AllowableCustomEnvVar {
21    const MAX_ENV_VAR_NAME_LEN: usize = 80;
22
23    fn allow_env_var(s: &str) -> bool {
24        !is_evobench_env_var(s)
25    }
26
27    fn expecting() -> String {
28        format!(
29            "a variable name that is *not* any of {}",
30            EVOBENCH_ENV_VARS.join(", ")
31        )
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use std::str::FromStr;
38
39    use crate::serde_types::allowed_env_var::AllowedEnvVar;
40
41    use super::*;
42
43    #[test]
44    fn t_allowable_custom_env_var_name() {
45        let allow = AllowableCustomEnvVar::allow_env_var;
46        assert!(allow("FOO"));
47        // We don't care whether the user decides to use unorthodox
48        // variable names
49        assert!(allow("foo"));
50        assert!(allow("%&/',é\nhmm"));
51        assert!(allow(
52            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
53        ));
54        // Too long, but have to rely on `AllowedEnvVar` to get the
55        // `MAX_ENV_VAR_NAME_LEN` constant checked
56        assert!(allow(
57            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
58        ));
59
60        let allow =
61            |s: &str| -> bool { AllowedEnvVar::<AllowableCustomEnvVar>::from_str(s).is_ok() };
62
63        assert!(allow("foo"));
64        assert!(allow("%&/',é\nhmm"));
65        assert!(allow(
66            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
67        ));
68
69        // Problems caughtby `AllowedEnvVar::from_str`
70        assert!(!allow(
71            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
72        ));
73        assert!(!allow("A\0B"));
74        assert!(!allow("foo=bar"));
75        assert!(!allow("EVOBENCH_LOG"));
76
77        assert_eq!(
78            AllowedEnvVar::<AllowableCustomEnvVar>::from_str("EVOBENCH_LOG")
79                .err()
80                .unwrap()
81                .to_string(),
82            "AllowableCustomEnvVar env variable \"EVOBENCH_LOG\" is reserved, expecting a variable name \
83             that is *not* any of EVOBENCH_LOG, BENCH_OUTPUT_LOG, COMMIT_ID, COMMIT_TAGS, DATASET_DIR"
84        );
85    }
86}
87
88// Can't make this const easily, but doesn't matter. It'll catch bugs
89// on the first job run.
90pub fn assert_evobench_env_var(s: &str) -> &str {
91    if is_evobench_env_var(s) {
92        s
93    } else {
94        panic!("Not a known EVOBENCH_ENV_VARS entry: {s:?}")
95    }
96}