test_conflicts and test_resolve_command: use indoc! to indent conflict markers in tests

Apart from (IMO) looking nicer, this will also sidestep the potential problem
that if the file contains actual jj conflict markers (`>>>>>>>` in the beginning
of a line, for example), jj would currently have trouble materializing and
subsequently parsing conflicts in the file if it actually became conflicted.

I'll demo this bug in either this or a subsequent PR. It's the kind of bug that
sounds serious in theory but might never cause a problem in practice.

After this PR, only `docs/tutorial.md` has a conflict marker that's not indented.
There's only one there, so hopefully it won't be too much of a pain to deal with.

I also indented other strings in `test_conflicts.rs`. IMO, this looks nice and
more consistent with the `insta::assert_snapshot` output. I didn't spend the
time to do the same for `test_resolve_command`.
This commit is contained in:
Ilya Grigoriev 2024-03-22 19:34:28 -07:00
parent d8c84940d9
commit 02a04d0d37
6 changed files with 187 additions and 159 deletions

2
Cargo.lock generated
View File

@ -1688,6 +1688,7 @@ dependencies = [
"gix", "gix",
"hex", "hex",
"indexmap", "indexmap",
"indoc",
"insta", "insta",
"itertools 0.12.1", "itertools 0.12.1",
"jj-cli", "jj-cli",
@ -1739,6 +1740,7 @@ dependencies = [
"glob", "glob",
"hex", "hex",
"ignore", "ignore",
"indoc",
"insta", "insta",
"itertools 0.12.1", "itertools 0.12.1",
"jj-lib-proc-macros", "jj-lib-proc-macros",

View File

@ -56,6 +56,7 @@ glob = "0.3.1"
hex = "0.4.3" hex = "0.4.3"
ignore = "0.4.20" ignore = "0.4.20"
indexmap = "2.2.5" indexmap = "2.2.5"
indoc = "2.0.4"
insta = { version = "1.35.1", features = ["filters"] } insta = { version = "1.35.1", features = ["filters"] }
itertools = "0.12.1" itertools = "0.12.1"
libc = { version = "0.2.153" } libc = { version = "0.2.153" }

View File

@ -82,6 +82,7 @@ anyhow = { workspace = true }
assert_cmd = { workspace = true } assert_cmd = { workspace = true }
assert_matches = { workspace = true } assert_matches = { workspace = true }
async-trait = { workspace = true } async-trait = { workspace = true }
indoc = { workspace = true }
insta = { workspace = true } insta = { workspace = true }
test-case = { workspace = true } test-case = { workspace = true }
testutils = { workspace = true } testutils = { workspace = true }

View File

@ -14,6 +14,8 @@
use std::path::Path; use std::path::Path;
use indoc::indoc;
use crate::common::TestEnvironment; use crate::common::TestEnvironment;
fn create_commit( fn create_commit(
@ -195,15 +197,16 @@ fn test_resolution() {
&editor_script, &editor_script,
[ [
"dump editor2", "dump editor2",
"write indoc! {"
<<<<<<< write
%%%%%%% <<<<<<<
-some %%%%%%%
+fake -some
+++++++ +fake
conflict +++++++
>>>>>>> conflict
", >>>>>>>
"},
] ]
.join("\0"), .join("\0"),
) )
@ -270,15 +273,16 @@ conflict
&editor_script, &editor_script,
[ [
"dump editor3", "dump editor3",
"write indoc! {"
<<<<<<< write
%%%%%%% <<<<<<<
-some %%%%%%%
+fake -some
+++++++ +fake
conflict +++++++
>>>>>>> conflict
", >>>>>>>
"},
] ]
.join("\0"), .join("\0"),
) )

View File

@ -72,6 +72,7 @@ winreg = { workspace = true }
assert_matches = { workspace = true } assert_matches = { workspace = true }
criterion = { workspace = true } criterion = { workspace = true }
esl01-renderdag = { workspace = true } esl01-renderdag = { workspace = true }
indoc = { workspace = true }
insta = { workspace = true } insta = { workspace = true }
num_cpus = { workspace = true } num_cpus = { workspace = true }
pretty_assertions = { workspace = true } pretty_assertions = { workspace = true }

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use indoc::indoc;
use jj_lib::backend::FileId; use jj_lib::backend::FileId;
use jj_lib::conflicts::{ use jj_lib::conflicts::{
extract_as_single_hunk, materialize_merge_result, parse_conflict, update_from_content, extract_as_single_hunk, materialize_merge_result, parse_conflict, update_from_content,
@ -32,34 +33,37 @@ fn test_materialize_conflict_basic() {
let base_id = testutils::write_file( let base_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 line 1
line 3 line 2
line 4 line 3
line 5 line 4
", line 5
"},
); );
let left_id = testutils::write_file( let left_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 line 1
left 3.1 line 2
left 3.2 left 3.1
left 3.3 left 3.2
line 4 left 3.3
line 5 line 4
", line 5
"},
); );
let right_id = testutils::write_file( let right_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 line 1
right 3.1 line 2
line 4 right 3.1
line 5 line 4
", line 5
"},
); );
// The left side should come first. The diff should be use the smaller (right) // The left side should come first. The diff should be use the smaller (right)
@ -122,37 +126,41 @@ fn test_materialize_conflict_multi_rebase_conflicts() {
let base_id = testutils::write_file( let base_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 base line 1
line 3 line 2 base
", line 3
"},
); );
let a_id = testutils::write_file( let a_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 a.1 line 1
line 2 a.2 line 2 a.1
line 2 a.3 line 2 a.2
line 3 line 2 a.3
", line 3
"},
); );
let b_id = testutils::write_file( let b_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 b.1 line 1
line 2 b.2 line 2 b.1
line 3 line 2 b.2
", line 3
"},
); );
let c_id = testutils::write_file( let c_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 c.1 line 1
line 3 line 2 c.1
", line 3
"},
); );
// The order of (a, b, c) should be preserved. For all cases, the "a" side // The order of (a, b, c) should be preserved. For all cases, the "a" side
@ -240,32 +248,35 @@ fn test_materialize_parse_roundtrip() {
let base_id = testutils::write_file( let base_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 line 1
line 3 line 2
line 4 line 3
line 5 line 4
", line 5
"},
); );
let left_id = testutils::write_file( let left_id = testutils::write_file(
store, store,
path, path,
"line 1 left indoc! {"
line 2 left line 1 left
line 3 line 2 left
line 4 line 3
line 5 left line 4
", line 5 left
"},
); );
let right_id = testutils::write_file( let right_id = testutils::write_file(
store, store,
path, path,
"line 1 right indoc! {"
line 2 line 1 right
line 3 line 2
line 4 right line 3
line 5 right line 4 right
", line 5 right
"},
); );
let conflict = Merge::from_removes_adds( let conflict = Merge::from_removes_adds(
@ -335,31 +346,34 @@ fn test_materialize_conflict_modify_delete() {
let base_id = testutils::write_file( let base_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 line 1
line 3 line 2
line 4 line 3
line 5 line 4
", line 5
"},
); );
let modified_id = testutils::write_file( let modified_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 line 1
modified line 2
line 4 modified
line 5 line 4
", line 5
"},
); );
let deleted_id = testutils::write_file( let deleted_id = testutils::write_file(
store, store,
path, path,
"line 1 indoc! {"
line 2 line 1
line 4 line 2
line 5 line 4
", line 5
"},
); );
// left modifies a line, right deletes the same line. // left modifies a line, right deletes the same line.
@ -479,12 +493,13 @@ fn test_materialize_conflict_two_forward_diffs() {
fn test_parse_conflict_resolved() { fn test_parse_conflict_resolved() {
assert_eq!( assert_eq!(
parse_conflict( parse_conflict(
b"line 1 indoc! {b"
line 1
line 2 line 2
line 3 line 3
line 4 line 4
line 5 line 5
", "},
2 2
), ),
None None
@ -494,19 +509,19 @@ line 5
#[test] #[test]
fn test_parse_conflict_simple() { fn test_parse_conflict_simple() {
insta::assert_debug_snapshot!( insta::assert_debug_snapshot!(
parse_conflict( parse_conflict(indoc! {b"
b"line 1 line 1
<<<<<<< <<<<<<<
%%%%%%% %%%%%%%
line 2 line 2
-line 3 -line 3
+left +left
line 4 line 4
+++++++ +++++++
right right
>>>>>>> >>>>>>>
line 5 line 5
", "},
2 2
), ),
@r###" @r###"
@ -535,23 +550,24 @@ line 5
fn test_parse_conflict_multi_way() { fn test_parse_conflict_multi_way() {
insta::assert_debug_snapshot!( insta::assert_debug_snapshot!(
parse_conflict( parse_conflict(
b"line 1 indoc! {b"
<<<<<<< line 1
%%%%%%% <<<<<<<
line 2 %%%%%%%
-line 3 line 2
+left -line 3
line 4 +left
+++++++ line 4
right +++++++
%%%%%%% right
line 2 %%%%%%%
+forward line 2
line 3 +forward
line 4 line 3
>>>>>>> line 4
line 5 >>>>>>>
", line 5
"},
3 3
), ),
@r###" @r###"
@ -582,18 +598,19 @@ line 5
fn test_parse_conflict_different_wrong_arity() { fn test_parse_conflict_different_wrong_arity() {
assert_eq!( assert_eq!(
parse_conflict( parse_conflict(
b"line 1 indoc! {b"
<<<<<<< line 1
%%%%%%% <<<<<<<
line 2 %%%%%%%
-line 3 line 2
+left -line 3
line 4 +left
+++++++ line 4
right +++++++
>>>>>>> right
line 5 >>>>>>>
", line 5
"},
3 3
), ),
None None
@ -605,17 +622,18 @@ fn test_parse_conflict_malformed_marker() {
// The conflict marker is missing `%%%%%%%` // The conflict marker is missing `%%%%%%%`
assert_eq!( assert_eq!(
parse_conflict( parse_conflict(
b"line 1 indoc! {b"
<<<<<<< line 1
line 2 <<<<<<<
-line 3 line 2
+left -line 3
line 4 +left
+++++++ line 4
right +++++++
>>>>>>> right
line 5 >>>>>>>
", line 5
"},
2 2
), ),
None None
@ -627,18 +645,19 @@ fn test_parse_conflict_malformed_diff() {
// The diff part is invalid (missing space before "line 4") // The diff part is invalid (missing space before "line 4")
assert_eq!( assert_eq!(
parse_conflict( parse_conflict(
b"line 1 indoc! {b"
<<<<<<< line 1
%%%%%%% <<<<<<<
line 2 %%%%%%%
-line 3 line 2
+left -line 3
line 4 +left
+++++++ line 4
right +++++++
>>>>>>> right
line 5 >>>>>>>
", line 5
"},
2 2
), ),
None None