Skip to content

Commit

Permalink
Return env vars in sorted order, to ensure determinism of output
Browse files Browse the repository at this point in the history
  • Loading branch information
bzm3r committed Jan 8, 2024
1 parent 0c22ff2 commit aa749f2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,10 @@ impl Shell {
///
/// The returned hash map contains a snapshot of the process’s environment variables at the time of this invocation. Modifications to environment variables afterwards will not be reflected in the returned iterator.
pub fn vars_os(&self) -> Vec<(OsString, OsString)> {
Vec::from_iter(self.env.borrow().iter().map(|(k, v)| (k.to_owned(), v.to_owned())))
let mut vars =
Vec::from_iter(self.env.borrow().iter().map(|(k, v)| (k.to_owned(), v.to_owned())));
vars.sort_unstable_by(|(a, _), (b, _)| a.cmp(b));
vars
}

/// Returns the environment variables set for this shell.
Expand All @@ -682,12 +685,14 @@ impl Shell {
/// the environment is not valid unicode. If this is not desired, consider
/// using [`Shell::vars_os`](Self::vars_os).
pub fn vars(&self) -> Vec<(String, String)> {
Vec::from_iter(
let mut vars = Vec::from_iter(
self.env
.borrow()
.iter()
.map(|(k, v)| (k.to_str().unwrap().to_string(), v.to_str().unwrap().to_string())),
)
);
vars.sort_unstable_by(|(a, _), (b, _)| a.cmp(b));
vars
}
}

Expand Down

0 comments on commit aa749f2

Please sign in to comment.