2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2022 The Jujutsu Authors
|
2022-04-09 22:53:32 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::exit;
|
2022-12-22 07:47:19 +00:00
|
|
|
use std::{env, fs};
|
2022-04-09 22:53:32 +00:00
|
|
|
|
|
|
|
use clap::Parser;
|
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
|
|
/// A fake editor, useful for testing
|
|
|
|
// It's overkill to use clap for a single argument, but we already use it in many other places...
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[clap()]
|
|
|
|
struct Args {
|
|
|
|
/// Path to the file to edit
|
|
|
|
file: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args: Args = Args::parse();
|
2022-12-22 07:47:19 +00:00
|
|
|
let edit_script_path = PathBuf::from(env::var_os("EDIT_SCRIPT").unwrap());
|
|
|
|
let edit_script = fs::read_to_string(&edit_script_path).unwrap();
|
2022-11-23 05:15:20 +00:00
|
|
|
|
|
|
|
let mut instructions = edit_script.split('\0').collect_vec();
|
|
|
|
if let Some(pos) = instructions.iter().position(|&i| i == "next invocation\n") {
|
|
|
|
// Overwrite the edit script. The next time `fake-editor` is called, it will
|
|
|
|
// only see the part after the `next invocation` command.
|
2022-12-22 07:47:19 +00:00
|
|
|
fs::write(&edit_script_path, instructions[pos + 1..].join("\0")).unwrap();
|
2022-11-23 05:15:20 +00:00
|
|
|
instructions.truncate(pos);
|
|
|
|
}
|
|
|
|
for instruction in instructions {
|
2022-04-09 22:53:32 +00:00
|
|
|
let (command, payload) = instruction.split_once('\n').unwrap_or((instruction, ""));
|
|
|
|
let parts = command.split(' ').collect_vec();
|
|
|
|
match parts.as_slice() {
|
|
|
|
[""] => {}
|
|
|
|
["fail"] => exit(1),
|
2022-12-22 07:44:04 +00:00
|
|
|
["dump", dest] => {
|
|
|
|
let dest_path = edit_script_path.parent().unwrap().join(dest);
|
|
|
|
fs::copy(&args.file, dest_path).unwrap();
|
|
|
|
}
|
2022-08-29 04:07:51 +00:00
|
|
|
["expect"] => {
|
2022-12-22 07:47:19 +00:00
|
|
|
let actual = String::from_utf8(fs::read(&args.file).unwrap()).unwrap();
|
2022-08-29 04:07:51 +00:00
|
|
|
if actual != payload {
|
2022-11-23 05:15:20 +00:00
|
|
|
eprintln!("fake-editor: Unexpected content.\n");
|
2022-11-28 01:45:27 +00:00
|
|
|
eprintln!("EXPECTED: <{payload}>\nRECEIVED: <{actual}>");
|
2022-08-29 04:07:51 +00:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
2023-01-05 04:55:20 +00:00
|
|
|
["expectpath"] => {
|
|
|
|
let actual = args.file.to_str().unwrap();
|
|
|
|
if actual != payload {
|
|
|
|
eprintln!("fake-editor: Unexpected path.\n");
|
|
|
|
eprintln!("EXPECTED: <{payload}>\nRECEIVED: <{actual}>");
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
2022-04-09 22:53:32 +00:00
|
|
|
["write"] => {
|
2023-01-05 04:55:20 +00:00
|
|
|
fs::write(&args.file, payload).unwrap_or_else(|_| {
|
|
|
|
panic!("Failed to write file {}", args.file.to_str().unwrap())
|
|
|
|
});
|
2022-04-09 22:53:32 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2022-12-15 02:30:06 +00:00
|
|
|
eprintln!("fake-editor: unexpected command: {command}");
|
2022-04-09 22:53:32 +00:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|