-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathsccache_cargo.rs
More file actions
411 lines (358 loc) · 12.2 KB
/
sccache_cargo.rs
File metadata and controls
411 lines (358 loc) · 12.2 KB
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
403
404
405
406
407
408
409
410
411
//! System tests for compiling Rust code with cargo.
//!
//! Any copyright is dedicated to the Public Domain.
//! http://creativecommons.org/publicdomain/zero/1.0/
pub mod helpers;
use anyhow::{Context, Result};
use helpers::{CARGO, CRATE_DIR, cargo_clean, stop_sccache};
use assert_cmd::prelude::*;
use fs_err as fs;
use helpers::{SCCACHE_BIN, SccacheTest};
use predicates::prelude::*;
use serial_test::serial;
use std::path::Path;
use std::process::Command;
#[macro_use]
extern crate log;
#[test]
#[serial]
fn test_rust_cargo_check() -> Result<()> {
test_rust_cargo_cmd("check", SccacheTest::new(None)?)
}
#[test]
#[serial]
fn test_rust_cargo_check_readonly() -> Result<()> {
test_rust_cargo_cmd_readonly("check", SccacheTest::new(None)?)
}
#[test]
#[serial]
fn test_rust_cargo_build() -> Result<()> {
test_rust_cargo_cmd("build", SccacheTest::new(None)?)
}
#[test]
#[serial]
fn test_rust_cargo_build_readonly() -> Result<()> {
test_rust_cargo_cmd_readonly("build", SccacheTest::new(None)?)
}
#[test]
#[serial]
#[cfg(unix)]
fn test_run_log_no_perm() -> Result<()> {
trace!("sccache with log");
stop_sccache()?;
let mut cmd = Command::new(SCCACHE_BIN.as_os_str());
cmd.arg("gcc")
.env("SCCACHE_ERROR_LOG", "/no-perm.log") // Should not work
.env("SCCACHE_LOG", "debug");
cmd.assert().failure().stderr(predicate::str::contains(
"Cannot open/write log file '/no-perm.log'",
));
Ok(())
}
#[test]
#[serial]
fn test_run_log() -> Result<()> {
trace!("sccache with log");
stop_sccache()?;
let tempdir = tempfile::Builder::new()
.prefix("sccache_test_rust_cargo")
.tempdir()
.context("Failed to create tempdir")?;
let tmppath = tempdir.path().join("perm.log");
let mut cmd = Command::new(SCCACHE_BIN.as_os_str());
cmd.arg("--start-server")
.env("SCCACHE_ERROR_LOG", &tmppath) // Should not work
.env("SCCACHE_LOG", "debug");
cmd.assert().success();
stop_sccache()?;
assert!(Path::new(&tmppath).is_file());
Ok(())
}
/// This test checks that changing an environment variable reference by env! is detected by
/// sccache, causes a rebuild and is correctly printed to stdout.
#[test]
#[serial]
fn test_rust_cargo_run_with_env_dep_parsing() -> Result<()> {
test_rust_cargo_env_dep(SccacheTest::new(None)?)
}
#[cfg(feature = "unstable")]
#[test]
#[serial]
fn test_rust_cargo_check_nightly() -> Result<()> {
use std::ffi::OsString;
test_rust_cargo_cmd(
"check",
SccacheTest::new(Some(&[(
"RUSTFLAGS",
OsString::from("-Cprofile-generate=."),
)]))?,
)
}
#[cfg(feature = "unstable")]
#[test]
#[serial]
fn test_rust_cargo_check_nightly_readonly() -> Result<()> {
use std::ffi::OsString;
test_rust_cargo_cmd_readonly(
"check",
SccacheTest::new(Some(&[(
"RUSTFLAGS",
OsString::from("-Cprofile-generate=."),
)]))?,
)
}
#[cfg(feature = "unstable")]
#[test]
#[serial]
fn test_rust_cargo_build_nightly() -> Result<()> {
use std::ffi::OsString;
test_rust_cargo_cmd(
"build",
SccacheTest::new(Some(&[(
"RUSTFLAGS",
OsString::from("-Cprofile-generate=."),
)]))?,
)
}
#[cfg(feature = "unstable")]
#[test]
#[serial]
fn test_rust_cargo_build_nightly_readonly() -> Result<()> {
use std::ffi::OsString;
test_rust_cargo_cmd_readonly(
"build",
SccacheTest::new(Some(&[(
"RUSTFLAGS",
OsString::from("-Cprofile-generate=."),
)]))?,
)
}
/// Test that building a simple Rust crate with cargo using sccache results in a cache hit
/// when built a second time and a cache miss, when the environment variable referenced via
/// env! is changed.
fn test_rust_cargo_cmd(cmd: &str, test_info: SccacheTest) -> Result<()> {
// `cargo clean` first, just to be sure there's no leftover build objects.
cargo_clean(&test_info)?;
// Now build the crate with cargo.
Command::new(CARGO.as_os_str())
.args([cmd, "--color=never"])
.envs(test_info.env.iter().cloned())
.current_dir(CRATE_DIR.as_os_str())
.assert()
.try_stderr(predicates::str::contains("\x1b[").from_utf8().not())?
.try_success()?;
// Clean it so we can build it again.
cargo_clean(&test_info)?;
Command::new(CARGO.as_os_str())
.args([cmd, "--color=always"])
.envs(test_info.env.iter().cloned())
.current_dir(CRATE_DIR.as_os_str())
.assert()
.try_stderr(predicates::str::contains("\x1b[").from_utf8())?
.try_success()?;
test_info
.show_stats()?
.try_stdout(
predicates::str::contains(
r#""cache_hits":{"counts":{"Rust":2},"adv_counts":{"rust":2}}"#,
)
.from_utf8(),
)?
.try_success()?;
Ok(())
}
fn restart_sccache(
test_info: &SccacheTest,
additional_envs: Option<Vec<(String, String)>>,
) -> Result<()> {
let cache_dir = test_info.tempdir.path().join("cache");
stop_sccache()?;
trace!("sccache --start-server");
let mut cmd = Command::new(SCCACHE_BIN.as_os_str());
cmd.arg("--start-server");
cmd.env("SCCACHE_DIR", &cache_dir);
if let Some(additional_envs) = additional_envs {
cmd.envs(additional_envs);
}
cmd.assert()
.try_success()
.context("Failed to start sccache server")?;
Ok(())
}
/// Test that building a simple Rust crate with cargo using sccache results in the following behaviors (for three different runs):
/// - In read-only mode, a cache miss.
/// - In read-write mode, a cache miss.
/// - In read-only mode, a cache hit.
///
/// The environment variable for read/write mode is added by this function.
fn test_rust_cargo_cmd_readonly(cmd: &str, test_info: SccacheTest) -> Result<()> {
// `cargo clean` first, just to be sure there's no leftover build objects.
cargo_clean(&test_info)?;
// The cache must be put into read-only mode, and that can only be configured
// when the server starts up, so we need to restart it.
restart_sccache(
&test_info,
Some(vec![("SCCACHE_LOCAL_RW_MODE".into(), "READ_ONLY".into())]),
)?;
// Now build the crate with cargo.
Command::new(CARGO.as_os_str())
.args([cmd, "--color=never"])
.envs(test_info.env.iter().cloned())
.current_dir(CRATE_DIR.as_os_str())
.assert()
.try_stderr(predicates::str::contains("\x1b[").from_utf8().not())?
.try_success()?;
// Stats reset on server restart, so this needs to be run for each build.
test_info
.show_stats()?
.try_stdout(
predicates::str::contains(r#""cache_hits":{"counts":{},"adv_counts":{}}"#).from_utf8(),
)?
.try_stdout(
predicates::str::contains(
r#""cache_misses":{"counts":{"Rust":2},"adv_counts":{"rust":2}}"#,
)
.from_utf8(),
)?
.try_success()?;
cargo_clean(&test_info)?;
restart_sccache(
&test_info,
Some(vec![("SCCACHE_LOCAL_RW_MODE".into(), "READ_WRITE".into())]),
)?;
Command::new(CARGO.as_os_str())
.args([cmd, "--color=always"])
.envs(test_info.env.iter().cloned())
.current_dir(CRATE_DIR.as_os_str())
.assert()
.try_stderr(predicates::str::contains("\x1b[").from_utf8())?
.try_success()?;
test_info
.show_stats()?
.try_stdout(
predicates::str::contains(r#""cache_hits":{"counts":{},"adv_counts":{}}"#).from_utf8(),
)?
.try_stdout(
predicates::str::contains(
r#""cache_misses":{"counts":{"Rust":2},"adv_counts":{"rust":2}}"#,
)
.from_utf8(),
)?
.try_success()?;
cargo_clean(&test_info)?;
restart_sccache(
&test_info,
Some(vec![("SCCACHE_LOCAL_RW_MODE".into(), "READ_ONLY".into())]),
)?;
Command::new(CARGO.as_os_str())
.args([cmd, "--color=always"])
.envs(test_info.env.iter().cloned())
.current_dir(CRATE_DIR.as_os_str())
.assert()
.try_stderr(predicates::str::contains("\x1b[").from_utf8())?
.try_success()?;
test_info
.show_stats()?
.try_stdout(
predicates::str::contains(
r#""cache_hits":{"counts":{"Rust":2},"adv_counts":{"rust":2}}"#,
)
.from_utf8(),
)?
.try_stdout(
predicates::str::contains(r#""cache_misses":{"counts":{},"adv_counts":{}}"#)
.from_utf8(),
)?
.try_success()?;
Ok(())
}
fn test_rust_cargo_env_dep(test_info: SccacheTest) -> Result<()> {
cargo_clean(&test_info)?;
// Now build the crate with cargo.
Command::new(CARGO.as_os_str())
.args(["run", "--color=never"])
.envs(test_info.env.iter().cloned())
.current_dir(CRATE_DIR.as_os_str())
.assert()
.try_stderr(predicates::str::contains("\x1b[").from_utf8().not())?
.try_stdout(predicates::str::contains("Env var: 1"))?
.try_success()?;
// Clean it so we can build it again.
cargo_clean(&test_info)?;
Command::new(CARGO.as_os_str())
.args(["run", "--color=always"])
.envs(test_info.env.iter().cloned())
.env("TEST_ENV_VAR", "OTHER_VALUE")
.current_dir(CRATE_DIR.as_os_str())
.assert()
.try_stderr(predicates::str::contains("\x1b[").from_utf8())?
.try_stdout(predicates::str::contains("Env var: OTHER_VALUE"))?
.try_success()?;
// Now get the stats and ensure that we had one cache hit for the second build.
// The test crate has one dependency (itoa) so there are two separate compilations, but only
// itoa should be cached (due to the changed environment variable).
test_info
.show_stats()?
.try_stdout(predicates::str::contains(r#""cache_hits":{"counts":{"Rust":1}"#).from_utf8())?
.try_success()?;
drop(test_info);
Ok(())
}
/// Test that building a simple Rust crate with cargo using sccache in read-only mode with an empty cache results in
/// a cache miss that is produced by the readonly storage wrapper (and does not attempt to write to the underlying cache).
#[test]
#[serial]
fn test_rust_cargo_cmd_readonly_preemtive_block() -> Result<()> {
let test_info = SccacheTest::new(None)?;
// `cargo clean` first, just to be sure there's no leftover build objects.
cargo_clean(&test_info)?;
let sccache_log = test_info.tempdir.path().join("sccache.log");
stop_sccache()?;
restart_sccache(
&test_info,
Some(vec![
("SCCACHE_LOCAL_RW_MODE".into(), "READ_ONLY".into()),
("SCCACHE_LOG".into(), "trace".into()),
(
"SCCACHE_ERROR_LOG".into(),
sccache_log.to_str().unwrap().into(),
),
]),
)?;
// Now build the crate with cargo.
// Assert that our cache miss is due to the readonly storage wrapper, not due to the underlying disk cache.
Command::new(CARGO.as_os_str())
.args(["build", "--color=never"])
.envs(test_info.env.iter().cloned())
.current_dir(CRATE_DIR.as_os_str())
.assert()
.try_stderr(predicates::str::contains("\x1b[").from_utf8().not())?
.try_success()?;
let log_contents = fs::read_to_string(sccache_log)?;
assert!(
predicates::str::contains("server has setup with ReadOnly").eval(log_contents.as_str())
);
assert!(
predicates::str::contains("Error executing cache write: Cannot write to read-only storage")
.eval(log_contents.as_str())
);
assert!(
predicates::str::contains("DiskCache::finish_put")
.not()
.eval(log_contents.as_str())
);
// Stats reset on server restart, so this needs to be run for each build.
test_info
.show_stats()?
.try_stdout(
predicates::str::contains(r#""cache_hits":{"counts":{},"adv_counts":{}}"#).from_utf8(),
)?
.try_stdout(
predicates::str::contains(
r#""cache_misses":{"counts":{"Rust":2},"adv_counts":{"rust":2}}"#,
)
.from_utf8(),
)?
.try_success()?;
Ok(())
}