ok/jj
1
0
Fork 0
forked from mirrors/jj

tests: add tests for jj describe

This commit is contained in:
Martin von Zweigbergk 2022-04-09 15:53:32 -07:00 committed by Martin von Zweigbergk
parent 90ca03b244
commit f8724ee5a9
5 changed files with 118 additions and 3 deletions

View file

@ -16,6 +16,10 @@ categories = ["command-line-utilities", "development-tools"]
name = "jj"
path = "src/main.rs"
[[bin]]
name = "fake-editor"
path = "testing/fake-editor.rs"
[[bin]]
name = "fake-diff-editor"
path = "testing/fake-diff-editor.rs"

View file

@ -48,7 +48,7 @@ fn files_recursively(dir: &PathBuf) -> HashSet<String> {
fn main() {
let args: Args = Args::parse();
let edit_script_path = PathBuf::from(std::env::var_os("EDIT_SCRIPT").unwrap());
let edit_script_path = PathBuf::from(std::env::var_os("DIFF_EDIT_SCRIPT").unwrap());
let edit_script = String::from_utf8(std::fs::read(&edit_script_path).unwrap()).unwrap();
for instruction in edit_script.split('\0') {
let (command, payload) = instruction.split_once('\n').unwrap_or((instruction, ""));

49
testing/fake-editor.rs Normal file
View file

@ -0,0 +1,49 @@
// Copyright 2022 Google LLC
//
// 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;
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();
let edit_script_path = PathBuf::from(std::env::var_os("EDIT_SCRIPT").unwrap());
let edit_script = String::from_utf8(std::fs::read(&edit_script_path).unwrap()).unwrap();
for instruction in edit_script.split('\0') {
let (command, payload) = instruction.split_once('\n').unwrap_or((instruction, ""));
let parts = command.split(' ').collect_vec();
match parts.as_slice() {
[""] => {}
["fail"] => exit(1),
["write"] => {
std::fs::write(&args.file, payload).unwrap();
}
_ => {
eprintln!("unexpected command: {}", command);
exit(1)
}
}
}
}

View file

@ -109,6 +109,19 @@ impl TestEnvironment {
self.env_vars.insert(key.to_string(), val.to_string());
}
/// Sets up the fake editor to read an edit script from the returned path
pub fn set_up_fake_editor(&mut self) -> PathBuf {
let editor_path = assert_cmd::cargo::cargo_bin("fake-editor");
assert!(editor_path.is_file());
// Simplified TOML escaping, hoping that there are no '"' or control characters
// in it
let escaped_editor_path = editor_path.to_str().unwrap().replace('\\', r"\\");
self.add_env_var("EDITOR", &escaped_editor_path);
let edit_script = self.env_root().join("edit_script");
self.add_env_var("EDIT_SCRIPT", edit_script.to_str().unwrap());
edit_script
}
/// Sets up the fake diff-editor to read an edit script from the returned
/// path
pub fn set_up_fake_diff_editor(&mut self) -> PathBuf {
@ -127,8 +140,8 @@ impl TestEnvironment {
)
.as_bytes(),
);
let edit_script = self.env_root().join("edit_script");
self.add_env_var("EDIT_SCRIPT", edit_script.to_str().unwrap());
let edit_script = self.env_root().join("diff_edit_script");
self.add_env_var("DIFF_EDIT_SCRIPT", edit_script.to_str().unwrap());
edit_script
}
}

View file

@ -0,0 +1,49 @@
// Copyright 2022 Google LLC
//
// 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 crate::common::TestEnvironment;
pub mod common;
#[test]
fn test_describe() {
let mut test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
let edit_script = test_env.set_up_fake_editor();
// Set a description using `-m` flag
let stdout = test_env.jj_cmd_success(&repo_path, &["describe", "-m", "description from CLI"]);
insta::assert_snapshot!(stdout, @"Working copy now at: 7e0db3b0ad17 description from CLI
");
// Test making no changes
std::fs::write(&edit_script, "").unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["describe"]);
insta::assert_snapshot!(stdout, @"Working copy now at: 45bfa10db64d description from CLI
");
// Set a description in editor
std::fs::write(&edit_script, "write\ndescription from editor").unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["describe"]);
insta::assert_snapshot!(stdout, @"Working copy now at: f2ce8f1ad8fa description from editor
");
// Lines in editor starting with "JJ: " are ignored
std::fs::write(&edit_script, "write\nJJ: ignored\ndescription among comment\nJJ: ignored").unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["describe"]);
insta::assert_snapshot!(stdout, @"Working copy now at: 95664f6316ae description among comment
");
}