| 1 |
//! Tests for the file index. |
| 2 |
//! See tests/test-file-index.t for more comprehensive testing. |
| 3 |
|
| 4 |
use super::*; |
| 5 |
use crate::revlog::path_encode::PathEncoding; |
| 6 |
use crate::utils::hg_path::HgPath; |
| 7 |
|
| 8 |
/// Creates an empty file index in a temporary directory. |
| 9 |
fn create_file_index() -> Result<(FileIndex, tempfile::TempDir), HgError> { |
| 10 |
let temp_dir = tempfile::tempdir().expect("creating tempdir"); |
| 11 |
let base = temp_dir.path().to_owned(); |
| 12 |
let vfs = VfsImpl::new(base, false, PathEncoding::None); |
| 13 |
let try_pending = false; |
| 14 |
let config = Config { |
| 15 |
vacuum_mode: VacuumMode::Never, |
| 16 |
max_unused_ratio: 1.0, |
| 17 |
gc_retention_s: 0, |
| 18 |
garbage_timestamp: None, |
| 19 |
}; |
| 20 |
let devel_sync_point = || {}; |
| 21 |
Ok((FileIndex::open(vfs, try_pending, config, devel_sync_point)?, temp_dir)) |
| 22 |
} |
| 23 |
|
| 24 |
/// Asserts that the file index contains the given tokens and paths. |
| 25 |
/// The tokens must be in ascending order. |
| 26 |
#[track_caller] |
| 27 |
fn check_paths(file_index: &FileIndex, tokens_and_paths: &[(u32, &HgPath)]) { |
| 28 |
let paths = |
| 29 |
tokens_and_paths.iter().map(|&(_, path)| path).collect::<Vec<_>>(); |
| 30 |
let actual_paths = file_index |
| 31 |
.iter() |
| 32 |
.map(|result| Ok(result?.0)) |
| 33 |
.collect::<Result<Vec<_>, Error>>() |
| 34 |
.expect("iterating paths"); |
| 35 |
assert_eq!(actual_paths, paths, "line {}", line!()); |
| 36 |
assert_eq!(file_index.len(), paths.len(), "line {}", line!()); |
| 37 |
assert_eq!(file_index.is_empty(), paths.is_empty(), "line {}", line!()); |
| 38 |
for &(token, path) in tokens_and_paths { |
| 39 |
let token = FileToken(token); |
| 40 |
let msg = &format!("checking token={token:?}, path={path}"); |
| 41 |
let get_path = file_index.get_path(token); |
| 42 |
assert_eq!(get_path, Ok(Some(path)), "line {}: {msg}", line!()); |
| 43 |
let get_token = file_index.get_token(path); |
| 44 |
assert_eq!(get_token, Ok(Some(token)), "line {}: {msg}", line!()); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
struct FakeTransaction; |
| 49 |
|
| 50 |
impl Transaction for FakeTransaction { |
| 51 |
fn add(&mut self, _file: impl AsRef<Path>, _offset: usize) { |
| 52 |
// No need to do anything. We only add new files to the transaction |
| 53 |
// to ensure that rolling back will delete them. |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn test_empty() { |
| 59 |
let (mut file_index, _temp_dir) = create_file_index().unwrap(); |
| 60 |
check_paths(&file_index, &[]); |
| 61 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 62 |
check_paths(&file_index, &[]); |
| 63 |
} |
| 64 |
|
| 65 |
#[test] |
| 66 |
fn test_single() { |
| 67 |
let (mut file_index, _temp_dir) = create_file_index().unwrap(); |
| 68 |
let foo = HgPath::new(b"foo"); |
| 69 |
assert_eq!(file_index.add(foo).unwrap(), (FileToken(1), true)); |
| 70 |
check_paths(&file_index, &[(1, foo)]); |
| 71 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 72 |
check_paths(&file_index, &[(1, foo)]); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn test_multiple_write_together() { |
| 77 |
let (mut file_index, _temp_dir) = create_file_index().unwrap(); |
| 78 |
let a = HgPath::new(b"a"); |
| 79 |
let bb = HgPath::new(b"bb"); |
| 80 |
let ccc = HgPath::new(b"ccc"); |
| 81 |
|
| 82 |
assert_eq!(file_index.add(a).unwrap(), (FileToken(1), true)); |
| 83 |
check_paths(&file_index, &[(1, a)]); |
| 84 |
assert_eq!(file_index.add(bb).unwrap(), (FileToken(2), true)); |
| 85 |
check_paths(&file_index, &[(1, a), (2, bb)]); |
| 86 |
assert_eq!(file_index.add(ccc).unwrap(), (FileToken(3), true)); |
| 87 |
check_paths(&file_index, &[(1, a), (2, bb), (3, ccc)]); |
| 88 |
|
| 89 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 90 |
check_paths(&file_index, &[(1, a), (2, bb), (3, ccc)]); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn test_multiple_write_separately() { |
| 95 |
let (mut file_index, _temp_dir) = create_file_index().unwrap(); |
| 96 |
let a = HgPath::new(b"a"); |
| 97 |
let bb = HgPath::new(b"bb"); |
| 98 |
let ccc = HgPath::new(b"ccc"); |
| 99 |
|
| 100 |
assert_eq!(file_index.add(a).unwrap(), (FileToken(1), true)); |
| 101 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 102 |
check_paths(&file_index, &[(1, a)]); |
| 103 |
assert_eq!(file_index.add(bb).unwrap(), (FileToken(2), true)); |
| 104 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 105 |
check_paths(&file_index, &[(1, a), (2, bb)]); |
| 106 |
assert_eq!(file_index.add(ccc).unwrap(), (FileToken(3), true)); |
| 107 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 108 |
check_paths(&file_index, &[(1, a), (2, bb), (3, ccc)]); |
| 109 |
} |
| 110 |
|
| 111 |
/// Returns a valid path of length `len`. |
| 112 |
fn path_of_length(len: usize) -> HgPathBuf { |
| 113 |
let char = |i| match (i & 0xff) as u8 { |
| 114 |
b'\0' | b'\n' | b'\r' => 1, |
| 115 |
c => c, |
| 116 |
}; |
| 117 |
HgPathBuf::from_bytes(&(0..len).map(char).collect::<Vec<_>>()) |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn test_maximum_label_length() { |
| 122 |
let (mut file_index, _temp_dir) = create_file_index().unwrap(); |
| 123 |
let path1 = &path_of_length(255); // maximum for on node |
| 124 |
let path2 = &path_of_length(255 + 1); // requires two nodes |
| 125 |
let path3 = &path_of_length(255 * 2 + 1); // requires three nodes |
| 126 |
assert_eq!(file_index.add(path1).unwrap(), (FileToken(1), true)); |
| 127 |
assert_eq!(file_index.add(path2).unwrap(), (FileToken(2), true)); |
| 128 |
assert_eq!(file_index.add(path3).unwrap(), (FileToken(3), true)); |
| 129 |
|
| 130 |
check_paths(&file_index, &[(1, path1), (2, path2), (3, path3)]); |
| 131 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 132 |
check_paths(&file_index, &[(1, path1), (2, path2), (3, path3)]); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn test_maximum_path_length() { |
| 137 |
let (mut file_index, _temp_dir) = create_file_index().unwrap(); |
| 138 |
// This is the maximum length allowed by mercurial/cext/pathencode.c |
| 139 |
// and by rust/hg-core/src/revlog/path_encode.rs. |
| 140 |
const MAX_LENGTH: usize = 4096 * 4; |
| 141 |
let path = &path_of_length(MAX_LENGTH); |
| 142 |
assert_eq!(file_index.add(path).unwrap(), (FileToken(1), true)); |
| 143 |
|
| 144 |
check_paths(&file_index, &[(1, path)]); |
| 145 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 146 |
check_paths(&file_index, &[(1, path)]); |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn test_add_existing_path() { |
| 151 |
let (mut file_index, _temp_dir) = create_file_index().unwrap(); |
| 152 |
let foo = HgPath::new(b"foo"); |
| 153 |
assert_eq!(file_index.add(foo).unwrap(), (FileToken(1), true)); |
| 154 |
assert_eq!(file_index.add(foo).unwrap(), (FileToken(1), false)); |
| 155 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 156 |
assert_eq!(file_index.add(foo).unwrap(), (FileToken(1), false)); |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn test_get_path_none() { |
| 161 |
let (file_index, _temp_dir) = create_file_index().unwrap(); |
| 162 |
assert_eq!(file_index.get_path(FileToken(1)).unwrap(), None); |
| 163 |
assert_eq!(file_index.get_path(FileToken(u32::MAX)).unwrap(), None); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn test_get_token_none() { |
| 168 |
let (file_index, _temp_dir) = create_file_index().unwrap(); |
| 169 |
assert_eq!(file_index.get_token(HgPath::new("")).unwrap(), None); |
| 170 |
assert_eq!(file_index.get_token(HgPath::new("fake")).unwrap(), None); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn test_remove_all() { |
| 175 |
let (mut file_index, _temp_dir) = create_file_index().unwrap(); |
| 176 |
let foo = HgPath::new(b"foo"); |
| 177 |
assert_eq!(file_index.add(foo).unwrap(), (FileToken(1), true)); |
| 178 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 179 |
file_index.remove(foo).unwrap(); |
| 180 |
check_paths(&file_index, &[]); |
| 181 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 182 |
check_paths(&file_index, &[]); |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
fn test_remove_some() { |
| 187 |
let (mut file_index, _temp_dir) = create_file_index().unwrap(); |
| 188 |
let foo = HgPath::new(b"foo"); |
| 189 |
let bar = HgPath::new(b"bar"); |
| 190 |
assert_eq!(file_index.add(foo).unwrap(), (FileToken(1), true)); |
| 191 |
assert_eq!(file_index.add(bar).unwrap(), (FileToken(2), true)); |
| 192 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 193 |
file_index.remove(foo).unwrap(); |
| 194 |
check_paths(&file_index, &[(2, bar)]); |
| 195 |
file_index.write(&mut FakeTransaction).unwrap(); |
| 196 |
check_paths(&file_index, &[(1, bar)]); |
| 197 |
} |