| 1 |
mod cli { |
| 2 |
use std::path::Path; |
| 3 |
use std::process::{Command, Stdio}; |
| 4 |
use std::time::{SystemTime, Duration}; |
| 5 |
|
| 6 |
use anyhow::Result; |
| 7 |
use test_dir::{TestDir, DirBuilder, FileType}; |
| 8 |
use std::fs::File; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
const COUNT_INVOCATIONS: &str = r#"file=${1:?} lines=0; \ |
| 13 |
printf '%s' '.' >> "$file"; \ |
| 14 |
read < "$file"; \ |
| 15 |
printf '%s' "${#REPLY}";"#; |
| 16 |
const PRINT_ARGS: &str = r#"args=("$@"); declare -p args;"#; |
| 17 |
const EXIT_WITH: &str = r#"exit "${1:?}";"#; |
| 18 |
const EXIT_WITH_ENV: &str = r#"exit "${EXIT_WITH:?}";"#; |
| 19 |
const AWAIT_AND_TOUCH: &str = r#"echo awaiting; \ |
| 20 |
until [[ -e "${1:?}" ]]; do sleep .1; done; \ |
| 21 |
echo > "${2:?}";"#; |
| 22 |
|
| 23 |
fn bkt<P: AsRef<Path>>(cache_dir: P) -> Command { |
| 24 |
let test_exe = std::env::current_exe().expect("Could not resolve test location"); |
| 25 |
let dir = test_exe |
| 26 |
.parent().expect("Could not resolve test directory") |
| 27 |
.parent().expect("Could not resolve binary directory"); |
| 28 |
let mut path = dir.join("bkt"); |
| 29 |
if !path.exists() { |
| 30 |
path.set_extension("exe"); |
| 31 |
} |
| 32 |
assert!(path.exists(), "Could not find bkt binary in {:?}", dir); |
| 33 |
let mut bkt = Command::new(&path); |
| 34 |
bkt.env("BKT_TMPDIR", cache_dir.as_ref().as_os_str()); |
| 35 |
bkt |
| 36 |
} |
| 37 |
|
| 38 |
#[derive(Eq, PartialEq, Debug)] |
| 39 |
struct CmdResult { |
| 40 |
out: String, |
| 41 |
err: String, |
| 42 |
status: Option<i32>, |
| 43 |
} |
| 44 |
|
| 45 |
impl From<std::process::Output> for CmdResult { |
| 46 |
fn from(output: std::process::Output) -> Self { |
| 47 |
CmdResult { |
| 48 |
out: std::str::from_utf8(&output.stdout).unwrap().into(), |
| 49 |
err: std::str::from_utf8(&output.stderr).unwrap().into(), |
| 50 |
status: output.status.code() |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
fn run(cmd: &mut Command) -> CmdResult { |
| 56 |
cmd.output().unwrap().into() |
| 57 |
} |
| 58 |
|
| 59 |
fn succeed(cmd: &mut Command) -> String { |
| 60 |
let result = run(cmd); |
| 61 |
if cfg!(feature="debug") { |
| 62 |
if !result.err.is_empty() { eprintln!("stderr:\n{}", result.err); } |
| 63 |
} else { |
| 64 |
|
| 65 |
assert_eq!(result.err, ""); |
| 66 |
} |
| 67 |
assert_eq!(result.status, Some(0)); |
| 68 |
result.out |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
fn wait_for_contents_to_change<P: AsRef<Path>>(file: P, initial_contents: &str) { |
| 78 |
for _ in 1..50 { |
| 79 |
if std::fs::read_to_string(&file).unwrap() != initial_contents { return; } |
| 80 |
std::thread::sleep(Duration::from_millis(100)); |
| 81 |
} |
| 82 |
panic!("Contents of {} did not change", file.as_ref().to_string_lossy()); |
| 83 |
} |
| 84 |
|
| 85 |
fn make_dir_stale<P: AsRef<Path>>(dir: P, age: Duration) -> Result<()> { |
| 86 |
debug_assert!(dir.as_ref().is_dir()); |
| 87 |
let desired_time = SystemTime::now() - age; |
| 88 |
let stale_time = filetime::FileTime::from_system_time(desired_time); |
| 89 |
for entry in std::fs::read_dir(dir)? { |
| 90 |
let path = entry?.path(); |
| 91 |
let last_modified = std::fs::metadata(&path)?.modified()?; |
| 92 |
|
| 93 |
if path.is_file() && last_modified > desired_time { |
| 94 |
filetime::set_file_mtime(&path, stale_time)?; |
| 95 |
} else if path.is_dir() { |
| 96 |
make_dir_stale(&path, age)?; |
| 97 |
} |
| 98 |
} |
| 99 |
Ok(()) |
| 100 |
} |
| 101 |
|
| 102 |
fn make_file_stale<P: AsRef<Path>>(file: P, age: Duration) -> Result<()> { |
| 103 |
debug_assert!(file.as_ref().is_file()); |
| 104 |
let desired_time = SystemTime::now() - age; |
| 105 |
let stale_time = filetime::FileTime::from_system_time(desired_time); |
| 106 |
filetime::set_file_mtime(&file, stale_time)?; |
| 107 |
Ok(()) |
| 108 |
} |
| 109 |
|
| 110 |
fn join<A: Clone>(beg: &[A], tail: &[A]) -> Vec<A> { |
| 111 |
beg.iter().chain(tail).cloned().collect() |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn help() { |
| 116 |
let dir = TestDir::temp(); |
| 117 |
let out = succeed(bkt(dir.path("cache")).arg("--help")); |
| 118 |
assert!(out.contains("bkt [OPTIONS] -- <COMMAND>...")); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn cached() { |
| 123 |
let dir = TestDir::temp(); |
| 124 |
let file = dir.path("file"); |
| 125 |
let args = ["--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 126 |
let first_result = run(bkt(dir.path("cache")).args(args)); |
| 127 |
|
| 128 |
for _ in 1..3 { |
| 129 |
let subsequent_result = run(bkt(dir.path("cache")).args(args)); |
| 130 |
if cfg!(feature="debug") { |
| 131 |
assert_eq!(first_result.status, subsequent_result.status); |
| 132 |
assert_eq!(first_result.out, subsequent_result.out); |
| 133 |
} else { |
| 134 |
assert_eq!(first_result, subsequent_result); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn cache_expires() { |
| 141 |
let dir = TestDir::temp(); |
| 142 |
let file = dir.path("file"); |
| 143 |
let args = ["--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 144 |
let first_result = succeed(bkt(dir.path("cache")).args(args)); |
| 145 |
assert_eq!(first_result, "1"); |
| 146 |
|
| 147 |
|
| 148 |
make_dir_stale(dir.path("cache"), Duration::from_secs(10)).unwrap(); |
| 149 |
let subsequent_result = succeed(bkt(dir.path("cache")).args(args)); |
| 150 |
assert_eq!(first_result, subsequent_result); |
| 151 |
|
| 152 |
make_dir_stale(dir.path("cache"), Duration::from_secs(120)).unwrap(); |
| 153 |
let after_stale_result = succeed(bkt(dir.path("cache")).args(args)); |
| 154 |
assert_eq!(after_stale_result, "2"); |
| 155 |
|
| 156 |
|
| 157 |
make_dir_stale(dir.path("cache"), Duration::from_secs(10)).unwrap(); |
| 158 |
let env_result = succeed(bkt(dir.path("cache")).env("BKT_TTL", "5s").args(args)); |
| 159 |
assert_eq!(env_result, "3"); |
| 160 |
} |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn cache_expires_separately() { |
| 164 |
let dir = TestDir::temp(); |
| 165 |
let file1 = dir.path("file1"); |
| 166 |
let file2 = dir.path("file2"); |
| 167 |
let args1 = ["--ttl=10s", "--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file1.to_str().unwrap()]; |
| 168 |
let args2 = ["--ttl=20s", "--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file2.to_str().unwrap()]; |
| 169 |
|
| 170 |
|
| 171 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args1)), "1"); |
| 172 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args2)), "1"); |
| 173 |
|
| 174 |
|
| 175 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args1)), "1"); |
| 176 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args2)), "1"); |
| 177 |
|
| 178 |
|
| 179 |
make_dir_stale(dir.path("cache"), Duration::from_secs(15)).unwrap(); |
| 180 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args1)), "2"); |
| 181 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args2)), "1"); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn cache_hits_with_different_settings() { |
| 186 |
let dir = TestDir::temp(); |
| 187 |
let file = dir.path("file"); |
| 188 |
let args1 = ["--ttl=10s", "--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 189 |
let args2 = ["--ttl=20s", "--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 190 |
|
| 191 |
|
| 192 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args1)), "1"); |
| 193 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args2)), "1"); |
| 194 |
|
| 195 |
|
| 196 |
make_dir_stale(dir.path("cache"), Duration::from_secs(15)).unwrap(); |
| 197 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args2)), "1"); |
| 198 |
|
| 199 |
|
| 200 |
make_dir_stale(dir.path("cache"), Duration::from_secs(60)).unwrap(); |
| 201 |
succeed(bkt(dir.path("cache")).args(["--", "bash", "-c", "sleep 1"])); |
| 202 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args1)), "2"); |
| 203 |
} |
| 204 |
|
| 205 |
#[test] |
| 206 |
fn cache_refreshes_in_background() { |
| 207 |
let dir = TestDir::temp(); |
| 208 |
let file = dir.path("file"); |
| 209 |
let args = ["--stale=10s", "--ttl=20s", "--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 210 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args)), "1"); |
| 211 |
|
| 212 |
make_dir_stale(dir.path("cache"), Duration::from_secs(15)).unwrap(); |
| 213 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args)), "1"); |
| 214 |
|
| 215 |
wait_for_contents_to_change(&file, "."); |
| 216 |
assert_eq!(std::fs::read_to_string(&file).unwrap(), ".."); |
| 217 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args)), "2"); |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn discard_failures() { |
| 222 |
let dir = TestDir::temp(); |
| 223 |
let file = dir.path("file"); |
| 224 |
let cmd = format!("{} false;", COUNT_INVOCATIONS); |
| 225 |
let args = ["--discard-failures", "--", "bash", "-c", &cmd, "arg0", file.to_str().unwrap()]; |
| 226 |
|
| 227 |
let result = run(bkt(dir.path("cache")).args(args)); |
| 228 |
assert_eq!(result.out, "1"); |
| 229 |
assert_eq!(result.status, Some(1)); |
| 230 |
|
| 231 |
|
| 232 |
let result = run(bkt(dir.path("cache")).args(args)); |
| 233 |
assert_eq!(result.out, "2"); |
| 234 |
assert_eq!(result.status, Some(1)); |
| 235 |
} |
| 236 |
|
| 237 |
#[test] |
| 238 |
fn discard_failure_cached_separately() { |
| 239 |
let dir = TestDir::temp(); |
| 240 |
|
| 241 |
let allow_args = ["--", "bash", "-c", EXIT_WITH_ENV, "arg0"]; |
| 242 |
let discard_args = join(&["--discard-failures"], &allow_args); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
let result1 = run(bkt(dir.path("cache")).args(allow_args).env("EXIT_WITH", "14")); |
| 247 |
assert_eq!(result1.status, Some(14)); |
| 248 |
let result2 = run(bkt(dir.path("cache")).args(discard_args).env("EXIT_WITH", "0")); |
| 249 |
assert_eq!(result2.status, Some(0)); |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn discard_failures_in_background() { |
| 254 |
let dir = TestDir::temp(); |
| 255 |
let file = dir.path("file"); |
| 256 |
let cmd = format!("{} ! \"${{FAIL:-false}}\";", COUNT_INVOCATIONS); |
| 257 |
let args = ["--ttl=20s", "--discard-failures", "--", "bash", "-c", &cmd, "arg0", file.to_str().unwrap()]; |
| 258 |
let stale_args = join(&["--stale=10s"], &args); |
| 259 |
|
| 260 |
|
| 261 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args)), "1"); |
| 262 |
|
| 263 |
|
| 264 |
std::env::set_var("FAIL", "true"); |
| 265 |
|
| 266 |
|
| 267 |
make_dir_stale(dir.path("cache"), Duration::from_secs(15)).unwrap(); |
| 268 |
assert_eq!(succeed(bkt(dir.path("cache")).args(&stale_args)), "1"); |
| 269 |
|
| 270 |
|
| 271 |
wait_for_contents_to_change(&file, "."); |
| 272 |
assert_eq!(std::fs::read_to_string(&file).unwrap(), ".."); |
| 273 |
|
| 274 |
|
| 275 |
assert_eq!(succeed(bkt(dir.path("cache")).args(args)), "1"); |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn respects_cache_dir() { |
| 280 |
let dir = TestDir::temp(); |
| 281 |
let file = dir.path("file"); |
| 282 |
let args = ["--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 283 |
|
| 284 |
let first_call = succeed(bkt(dir.path("cache")).arg(format!("--cache-dir={}", dir.path("cache").display())).args(args)); |
| 285 |
assert_eq!(first_call, "1"); |
| 286 |
assert_eq!(first_call, succeed(bkt(dir.path("cache")).arg(format!("--cache-dir={}", dir.path("cache").display())).args(args))); |
| 287 |
|
| 288 |
let diff_cache = succeed(bkt(dir.path("cache")).arg(format!("--cache-dir={}", dir.path("new-cache").display())).args(args)); |
| 289 |
assert_eq!(diff_cache, "2"); |
| 290 |
|
| 291 |
let env_cache = succeed(bkt(dir.path("cache")).env("BKT_CACHE_DIR", dir.path("env-cache").as_os_str()).args(args)); |
| 292 |
assert_eq!(env_cache, "3"); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
#[test] |
| 297 |
fn respects_relative_cache() { |
| 298 |
let dir = TestDir::temp(); |
| 299 |
let cwd = dir.path("cwd"); |
| 300 |
std::fs::create_dir(&cwd).unwrap(); |
| 301 |
let file = dir.path("file"); |
| 302 |
let args = ["--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 303 |
|
| 304 |
let first_call = succeed(bkt(dir.path("unused")).arg("--cache-dir=cache").args(args).current_dir(&cwd)); |
| 305 |
assert_eq!(first_call, "1"); |
| 306 |
assert_eq!(first_call, succeed(bkt(dir.path("unused")).arg("--cache-dir=cache").args(args).current_dir(&cwd))); |
| 307 |
} |
| 308 |
|
| 309 |
#[test] |
| 310 |
fn respects_cache_scope() { |
| 311 |
let dir = TestDir::temp(); |
| 312 |
let file = dir.path("file"); |
| 313 |
let args = ["--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 314 |
|
| 315 |
let first_call = succeed(bkt(dir.path("cache")).args(args)); |
| 316 |
assert_eq!(first_call, "1"); |
| 317 |
assert_eq!(first_call, succeed(bkt(dir.path("cache")).args(args))); |
| 318 |
|
| 319 |
let diff_scope = succeed(bkt(dir.path("cache")) |
| 320 |
.arg("--scope=foo").args(args)); |
| 321 |
assert_eq!(diff_scope, "2"); |
| 322 |
assert_eq!(diff_scope, succeed(bkt(dir.path("cache")) |
| 323 |
.arg("--scope=foo").args(args))); |
| 324 |
assert_eq!(diff_scope, succeed(bkt(dir.path("cache")) |
| 325 |
.env("BKT_SCOPE", "foo").args(args))); |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn respects_args() { |
| 330 |
let dir = TestDir::temp(); |
| 331 |
let file = dir.path("file"); |
| 332 |
let args = ["--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 333 |
|
| 334 |
let first_call = succeed(bkt(dir.path("cache")).args(args)); |
| 335 |
assert_eq!(first_call, "1"); |
| 336 |
assert_eq!(first_call, succeed(bkt(dir.path("cache")).args(args))); |
| 337 |
|
| 338 |
let diff_args = succeed(bkt(dir.path("cache")).args(args).arg("A B")); |
| 339 |
assert_eq!(diff_args, "2"); |
| 340 |
|
| 341 |
let split_args = succeed(bkt(dir.path("cache")).args(args).args(["A", "B"])); |
| 342 |
assert_eq!(split_args, "3"); |
| 343 |
} |
| 344 |
|
| 345 |
#[test] |
| 346 |
fn respects_cwd() { |
| 347 |
let dir = TestDir::temp() |
| 348 |
.create("dir1", FileType::Dir) |
| 349 |
.create("dir2", FileType::Dir); |
| 350 |
let args = ["--", "bash", "-c", "pwd"]; |
| 351 |
let cwd_args = join(&["--cwd"], &args); |
| 352 |
|
| 353 |
let without_cwd_dir1 = succeed(bkt(dir.path("cache")).args(args).current_dir(dir.path("dir1"))); |
| 354 |
let without_cwd_dir2 = succeed(bkt(dir.path("cache")).args(args).current_dir(dir.path("dir2"))); |
| 355 |
assert!(without_cwd_dir1.trim().ends_with("/dir1")); |
| 356 |
assert!(without_cwd_dir2.trim().ends_with("/dir1")); |
| 357 |
|
| 358 |
let cwd_dir1 = succeed(bkt(dir.path("cache")).args(&cwd_args).current_dir(dir.path("dir1"))); |
| 359 |
let cwd_dir2 = succeed(bkt(dir.path("cache")).args(&cwd_args).current_dir(dir.path("dir2"))); |
| 360 |
assert!(cwd_dir1.trim().ends_with("/dir1")); |
| 361 |
assert!(cwd_dir2.trim().ends_with("/dir2")); |
| 362 |
} |
| 363 |
|
| 364 |
#[test] |
| 365 |
#[cfg(not(feature = "debug"))] |
| 366 |
fn respects_env() { |
| 367 |
let dir = TestDir::temp(); |
| 368 |
let args = ["--", "bash", "-c", r#"printf 'foo:%s bar:%s baz:%s' "$FOO" "$BAR" "$BAZ""#]; |
| 369 |
let env_args = join(&["--env=FOO", "--env=BAR"], &args); |
| 370 |
|
| 371 |
let without_env = succeed(bkt(dir.path("cache")).args(args) |
| 372 |
.env("FOO", "1").env("BAR", "1").env("BAZ", "1")); |
| 373 |
assert_eq!(without_env, succeed(bkt(dir.path("cache")).args(args))); |
| 374 |
|
| 375 |
assert_eq!(without_env, succeed(bkt(dir.path("cache")).args(&env_args))); |
| 376 |
|
| 377 |
let env = succeed(bkt(dir.path("cache")).args(&env_args) |
| 378 |
.env("FOO", "2").env("BAR", "2").env("BAZ", "2")); |
| 379 |
assert_eq!(env, "foo:2 bar:2 baz:2"); |
| 380 |
let env = succeed(bkt(dir.path("cache")).args(&env_args) |
| 381 |
.env("FOO", "3").env("BAR", "2").env("BAZ", "3")); |
| 382 |
assert_eq!(env, "foo:3 bar:2 baz:3"); |
| 383 |
let env = succeed(bkt(dir.path("cache")).args(&env_args) |
| 384 |
.env("FOO", "4").env("BAR", "4").env("BAZ", "4")); |
| 385 |
assert_eq!(env, "foo:4 bar:4 baz:4"); |
| 386 |
let env = succeed(bkt(dir.path("cache")).args(&env_args) |
| 387 |
.env("FOO", "2").env("BAR", "2").env("BAZ", "5")); |
| 388 |
assert_eq!(env, "foo:2 bar:2 baz:2"); |
| 389 |
} |
| 390 |
|
| 391 |
#[test] |
| 392 |
fn respects_modtime() { |
| 393 |
let dir = TestDir::temp(); |
| 394 |
let file = dir.path("file"); |
| 395 |
let watch_file = dir.path("watch"); |
| 396 |
let args = ["--modtime", watch_file.to_str().unwrap(), "--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 397 |
let no_file_result = succeed(bkt(dir.path("cache")).args(args)); |
| 398 |
|
| 399 |
assert_eq!(no_file_result, "1"); |
| 400 |
assert_eq!(no_file_result, succeed(bkt(dir.path("cache")).args(args))); |
| 401 |
|
| 402 |
|
| 403 |
File::create(&watch_file).unwrap(); |
| 404 |
let new_file_result = succeed(bkt(dir.path("cache")).args(args)); |
| 405 |
assert_eq!(new_file_result, "2"); |
| 406 |
assert_eq!(new_file_result, succeed(bkt(dir.path("cache")).args(args))); |
| 407 |
|
| 408 |
|
| 409 |
make_file_stale(&watch_file, Duration::from_secs(10)).unwrap(); |
| 410 |
let old_file_result = succeed(bkt(dir.path("cache")).args(args)); |
| 411 |
assert_eq!(old_file_result, "3"); |
| 412 |
assert_eq!(old_file_result, succeed(bkt(dir.path("cache")).args(args))); |
| 413 |
} |
| 414 |
|
| 415 |
#[test] |
| 416 |
#[cfg(not(feature="debug"))] |
| 417 |
fn no_debug_output() { |
| 418 |
let dir = TestDir::temp(); |
| 419 |
let args = ["--", "bash", "-c", "true"]; |
| 420 |
|
| 421 |
|
| 422 |
assert_eq!(run(bkt(dir.path("cache")).args(args)), |
| 423 |
CmdResult { out: "".into(), err: "".into(), status: Some(0) }); |
| 424 |
|
| 425 |
assert_eq!(run(bkt(dir.path("cache")).args(args)), |
| 426 |
CmdResult { out: "".into(), err: "".into(), status: Some(0) }); |
| 427 |
} |
| 428 |
|
| •429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
#[test] |
| 451 |
fn output_preserved() { |
| 452 |
let dir = TestDir::temp(); |
| 453 |
fn same_output(dir: &TestDir, args: &[&str]) { |
| 454 |
let bkt_args = ["--", "bash", "-c", PRINT_ARGS, "arg0"]; |
| 455 |
|
| 456 |
assert_eq!( |
| 457 |
succeed(bkt(dir.path("cache")).args(bkt_args).args(args)), |
| 458 |
succeed(bkt(dir.path("cache")).args(bkt_args).args(args))); |
| 459 |
} |
| 460 |
|
| 461 |
same_output(&dir, &[]); |
| 462 |
same_output(&dir, &[""]); |
| 463 |
same_output(&dir, &["a", "b"]); |
| 464 |
same_output(&dir, &["a b"]); |
| 465 |
same_output(&dir, &["a b", "c"]); |
| 466 |
} |
| 467 |
|
| 468 |
#[test] |
| 469 |
#[cfg(not(feature="debug"))] |
| 470 |
fn sensitive_output() { |
| 471 |
let dir = TestDir::temp(); |
| 472 |
let args = ["--", "bash", "-c", r"printf 'foo\0bar'; printf 'bar\0baz\n' >&2"]; |
| 473 |
|
| 474 |
|
| 475 |
let output = run(bkt(dir.path("cache")).args(args)); |
| 476 |
assert_eq!(output, |
| 477 |
CmdResult { out: "foo\u{0}bar".into(), err: "bar\u{0}baz\n".into(), status: Some(0) }); |
| 478 |
|
| 479 |
assert_eq!(run(bkt(dir.path("cache")).args(args)), output); |
| 480 |
} |
| 481 |
|
| 482 |
#[test] |
| 483 |
fn exit_code_preserved() { |
| 484 |
let dir = TestDir::temp(); |
| 485 |
let args = ["--", "bash", "-c", EXIT_WITH, "arg0"]; |
| 486 |
|
| 487 |
assert_eq!(run(bkt(dir.path("cache")).args(args).arg("14")).status, Some(14)); |
| 488 |
assert_eq!(run(bkt(dir.path("cache")).args(args).arg("14")).status, Some(14)); |
| 489 |
} |
| 490 |
|
| 491 |
#[test] |
| 492 |
fn warm() { |
| 493 |
let dir = TestDir::temp(); |
| 494 |
let await_file = dir.path("await"); |
| 495 |
let touch_file = dir.path("touch"); |
| 496 |
let args = ["--", "bash", "-c", AWAIT_AND_TOUCH, "arg0", |
| 497 |
await_file.to_str().unwrap(), touch_file.to_str().unwrap()]; |
| 498 |
let warm_args = join(&["--warm"], &args); |
| 499 |
|
| 500 |
let output = succeed(bkt(dir.path("cache")).args(warm_args)); |
| 501 |
assert_eq!(output, ""); |
| 502 |
assert!(!touch_file.exists()); |
| 503 |
|
| 504 |
File::create(&await_file).unwrap(); |
| 505 |
for _ in 0..10 { |
| 506 |
if touch_file.exists() { break; } |
| 507 |
std::thread::sleep(Duration::from_millis(200)); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
assert!(touch_file.exists()); |
| 513 |
|
| 514 |
std::fs::remove_file(&await_file).unwrap(); |
| 515 |
let output = succeed(bkt(dir.path("cache")).args(args)); |
| 516 |
assert_eq!(output, "awaiting\n"); |
| 517 |
} |
| 518 |
|
| 519 |
#[test] |
| 520 |
fn force() { |
| 521 |
let dir = TestDir::temp(); |
| 522 |
let file = dir.path("file"); |
| 523 |
let args = ["--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap()]; |
| 524 |
let args_force = join(&["--force"], &args); |
| 525 |
|
| 526 |
let output = succeed(bkt(dir.path("cache")).args(args)); |
| 527 |
assert_eq!(output, "1"); |
| 528 |
let output = succeed(bkt(dir.path("cache")).args(args)); |
| 529 |
assert_eq!(output, "1"); |
| 530 |
|
| 531 |
let output = succeed(bkt(dir.path("cache")).args(args_force)); |
| 532 |
assert_eq!(output, "2"); |
| 533 |
let output = succeed(bkt(dir.path("cache")).args(args)); |
| 534 |
assert_eq!(output, "2"); |
| 535 |
} |
| 536 |
|
| 537 |
#[test] |
| 538 |
fn concurrent_call_race() { |
| 539 |
let dir = TestDir::temp(); |
| 540 |
let file = dir.path("file"); |
| 541 |
let slow_count_invocations = format!(r#"sleep "0.5$RANDOM"; {}"#, COUNT_INVOCATIONS); |
| 542 |
let args = ["--", "bash", "-c", &slow_count_invocations, "arg0", file.to_str().unwrap()]; |
| 543 |
println!("{:?}", args); |
| 544 |
|
| 545 |
let proc1 = bkt(dir.path("cache")).args(args).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap(); |
| 546 |
let proc2 = bkt(dir.path("cache")).args(args).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap(); |
| 547 |
let result1: CmdResult = proc1.wait_with_output().unwrap().into(); |
| 548 |
if !cfg!(feature="debug") { assert_eq!(result1.err, ""); } |
| 549 |
assert_eq!(result1.status, Some(0)); |
| 550 |
let result2: CmdResult = proc2.wait_with_output().unwrap().into(); |
| 551 |
if !cfg!(feature="debug") { assert_eq!(result2.err, ""); } |
| 552 |
assert_eq!(result2.status, Some(0)); |
| 553 |
|
| 554 |
assert_eq!(std::fs::read_to_string(&file).unwrap(), ".."); |
| 555 |
assert!(result1.out == "2" || result2.out == "2"); |
| 556 |
} |
| 557 |
} |