Skip to content

Commit

Permalink
feat(rust/test): show filename of binary files in snapshot (#2561)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
  • Loading branch information
hyf0 authored Nov 3, 2024
1 parent 918a4e0 commit 9f9689d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ snapshot_kind: text
---
# Assets

## assets/foo-vpuLoj-k.txt

## main.js

```js
Expand Down
58 changes: 33 additions & 25 deletions crates/rolldown_testing/src/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,7 @@ impl IntegrationTest {
let artifacts = assets
.iter()
.filter_map(|asset| {
let content = match asset {
Output::Chunk(inner) => &inner.code,
Output::Asset(inner) => match &inner.source {
rolldown_common::StrOrBytes::Str(inner) => inner,
// Snapshot buffer is meaningless
rolldown_common::StrOrBytes::Bytes(_) => return None,
},
};
let content = if self.test_meta.hidden_runtime_module {
RUNTIME_MODULE_OUTPUT_RE.replace_all(content, "")
} else {
Cow::Borrowed(content.as_str())
};

let filename = asset.filename();

let file_ext = filename.as_path().extension().and_then(OsStr::to_str).map_or(
"unknown",
|ext| match ext {
Expand All @@ -234,17 +219,40 @@ impl IntegrationTest {
},
);

if file_ext == "map" {
// Skip sourcemap for now
return None;
match asset {
Output::Chunk(output_chunk) => {
let content = &output_chunk.code;
let content = if self.test_meta.hidden_runtime_module {
RUNTIME_MODULE_OUTPUT_RE.replace_all(content, "")
} else {
Cow::Borrowed(content.as_str())
};

Some(vec![
Cow::Owned(format!("## {}\n", asset.filename())),
Cow::Owned(format!("```{file_ext}")),
content,
"```".into(),
])
}
Output::Asset(output_asset) => {
if file_ext == "map" {
// Skip sourcemap for now
return None;
}
match &output_asset.source {
rolldown_common::StrOrBytes::Str(content) => Some(vec![
Cow::Owned(format!("## {}\n", asset.filename())),
Cow::Owned(format!("```{file_ext}")),
Cow::Borrowed(content),
"```".into(),
]),
rolldown_common::StrOrBytes::Bytes(_) => {
Some(vec![Cow::Owned(format!("## {}\n", asset.filename()))])
}
}
}
}

Some([
Cow::Owned(format!("## {}\n", asset.filename())),
Cow::Owned(format!("```{file_ext}")),
content,
"```".into(),
])
})
.flatten()
.collect::<Vec<_>>()
Expand Down

0 comments on commit 9f9689d

Please sign in to comment.