-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmod.rs
More file actions
128 lines (116 loc) · 4.33 KB
/
mod.rs
File metadata and controls
128 lines (116 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
* This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc.
**/
use assert_cmd::prelude::*;
use nodejs_semver::Version;
use orchestrion_js::*;
use std::io::prelude::*;
use std::path::PathBuf;
use std::process::Command;
use std::sync::Arc;
use swc::{
config::{IsModule, SourceMapsConfig},
try_with_handler, Compiler, HandlerOpts, PrintArgs,
};
use swc_core::common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping};
use swc_core::ecma::ast::EsVersion;
use swc_ecma_parser::{EsSyntax, Syntax};
use swc_ecma_visit::VisitMutWith;
fn print_result(original: &str, modified: &str) {
println!(
"\n - == === Original === == - \n{}\n\n\n - == === Modified === == - \n{}\n\n",
original, modified
);
}
fn transpile(
contents: &str,
is_module: IsModule,
instrumentation: &mut InstrumentationVisitor,
) -> String {
let compiler = Compiler::new(Arc::new(swc_core::common::SourceMap::new(
FilePathMapping::empty(),
)));
try_with_handler(
compiler.cm.clone(),
HandlerOpts {
color: ColorConfig::Never,
skip_filename: false,
},
|handler| {
let source_file = compiler.cm.new_source_file(
Arc::new(FileName::Real(PathBuf::from("index.mjs"))),
contents.to_string(),
);
let program = compiler
.parse_js(
source_file.to_owned(),
handler,
EsVersion::latest(),
Syntax::Es(EsSyntax {
explicit_resource_management: true,
import_attributes: true,
decorators: true,
..Default::default()
}),
is_module,
Some(&compiler.comments() as &dyn Comments),
)
.map(|mut program| {
program.visit_mut_with(instrumentation);
program
})
.unwrap();
let result = compiler
.print(
&program,
PrintArgs {
source_file_name: None,
source_map: SourceMapsConfig::Bool(false),
comments: None,
emit_source_map_columns: false,
..Default::default()
},
)
.unwrap();
print_result(contents, &result.code);
Ok(result.code)
},
)
.unwrap()
}
static TEST_MODULE_NAME: &str = "undici";
static TEST_MODULE_PATH: &str = "index.mjs";
pub fn transpile_and_test(test_file: &str, mjs: bool, config: Config) {
let test_file = PathBuf::from(test_file);
let test_dir = test_file.parent().expect("Couldn't find test directory");
let file_path = PathBuf::from("index.mjs");
let mut instrumentor = Instrumentor::new(config);
let dep = Dependency {
name: TEST_MODULE_NAME.to_string(),
version: Version::parse("0.0.1").unwrap(),
relative_path: file_path,
};
let abs = PathBuf::from("");
let mut instrumentations = instrumentor.get_matching_instrumentations(&abs, Some(&dep));
let extension = if mjs { "mjs" } else { "js" };
let instrumentable = test_dir.join(format!("mod.{}", extension));
let mut file = std::fs::File::open(&instrumentable).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let result = transpile(&contents, IsModule::Bool(mjs), &mut instrumentations);
let instrumented_file = test_dir.join(format!("instrumented.{}", extension));
let mut file = std::fs::File::create(&instrumented_file).unwrap();
file.write_all(result.as_bytes()).unwrap();
let test_file = format!("test.{}", extension);
Command::new("node")
.current_dir(test_dir)
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.arg(&test_file)
.assert()
.success();
}
pub fn test_module_matcher() -> CodeMatcher {
CodeMatcher::dependency(TEST_MODULE_NAME, ">=0.0.1", TEST_MODULE_PATH).unwrap()
}