From 6b2cb16a348c3bd833fb17fc925fe447dced6159 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Thu, 19 Dec 2024 17:59:42 +0800 Subject: [PATCH] fix: test case --- .../src/cache/persistent/snapshot/mod.rs | 51 ++++++++++++------- .../src/cache/persistent/snapshot/strategy.rs | 14 +++-- crates/rspack_core/src/compiler/mod.rs | 7 +-- crates/rspack_fs/src/memory_fs.rs | 10 ++-- 4 files changed, 50 insertions(+), 32 deletions(-) diff --git a/crates/rspack_core/src/cache/persistent/snapshot/mod.rs b/crates/rspack_core/src/cache/persistent/snapshot/mod.rs index 994ecf6d266f..cc00da4078a0 100644 --- a/crates/rspack_core/src/cache/persistent/snapshot/mod.rs +++ b/crates/rspack_core/src/cache/persistent/snapshot/mod.rs @@ -104,6 +104,7 @@ impl Snapshot { #[cfg(test)] mod tests { + use std::borrow::Cow; use std::sync::Arc; use rspack_fs::{MemoryFileSystem, WritableFileSystem}; @@ -131,28 +132,36 @@ mod tests { .await .unwrap(); fs.create_dir_all("/node_modules/lib".into()).await.unwrap(); - fs.write("/file1".into(), "abc".as_bytes()).await.unwrap(); - fs.write("/constant".into(), "abc".as_bytes()) + fs.write("/file1".into(), Cow::Borrowed("abc".as_bytes())) + .await + .unwrap(); + fs.write("/constant".into(), Cow::Borrowed("abc".as_bytes())) .await .unwrap(); fs.write( "/node_modules/project/package.json".into(), - r#"{"version":"1.0.0"}"#.as_bytes(), + Cow::Borrowed(r#"{"version":"1.0.0"}"#.as_bytes()), + ) + .await + .unwrap(); + fs.write( + "/node_modules/project/file1".into(), + Cow::Borrowed("abc".as_bytes()), ) .await .unwrap(); - fs.write("/node_modules/project/file1".into(), "abc".as_bytes()) - .await - .unwrap(); fs.write( "/node_modules/lib/package.json".into(), - r#"{"version":"1.1.0"}"#.as_bytes(), + Cow::Borrowed(r#"{"version":"1.1.0"}"#.as_bytes()), + ) + .await + .unwrap(); + fs.write( + "/node_modules/lib/file1".into(), + Cow::Borrowed("abc".as_bytes()), ) .await .unwrap(); - fs.write("/node_modules/lib/file1".into(), "abc".as_bytes()) - .await - .unwrap(); let snapshot = Snapshot::new(options, fs.clone(), storage); @@ -168,16 +177,24 @@ mod tests { ) .await; std::thread::sleep(std::time::Duration::from_millis(100)); - fs.write("/file1".into(), "abcd".as_bytes()).await.unwrap(); - fs.write("/constant".into(), "abcd".as_bytes()) - .await - .unwrap(); - fs.write("/node_modules/project/file1".into(), "abcd".as_bytes()) + fs.write("/file1".into(), Cow::Borrowed("abcd".as_bytes())) .await .unwrap(); - fs.write("/node_modules/lib/file1".into(), "abcd".as_bytes()) + fs.write("/constant".into(), Cow::Borrowed("abcd".as_bytes())) .await .unwrap(); + fs.write( + "/node_modules/project/file1".into(), + Cow::Borrowed("abcd".as_bytes()), + ) + .await + .unwrap(); + fs.write( + "/node_modules/lib/file1".into(), + Cow::Borrowed("abcd".as_bytes()), + ) + .await + .unwrap(); let (modified_paths, deleted_paths) = snapshot.calc_modified_paths().await.unwrap(); assert!(deleted_paths.is_empty()); @@ -188,7 +205,7 @@ mod tests { fs.write( "/node_modules/lib/package.json".into(), - r#"{"version":"1.3.0"}"#.as_bytes(), + Cow::Borrowed(r#"{"version":"1.3.0"}"#.as_bytes()), ) .await .unwrap(); diff --git a/crates/rspack_core/src/cache/persistent/snapshot/strategy.rs b/crates/rspack_core/src/cache/persistent/snapshot/strategy.rs index 317e765d7115..b7cd94ebcbbc 100644 --- a/crates/rspack_core/src/cache/persistent/snapshot/strategy.rs +++ b/crates/rspack_core/src/cache/persistent/snapshot/strategy.rs @@ -137,7 +137,7 @@ impl StrategyHelper { #[cfg(test)] mod tests { - use std::{path::Path, sync::Arc}; + use std::{borrow::Cow, path::Path, sync::Arc}; use rspack_fs::{MemoryFileSystem, ReadableFileSystem, WritableFileSystem}; @@ -150,17 +150,19 @@ mod tests { fs.create_dir_all("/packages/p2".into()).await.unwrap(); fs.write( "/packages/p1/package.json".into(), - r#"{"version": "1.0.0"}"#.as_bytes(), + Cow::Borrowed(r#"{"version": "1.0.0"}"#.as_bytes()), ) .await .unwrap(); fs.write( "/packages/p2/package.json".into(), - r#"{"version": "1.1.0"}"#.as_bytes(), + Cow::Borrowed(r#"{"version": "1.1.0"}"#.as_bytes()), ) .await .unwrap(); - fs.write("/file1".into(), "abc".as_bytes()).await.unwrap(); + fs.write("/file1".into(), Cow::Borrowed("abc".as_bytes())) + .await + .unwrap(); // compile_time let Strategy::CompileTime(time1) = StrategyHelper::compile_time() else { @@ -243,7 +245,9 @@ mod tests { ValidateResult::NoChanged )); std::thread::sleep(std::time::Duration::from_millis(100)); - fs.write("/file1".into(), "abcd".as_bytes()).await.unwrap(); + fs.write("/file1".into(), Cow::Borrowed("abcd".as_bytes())) + .await + .unwrap(); assert!(matches!( helper.validate(Path::new("/file1"), &now).await, ValidateResult::Modified diff --git a/crates/rspack_core/src/compiler/mod.rs b/crates/rspack_core/src/compiler/mod.rs index e9ea71503a13..3b0b23c15a42 100644 --- a/crates/rspack_core/src/compiler/mod.rs +++ b/crates/rspack_core/src/compiler/mod.rs @@ -365,7 +365,7 @@ impl Compiler { ) .await?; - let content: Vec = source.buffer().into(); + let content = source.buffer(); let mut immutable = asset.info.immutable.unwrap_or(false); if !query.is_empty() { @@ -410,10 +410,7 @@ impl Compiler { }; if need_write { - self - .output_filesystem - .write(&file_path, Cow::Owned(content)) - .await?; + self.output_filesystem.write(&file_path, content).await?; self.compilation.emitted_assets.insert(filename.to_string()); } diff --git a/crates/rspack_fs/src/memory_fs.rs b/crates/rspack_fs/src/memory_fs.rs index 82f425354344..13fa2c4d0e51 100644 --- a/crates/rspack_fs/src/memory_fs.rs +++ b/crates/rspack_fs/src/memory_fs.rs @@ -387,7 +387,7 @@ mod tests { WritableFileSystem::create_dir_all(&fs, Utf8Path::new("/a/b/c")) .await .unwrap(); - WritableFileSystem::write(&fs, Utf8Path::new("/a/file1"), file_content) + WritableFileSystem::write(&fs, Utf8Path::new("/a/file1"), file_content.clone()) .await .unwrap(); tokio::time::sleep(std::time::Duration::from_millis(100)).await; @@ -465,16 +465,16 @@ mod tests { .is_err() ); assert_eq!( - ReadableFileSystem::async_read(&fs, Utf8Path::new("/a/file1")) + *ReadableFileSystem::async_read(&fs, Utf8Path::new("/a/file1")) .await .unwrap(), - file_content + *file_content ); assert_eq!( - ReadableFileSystem::async_read(&fs, Utf8Path::new("/a/file2")) + *ReadableFileSystem::async_read(&fs, Utf8Path::new("/a/file2")) .await .unwrap(), - file_content + *file_content ); // stat