734 lines · 25.1 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 indoc::indoc; use crate::common::TestEnvironment; #[test]fn test_diffedit() {    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");     std::fs::write(repo_path.join("file1"), "a\n").unwrap();    test_env.jj_cmd_ok(&repo_path, &["new"]);    std::fs::write(repo_path.join("file2"), "a\n").unwrap();    std::fs::write(repo_path.join("file3"), "a\n").unwrap();    test_env.jj_cmd_ok(&repo_path, &["new"]);    std::fs::remove_file(repo_path.join("file1")).unwrap();    std::fs::write(repo_path.join("file2"), "b\n").unwrap();     let edit_script = test_env.set_up_fake_diff_editor();     // Test the setup; nothing happens if we make no changes    std::fs::write(        &edit_script,        [            "files-before file1 file2",            "files-after JJ-INSTRUCTIONS file2",            "dump JJ-INSTRUCTIONS instrs",        ]        .join("\0"),    )    .unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["diffedit"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Nothing changed.    "###);    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("instrs")).unwrap(), @r###"    You are editing changes in: kkmpptxz 3d4cce89 (no description set)     The diff initially shows the commit's changes.     Adjust the right side until it shows the contents you want. If you    don't make any changes, then the operation will be aborted.    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    M file2    "###);     // Try again with ui.diff-instructions=false    std::fs::write(&edit_script, "files-before file1 file2\0files-after file2").unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(        &repo_path,        &["diffedit", "--config=ui.diff-instructions=false"],    );    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Nothing changed.    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    M file2    "###);     // Try again with --tool=<name>    std::fs::write(        &edit_script,        "files-before file1 file2\0files-after JJ-INSTRUCTIONS file2",    )    .unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(        &repo_path,        &[            "diffedit",            "--config=ui.diff-editor='false'",            "--tool=fake-diff-editor",        ],    );    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Nothing changed.    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    M file2    "###);     // Nothing happens if the diff-editor exits with an error    std::fs::write(&edit_script, "rm file2\0fail").unwrap();    let stderr = &test_env.jj_cmd_failure(&repo_path, &["diffedit"]);    insta::assert_snapshot!(stderr.replace("exit code:", "exit status:"), @r###"    Error: Failed to edit diff    Caused by: Tool exited with exit status: 1 (run with --debug to see the exact invocation)    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    M file2    "###);     // Can edit changes to individual files    std::fs::write(&edit_script, "reset file2").unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["diffedit"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Created kkmpptxz cbc7a725 (no description set)    Working copy now at: kkmpptxz cbc7a725 (no description set)    Parent commit      : rlvkpnrz a72506cd (no description set)    Added 0 files, modified 1 files, removed 0 files    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    "###);     // Changes to a commit are propagated to descendants    test_env.jj_cmd_ok(&repo_path, &["undo"]);    std::fs::write(&edit_script, "write file3\nmodified\n").unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["diffedit", "-r", "@-"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Created rlvkpnrz d4eef3fc (no description set)    Rebased 1 descendant commits    Working copy now at: kkmpptxz 59ef1b95 (no description set)    Parent commit      : rlvkpnrz d4eef3fc (no description set)    Added 0 files, modified 1 files, removed 0 files    "###);    let contents = String::from_utf8(std::fs::read(repo_path.join("file3")).unwrap()).unwrap();    insta::assert_snapshot!(contents, @r###"    modified    "###);     // Test diffedit --from @--    test_env.jj_cmd_ok(&repo_path, &["undo"]);    std::fs::write(        &edit_script,        "files-before file1\0files-after JJ-INSTRUCTIONS file2 file3\0reset file2",    )    .unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["diffedit", "--from", "@--"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Created kkmpptxz 5b585bd1 (no description set)    Working copy now at: kkmpptxz 5b585bd1 (no description set)    Parent commit      : rlvkpnrz a72506cd (no description set)    Added 0 files, modified 0 files, removed 1 files    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    D file2    "###);} #[test]fn test_diffedit_new_file() {    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");     std::fs::write(repo_path.join("file1"), "a\n").unwrap();    test_env.jj_cmd_ok(&repo_path, &["new"]);    std::fs::remove_file(repo_path.join("file1")).unwrap();    std::fs::write(repo_path.join("file2"), "b\n").unwrap();     let edit_script = test_env.set_up_fake_diff_editor();     // Test the setup; nothing happens if we make no changes    std::fs::write(        &edit_script,        "files-before file1\0files-after JJ-INSTRUCTIONS file2",    )    .unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["diffedit"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Nothing changed.    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    A file2    "###);     // Creating `file1` on the right side is noticed by `jj diffedit`    std::fs::write(&edit_script, "write file1\nmodified\n").unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["diffedit"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Created rlvkpnrz b0376e2b (no description set)    Working copy now at: rlvkpnrz b0376e2b (no description set)    Parent commit      : qpvuntsm b739eb46 (no description set)    Added 1 files, modified 0 files, removed 0 files    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    M file1    A file2    "###);     // Creating a file that wasn't on either side is ignored by diffedit.    // TODO(ilyagr) We should decide whether we like this behavior.    //    // On one hand, it is unexpected and potentially a minor BUG. On the other    // hand, this prevents `jj` from loading any backup files the merge tool    // generates.    test_env.jj_cmd_ok(&repo_path, &["undo"]);    std::fs::write(&edit_script, "write new_file\nnew file\n").unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["diffedit"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Nothing changed.    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    A file2    "###);} #[test]fn test_diffedit_external_tool_conflict_marker_style() {    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");    let file_path = repo_path.join("file");     // Create a conflict    std::fs::write(        &file_path,        indoc! {"        line 1        line 2        line 3        line 4        line 5    "},    )    .unwrap();    test_env.jj_cmd_ok(&repo_path, &["commit", "-m", "base"]);    std::fs::write(        &file_path,        indoc! {"        line 1        line 2.1        line 2.2        line 3        line 4.1        line 5    "},    )    .unwrap();    test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "side-a"]);    test_env.jj_cmd_ok(&repo_path, &["new", "description(base)", "-m", "side-b"]);    std::fs::write(        &file_path,        indoc! {"        line 1        line 2.3        line 3        line 4.2        line 4.3        line 5    "},    )    .unwrap();     // Resolve one of the conflicts in the working copy    test_env.jj_cmd_ok(        &repo_path,        &["new", "description(side-a)", "description(side-b)"],    );    std::fs::write(        &file_path,        indoc! {"        line 1        line 2.1        line 2.2        line 2.3        line 3        <<<<<<<        %%%%%%%        -line 4        +line 4.1        +++++++        line 4.2        line 4.3        >>>>>>>        line 5    "},    )    .unwrap();     // Set up diff editor to use "snapshot" conflict markers    let edit_script = test_env.set_up_fake_diff_editor();    test_env.add_config(r#"merge-tools.fake-diff-editor.conflict-marker-style = "snapshot""#);     // We want to see whether the diff editor is using the correct conflict markers,    // and reset it to make sure that it parses the conflict markers as well    std::fs::write(        &edit_script,        [            "files-before file",            "files-after JJ-INSTRUCTIONS file",            "dump file after-file",            "reset file",            "dump file before-file",        ]        .join("\0"),    )    .unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["diffedit"]);    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Created mzvwutvl fb39e804 (conflict) (empty) (no description set)    Working copy now at: mzvwutvl fb39e804 (conflict) (empty) (no description set)    Parent commit      : rlvkpnrz 3765cc27 side-a    Parent commit      : zsuskuln 8b3de837 side-b    Added 0 files, modified 1 files, removed 0 files    There are unresolved conflicts at these paths:    file    2-sided conflict    Existing conflicts were resolved or abandoned from these commits:      mzvwutvl hidden a813239f (conflict) (no description set)    "###);    // Conflicts should render using "snapshot" format in diff editor    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("before-file")).unwrap(), @r##"    line 1    <<<<<<< Conflict 1 of 2    +++++++ Contents of side #1    line 2.1    line 2.2    ------- Contents of base    line 2    +++++++ Contents of side #2    line 2.3    >>>>>>> Conflict 1 of 2 ends    line 3    <<<<<<< Conflict 2 of 2    +++++++ Contents of side #1    line 4.1    ------- Contents of base    line 4    +++++++ Contents of side #2    line 4.2    line 4.3    >>>>>>> Conflict 2 of 2 ends    line 5    "##);    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("after-file")).unwrap(), @r##"    line 1    line 2.1    line 2.2    line 2.3    line 3    <<<<<<< Conflict 1 of 1    +++++++ Contents of side #1    line 4.1    ------- Contents of base    line 4    +++++++ Contents of side #2    line 4.2    line 4.3    >>>>>>> Conflict 1 of 1 ends    line 5    "##);    // Conflicts should be materialized using "diff" format in working copy    insta::assert_snapshot!(        std::fs::read_to_string(&file_path).unwrap(), @r##"    line 1    <<<<<<< Conflict 1 of 2    +++++++ Contents of side #1    line 2.1    line 2.2    %%%%%%% Changes from base to side #2    -line 2    +line 2.3    >>>>>>> Conflict 1 of 2 ends    line 3    <<<<<<< Conflict 2 of 2    %%%%%%% Changes from base to side #1    -line 4    +line 4.1    +++++++ Contents of side #2    line 4.2    line 4.3    >>>>>>> Conflict 2 of 2 ends    line 5    "##);     // File should be conflicted with no changes    let stdout = test_env.jj_cmd_success(&repo_path, &["st"]);    insta::assert_snapshot!(stdout, @r###"    The working copy is clean    There are unresolved conflicts at these paths:    file    2-sided conflict    Working copy : mzvwutvl fb39e804 (conflict) (empty) (no description set)    Parent commit: rlvkpnrz 3765cc27 side-a    Parent commit: zsuskuln 8b3de837 side-b    "###);} #[test]fn test_diffedit_3pane() {    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");     std::fs::write(repo_path.join("file1"), "a\n").unwrap();    test_env.jj_cmd_ok(&repo_path, &["new"]);    std::fs::write(repo_path.join("file2"), "a\n").unwrap();    std::fs::write(repo_path.join("file3"), "a\n").unwrap();    test_env.jj_cmd_ok(&repo_path, &["new"]);    std::fs::remove_file(repo_path.join("file1")).unwrap();    std::fs::write(repo_path.join("file2"), "b\n").unwrap();     // 2 configs for a 3-pane setup. In the first, "$right" is passed to what the    // fake diff editor considers the "after" state.    let config_with_right_as_after =        "merge-tools.fake-diff-editor.edit-args=['$left', '$right', '--ignore=$output']";    let config_with_output_as_after =        "merge-tools.fake-diff-editor.edit-args=['$left', '$output', '--ignore=$right']";    let edit_script = test_env.set_up_fake_diff_editor();    std::fs::write(&edit_script, "").unwrap();     // Nothing happens if we make no changes    std::fs::write(        &edit_script,        "files-before file1 file2\0files-after JJ-INSTRUCTIONS file2",    )    .unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(        &repo_path,        &["diffedit", "--config", config_with_output_as_after],    );    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Nothing changed.    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    M file2    "###);    // Nothing happens if we make no changes, `config_with_right_as_after` version    let (stdout, stderr) = test_env.jj_cmd_ok(        &repo_path,        &["diffedit", "--config", config_with_right_as_after],    );    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Nothing changed.    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    M file2    "###);     // Can edit changes to individual files    std::fs::write(&edit_script, "reset file2").unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(        &repo_path,        &["diffedit", "--config", config_with_output_as_after],    );    insta::assert_snapshot!(stdout, @"");    insta::assert_snapshot!(stderr, @r###"    Created kkmpptxz ed8aada3 (no description set)    Working copy now at: kkmpptxz ed8aada3 (no description set)    Parent commit      : rlvkpnrz a72506cd (no description set)    Added 0 files, modified 1 files, removed 0 files    "###);    let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);    insta::assert_snapshot!(stdout, @r###"    D file1    "###);     // Can write something new to `file1`    test_env.jj_cmd_ok(&repo_path, &["undo"]);    std::fs::write(&edit_script, "write file1\nnew content").unwrap();    let (stdout, stderr) = test_env.jj_cmd_ok(

Lines 1–500 of 734.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t