| 1 |
// Copyright 2025 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::env; |
| 16 |
use std::fs; |
| 17 |
use std::path::PathBuf; |
| 18 |
use std::process::exit; |
| 19 |
|
| 20 |
use clap::Parser; |
| 21 |
use itertools::Itertools as _; |
| 22 |
#[cfg(unix)] |
| 23 |
use nix::sys::signal; |
| 24 |
|
| 25 |
/// A fake diff-editor, useful for testing |
| 26 |
#[derive(Parser, Debug)] |
| 27 |
#[clap()] |
| 28 |
struct Args { |
| 29 |
/// Fail if the given file doesn't exist |
| 30 |
#[arg(long)] |
| 31 |
require_file: Option<String>, |
| 32 |
} |
| 33 |
|
| 34 |
fn main() { |
| 35 |
let args: Args = Args::parse(); |
| 36 |
let edit_script_path = PathBuf::from(env::var_os("BISECTION_SCRIPT").unwrap()); |
| 37 |
let commit_to_test = env::var_os("JJ_BISECT_TARGET") |
| 38 |
.unwrap() |
| 39 |
.to_str() |
| 40 |
.unwrap() |
| 41 |
.to_owned(); |
| 42 |
println!("fake-bisector testing commit {commit_to_test}"); |
| 43 |
let edit_script = fs::read_to_string(&edit_script_path).unwrap(); |
| 44 |
|
| 45 |
if let Some(path) = args.require_file |
| 46 |
&& !std::fs::exists(&path).unwrap() |
| 47 |
{ |
| 48 |
exit(1) |
| 49 |
} |
| 50 |
|
| 51 |
let mut instructions = edit_script.split('\0').collect_vec(); |
| 52 |
if let Some(pos) = instructions.iter().position(|&i| i == "next invocation\n") { |
| 53 |
// Overwrite the edit script. The next time `fake-bisector` is called, it will |
| 54 |
// only see the part after the `next invocation` command. |
| 55 |
fs::write(&edit_script_path, instructions[pos + 1..].join("\0")).unwrap(); |
| 56 |
instructions.truncate(pos); |
| 57 |
} |
| 58 |
for instruction in instructions { |
| 59 |
let (command, payload) = instruction.split_once('\n').unwrap_or((instruction, "")); |
| 60 |
let parts = command.split(' ').collect_vec(); |
| 61 |
match parts.as_slice() { |
| 62 |
[""] => {} |
| 63 |
["crash"] => { |
| 64 |
// Coredump generation varies by UNIX and is irrelevant to tests |
| 65 |
// Prefer raising SIGTERM to crash without dumping core |
| 66 |
#[cfg(unix)] |
| 67 |
let _ = signal::raise(signal::Signal::SIGTERM); |
| 68 |
std::process::abort() |
| 69 |
} |
| 70 |
["abort"] => exit(127), |
| 71 |
["skip"] => exit(125), |
| 72 |
["fail"] => exit(1), |
| 73 |
["fail-if-target-is", bad_target_commit] => { |
| 74 |
if commit_to_test == *bad_target_commit { |
| 75 |
exit(1) |
| 76 |
} |
| 77 |
} |
| 78 |
["write", path] => { |
| 79 |
fs::write(path, payload).unwrap_or_else(|_| panic!("Failed to write file {path}")); |
| 80 |
} |
| 81 |
["jj", args @ ..] => { |
| 82 |
let jj_executable_path = PathBuf::from(env::var_os("JJ_EXECUTABLE_PATH").unwrap()); |
| 83 |
let status = std::process::Command::new(&jj_executable_path) |
| 84 |
.args(args) |
| 85 |
.status() |
| 86 |
.unwrap(); |
| 87 |
if !status.success() { |
| 88 |
eprintln!("fake-bisector: failed to run jj: {status:?}"); |
| 89 |
exit(1) |
| 90 |
} |
| 91 |
} |
| 92 |
_ => { |
| 93 |
eprintln!("fake-bisector: unexpected command: {command}"); |
| 94 |
exit(1) |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |