Repository: gitbutlerapp/gitbutler
File: crates/but/src/id/tests.rs (Rust)

Code before:
        "the case matters for committed files"
    );

    Ok(())
}

#[test]
fn branch_and_file_by_name() -> anyhow::Result<()> {
    let stacks = &[stack([segment("foo", [id(1)], None, [])])];
    let mut id_map = IdMap::new_for_branches_and_commits(stacks)?;
    let changed_paths_fn = |commit_id: gix::ObjectId,
                            parent_id: Option<gix::ObjectId>|
     -> anyhow::Result<Vec<BString>> {
        Ok(if commit_id == id(1) && parent_id.is_none() {
            vec![]
        } else {
            bail!("unexpected IDs {} {:?}", commit_id, parent_id);
        })
    };
    let hunk_assignments = vec![hunk_assignment("foo", None)];
    id_map.add_file_info(changed_paths_fn, hunk_assignments)?;

    insta::assert_debug_snapshot!(id_map.resolve_entity_to_ids("foo")?, @r#"
    [
        Branch {
            name: "foo",
            id: "fo",
        },
        Uncommitted(
            UncommittedCliId {
                id: "foo",
                hunk_assignments: NonEmpty {
                    head: HunkAssignment {
                        id: None,
                        hunk_header: None,
                        path: "",
                        path_bytes: "foo",
                        stack_id: None,
                        hunk_locks: None,
                        line_nums_added: None,
                        line_nums_removed: None,
                        diff: None,
                    },
                    tail: [],
                },
                is_entire_file: true,
            },
        ),
    ]
    "#);


Review comment (suggestion):
I think it would help a lot to have more of an explanation what's under test there. What's important about the return value, what makes it special?

Unfortunately, `insta` doesn't support the 'third argument' in its assertions, so all we can do here is add an ordinary comment above it.

Code after:
        "the case matters for committed files"
    );

    Ok(())
}

#[test]
fn branch_and_file_by_name() -> anyhow::Result<()> {
    let stacks = &[stack([segment("foo", [id(1)], None, [])])];
    let mut id_map = IdMap::new_for_branches_and_commits(stacks)?;
    let changed_paths_fn = |commit_id: gix::ObjectId,
                            parent_id: Option<gix::ObjectId>|
     -> anyhow::Result<Vec<BString>> {
        Ok(if commit_id == id(1) && parent_id.is_none() {
            vec![]
        } else {
            bail!("unexpected IDs {} {:?}", commit_id, parent_id);
        })
    };
    let hunk_assignments = vec![hunk_assignment("foo", None)];
    id_map.add_file_info(changed_paths_fn, hunk_assignments)?;

    // Both branches and uncommitted, unassigned files match by name, and none
    // have priority over the other (i.e. if there is both a branch and a file
    // that matches, the result is ambiguous).
    insta::assert_debug_snapshot!(id_map.resolve_entity_to_ids("foo")?, @r#"
    [
        Branch {
            name: "foo",
            id: "fo",
        },
        Uncommitted(
            UncommittedCliId {
                id: "foo",
                hunk_assignments: NonEmpty {
                    head: HunkAssignment {
                        id: None,
                        hunk_header: None,
                        path: "",
                        path_bytes: "foo",
                        stack_id: None,
                        hunk_locks: None,
                        line_nums_added: None,
                        line_nums_removed: None,
                        diff: None,
                    },
                    tail: [],
                },
                is_entire_file: true,
            },
        ),