Skip to content

Commit 8d596b4

Browse files
committed
Add env testtools subcommand to show the environment
(This is only temporarily in `gix-testtools` and shall either be moved to `internal-tools` or removed altogether.) It is useful to be able to observe environment variables that are set when running code with tools such as `cargo` or `cross`. This is therefore not intended for general-purpose use. A system command like `printenv` or `env` or a shell facility should be preferred where applicable, since it is considered a feature rather than a bug that commands like `cargo run -p gix-testtools env` include changes to the environment from `cargo` itself. Since one use for checking environment variables is to investigate the effects of environments that contain variable names or values that are not valid Unicode, this avoids requiring that environment variables all be Unicode. Any name or value that is not Unicode is shown in its Rust debug representation. This is always quoted, and to decrease ambiguity any name or (more likely) value that contains literal double quotes is likewise shown in its debug representation so that it is always clear if a quotation mark is just for display. Each name and value is otherwise shown literally. In either case the name or its representation is separated by a `=` from the value or its representation.
1 parent 07022ee commit 8d596b4

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tests/tools/src/main.rs

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ fn bash_program() -> io::Result<()> {
99
Ok(())
1010
}
1111

12+
fn env() -> io::Result<()> {
13+
fn repr(text: &std::ffi::OsStr) -> String {
14+
text.to_str()
15+
.filter(|s| !s.contains('"'))
16+
.map(ToOwned::to_owned)
17+
.unwrap_or_else(|| format!("{text:?}"))
18+
}
19+
for (name, value) in std::env::vars_os() {
20+
println!("{}={}", repr(&name), repr(&value));
21+
}
22+
Ok(())
23+
}
24+
1225
fn mess_in_the_middle(path: PathBuf) -> io::Result<()> {
1326
let mut file = fs::OpenOptions::new().read(false).write(true).open(path)?;
1427
file.seek(io::SeekFrom::Start(file.metadata()?.len() / 2))?;
@@ -27,6 +40,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2740
let scmd = args.next().expect("sub command");
2841
match &*scmd {
2942
"bash-program" | "bp" => bash_program()?,
43+
"env" => env()?,
3044
"mess-in-the-middle" => mess_in_the_middle(PathBuf::from(args.next().expect("path to file to mess with")))?,
3145
#[cfg(unix)]
3246
"umask" => umask()?,

0 commit comments

Comments
 (0)