Skip to content

Commit 52d506e

Browse files
committed
Test Env::{nth, count, last} overrides
1 parent 7f32505 commit 52d506e

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/test/run-pass/env-vars-iter.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::process::Command;
12+
use std::env;
13+
14+
fn check_var(&(ref key, ref val): &(String, String)) -> bool {
15+
match &**key {
16+
"FOO" => { assert_eq!(val, "BAR"); true },
17+
"BAR" => { assert_eq!(val, "BAZ"); true },
18+
"BAZ" => { assert_eq!(val, "FOO"); true },
19+
_ => false,
20+
}
21+
}
22+
23+
fn main() {
24+
if let Some(arg) = env::args().nth(1) {
25+
match &*arg {
26+
"empty" => {
27+
assert_eq!(env::vars().count(), 0);
28+
assert_eq!(env::vars().next(), None);
29+
assert_eq!(env::vars().nth(1), None);
30+
assert_eq!(env::vars().last(), None);
31+
},
32+
"many" => {
33+
assert!(env::vars().count() >= 3);
34+
assert!(env::vars().last().is_some());
35+
assert_eq!(env::vars().filter(check_var).count(), 3);
36+
assert_eq!(
37+
(0..env::vars().count())
38+
.map(|i| env::vars().nth(i).unwrap())
39+
.filter(check_var)
40+
.count(),
41+
3);
42+
},
43+
arg => {
44+
panic!("Unexpected arg {}", arg);
45+
},
46+
}
47+
} else {
48+
// Command::env_clear does not work on Windows.
49+
// https://github.com/rust-lang/rust/issues/31259
50+
if !cfg!(windows) {
51+
let status = Command::new(env::current_exe().unwrap())
52+
.arg("empty")
53+
.env_clear()
54+
.status()
55+
.unwrap();
56+
assert_eq!(status.code(), Some(0));
57+
}
58+
59+
let status = Command::new(env::current_exe().unwrap())
60+
.arg("many")
61+
.env("FOO", "BAR")
62+
.env("BAR", "BAZ")
63+
.env("BAZ", "FOO")
64+
.status()
65+
.unwrap();
66+
assert_eq!(status.code(), Some(0));
67+
}
68+
}

0 commit comments

Comments
 (0)