-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
tool_paths.rs
402 lines (356 loc) · 10.6 KB
/
tool_paths.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//! Tests for configuration values that point to programs.
use cargo_test_support::{basic_lib_manifest, project, rustc_host, rustc_host_env};
#[cargo_test]
fn pathless_tools() {
let target = rustc_host();
let foo = project()
.file("Cargo.toml", &basic_lib_manifest("foo"))
.file("src/lib.rs", "")
.file(
".cargo/config",
&format!(
r#"
[target.{}]
linker = "nonexistent-linker"
"#,
target
),
)
.build();
foo.cargo("build --verbose")
.with_stderr(
"\
[COMPILING] foo v0.5.0 ([CWD])
[RUNNING] `rustc [..] -C linker=nonexistent-linker [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
)
.run();
}
#[cargo_test]
fn absolute_tools() {
let target = rustc_host();
// Escaped as they appear within a TOML config file
let linker = if cfg!(windows) {
r#"C:\\bogus\\nonexistent-linker"#
} else {
r#"/bogus/nonexistent-linker"#
};
let foo = project()
.file("Cargo.toml", &basic_lib_manifest("foo"))
.file("src/lib.rs", "")
.file(
".cargo/config",
&format!(
r#"
[target.{target}]
linker = "{linker}"
"#,
target = target,
linker = linker
),
)
.build();
foo.cargo("build --verbose")
.with_stderr(
"\
[COMPILING] foo v0.5.0 ([CWD])
[RUNNING] `rustc [..] -C linker=[..]bogus/nonexistent-linker [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
)
.run();
}
#[cargo_test]
fn relative_tools() {
let target = rustc_host();
// Escaped as they appear within a TOML config file
let linker = if cfg!(windows) {
r#".\\tools\\nonexistent-linker"#
} else {
r#"./tools/nonexistent-linker"#
};
// Funky directory structure to test that relative tool paths are made absolute
// by reference to the `.cargo/..` directory and not to (for example) the CWD.
let p = project()
.no_manifest()
.file("bar/Cargo.toml", &basic_lib_manifest("bar"))
.file("bar/src/lib.rs", "")
.file(
".cargo/config",
&format!(
r#"
[target.{target}]
linker = "{linker}"
"#,
target = target,
linker = linker
),
)
.build();
let prefix = p.root().into_os_string().into_string().unwrap();
p.cargo("build --verbose")
.cwd("bar")
.with_stderr(&format!(
"\
[COMPILING] bar v0.5.0 ([CWD])
[RUNNING] `rustc [..] -C linker={prefix}/./tools/nonexistent-linker [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
prefix = prefix,
))
.run();
}
#[cargo_test]
fn custom_runner() {
let target = rustc_host();
let p = project()
.file("src/main.rs", "fn main() {}")
.file("tests/test.rs", "")
.file("benches/bench.rs", "")
.file(
".cargo/config",
&format!(
r#"
[target.{}]
runner = "nonexistent-runner -r"
"#,
target
),
)
.build();
p.cargo("run -- --param")
.with_status(101)
.with_stderr_contains(
"\
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `nonexistent-runner -r target/debug/foo[EXE] --param`
",
)
.run();
p.cargo("test --test test --verbose -- --param")
.with_status(101)
.with_stderr_contains(
"\
[COMPILING] foo v0.0.1 ([CWD])
[RUNNING] `rustc [..]`
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `nonexistent-runner -r [..]/target/debug/deps/test-[..][EXE] --param`
",
)
.run();
p.cargo("bench --bench bench --verbose -- --param")
.with_status(101)
.with_stderr_contains(
"\
[COMPILING] foo v0.0.1 ([CWD])
[RUNNING] `rustc [..]`
[RUNNING] `rustc [..]`
[FINISHED] bench [optimized] target(s) in [..]
[RUNNING] `nonexistent-runner -r [..]/target/release/deps/bench-[..][EXE] --param --bench`
",
)
.run();
}
// can set a custom runner via `target.'cfg(..)'.runner`
#[cargo_test]
fn custom_runner_cfg() {
let p = project()
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config",
r#"
[target.'cfg(not(target_os = "none"))']
runner = "nonexistent-runner -r"
"#,
)
.build();
p.cargo("run -- --param")
.with_status(101)
.with_stderr_contains(
"\
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `nonexistent-runner -r target/debug/foo[EXE] --param`
",
)
.run();
}
// custom runner set via `target.$triple.runner` have precedence over `target.'cfg(..)'.runner`
#[cargo_test]
fn custom_runner_cfg_precedence() {
let target = rustc_host();
let p = project()
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config",
&format!(
r#"
[target.'cfg(not(target_os = "none"))']
runner = "ignored-runner"
[target.{}]
runner = "nonexistent-runner -r"
"#,
target
),
)
.build();
p.cargo("run -- --param")
.with_status(101)
.with_stderr_contains(
"\
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `nonexistent-runner -r target/debug/foo[EXE] --param`
",
)
.run();
}
#[cargo_test]
fn custom_runner_cfg_collision() {
let p = project()
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config",
r#"
[target.'cfg(not(target_arch = "avr"))']
runner = "true"
[target.'cfg(not(target_os = "none"))']
runner = "false"
"#,
)
.build();
p.cargo("run -- --param")
.with_status(101)
.with_stderr(
"\
[ERROR] several matching instances of `target.'cfg(..)'.runner` in configurations
first match `cfg(not(target_arch = \"avr\"))` located in [..]/foo/.cargo/config
second match `cfg(not(target_os = \"none\"))` located in [..]/foo/.cargo/config
",
)
.run();
}
#[cargo_test]
fn custom_runner_env() {
let p = project().file("src/main.rs", "fn main() {}").build();
let key = format!("CARGO_TARGET_{}_RUNNER", rustc_host_env());
p.cargo("run")
.env(&key, "nonexistent-runner --foo")
.with_status(101)
// FIXME: Update "Caused by" error message once rust/pull/87704 is merged.
// On Windows, changing to a custom executable resolver has changed the
// error messages.
.with_stderr(&format!(
"\
[COMPILING] foo [..]
[FINISHED] dev [..]
[RUNNING] `nonexistent-runner --foo target/debug/foo[EXE]`
[ERROR] could not execute process `nonexistent-runner --foo target/debug/foo[EXE]` (never executed)
Caused by:
[..]
"
))
.run();
}
#[cargo_test]
fn custom_runner_env_overrides_config() {
let target = rustc_host();
let p = project()
.file("src/main.rs", "fn main() {}")
.file(
".cargo/config.toml",
&format!(
r#"
[target.{}]
runner = "should-not-run -r"
"#,
target
),
)
.build();
let key = format!("CARGO_TARGET_{}_RUNNER", rustc_host_env());
p.cargo("run")
.env(&key, "should-run --foo")
.with_status(101)
.with_stderr_contains("[RUNNING] `should-run --foo target/debug/foo[EXE]`")
.run();
}
#[cargo_test]
#[cfg(unix)] // Assumes `true` is in PATH.
fn custom_runner_env_true() {
// Check for a bug where "true" was interpreted as a boolean instead of
// the executable.
let p = project().file("src/main.rs", "fn main() {}").build();
let key = format!("CARGO_TARGET_{}_RUNNER", rustc_host_env());
p.cargo("run")
.env(&key, "true")
.with_stderr_contains("[RUNNING] `true target/debug/foo[EXE]`")
.run();
}
#[cargo_test]
fn custom_linker_env() {
let p = project().file("src/main.rs", "fn main() {}").build();
let key = format!("CARGO_TARGET_{}_LINKER", rustc_host_env());
p.cargo("build -v")
.env(&key, "nonexistent-linker")
.with_status(101)
.with_stderr_contains("[RUNNING] `rustc [..]-C linker=nonexistent-linker [..]")
.run();
}
#[cargo_test]
fn target_in_environment_contains_lower_case() {
let p = project().file("src/main.rs", "fn main() {}").build();
let target = rustc_host();
let env_key = format!(
"CARGO_TARGET_{}_LINKER",
target.to_lowercase().replace('-', "_")
);
p.cargo("build -v --target")
.arg(target)
.env(&env_key, "nonexistent-linker")
.with_stderr_contains(format!(
"warning: Environment variables are expected to use uppercase \
letters and underscores, the variable `{}` will be ignored and \
have no effect",
env_key
))
.run();
}
#[cargo_test]
fn cfg_ignored_fields() {
// Test for some ignored fields in [target.'cfg()'] tables.
let p = project()
.file(
".cargo/config",
r#"
# Try some empty tables.
[target.'cfg(not(foo))']
[target.'cfg(not(bar))'.somelib]
# A bunch of unused fields.
[target.'cfg(not(target_os = "none"))']
linker = 'false'
ar = 'false'
foo = {rustc-flags = "-l foo"}
invalid = 1
runner = 'false'
rustflags = ''
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check")
.with_stderr(
"\
[WARNING] unused key `somelib` in [target] config table `cfg(not(bar))`
[WARNING] unused key `ar` in [target] config table `cfg(not(target_os = \"none\"))`
[WARNING] unused key `foo` in [target] config table `cfg(not(target_os = \"none\"))`
[WARNING] unused key `invalid` in [target] config table `cfg(not(target_os = \"none\"))`
[WARNING] unused key `linker` in [target] config table `cfg(not(target_os = \"none\"))`
[CHECKING] foo v0.0.1 ([..])
[FINISHED] [..]
",
)
.run();
}