983 lines · 34.8 KB · Rust
RawDownload
// Copyright 2022 The Jujutsu Authors//// 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::Path; use indoc::indoc; use crate::common::TestEnvironment; fn create_commit(    test_env: &TestEnvironment,    repo_path: &Path,    name: &str,    parents: &[&str],    files: &[(&str, &str)],) {    if parents.is_empty() {        test_env.jj_cmd_ok(repo_path, &["new", "root()", "-m", name]);    } else {        let mut args = vec!["new", "-m", name];        args.extend(parents);        test_env.jj_cmd_ok(repo_path, &args);    }    for (name, content) in files {        std::fs::write(repo_path.join(name), content).unwrap();    }    test_env.jj_cmd_ok(repo_path, &["bookmark", "create", name]);} fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {    test_env.jj_cmd_success(repo_path, &["log", "-T", "bookmarks"])} #[test]fn test_resolution() {    let mut test_env = TestEnvironment::default();    test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);    let repo_path = test_env.env_root().join("repo");     create_commit(&test_env, &repo_path, "base", &[], &[("file", "base\n")]);    create_commit(&test_env, &repo_path, "a", &["base"], &[("file", "a\n")]);    create_commit(&test_env, &repo_path, "b", &["base"], &[("file", "b\n")]);    create_commit(&test_env, &repo_path, "conflict", &["a", "b"], &[]);    // Test the setup    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"    @    conflict    ├─╮    │ ○  b    ○ │  a    ├─╯    ○  base    ◆    "###);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["resolve", "--list"]),     @r###"    file    2-sided conflict    "###);    insta::assert_snapshot!(    std::fs::read_to_string(repo_path.join("file")).unwrap()        , @r###"    <<<<<<< Conflict 1 of 1    %%%%%%% Changes from base to side #1    -base    +a    +++++++ Contents of side #2    b    >>>>>>> Conflict 1 of 1 ends    "###);     let editor_script = test_env.set_up_fake_editor();    // Check that output file starts out empty and resolve the conflict    std::fs::write(        &editor_script,        ["dump editor0", "write\nresolution\n"].join("\0"),    )    .unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["resolve"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Resolving conflicts in: file    Working copy now at: vruxwmqv e069f073 conflict | conflict    Parent commit      : zsuskuln aa493daf a | a    Parent commit      : royxmykx db6a4daf b | b    Added 0 files, modified 1 files, removed 0 files    "###);    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r###"    "###);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", "--git"]),     @r###"    diff --git a/file b/file    index 0000000000..88425ec521 100644    --- a/file    +++ b/file    @@ -1,7 +1,1 @@    -<<<<<<< Conflict 1 of 1    -%%%%%%% Changes from base to side #1    --base    -+a    -+++++++ Contents of side #2    -b    ->>>>>>> Conflict 1 of 1 ends    +resolution    "###);    insta::assert_snapshot!(test_env.jj_cmd_cli_error(&repo_path, &["resolve", "--list"]),     @r###"    Error: No conflicts found at this revision    "###);     // Try again with --tool=<name>    test_env.jj_cmd_ok(&repo_path, &["undo"]);    std::fs::write(&editor_script, "write\nresolution\n").unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(        &repo_path,        &[            "resolve",            "--config-toml=ui.merge-editor='false'",            "--tool=fake-editor",        ],    );    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Resolving conflicts in: file    Working copy now at: vruxwmqv 1a70c7c6 conflict | conflict    Parent commit      : zsuskuln aa493daf a | a    Parent commit      : royxmykx db6a4daf b | b    Added 0 files, modified 1 files, removed 0 files    "###);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", "--git"]),    @r###"    diff --git a/file b/file    index 0000000000..88425ec521 100644    --- a/file    +++ b/file    @@ -1,7 +1,1 @@    -<<<<<<< Conflict 1 of 1    -%%%%%%% Changes from base to side #1    --base    -+a    -+++++++ Contents of side #2    -b    ->>>>>>> Conflict 1 of 1 ends    +resolution    "###);    insta::assert_snapshot!(test_env.jj_cmd_cli_error(&repo_path, &["resolve", "--list"]),    @r###"    Error: No conflicts found at this revision    "###);     // Check that the output file starts with conflict markers if    // `merge-tool-edits-conflict-markers=true`    test_env.jj_cmd_ok(&repo_path, &["undo"]);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", "--git"]),    @"");    std::fs::write(        &editor_script,        ["dump editor1", "write\nresolution\n"].join("\0"),    )    .unwrap();    test_env.jj_cmd_ok(        &repo_path,        &[            "resolve",            "--config-toml",            "merge-tools.fake-editor.merge-tool-edits-conflict-markers=true",        ],    );    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("editor1")).unwrap(), @r###"    <<<<<<< Conflict 1 of 1    %%%%%%% Changes from base to side #1    -base    +a    +++++++ Contents of side #2    b    >>>>>>> Conflict 1 of 1 ends    "###);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", "--git"]),     @r###"    diff --git a/file b/file    index 0000000000..88425ec521 100644    --- a/file    +++ b/file    @@ -1,7 +1,1 @@    -<<<<<<< Conflict 1 of 1    -%%%%%%% Changes from base to side #1    --base    -+a    -+++++++ Contents of side #2    -b    ->>>>>>> Conflict 1 of 1 ends    +resolution    "###);     // Check that if merge tool leaves conflict markers in output file and    // `merge-tool-edits-conflict-markers=true`, these markers are properly parsed.    test_env.jj_cmd_ok(&repo_path, &["undo"]);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", "--git"]),     @"");    std::fs::write(        &editor_script,        [            "dump editor2",            indoc! {"                write                <<<<<<<                %%%%%%%                -some                +fake                +++++++                conflict                >>>>>>>            "},        ]        .join("\0"),    )    .unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(        &repo_path,        &[            "resolve",            "--config-toml",            "merge-tools.fake-editor.merge-tool-edits-conflict-markers=true",        ],    );    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Resolving conflicts in: file    Working copy now at: vruxwmqv 7699b9c3 conflict | (conflict) conflict    Parent commit      : zsuskuln aa493daf a | a    Parent commit      : royxmykx db6a4daf b | b    Added 0 files, modified 1 files, removed 0 files    There are unresolved conflicts at these paths:    file    2-sided conflict    New conflicts appeared in these commits:      vruxwmqv 7699b9c3 conflict | (conflict) conflict    To resolve the conflicts, start by updating to it:      jj new vruxwmqv    Then use `jj resolve`, or edit the conflict markers in the file directly.    Once the conflicts are resolved, you may want to inspect the result with `jj diff`.    Then run `jj squash` to move the resolution into the conflicted commit.    "###);    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("editor2")).unwrap(), @r###"    <<<<<<< Conflict 1 of 1    %%%%%%% Changes from base to side #1    -base    +a    +++++++ Contents of side #2    b    >>>>>>> Conflict 1 of 1 ends    "###);    // Note the "Modified" below    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", "--git"]),     @r###"    diff --git a/file b/file    --- a/file    +++ b/file    @@ -1,7 +1,7 @@     <<<<<<< Conflict 1 of 1     %%%%%%% Changes from base to side #1    --base    -+a    +-some    ++fake     +++++++ Contents of side #2    -b    +conflict     >>>>>>> Conflict 1 of 1 ends    "###);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["resolve", "--list"]),     @r###"    file    2-sided conflict    "###);     // Check that if merge tool leaves conflict markers in output file but    // `merge-tool-edits-conflict-markers=false` or is not specified,    // `jj` considers the conflict resolved.    test_env.jj_cmd_ok(&repo_path, &["undo"]);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", "--git"]),     @"");    std::fs::write(        &editor_script,        [            "dump editor3",            indoc! {"                write                <<<<<<<                %%%%%%%                -some                +fake                +++++++                conflict                >>>>>>>            "},        ]        .join("\0"),    )    .unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["resolve"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Resolving conflicts in: file    Working copy now at: vruxwmqv 3166dfd2 conflict | conflict    Parent commit      : zsuskuln aa493daf a | a    Parent commit      : royxmykx db6a4daf b | b    Added 0 files, modified 1 files, removed 0 files    "###);    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("editor3")).unwrap(), @r###"    "###);    // Note the "Resolved" below    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", "--git"]),     @r###"    diff --git a/file b/file    index 0000000000..0610716cc1 100644    --- a/file    +++ b/file    @@ -1,7 +1,7 @@    -<<<<<<< Conflict 1 of 1    -%%%%%%% Changes from base to side #1    --base    -+a    -+++++++ Contents of side #2    -b    ->>>>>>> Conflict 1 of 1 ends    +<<<<<<<    +%%%%%%%    +-some    ++fake    ++++++++    +conflict    +>>>>>>>    "###);    insta::assert_snapshot!(test_env.jj_cmd_cli_error(&repo_path, &["resolve", "--list"]),     @r###"    Error: No conflicts found at this revision    "###);     // TODO: Check that running `jj new` and then `jj resolve -r conflict` works    // correctly.} fn check_resolve_produces_input_file(    test_env: &mut TestEnvironment,    repo_path: &Path,    filename: &str,    role: &str,    expected_content: &str,) {    let editor_script = test_env.set_up_fake_editor();    std::fs::write(editor_script, format!("expect\n{expected_content}")).unwrap();     let merge_arg_config = format!(r#"merge-tools.fake-editor.merge-args = ["${role}"]"#);    // This error means that fake-editor exited successfully but did not modify the    // output file.    // We cannot use `insta::assert_snapshot!` here after insta 1.22 due to    // https://github.com/mitsuhiko/insta/commit/745b45b. Hopefully, this will again become possible    // in the future. See also https://github.com/mitsuhiko/insta/issues/313.    assert_eq!(        test_env.jj_cmd_failure(            repo_path,            &["resolve", "--config-toml", &merge_arg_config, filename]        ),        format!(            "Resolving conflicts in: {filename}\nError: Failed to resolve conflicts\nCaused by: \             The output file is either unchanged or empty after the editor quit (run with --debug \             to see the exact invocation).\n"        )    );} #[test]fn test_normal_conflict_input_files() {    let mut test_env = TestEnvironment::default();    test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);    let repo_path = test_env.env_root().join("repo");     create_commit(&test_env, &repo_path, "base", &[], &[("file", "base\n")]);    create_commit(&test_env, &repo_path, "a", &["base"], &[("file", "a\n")]);    create_commit(&test_env, &repo_path, "b", &["base"], &[("file", "b\n")]);    create_commit(&test_env, &repo_path, "conflict", &["a", "b"], &[]);    // Test the setup    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"    @    conflict    ├─╮    │ ○  b    ○ │  a    ├─╯    ○  base    ◆    "###);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["resolve", "--list"]),     @r###"    file    2-sided conflict    "###);    insta::assert_snapshot!(    std::fs::read_to_string(repo_path.join("file")).unwrap()        , @r###"    <<<<<<< Conflict 1 of 1    %%%%%%% Changes from base to side #1    -base    +a    +++++++ Contents of side #2    b    >>>>>>> Conflict 1 of 1 ends    "###);     check_resolve_produces_input_file(&mut test_env, &repo_path, "file", "base", "base\n");    check_resolve_produces_input_file(&mut test_env, &repo_path, "file", "left", "a\n");    check_resolve_produces_input_file(&mut test_env, &repo_path, "file", "right", "b\n");} #[test]fn test_baseless_conflict_input_files() {    let mut test_env = TestEnvironment::default();    test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);    let repo_path = test_env.env_root().join("repo");     create_commit(&test_env, &repo_path, "base", &[], &[]);    create_commit(&test_env, &repo_path, "a", &["base"], &[("file", "a\n")]);    create_commit(&test_env, &repo_path, "b", &["base"], &[("file", "b\n")]);    create_commit(&test_env, &repo_path, "conflict", &["a", "b"], &[]);    // Test the setup    insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"    @    conflict    ├─╮    │ ○  b    ○ │  a    ├─╯    ○  base    ◆    "###);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["resolve", "--list"]),     @r###"    file    2-sided conflict    "###);    insta::assert_snapshot!(    std::fs::read_to_string(repo_path.join("file")).unwrap()        , @r###"    <<<<<<< Conflict 1 of 1    %%%%%%% Changes from base to side #1    +a    +++++++ Contents of side #2    b    >>>>>>> Conflict 1 of 1 ends    "###);     check_resolve_produces_input_file(&mut test_env, &repo_path, "file", "base", "");    check_resolve_produces_input_file(&mut test_env, &repo_path, "file", "left", "a\n");    check_resolve_produces_input_file(&mut test_env, &repo_path, "file", "right", "b\n");} #[test]fn test_too_many_parents() {    let test_env = TestEnvironment::default();    test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);    let repo_path = test_env.env_root().join("repo");     create_commit(&test_env, &repo_path, "base", &[], &[("file", "base\n")]);    create_commit(&test_env, &repo_path, "a", &["base"], &[("file", "a\n")]);    create_commit(&test_env, &repo_path, "b", &["base"], &[("file", "b\n")]);    create_commit(&test_env, &repo_path, "c", &["base"], &[("file", "c\n")]);    create_commit(&test_env, &repo_path, "conflict", &["a", "b", "c"], &[]);    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["resolve", "--list"]),     @r###"    file    3-sided conflict    "###);    // Test warning color    insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["resolve", "--list", "--color=always"]),     @r###"    file    3-sided conflict    "###);     let error = test_env.jj_cmd_failure(&repo_path, &["resolve"]);    insta::assert_snapshot!(error, @r###"    Hint: Using default editor ':builtin'; run `jj config set --user ui.merge-editor :builtin` to disable this message.    Resolving conflicts in: file    Error: Failed to resolve conflicts    Caused by: The conflict at "file" has 3 sides. At most 2 sides are supported.    "###);} #[test]fn test_simplify_conflict_sides() {    let mut test_env = TestEnvironment::default();    test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);    let repo_path = test_env.env_root().join("repo"); 

Lines 1–500 of 983.