|
1 |
| -use bstr::BString; |
2 | 1 | use gix_object::{
|
3 | 2 | bstr::ByteSlice,
|
4 |
| - tree::{self, Entry, EntryRef}, |
| 3 | + tree::{self, EntryRef}, |
5 | 4 | TreeRefIter,
|
6 | 5 | };
|
7 | 6 | use pretty_assertions::assert_eq;
|
@@ -59,59 +58,73 @@ fn everything() -> crate::Result {
|
59 | 58 | Ok(())
|
60 | 59 | }
|
61 | 60 |
|
62 |
| -#[test] |
63 |
| -fn lookup_entry_toplevel() -> crate::Result { |
64 |
| - let entry = utils::lookup_entry_by_path("bin")?; |
65 |
| - |
66 |
| - let mode: tree::EntryMode = tree::EntryMode(33188); |
67 |
| - let filename: BString = "bin".into(); |
68 |
| - let oid = hex_to_id("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"); |
69 |
| - |
70 |
| - assert_eq!(entry, Some(Entry { mode, filename, oid })); |
71 |
| - |
72 |
| - Ok(()) |
73 |
| -} |
74 |
| - |
75 |
| -#[test] |
76 |
| -fn lookup_entry_nested_path() -> crate::Result { |
77 |
| - let entry = utils::lookup_entry_by_path("file/a")?; |
78 |
| - |
79 |
| - let mode: tree::EntryMode = tree::EntryMode(33188); |
80 |
| - let filename: BString = "a".into(); |
81 |
| - let oid = hex_to_id("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"); |
82 |
| - |
83 |
| - assert_eq!(entry, Some(Entry { mode, filename, oid })); |
84 |
| - |
85 |
| - Ok(()) |
86 |
| -} |
| 61 | +mod lookup_entry { |
| 62 | + use crate::hex_to_id; |
| 63 | + use gix_object::tree::EntryKind; |
| 64 | + use utils::entry; |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn top_level_directory() -> crate::Result { |
| 68 | + assert_eq!( |
| 69 | + utils::lookup_entry_by_path("bin")?, |
| 70 | + entry( |
| 71 | + "bin", |
| 72 | + EntryKind::Blob, |
| 73 | + hex_to_id("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391") |
| 74 | + ) |
| 75 | + ); |
| 76 | + Ok(()) |
| 77 | + } |
87 | 78 |
|
88 |
| -#[test] |
89 |
| -fn lookup_entry_that_does_not_exist() -> crate::Result { |
90 |
| - let entry = utils::lookup_entry_by_path("file/does-not-exist")?; |
| 79 | + #[test] |
| 80 | + fn nested_file() -> crate::Result { |
| 81 | + assert_eq!( |
| 82 | + utils::lookup_entry_by_path("file/a")?, |
| 83 | + entry( |
| 84 | + "a", |
| 85 | + EntryKind::Blob, |
| 86 | + hex_to_id("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391") |
| 87 | + ) |
| 88 | + ); |
| 89 | + Ok(()) |
| 90 | + } |
91 | 91 |
|
92 |
| - assert_eq!(entry, None); |
| 92 | + #[test] |
| 93 | + fn non_existing_nested_file() -> crate::Result { |
| 94 | + for path in ["file/does-not-exist", "non-existing", "file/a/through-file"] { |
| 95 | + let actual = utils::lookup_entry_by_path(path)?; |
| 96 | + assert_eq!(actual, None); |
| 97 | + } |
| 98 | + Ok(()) |
| 99 | + } |
93 | 100 |
|
94 |
| - Ok(()) |
95 |
| -} |
| 101 | + mod utils { |
| 102 | + use crate::hex_to_id; |
96 | 103 |
|
97 |
| -mod utils { |
98 |
| - use crate::hex_to_id; |
| 104 | + use gix_object::{tree, FindExt}; |
99 | 105 |
|
100 |
| - use gix_object::FindExt; |
| 106 | + pub(super) fn entry(filename: &str, mode: tree::EntryKind, oid: gix_hash::ObjectId) -> Option<tree::Entry> { |
| 107 | + Some(tree::Entry { |
| 108 | + mode: mode.into(), |
| 109 | + filename: filename.into(), |
| 110 | + oid, |
| 111 | + }) |
| 112 | + } |
101 | 113 |
|
102 |
| - pub(super) fn tree_odb() -> gix_testtools::Result<gix_odb::Handle> { |
103 |
| - let root = gix_testtools::scripted_fixture_read_only("make_trees.sh")?; |
104 |
| - Ok(gix_odb::at(root.join(".git/objects"))?) |
105 |
| - } |
| 114 | + pub(super) fn tree_odb() -> gix_testtools::Result<gix_odb::Handle> { |
| 115 | + let root = gix_testtools::scripted_fixture_read_only("make_trees.sh")?; |
| 116 | + Ok(gix_odb::at(root.join(".git/objects"))?) |
| 117 | + } |
106 | 118 |
|
107 |
| - pub(super) fn lookup_entry_by_path(path: &str) -> gix_testtools::Result<Option<gix_object::tree::Entry>> { |
108 |
| - let odb = tree_odb()?; |
109 |
| - let root_tree_id = hex_to_id("ff7e7d2aecae1c3fb15054b289a4c58aa65b8646"); |
| 119 | + pub(super) fn lookup_entry_by_path(path: &str) -> gix_testtools::Result<Option<gix_object::tree::Entry>> { |
| 120 | + let odb = tree_odb()?; |
| 121 | + let root_tree_id = hex_to_id("ff7e7d2aecae1c3fb15054b289a4c58aa65b8646"); |
110 | 122 |
|
111 |
| - let mut buf = Vec::new(); |
112 |
| - let root_tree = odb.find_tree_iter(&root_tree_id, &mut buf)?; |
| 123 | + let mut buf = Vec::new(); |
| 124 | + let root_tree = odb.find_tree_iter(&root_tree_id, &mut buf)?; |
113 | 125 |
|
114 |
| - let mut buf = Vec::new(); |
115 |
| - Ok(root_tree.lookup_entry_by_path(&odb, &mut buf, path).unwrap()) |
| 126 | + let mut buf = Vec::new(); |
| 127 | + root_tree.lookup_entry_by_path(&odb, &mut buf, path) |
| 128 | + } |
116 | 129 | }
|
117 | 130 | }
|
0 commit comments