Skip to content

Commit

Permalink
fix: test case
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 19, 2024
1 parent efd9562 commit 6b2cb16
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
51 changes: 34 additions & 17 deletions crates/rspack_core/src/cache/persistent/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl Snapshot {

#[cfg(test)]
mod tests {
use std::borrow::Cow;
use std::sync::Arc;

use rspack_fs::{MemoryFileSystem, WritableFileSystem};
Expand Down Expand Up @@ -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);

Expand All @@ -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());
Expand All @@ -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();
Expand Down
14 changes: 9 additions & 5 deletions crates/rspack_core/src/cache/persistent/snapshot/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ impl Compiler {
)
.await?;

let content: Vec<u8> = source.buffer().into();
let content = source.buffer();

let mut immutable = asset.info.immutable.unwrap_or(false);
if !query.is_empty() {
Expand Down Expand Up @@ -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());
}

Expand Down
10 changes: 5 additions & 5 deletions crates/rspack_fs/src/memory_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6b2cb16

Please sign in to comment.