Repository: zellij-org/zellij
File: zellij-utils/src/input/unit/layout_test.rs (Rust)

Code before:
        ..Default::default()
    };
    assert_eq!(layout, expected_layout);
}

#[track_caller]
fn env_test_helper(layout_str: &str, env_vars: Vec<(&str, &str)>, expected_output: Vec<&str>) {
    for (key, value) in &env_vars {
        std::env::set_var(key, value);
    }
    let layout = Layout::from_kdl(layout_str, "layout_file_name".into(), None, None).unwrap();
    let layout = format!("{layout:#?}",);
    for (key, value) in &env_vars {
        assert!(
            !layout.contains(&format!("${key}")) && layout.contains(value),
            "environment variable `{key}={value}` was not properly expanded",
        );
    }
    for s in expected_output {
        assert!(layout.contains(s), "expected string `{s}` was not found");
    }
}

#[test]
fn env_valid_global_cwd() {
    env_test_helper(
        r#"
        layout {
            cwd "$Z_VALID_GLOBAL"
            pane cwd="relative"      // -> /abs/path/relative
            pane cwd="/another/abs"  // -> /another/abs
        }
        "#,
        vec![("Z_VALID_GLOBAL", "/abs/path")],
        vec!["/abs/path/relative", "/another/abs"],
    );
}

#[test]
fn env_valid_local_abs_cwd() {
    env_test_helper(
        r#"
        layout {
            cwd "/abs/path"
            pane cwd="relative"        // -> /abs/path/relative
            pane cwd="$Z_VALID_LOCAL"  // -> /another/abs
        }
        "#,
        vec![("Z_VALID_LOCAL", "/another/abs")],
        vec!["/abs/path/relative", "/another/abs"],
    );

Review comment (suggestion):
This is great, but I personally find it a little hard to understand at a glance. Which I think is important when we're considering tests. How about we change it to something like (pseudocode, if you have a better idea then by all means!)
```rust
LayoutTesterWithEnvironment::new()
    .set_env_variables(/* ... */)
    .parse_layout(stringified_layout)
    .assert(/* .. */)
    .assert(/* .. */)
```

Code after:
                    },
                    TiledPaneLayout {
                        run: Some(Run::Plugin(RunPlugin {
                            _allow_exec_host_cmd: false,
                            location: RunPluginLocation::File(PathBuf::from("plugin.wasm")),
                        })),
                        ..Default::default()
                    },
                    TiledPaneLayout {
                        run: Some(Run::Plugin(RunPlugin {
                            _allow_exec_host_cmd: false,
                            location: RunPluginLocation::File(PathBuf::from(
                                "relative/with space/plugin.wasm",
                            )),
                        })),
                        ..Default::default()
                    },
                    TiledPaneLayout {
                        run: Some(Run::Plugin(RunPlugin {
                            _allow_exec_host_cmd: false,
                            location: RunPluginLocation::File(PathBuf::from(
                                "/absolute/with space/plugin.wasm",
                            )),
                        })),
                        ..Default::default()
                    },
                    TiledPaneLayout {
                        run: Some(Run::Plugin(RunPlugin {
                            _allow_exec_host_cmd: false,
                            location: RunPluginLocation::File(PathBuf::from(
                                "c:/absolute/windows/plugin.wasm",
                            )),
                        })),
                        ..Default::default()
                    },
                ],
                ..Default::default()
            },
            vec![],
        )),
        ..Default::default()
    };
    assert_eq!(layout, expected_layout);
}

#[test]
fn env_var_expansion() {
    let raw_layout = r#"
        layout {
            // cwd tests + composition
            cwd "$TEST_GLOBAL_CWD"