Skip to content

Commit

Permalink
chore(rust/test): add visualizable tests of sourcemap (rolldown#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Apr 2, 2024
1 parent 667c7df commit d8fc0c6
Show file tree
Hide file tree
Showing 37 changed files with 916 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions crates/rolldown/tests/common/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::fixture::Fixture;
use rolldown::RolldownOutput;
use rolldown_common::Output;
use rolldown_error::BuildError;
use rolldown_sourcemap::SourcemapVisualizer;

pub struct Case {
fixture: Fixture,
Expand Down Expand Up @@ -68,7 +69,7 @@ impl Case {
assets.sort_by_key(|c| c.file_name().to_string());
let artifacts = assets
.iter()
.filter(|asset| !asset.file_name().contains("$runtime$"))
.filter(|asset| !asset.file_name().contains("$runtime$") && matches!(asset, Output::Chunk(_)))
.flat_map(|asset| {
[
Cow::Owned(format!("## {}\n", asset.file_name())),
Expand All @@ -82,14 +83,18 @@ impl Case {
self.snapshot.push_str(&artifacts);

if self.fixture.test_config().snapshot_output_stats {
self.render_stats_to_snapshot(assets);
self.render_stats_to_snapshot(&assets);
}

if self.fixture.test_config().sourcemap {
self.render_sourcemap_visualizer_to_snapshot(&assets);
}
}

fn render_stats_to_snapshot(&mut self, assets: Vec<Output>) {
fn render_stats_to_snapshot(&mut self, assets: &[Output]) {
self.snapshot.push_str("\n\n## Output Stats\n\n");
let stats = assets
.into_iter()
.iter()
.flat_map(|asset| match asset {
Output::Chunk(chunk) => {
vec![Cow::Owned(format!(
Expand Down Expand Up @@ -123,6 +128,23 @@ impl Case {
self.snapshot.push_str(&rendered);
}

fn render_sourcemap_visualizer_to_snapshot(&mut self, assets: &[Output]) {
self.snapshot.push_str("\n\n# Sourcemap Visualizer\n\n");
let visualizer_result = assets
.iter()
.filter(|asset| !asset.file_name().contains("$runtime$"))
.filter_map(|asset| match asset {
Output::Chunk(chunk) => chunk
.map
.as_ref()
.map(|sourcemap| SourcemapVisualizer::new(&chunk.code, sourcemap).into_visualizer_text()),
Output::Asset(_) => None,
})
.collect::<Vec<_>>()
.join("\n");
self.snapshot.push_str(&visualizer_result);
}

fn make_snapshot(&self) {
// Configure insta to use the fixture path as the snapshot path
let fixture_folder = self.fixture.dir_path();
Expand Down
3 changes: 2 additions & 1 deletion crates/rolldown/tests/common/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
process::Command,
};

use rolldown::{Bundler, External, InputOptions, OutputOptions, RolldownOutput};
use rolldown::{Bundler, External, InputOptions, OutputOptions, RolldownOutput, SourceMapType};
use rolldown_error::BuildError;
use rolldown_testing::TestConfig;

Expand Down Expand Up @@ -127,6 +127,7 @@ impl Fixture {
OutputOptions {
entry_file_names: "[name].mjs".to_string().into(),
chunk_file_names: "[name].mjs".to_string().into(),
sourcemap: test_config.sourcemap.then_some(SourceMapType::File),
..Default::default()
},
);
Expand Down
18 changes: 18 additions & 0 deletions crates/rolldown/tests/fixtures/basic/artifacts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@ const a = 'shared.js';
// main.js
const a$1 = 'index.js';
console.log(a$1, a);
//# sourceMappingURL=main.mjs.map
```

# Sourcemap Visualizer

- shared.js
(0:0-0:6) "const " --> (2:0-2:6) "\nconst"
(0:6-0:10) "a = " --> (2:6-2:10) " a ="
(0:10-2:13) "'shared.js'\n\nexport { a }" --> (2:10-5:0) " 'shared.js';\n\n// main.js"
- main.js
(1:0-1:6) "\nconst" --> (5:0-5:6) "\nconst"
(1:6-1:10) " a =" --> (5:6-5:12) " a$1 ="
(1:10-2:0) " 'index.js'" --> (5:12-6:0) " 'index.js';"
(2:0-2:8) "\nconsole" --> (6:0-6:8) "\nconsole"
(2:8-2:12) ".log" --> (6:8-6:12) ".log"
(2:12-2:15) "(a," --> (6:12-6:17) "(a$1,"
(2:15-2:18) " a2" --> (6:17-6:19) " a"
(2:18-3:1) ")\n" --> (6:19-8:34) ");\n\n//# sourceMappingURL=main.mjs.map"
4 changes: 3 additions & 1 deletion crates/rolldown/tests/fixtures/basic/test.config.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"sourcemap": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,39 @@ function foo(a$1$1) {
return [a$1$1, a$1, a];
}
assert.deepEqual(foo('foo'), ['foo', 'main.js', 'a.js']);
//# sourceMappingURL=main.mjs.map
```

# Sourcemap Visualizer

- a.js
(0:0-0:6) "const " --> (3:0-3:6) "\nconst"
(0:6-0:10) "a = " --> (3:6-3:10) " a ="
(0:10-3:1) "'a.js'\n\nexport { a }\n" --> (3:10-6:0) " 'a.js';\n\n// main.js"
- main.js
(2:0-2:6) "\nconst" --> (6:0-6:6) "\nconst"
(2:6-2:10) " a =" --> (6:6-6:12) " a$1 ="
(2:10-5:0) " 'main.js'\n\n" --> (6:12-7:0) " 'main.js';"
(5:0-5:9) "\nfunction" --> (7:0-7:9) "\nfunction"
(5:9-5:13) " foo" --> (7:9-7:13) " foo"
(5:13-5:18) "(a$1)" --> (7:13-7:20) "(a$1$1)"
(5:18-6:2) " {\n " --> (7:20-8:0) " {"
(6:2-6:9) " return" --> (8:0-8:8) "\n\treturn"
(6:9-6:10) " " --> (8:8-8:9) " "
(6:10-6:15) "[a$1," --> (8:9-8:16) "[a$1$1,"
(6:15-6:18) " a," --> (8:16-8:21) " a$1,"
(6:18-6:22) " aJs" --> (8:21-8:23) " a"
(6:22-7:1) "]\n" --> (8:23-9:0) "];"
(7:1-9:0) "}\n" --> (9:0-10:0) "\n}"
(9:0-9:7) "\nassert" --> (10:0-10:7) "\nassert"
(9:7-9:17) ".deepEqual" --> (10:7-10:17) ".deepEqual"
(9:17-9:21) "(foo" --> (10:17-10:21) "(foo"
(9:21-9:27) "('foo'" --> (10:21-10:27) "('foo'"
(9:27-9:29) ")," --> (10:27-10:29) "),"
(9:29-9:30) " " --> (10:29-10:30) " "
(9:30-9:37) "['foo'," --> (10:30-10:37) "['foo',"
(9:37-9:48) " 'main.js'," --> (10:37-10:48) " 'main.js',"
(9:48-9:55) " 'a.js'" --> (10:48-10:55) " 'a.js'"
(9:55-9:56) "]" --> (10:55-10:56) "]"
(9:56-10:1) ")\n" --> (10:56-12:34) ");\n\n//# sourceMappingURL=main.mjs.map"
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"external": [
"assert"
]
}
},
"sourcemap": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,143 @@ assert.deepStrictEqual(ret.names, ['a2', 'b2', 'c2', 'd2', 'e2']);
assert.deepStrictEqual(ret.params, ['a$1', 'b$1', 'c$1', 'd$1', 'e$1']);
export { a$1 as a, b$1 as b, c$1 as c, d$1 as d, e$1 as e };
//# sourceMappingURL=main.mjs.map
```

# Sourcemap Visualizer

- names.js
(0:7-0:13) "const " --> (3:0-3:6) "\nconst"
(0:13-0:14) "[" --> (3:6-3:7) " "
(0:14-0:17) "a, " --> (3:7-3:9) "[a"
(0:17-0:20) "b, " --> (3:9-3:11) ",b"
(0:20-0:23) "c, " --> (3:11-3:13) ",c"
(0:23-0:26) "d, " --> (3:13-3:15) ",d"
(0:26-0:28) "e]" --> (3:15-3:17) ",e"
(0:28-0:31) " = " --> (3:17-3:20) "] ="
(0:31-0:32) "[" --> (3:20-3:21) " "
(0:32-0:38) "'a2', " --> (3:21-3:27) "['a2',"
(0:38-0:44) "'b2', " --> (3:27-3:33) " 'b2',"
(0:44-0:50) "'c2', " --> (3:33-3:39) " 'c2',"
(0:50-0:56) "'d2', " --> (3:39-3:45) " 'd2',"
(0:56-0:61) "'e2']" --> (3:45-3:50) " 'e2'"
(0:61-1:1) "\n" --> (3:50-6:0) "];\n\n// main.js"
- main.js
(9:7-9:13) " const" --> (6:0-6:6) "\nconst"
(9:13-9:14) " " --> (6:6-6:7) " "
(9:14-9:17) "[a," --> (6:7-6:11) "[a$1"
(9:17-9:20) " b," --> (6:11-6:15) ",b$1"
(9:20-9:23) " c," --> (6:15-6:19) ",c$1"
(9:23-9:26) " d," --> (6:19-6:23) ",d$1"
(9:26-9:28) " e" --> (6:23-6:27) ",e$1"
(9:28-9:31) "] =" --> (6:27-6:30) "] ="
(9:31-9:32) " " --> (6:30-6:31) " "
(9:32-9:37) "['a'," --> (6:31-6:36) "['a',"
(9:37-9:42) " 'b'," --> (6:36-6:41) " 'b',"
(9:42-9:47) " 'c'," --> (6:41-6:46) " 'c',"
(9:47-9:52) " 'd'," --> (6:46-6:51) " 'd',"
(9:52-9:56) " 'e'" --> (6:51-6:55) " 'e'"
(9:56-11:0) "]\n" --> (6:55-7:0) "];"
(11:0-11:9) "\nfunction" --> (7:0-7:9) "\nfunction"
(11:9-11:13) " foo" --> (7:9-7:13) " foo"
(11:13-11:18) "(a$1," --> (7:13-7:20) "(a$1$1,"
(11:18-11:20) " {" --> (7:20-7:21) " "
(11:20-11:25) " b$1 " --> (7:21-7:31) "{b$1:b$1$1"
(11:25-11:27) "}," --> (7:31-7:33) "},"
(11:27-11:29) " {" --> (7:33-7:34) " "
(11:29-11:35) " c$1 =" --> (7:34-7:44) "{c$1:c$1$1"
(11:35-11:42) " 'c$1' " --> (7:44-7:50) "='c$1'"
(11:42-11:44) "}," --> (7:50-7:52) "},"
(11:44-11:46) " {" --> (7:52-7:53) " "
(11:46-11:51) " d$1 " --> (7:53-7:63) "{d$1:d$1$1"
(11:51-11:54) "} =" --> (7:63-7:64) "}"
(11:54-11:56) " {" --> (7:64-8:1) "={\n"
(11:56-11:61) " d$1:" --> (8:1-8:5) "\td$1"
(11:61-11:68) " 'd$1' " --> (8:5-9:1) ":'d$1'\n"
(11:68-11:70) "}," --> (9:1-9:3) "},"
(11:70-11:72) " {" --> (9:3-9:4) " "
(11:72-11:78) " e$1 =" --> (9:4-9:14) "{e$1:e$1$1"
(11:78-11:82) " '' " --> (9:14-9:17) "=''"
(11:82-11:85) "} =" --> (9:17-9:18) "}"
(11:85-11:87) " {" --> (9:18-10:1) "={\n"
(11:87-11:92) " e$1:" --> (10:1-10:5) "\te$1"
(11:92-11:99) " 'e$1' " --> (10:5-11:1) ":'e$1'\n"
(11:99-11:101) "})" --> (11:1-11:3) "})"
(11:101-12:2) " {\n " --> (11:3-12:0) " {"
(12:2-12:9) " return" --> (12:0-12:8) "\n\treturn"
(12:9-13:4) " {\n " --> (12:8-13:2) " {\n\t"
(13:4-13:10) " main:" --> (13:2-13:7) "\tmain"
(13:10-13:11) " " --> (13:7-13:8) ":"
(13:11-13:14) "[a," --> (13:8-13:13) "[a$1,"
(13:14-13:17) " b," --> (13:13-13:18) " b$1,"
(13:17-13:20) " c," --> (13:18-13:23) " c$1,"
(13:20-13:23) " d," --> (13:23-13:28) " d$1,"
(13:23-13:25) " e" --> (13:28-13:32) " e$1"
(13:25-14:4) "],\n " --> (13:32-14:2) "],\n\t"
(14:4-14:11) " names:" --> (14:2-14:8) "\tnames"
(14:11-14:12) " " --> (14:8-14:9) ":"
(14:12-14:16) "[a2," --> (14:9-14:12) "[a,"
(14:16-14:20) " b2," --> (14:12-14:15) " b,"
(14:20-14:24) " c2," --> (14:15-14:18) " c,"
(14:24-14:28) " d2," --> (14:18-14:21) " d,"
(14:28-14:31) " e2" --> (14:21-14:23) " e"
(14:31-15:4) "],\n " --> (14:23-15:2) "],\n\t"
(15:4-15:12) " params:" --> (15:2-15:9) "\tparams"
(15:12-15:13) " " --> (15:9-15:10) ":"
(15:13-15:18) "[a$1," --> (15:10-15:17) "[a$1$1,"
(15:18-15:23) " b$1," --> (15:17-15:24) " b$1$1,"
(15:23-15:28) " c$1," --> (15:24-15:31) " c$1$1,"
(15:28-15:33) " d$1," --> (15:31-15:38) " d$1$1,"
(15:33-15:37) " e$1" --> (15:38-15:44) " e$1$1"
(15:37-16:3) "],\n " --> (15:44-16:2) "]\n\t"
(16:3-17:1) "}\n" --> (16:2-17:0) "};"
(17:1-19:0) "}\n" --> (17:0-18:0) "\n}"
(19:0-19:6) "\nconst" --> (18:0-18:6) "\nconst"
(19:6-19:12) " ret =" --> (18:6-18:12) " ret ="
(19:12-19:16) " foo" --> (18:12-18:16) " foo"
(19:16-19:23) "('a$1'," --> (18:16-18:23) "('a$1',"
(19:23-19:25) " {" --> (18:23-19:1) " {\n"
(19:25-19:30) " b$1:" --> (19:1-19:5) "\tb$1"
(19:30-19:37) " 'b$1' " --> (19:5-20:1) ":'b$1'\n"
(19:37-19:39) "}," --> (20:1-20:3) "},"
(19:39-19:41) " {" --> (20:3-20:5) " {"
(19:41-19:43) "}," --> (20:5-20:7) "},"
(19:43-19:54) " undefined," --> (20:7-20:18) " undefined,"
(19:54-19:64) " undefined" --> (20:18-20:28) " undefined"
(19:64-20:0) ")" --> (20:28-21:0) ");"
(20:0-20:7) "\nassert" --> (21:0-21:7) "\nassert"
(20:7-20:23) ".deepStrictEqual" --> (21:7-21:23) ".deepStrictEqual"
(20:23-20:27) "(ret" --> (21:23-21:27) "(ret"
(20:27-20:33) ".main," --> (21:27-21:33) ".main,"
(20:33-20:34) " " --> (21:33-21:34) " "
(20:34-20:39) "['a'," --> (21:34-21:39) "['a',"
(20:39-20:44) " 'b'," --> (21:39-21:44) " 'b',"
(20:44-20:49) " 'c'," --> (21:44-21:49) " 'c',"
(20:49-20:54) " 'd'," --> (21:49-21:54) " 'd',"
(20:54-20:58) " 'e'" --> (21:54-21:58) " 'e'"
(20:58-20:59) "]" --> (21:58-21:59) "]"
(20:59-21:0) ")" --> (21:59-22:0) ");"
(21:0-21:7) "\nassert" --> (22:0-22:7) "\nassert"
(21:7-21:23) ".deepStrictEqual" --> (22:7-22:23) ".deepStrictEqual"
(21:23-21:27) "(ret" --> (22:23-22:27) "(ret"
(21:27-21:34) ".names," --> (22:27-22:34) ".names,"
(21:34-21:35) " " --> (22:34-22:35) " "
(21:35-21:41) "['a2'," --> (22:35-22:41) "['a2',"
(21:41-21:47) " 'b2'," --> (22:41-22:47) " 'b2',"
(21:47-21:53) " 'c2'," --> (22:47-22:53) " 'c2',"
(21:53-21:59) " 'd2'," --> (22:53-22:59) " 'd2',"
(21:59-21:64) " 'e2'" --> (22:59-22:64) " 'e2'"
(21:64-21:65) "]" --> (22:64-22:65) "]"
(21:65-22:0) ")" --> (22:65-23:0) ");"
(22:0-22:7) "\nassert" --> (23:0-23:7) "\nassert"
(22:7-22:23) ".deepStrictEqual" --> (23:7-23:23) ".deepStrictEqual"
(22:23-22:27) "(ret" --> (23:23-23:27) "(ret"
(22:27-22:35) ".params," --> (23:27-23:35) ".params,"
(22:35-22:36) " " --> (23:35-23:36) " "
(22:36-22:43) "['a$1'," --> (23:36-23:43) "['a$1',"
(22:43-22:50) " 'b$1'," --> (23:43-23:50) " 'b$1',"
(22:50-22:57) " 'c$1'," --> (23:50-23:57) " 'c$1',"
(22:57-22:64) " 'd$1'," --> (23:57-23:64) " 'd$1',"
(22:64-22:70) " 'e$1'" --> (23:64-23:70) " 'e$1'"
(22:70-22:71) "]" --> (23:70-23:71) "]"
(22:71-23:1) ")\n" --> (23:71-26:34) ");\n\nexport { a$1 as a, b$1 as b, c$1 as c, d$1 as d, e$1 as e };\n//# sourceMappingURL=main.mjs.map"
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"external": [
"assert"
]
}
},
"sourcemap": true
}
Loading

0 comments on commit d8fc0c6

Please sign in to comment.