Skip to content

Commit 8cac2a1

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 8581b62 commit 8cac2a1

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
@@ -1,5 +1,18 @@
11
use std::{fs, io, io::prelude::*, path::PathBuf};
22

3+
fn env() -> io::Result<()> {
4+
fn repr(text: &std::ffi::OsStr) -> String {
5+
text.to_str()
6+
.filter(|s| !s.contains('"'))
7+
.map(ToOwned::to_owned)
8+
.unwrap_or_else(|| format!("{text:?}"))
9+
}
10+
for (name, value) in std::env::vars_os() {
11+
println!("{}={}", repr(&name), repr(&value));
12+
}
13+
Ok(())
14+
}
15+
316
fn mess_in_the_middle(path: PathBuf) -> io::Result<()> {
417
let mut file = fs::OpenOptions::new().read(false).write(true).open(path)?;
518
file.seek(io::SeekFrom::Start(file.metadata()?.len() / 2))?;
@@ -17,6 +30,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1730
let mut args = std::env::args().skip(1);
1831
let scmd = args.next().expect("sub command");
1932
match &*scmd {
33+
"env" => env()?,
2034
"mess-in-the-middle" => mess_in_the_middle(PathBuf::from(args.next().expect("path to file to mess with")))?,
2135
#[cfg(unix)]
2236
"umask" => umask()?,

0 commit comments

Comments
 (0)