Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tests/data/packs/* filter=lfs diff=lfs merge=lfs -text
*.pack filter=lfs diff=lfs merge=lfs -text
20de184187340e8b98da0fa14685b6a3e5c638ba2cf6b7b193b17c0ee1da38e8 filter=lfs diff=lfs merge=lfs -text
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Cargo.lock
target/
.cache_temp
9 changes: 5 additions & 4 deletions src/delta/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,12 @@ mod tests {

#[test]
fn test_delta_fn() {
let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap());
source.push("../tests/diff/16ecdcc8f663777896bd39ca025a041b7f005e");
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/diff/16ecdcc8f663777896bd39ca025a041b7f005e");
let old_data = read_zlib_data(&source).unwrap();
let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap());
source.push("../tests/diff/bee0d45f981adf7c2926a0dc04deb7f006bcc3");

let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/diff/bee0d45f981adf7c2926a0dc04deb7f006bcc3");
let new_data = read_zlib_data(&source).unwrap();

let d = DeltaDiff::new(&old_data, &new_data);
Expand Down
6 changes: 4 additions & 2 deletions src/delta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,13 @@ mod tests {
let data3 = vec![0u8; 100];
let mut data4 = vec![0u8; 100];

for i in 0..2 {
data4[i] = 1;
for i in data4.iter_mut().take(2) {
*i = 1;
}

let rate1 = heuristic_encode_rate_parallel(&data3, &data4);
let rate2 = encode_rate(&data3, &data4);

println!(
"Large partially matching data rate = {}, accurate rate = {}",
rate1, rate2
Expand Down
2 changes: 1 addition & 1 deletion src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod tests {

#[test]
fn test_signature_without_delta() {
let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap());
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/packs/pack-1d0e6c14760c956c173ede71cb28f33d921e232f.pack");

let f = std::fs::File::open(source).unwrap();
Expand Down
25 changes: 19 additions & 6 deletions src/internal/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use sha1::{Digest, Sha1};
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};
use std::fs::{self, File};
Expand All @@ -10,6 +8,9 @@ use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use sha1::{Digest, Sha1};

use crate::errors::GitError;
use crate::hash::SHA1;
use crate::internal::pack::wrapper::Wrapper;
Expand Down Expand Up @@ -554,14 +555,20 @@ mod tests {

#[test]
fn test_check_header() {
let file = File::open("../tests/data/index/index-2").unwrap();
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/index/index-2");

let file = File::open(source).unwrap();
let entries = Index::check_header(&mut BufReader::new(file)).unwrap();
assert_eq!(entries, 2);
}

#[test]
fn test_index() {
let index = Index::from_file("../tests/data/index/index-760").unwrap();
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/index/index-760");

let index = Index::from_file(source).unwrap();
assert_eq!(index.size(), 760);
for (_, entry) in index.entries.iter() {
println!("{entry}");
Expand All @@ -570,15 +577,21 @@ mod tests {

#[test]
fn test_index_to_file() {
let index = Index::from_file("../tests/data/index/index-760").unwrap();
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/index/index-760");

let index = Index::from_file(source).unwrap();
index.to_file("/tmp/index-760").unwrap();
let new_index = Index::from_file("/tmp/index-760").unwrap();
assert_eq!(index.size(), new_index.size());
}

#[test]
fn test_index_entry_create() {
let file = Path::new("Cargo.toml"); // use as a normal file
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("Cargo.toml");

let file = Path::new(source.as_path()); // use as a normal file
let hash = SHA1::from_bytes(&[0; 20]);
let workdir = Path::new("../");
let entry = IndexEntry::new_from_file(file, hash, workdir).unwrap();
Expand Down
1 change: 0 additions & 1 deletion src/internal/object/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ mod tests {

/// When the test case run in the GitHub Action, the timezone is +0000, so we ignore it.
#[test]
#[ignore]
fn test_signature_with_time() {
let sign = Signature::new(
SignatureType::Author,
Expand Down
9 changes: 4 additions & 5 deletions src/internal/pack/cache_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,8 @@ mod test {
use lru_mem::LruCache;

use super::*;

#[test]
#[ignore = "only in single thread"]
// 只在单线程测试
fn test_heap_size_record() {
let mut obj = CacheObject {
info: CacheObjectInfo::BaseObject(ObjectType::Blob, SHA1::default()),
Expand All @@ -356,7 +355,7 @@ mod test {
assert_eq!(mem.load(Ordering::Relaxed), 0);
obj.set_mem_recorder(mem.clone());
obj.record_mem_size();
assert_eq!(mem.load(Ordering::Relaxed), 1120);
assert_eq!(mem.load(Ordering::Relaxed), obj.mem_size());
drop(obj);
assert_eq!(mem.load(Ordering::Relaxed), 0);
}
Expand All @@ -375,8 +374,8 @@ mod test {
let b = ArcWrapper::new(Arc::new(a.clone()), Arc::new(AtomicBool::new(false)), None);
assert!(b.heap_size() == 1024);
}

#[test]
#[ignore]
fn test_cache_object_with_lru() {
let mut cache = LruCache::new(2048);

Expand Down Expand Up @@ -424,7 +423,7 @@ mod test {
{
// a should be ejected
let r = cache.get(&hash_a.to_string());
assert!(r.is_some());
assert!(r.is_none());
Comment on lines 425 to +426

Copilot AI Sep 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test logic appears incorrect. After ejecting an item from the cache, the assertion expects the item to be None, but the comment states 'a should be ejected' suggesting it should be present. This contradicts the expected behavior.

Copilot uses AI. Check for mistakes.
}
}

Expand Down
40 changes: 14 additions & 26 deletions src/internal/pack/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,11 +706,8 @@ mod tests {

#[tokio::test]
async fn test_pack_check_header() {
let res = crate::test_utils::setup_lfs_file().await;
println!("{res:?}");
let source = res
.get("git-2d187177923cd618a75da6c6db45bb89d92bd504.pack")
.unwrap();
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/packs/git-2d187177923cd618a75da6c6db45bb89d92bd504.pack");

let f = fs::File::open(source).unwrap();
let mut buf_reader = BufReader::new(f);
Expand Down Expand Up @@ -744,7 +741,7 @@ mod tests {

#[test]
fn test_pack_decode_without_delta() {
let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap());
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/packs/pack-1d0e6c14760c956c173ede71cb28f33d921e232f.pack");

let tmp = PathBuf::from("/tmp/.cache_temp");
Expand All @@ -760,7 +757,7 @@ mod tests {
fn test_pack_decode_with_ref_delta() {
init_logger();

let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap());
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/packs/ref-delta-65d47638aa7cb7c39f1bd1d5011a415439b887a8.pack");

let tmp = PathBuf::from("/tmp/.cache_temp");
Expand All @@ -773,7 +770,7 @@ mod tests {

#[test]
fn test_pack_decode_no_mem_limit() {
let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap());
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/packs/pack-1d0e6c14760c956c173ede71cb28f33d921e232f.pack");

let tmp = PathBuf::from("/tmp/.cache_temp");
Expand All @@ -785,13 +782,10 @@ mod tests {
}

#[tokio::test]
#[ignore] // Take too long time
async fn test_pack_decode_with_large_file_with_delta_without_ref() {
init_logger();
let file_map = crate::test_utils::setup_lfs_file().await;
let source = file_map
.get("git-2d187177923cd618a75da6c6db45bb89d92bd504.pack")
.unwrap();
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/packs/git-2d187177923cd618a75da6c6db45bb89d92bd504.pack");

let tmp = PathBuf::from("/tmp/.cache_temp");

Expand All @@ -815,10 +809,8 @@ mod tests {
#[tokio::test]
async fn test_decode_large_file_stream() {
init_logger();
let file_map = crate::test_utils::setup_lfs_file().await;
let source = file_map
.get("git-2d187177923cd618a75da6c6db45bb89d92bd504.pack")
.unwrap();
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/packs/git-2d187177923cd618a75da6c6db45bb89d92bd504.pack");

let tmp = PathBuf::from("/tmp/.cache_temp");
let f = tokio::fs::File::open(source).await.unwrap();
Expand Down Expand Up @@ -850,12 +842,9 @@ mod tests {
}

#[tokio::test]
#[ignore] // Take too long time, duplicate with `test_decode_large_file_stream`
async fn test_decode_large_file_async() {
let file_map = crate::test_utils::setup_lfs_file().await;
let source = file_map
.get("git-2d187177923cd618a75da6c6db45bb89d92bd504.pack")
.unwrap();
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/packs/git-2d187177923cd618a75da6c6db45bb89d92bd504.pack");

let tmp = PathBuf::from("/tmp/.cache_temp");
let f = fs::File::open(source).unwrap();
Expand All @@ -870,7 +859,7 @@ mod tests {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let handle = p.decode_async(buffered, tx); // new thread
let mut cnt = 0;
while let Ok(_entry) = rx.try_recv() {
while let Some(_entry) = rx.recv().await {
cnt += 1; //use entry here
}
let p = handle.join().unwrap();
Expand All @@ -879,7 +868,7 @@ mod tests {

#[test]
fn test_pack_decode_with_delta_without_ref() {
let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap());
let mut source = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
source.push("tests/data/packs/pack-d50df695086eea6253a237cb5ac44af1629e7ced.pack");

let tmp = PathBuf::from("/tmp/.cache_temp");
Expand All @@ -890,8 +879,7 @@ mod tests {
p.decode(&mut buffered, |_, _| {}).unwrap();
}

#[test]
#[ignore] // Take too long time
#[test]// Take too long time

Copilot AI Sep 28, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after #[test] attribute. Should be #[test] // Take too long time

Suggested change
#[test]// Take too long time
#[test] // Take too long time

Copilot uses AI. Check for mistakes.
fn test_pack_decode_multi_task_with_large_file_with_delta_without_ref() {
let task1 = std::thread::spawn(|| {
test_pack_decode_with_large_file_with_delta_without_ref();
Expand Down
13 changes: 4 additions & 9 deletions src/internal/pack/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ impl PackEncoder {
]
.concat();

// 按顺序发送合并后的结果
for data in all_encoded_data {
self.write_all_and_update(&data).await;
}
Expand Down Expand Up @@ -582,16 +581,12 @@ mod tests {
}

async fn get_entries_for_test() -> Arc<Mutex<Vec<Entry>>> {
let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap());
source.push("tests/data/packs/pack-f8bbb573cef7d851957caceb491c073ee8e8de41.pack");
// let file_map = crate::test_utils::setup_lfs_file().await;
// let source = file_map
// .get("git-2d187177923cd618a75da6c6db45bb89d92bd504.pack")
// .unwrap();
// decode pack file to get entries
let source = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/data/packs/pack-f8bbb573cef7d851957caceb491c073ee8e8de41.pack");

let mut p = Pack::new(None, None, Some(PathBuf::from("/tmp/.cache_temp")), true);

let f = std::fs::File::open(source).unwrap();
let f = std::fs::File::open(&source).unwrap();
tracing::info!("pack file size: {}", f.metadata().unwrap().len());
let mut reader = std::io::BufReader::new(f);
let entries = Arc::new(Mutex::new(Vec::new()));
Expand Down
17 changes: 8 additions & 9 deletions src/internal/pack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ pub struct Pack {
pub waitlist: Arc<Waitlist>,
pub caches: Arc<Caches>,
pub mem_limit: Option<usize>,
pub cache_objs_mem: Arc<AtomicUsize>, // the memory size of CacheObjects in this Pack
pub cache_objs_mem: Arc<AtomicUsize>,
pub clean_tmp: bool,
}

#[cfg(test)]
mod tests {
use tracing_subscriber::util::SubscriberInitExt;

/// CAUTION: This two is same
/// 1.
/// tracing_subscriber::fmt().init();
///
/// 2.
/// env::set_var("RUST_LOG", "debug"); // must be set if use `fmt::init()`, or no output
/// tracing_subscriber::fmt::init();
pub(crate) fn init_logger() {
let _ = tracing_subscriber::fmt::Subscriber::builder()
.with_target(false)
Expand All @@ -46,13 +53,5 @@ mod tests {
.with_max_level(tracing::Level::DEBUG)
.finish()
.try_init(); // avoid multi-init

// CAUTION: This two is same
// 1.
// tracing_subscriber::fmt().init();
//
// 2.
// env::set_var("RUST_LOG", "debug"); // must be set if use `fmt::init()`, or no output
// tracing_subscriber::fmt::init();
}
}
Loading