905 lines · 30.2 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::PathBuf; use indoc::indoc; use crate::common::CommandOutput;use crate::common::TestEnvironment;use crate::common::TestWorkDir; #[test]fn test_describe() {    let mut test_env = TestEnvironment::default();    let edit_script = test_env.set_up_fake_editor();    test_env.run_jj_in(".", ["git", "init", "repo"]).success();    let work_dir = test_env.work_dir("repo");     // Set a description using `-m` flag    let output = work_dir.run_jj(["describe", "-m", "description from CLI"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: qpvuntsm 95979928 (empty) description from CLI    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)    [EOF]    ");     // Set the same description using `-m` flag, but with explicit newline    let output = work_dir.run_jj(["describe", "-m", "description from CLI\n"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");     // Check that the text file gets initialized with the current description and    // make no changes    std::fs::write(&edit_script, "dump editor0").unwrap();    let output = work_dir.run_jj(["describe"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r#"    description from CLI     JJ: Lines starting with "JJ:" (like this one) will be removed.    "#);     // Set a description in editor    std::fs::write(&edit_script, "write\ndescription from editor").unwrap();    let output = work_dir.run_jj(["describe"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: qpvuntsm 94fcb906 (empty) description from editor    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)    [EOF]    ");     // Lines in editor starting with "JJ: " are ignored    std::fs::write(        &edit_script,        "write\nJJ: ignored\ndescription among comment\nJJ: ignored",    )    .unwrap();    let output = work_dir.run_jj(["describe"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: qpvuntsm 7a348923 (empty) description among comment    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)    [EOF]    ");     // Multi-line description    std::fs::write(&edit_script, "write\nline1\nline2\n\nline4\n\n").unwrap();    let output = work_dir.run_jj(["describe"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: qpvuntsm 749361b5 (empty) line1    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)    [EOF]    ");    let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);    insta::assert_snapshot!(output, @r"    line1    line2     line4    [EOF]    ");     // Multi-line description again with CRLF, which should make no changes    std::fs::write(&edit_script, "write\nline1\r\nline2\r\n\r\nline4\r\n\r\n").unwrap();    let output = work_dir.run_jj(["describe"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");     // Multi-line description starting with newlines    std::fs::write(&edit_script, "write\n\n\nline1\nline2").unwrap();    let output = work_dir.run_jj(["describe"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: qpvuntsm dc44dbee (empty) line1    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)    [EOF]    ");    let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);    insta::assert_snapshot!(output, @r"    line1    line2    [EOF]    ");     // Clear description    let output = work_dir.run_jj(["describe", "-m", ""]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: qpvuntsm 6296963b (empty) (no description set)    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)    [EOF]    ");    std::fs::write(&edit_script, "write\n").unwrap();    let output = work_dir.run_jj(["describe"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");     // Fails if the editor fails    std::fs::write(&edit_script, "fail").unwrap();    let output = work_dir.run_jj(["describe"]);    insta::with_settings!({        filters => [            (r"\bEditor '[^']*'", "Editor '<redacted>'"),            (r"\b(editor-)[^.]*(\.jjdescription)\b", "$1<redacted>$2"),            ("exit code", "exit status"), // Windows        ],    }, {        insta::assert_snapshot!(output, @r"        ------- stderr -------        Error: Failed to edit description        Caused by: Editor '<redacted>' exited with exit status: 1        Hint: Edited description is left in $TEST_ENV/repo/.jj/repo/editor-<redacted>.jjdescription        [EOF]        [exit status: 1]        ");    });     // ignore everything after the first ignore-rest line    std::fs::write(        &edit_script,        indoc! {"            write            description from editor             content of message from editor            JJ: ignore-rest            content after ignore line should not be included            JJ: ignore-rest            ignore everything until EOF or next description        "},    )    .unwrap();    let output = work_dir.run_jj(["describe"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Working copy  (@) now at: qpvuntsm 10fa2dc7 (empty) description from editor    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)    [EOF]    ");    let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);    insta::assert_snapshot!(output, @r"    description from editor     content of message from editor    [EOF]    ");} #[test]fn test_describe_editor_env() {    let test_env = TestEnvironment::default();    test_env.run_jj_in(".", ["git", "init", "repo"]).success();    let work_dir = test_env.work_dir("repo");     // Fails if the editor doesn't exist    let output = work_dir.run_jj_with(|cmd| {        cmd.arg("describe")            .env("EDITOR", "this-editor-does-not-exist")    });    insta::assert_snapshot!(        output.normalize_stderr_with(|s| s.split_inclusive('\n').take(3).collect()), @r"    ------- stderr -------    Error: Failed to edit description    Caused by:    1: Failed to run editor 'this-editor-does-not-exist'    [EOF]    [exit status: 1]    ");     // `$VISUAL` overrides `$EDITOR`    let output = work_dir.run_jj_with(|cmd| {        cmd.arg("describe")            .env("VISUAL", "bad-editor-from-visual-env")            .env("EDITOR", "bad-editor-from-editor-env")    });    insta::assert_snapshot!(        output.normalize_stderr_with(|s| s.split_inclusive('\n').take(3).collect()), @r"    ------- stderr -------    Error: Failed to edit description    Caused by:    1: Failed to run editor 'bad-editor-from-visual-env'    [EOF]    [exit status: 1]    ");     // `ui.editor` config overrides `$VISUAL`    test_env.add_config(r#"ui.editor = "bad-editor-from-config""#);    let output = work_dir.run_jj_with(|cmd| {        cmd.arg("describe")            .env("VISUAL", "bad-editor-from-visual-env")    });    insta::assert_snapshot!(        output.normalize_stderr_with(|s| s.split_inclusive('\n').take(3).collect()), @r"    ------- stderr -------    Error: Failed to edit description    Caused by:    1: Failed to run editor 'bad-editor-from-config'    [EOF]    [exit status: 1]    ");     // `$JJ_EDITOR` overrides `ui.editor` config    let output = work_dir.run_jj_with(|cmd| {        cmd.arg("describe")            .env("JJ_EDITOR", "bad-jj-editor-from-jj-editor-env")    });    insta::assert_snapshot!(        output.normalize_stderr_with(|s| s.split_inclusive('\n').take(3).collect()), @r"    ------- stderr -------    Error: Failed to edit description    Caused by:    1: Failed to run editor 'bad-jj-editor-from-jj-editor-env'    [EOF]    [exit status: 1]    ");} #[test]fn test_describe_multiple_commits() {    let mut test_env = TestEnvironment::default();    let edit_script = test_env.set_up_fake_editor();    test_env.run_jj_in(".", ["git", "init", "repo"]).success();    let work_dir = test_env.work_dir("repo");     // Initial setup    work_dir.run_jj(["new"]).success();    work_dir.run_jj(["new"]).success();    insta::assert_snapshot!(get_log_output(&work_dir), @r"    @  c6349e79bbfd    ○  65b6b74e0897    ○  230dd059e1b0    ◆  000000000000    [EOF]    ");     // Set the description of multiple commits using `-m` flag    let output = work_dir.run_jj(["describe", "-r@", "-r@--", "-m", "description from CLI"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Updated 2 commits    Rebased 1 descendant commits    Working copy  (@) now at: kkmpptxz 41659b84 (empty) description from CLI    Parent commit (@-)      : rlvkpnrz 8d650510 (empty) (no description set)    [EOF]    ");    insta::assert_snapshot!(get_log_output(&work_dir), @r"    @  41659b846096 description from CLI    ○  8d650510daad    ○  a42f5755e688 description from CLI    ◆  000000000000    [EOF]    ");     // Check that the text file gets initialized with the current description of    // each commit and doesn't update commits if no changes are made.    // Commit descriptions are edited in topological order    std::fs::write(&edit_script, "dump editor0").unwrap();    let output = work_dir.run_jj(["describe", "-r@", "@-"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Nothing changed.    [EOF]    ");    insta::assert_snapshot!(        std::fs::read_to_string(test_env.env_root().join("editor0")).unwrap(), @r#"    JJ: Enter or edit commit descriptions after the `JJ: describe` lines.    JJ: Warning:    JJ: - The text you enter will be lost on a syntax error.    JJ: - The syntax of the separator lines may change in the future.    JJ:    JJ: describe 8d650510daad -------      JJ: describe 41659b846096 -------    description from CLI     JJ: Lines starting with "JJ:" (like this one) will be removed.    "#);     // Set the description of multiple commits in the editor    std::fs::write(        &edit_script,        indoc! {"            write            JJ: Enter or edit commit descriptions after the `JJ: describe` lines.             JJ: More header tests. Library tests verify parsing in other situations.             JJ: describe 8d650510daad -------            description from editor of @-             further commit message of @-             JJ: describe 41659b846096 -------            description from editor of @             further commit message of @             JJ: Lines starting with \"JJ: \" (like this one) will be removed.        "},    )    .unwrap();    let output = work_dir.run_jj(["describe", "@", "@-"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Updated 2 commits    Working copy  (@) now at: kkmpptxz f203494a (empty) description from editor of @    Parent commit (@-)      : rlvkpnrz 0d76a92c (empty) description from editor of @-    [EOF]    ");    insta::assert_snapshot!(get_log_output(&work_dir), @r"    @  f203494a4507 description from editor of @    │    │  further commit message of @    ○  0d76a92ca7cc description from editor of @-    │    │  further commit message of @-    ○  a42f5755e688 description from CLI    ◆  000000000000    [EOF]    ");     // Fails if the edited message has a commit with multiple descriptions    std::fs::write(        &edit_script,        indoc! {"            write            JJ: describe 0d76a92ca7cc -------            first description from editor of @-             further commit message of @-             JJ: describe 0d76a92ca7cc -------            second description from editor of @-             further commit message of @-             JJ: describe f203494a4507 -------            updated description from editor of @             further commit message of @             JJ: Lines starting with \"JJ: \" (like this one) will be removed.        "},    )    .unwrap();    let output = work_dir.run_jj(["describe", "@", "@-"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Error: The following commits were found in the edited message multiple times: 0d76a92ca7cc    [EOF]    [exit status: 1]    ");     // Fails if the edited message has unexpected commit IDs    std::fs::write(        &edit_script,        indoc! {"            write            JJ: describe 000000000000 -------            unexpected commit ID             JJ: describe 0d76a92ca7cc -------            description from editor of @-             further commit message of @-             JJ: describe f203494a4507 -------            description from editor of @             further commit message of @             JJ: Lines starting with \"JJ: \" (like this one) will be removed.        "},    )    .unwrap();    let output = work_dir.run_jj(["describe", "@", "@-"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Error: The following commits were not being edited, but were found in the edited message: 000000000000    [EOF]    [exit status: 1]    ");     // Fails if the edited message has missing commit messages    std::fs::write(        &edit_script,        indoc! {"            write            JJ: describe f203494a4507 -------            description from editor of @             further commit message of @             JJ: Lines starting with \"JJ: \" (like this one) will be removed.        "},    )    .unwrap();    let output = work_dir.run_jj(["describe", "@", "@-"]);    insta::assert_snapshot!(output, @r"    ------- stderr -------    Error: The description for the following commits were not found in the edited message: 0d76a92ca7cc    [EOF]    [exit status: 1]    ");     // Fails if the edited message has a line which does not have any preceding    // `JJ: describe` headers    std::fs::write(        &edit_script,        indoc! {"            write            description from editor of @-             JJ: describe f203494a4507 -------            description from editor of @             JJ: Lines starting with \"JJ: \" (like this one) will be removed.        "},    )    .unwrap();    let output = work_dir.run_jj(["describe", "@", "@-"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Error: Found the following line without a commit header: "description from editor of @-"    [EOF]    [exit status: 1]    "#);     // Fails if the editor fails    std::fs::write(&edit_script, "fail").unwrap();    let output = work_dir.run_jj(["describe", "@", "@-"]);    insta::with_settings!({        filters => [            (r"\bEditor '[^']*'", "Editor '<redacted>'"),            (r"\b(editor-)[^.]*(\.jjdescription)\b", "$1<redacted>$2"),            ("exit code", "exit status"), // Windows        ],    }, {        insta::assert_snapshot!(output, @r"        ------- stderr -------        Error: Failed to edit description        Caused by: Editor '<redacted>' exited with exit status: 1        Hint: Edited description is left in $TEST_ENV/repo/.jj/repo/editor-<redacted>.jjdescription        [EOF]        [exit status: 1]        ");    });     // describe lines should take priority over ignore-rest    std::fs::write(        &edit_script,

Lines 1–500 of 905.