forked from uutils/coreutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pwd.rs
170 lines (149 loc) · 4.02 KB
/
test_pwd.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (words) symdir somefakedir
use std::path::PathBuf;
use crate::common::util::{TestScenario, UCommand};
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
#[test]
fn test_default() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.succeeds().stdout_is(at.root_dir_resolved() + "\n");
}
#[test]
fn test_failed() {
let (_at, mut ucmd) = at_and_ucmd!();
ucmd.arg("will-fail").fails();
}
#[cfg(unix)]
#[test]
fn test_deleted_dir() {
use std::process::Command;
let ts = TestScenario::new(util_name!());
let at = ts.fixtures;
let output = Command::new("sh")
.arg("-c")
.arg(format!(
"cd '{}'; mkdir foo; cd foo; rmdir ../foo; exec '{}' {}",
at.root_dir_resolved(),
ts.bin_path.to_str().unwrap(),
ts.util_name,
))
.output()
.unwrap();
assert!(!output.status.success());
assert!(output.stdout.is_empty());
assert_eq!(
output.stderr,
b"pwd: failed to get current directory: No such file or directory\n"
);
}
struct Env {
ucmd: UCommand,
#[cfg(not(windows))]
root: String,
subdir: String,
symdir: String,
}
fn symlinked_env() -> Env {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("subdir");
// Note: on Windows this requires admin permissions
at.symlink_dir("subdir", "symdir");
let root = PathBuf::from(at.root_dir_resolved());
ucmd.current_dir(root.join("symdir"));
#[cfg(not(windows))]
ucmd.env("PWD", root.join("symdir"));
Env {
ucmd,
#[cfg(not(windows))]
root: root.to_string_lossy().into_owned(),
subdir: root.join("subdir").to_string_lossy().into_owned(),
symdir: root.join("symdir").to_string_lossy().into_owned(),
}
}
#[test]
fn test_symlinked_logical() {
let mut env = symlinked_env();
env.ucmd.arg("-L").succeeds().stdout_is(env.symdir + "\n");
}
#[test]
fn test_symlinked_physical() {
let mut env = symlinked_env();
env.ucmd.arg("-P").succeeds().stdout_is(env.subdir + "\n");
}
#[test]
fn test_symlinked_default() {
let mut env = symlinked_env();
env.ucmd.succeeds().stdout_is(env.subdir + "\n");
}
#[test]
fn test_symlinked_default_posix() {
let mut env = symlinked_env();
env.ucmd
.env("POSIXLY_CORRECT", "1")
.succeeds()
.stdout_is(env.symdir.clone() + "\n");
}
#[test]
fn test_symlinked_default_posix_l() {
let mut env = symlinked_env();
env.ucmd
.env("POSIXLY_CORRECT", "1")
.arg("-L")
.succeeds()
.stdout_is(env.symdir + "\n");
}
#[test]
fn test_symlinked_default_posix_p() {
let mut env = symlinked_env();
env.ucmd
.env("POSIXLY_CORRECT", "1")
.arg("-P")
.succeeds()
.stdout_is(env.subdir + "\n");
}
#[cfg(not(windows))]
pub mod untrustworthy_pwd_var {
use std::path::Path;
use super::*;
#[test]
fn test_nonexistent_logical() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.arg("-L")
.env("PWD", "/somefakedir")
.succeeds()
.stdout_is(at.root_dir_resolved() + "\n");
}
#[test]
fn test_wrong_logical() {
let mut env = symlinked_env();
env.ucmd
.arg("-L")
.env("PWD", env.root)
.succeeds()
.stdout_is(env.subdir + "\n");
}
#[test]
fn test_redundant_logical() {
let mut env = symlinked_env();
env.ucmd
.arg("-L")
.env("PWD", Path::new(&env.symdir).join("."))
.succeeds()
.stdout_is(env.subdir + "\n");
}
#[test]
fn test_relative_logical() {
let mut env = symlinked_env();
env.ucmd
.arg("-L")
.env("PWD", ".")
.succeeds()
.stdout_is(env.subdir + "\n");
}
}