2020-06-13 17:09:45 +03:00
|
|
|
#![feature(test)]
|
|
|
|
|
|
|
|
extern crate test;
|
|
|
|
|
2020-10-02 05:07:40 +03:00
|
|
|
use anyhow::Error;
|
2021-06-24 09:32:09 +03:00
|
|
|
use spack::loaders::swc::SwcLoader;
|
2020-06-13 17:09:45 +03:00
|
|
|
use std::{
|
2020-08-14 19:47:28 +03:00
|
|
|
collections::HashMap,
|
2020-06-13 17:09:45 +03:00
|
|
|
env,
|
|
|
|
fs::{create_dir_all, read_dir},
|
|
|
|
io::{self},
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2021-06-24 09:32:09 +03:00
|
|
|
use swc::{config::SourceMapsConfig, resolver::NodeResolver};
|
2020-11-02 09:22:21 +03:00
|
|
|
use swc_atoms::js_word;
|
|
|
|
use swc_bundler::{BundleKind, Bundler, Config, ModuleRecord};
|
2020-10-02 05:07:40 +03:00
|
|
|
use swc_common::{FileName, Span, GLOBALS};
|
2020-11-02 09:22:21 +03:00
|
|
|
use swc_ecma_ast::{
|
|
|
|
Bool, Expr, ExprOrSuper, Ident, KeyValueProp, Lit, MemberExpr, MetaPropExpr, PropName, Str,
|
|
|
|
};
|
2020-12-03 23:03:26 +03:00
|
|
|
use swc_ecma_parser::JscTarget;
|
2020-08-14 15:02:39 +03:00
|
|
|
use swc_ecma_transforms::fixer;
|
|
|
|
use swc_ecma_visit::FoldWith;
|
2020-06-13 17:09:45 +03:00
|
|
|
use test::{
|
|
|
|
test_main, DynTestFn, Options, ShouldPanic::No, TestDesc, TestDescAndFn, TestName, TestType,
|
|
|
|
};
|
|
|
|
use testing::NormalizedOutput;
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
fn add_test<F: FnOnce() + Send + 'static>(
|
|
|
|
tests: &mut Vec<TestDescAndFn>,
|
|
|
|
name: String,
|
|
|
|
ignore: bool,
|
|
|
|
f: F,
|
|
|
|
) {
|
|
|
|
tests.push(TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
test_type: TestType::UnitTest,
|
|
|
|
name: TestName::DynTestName(name.replace("-", "_").replace("/", "::")),
|
|
|
|
ignore,
|
|
|
|
should_panic: No,
|
|
|
|
allow_fail: false,
|
|
|
|
},
|
2020-07-23 20:18:22 +03:00
|
|
|
testfn: DynTestFn(Box::new(f)),
|
2020-06-13 17:09:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reference_tests(tests: &mut Vec<TestDescAndFn>, errors: bool) -> Result<(), io::Error> {
|
|
|
|
let root = {
|
|
|
|
let mut root = Path::new(env!("CARGO_MANIFEST_DIR")).to_path_buf();
|
|
|
|
root.push("tests");
|
|
|
|
root.push(if errors { "error" } else { "pass" });
|
|
|
|
root
|
|
|
|
};
|
|
|
|
|
|
|
|
eprintln!("Loading tests from {}", root.display());
|
|
|
|
|
|
|
|
let dir = root;
|
|
|
|
|
|
|
|
for entry in WalkDir::new(&dir).into_iter() {
|
|
|
|
let entry = entry?;
|
|
|
|
if !entry.path().join("input").exists() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ignore = entry
|
|
|
|
.path()
|
|
|
|
.file_name()
|
|
|
|
.unwrap()
|
|
|
|
.to_string_lossy()
|
|
|
|
.starts_with(".");
|
|
|
|
|
|
|
|
let dir_name = entry
|
|
|
|
.path()
|
|
|
|
.strip_prefix(&dir)
|
|
|
|
.expect("failed to strip prefix")
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let _ = create_dir_all(entry.path().join("output"));
|
|
|
|
|
|
|
|
let entries = read_dir(entry.path().join("input"))?
|
|
|
|
.filter(|e| match e {
|
|
|
|
Ok(e) => {
|
|
|
|
if e.path()
|
|
|
|
.file_name()
|
|
|
|
.unwrap()
|
|
|
|
.to_string_lossy()
|
|
|
|
.starts_with("entry")
|
|
|
|
{
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
.map(|e| -> Result<_, io::Error> {
|
|
|
|
let e = e?;
|
2020-08-12 16:18:47 +03:00
|
|
|
Ok((
|
|
|
|
e.file_name().to_string_lossy().to_string(),
|
|
|
|
FileName::Real(e.path()),
|
|
|
|
))
|
2020-06-13 17:09:45 +03:00
|
|
|
})
|
2020-08-14 19:47:28 +03:00
|
|
|
.collect::<Result<HashMap<_, _>, _>>()?;
|
2020-06-13 17:09:45 +03:00
|
|
|
|
|
|
|
let name = format!(
|
|
|
|
"fixture::{}::{}",
|
|
|
|
if errors { "error" } else { "pass" },
|
|
|
|
dir_name
|
|
|
|
);
|
|
|
|
|
|
|
|
let ignore = ignore
|
|
|
|
|| !name.contains(
|
|
|
|
&env::var("TEST")
|
|
|
|
.ok()
|
|
|
|
.unwrap_or("".into())
|
|
|
|
.replace("::", "/")
|
|
|
|
.replace("_", "-"),
|
|
|
|
);
|
|
|
|
|
|
|
|
add_test(tests, name, ignore, move || {
|
|
|
|
eprintln!("\n\n========== Running reference test {}\n", dir_name);
|
|
|
|
|
|
|
|
testing::run_test2(false, |cm, handler| {
|
2020-08-15 11:19:17 +03:00
|
|
|
let compiler = Arc::new(swc::Compiler::new(cm.clone(), Arc::new(handler)));
|
|
|
|
|
|
|
|
GLOBALS.set(compiler.globals(), || {
|
2020-08-12 16:18:47 +03:00
|
|
|
let loader = SwcLoader::new(
|
|
|
|
compiler.clone(),
|
|
|
|
swc::config::Options {
|
|
|
|
swcrc: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let bundler = Bundler::new(
|
2020-08-15 11:19:17 +03:00
|
|
|
compiler.globals(),
|
2020-08-12 16:18:47 +03:00
|
|
|
cm.clone(),
|
|
|
|
&loader,
|
2021-06-24 09:32:09 +03:00
|
|
|
NodeResolver::default(),
|
2020-08-12 16:18:47 +03:00
|
|
|
Config {
|
|
|
|
require: true,
|
2020-09-04 16:40:03 +03:00
|
|
|
disable_inliner: true,
|
2020-10-28 15:20:11 +03:00
|
|
|
module: Default::default(),
|
2020-08-12 16:18:47 +03:00
|
|
|
external_modules: vec![
|
|
|
|
"assert",
|
|
|
|
"buffer",
|
|
|
|
"child_process",
|
|
|
|
"console",
|
|
|
|
"cluster",
|
|
|
|
"crypto",
|
|
|
|
"dgram",
|
|
|
|
"dns",
|
|
|
|
"events",
|
|
|
|
"fs",
|
|
|
|
"http",
|
|
|
|
"http2",
|
|
|
|
"https",
|
|
|
|
"net",
|
|
|
|
"os",
|
|
|
|
"path",
|
|
|
|
"perf_hooks",
|
|
|
|
"process",
|
|
|
|
"querystring",
|
|
|
|
"readline",
|
|
|
|
"repl",
|
|
|
|
"stream",
|
|
|
|
"string_decoder",
|
|
|
|
"timers",
|
|
|
|
"tls",
|
|
|
|
"tty",
|
|
|
|
"url",
|
|
|
|
"util",
|
|
|
|
"v8",
|
|
|
|
"vm",
|
|
|
|
"wasi",
|
|
|
|
"worker",
|
|
|
|
"zlib",
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.map(From::from)
|
|
|
|
.collect(),
|
|
|
|
},
|
2020-10-02 05:07:40 +03:00
|
|
|
Box::new(Hook),
|
2020-08-12 16:18:47 +03:00
|
|
|
);
|
|
|
|
|
2020-08-15 11:19:17 +03:00
|
|
|
let modules = bundler
|
|
|
|
.bundle(entries)
|
|
|
|
.map_err(|err| println!("{:?}", err))?;
|
|
|
|
println!("Bundled as {} modules", modules.len());
|
2020-08-12 16:18:47 +03:00
|
|
|
|
|
|
|
let mut error = false;
|
|
|
|
|
|
|
|
for bundled in modules {
|
|
|
|
let code = compiler
|
2020-08-14 15:02:39 +03:00
|
|
|
.print(
|
|
|
|
&bundled.module.fold_with(&mut fixer(None)),
|
2021-07-08 10:32:06 +03:00
|
|
|
None,
|
2021-08-02 18:49:34 +03:00
|
|
|
None,
|
2020-12-03 23:03:26 +03:00
|
|
|
JscTarget::Es2020,
|
2020-08-14 15:02:39 +03:00
|
|
|
SourceMapsConfig::Bool(false),
|
|
|
|
None,
|
|
|
|
false,
|
|
|
|
)
|
2020-08-12 16:18:47 +03:00
|
|
|
.expect("failed to print?")
|
|
|
|
.code;
|
|
|
|
|
|
|
|
let name = match bundled.kind {
|
|
|
|
BundleKind::Named { name } | BundleKind::Lib { name } => {
|
|
|
|
PathBuf::from(name)
|
|
|
|
}
|
|
|
|
BundleKind::Dynamic => format!("dynamic.{}.js", bundled.id).into(),
|
|
|
|
};
|
|
|
|
|
2020-11-19 14:42:56 +03:00
|
|
|
let output_path = entry
|
|
|
|
.path()
|
|
|
|
.join("output")
|
|
|
|
.join(name.file_name().unwrap())
|
|
|
|
.with_extension("js");
|
2020-08-12 16:18:47 +03:00
|
|
|
|
2020-08-15 11:19:17 +03:00
|
|
|
println!("Printing {}", output_path.display());
|
2020-08-12 16:18:47 +03:00
|
|
|
|
2020-11-19 14:42:56 +03:00
|
|
|
// {
|
|
|
|
// let status = Command::new("node")
|
|
|
|
// .arg(&output_path)
|
|
|
|
// .stdout(Stdio::inherit())
|
|
|
|
// .stderr(Stdio::inherit())
|
|
|
|
// .status()
|
|
|
|
// .unwrap();
|
|
|
|
// assert!(status.success());
|
|
|
|
// }
|
|
|
|
|
2020-08-12 16:18:47 +03:00
|
|
|
let s = NormalizedOutput::from(code);
|
|
|
|
|
|
|
|
match s.compare_to_file(&output_path) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
2020-08-15 11:19:17 +03:00
|
|
|
println!("Diff: {:?}", err);
|
2020-08-12 16:18:47 +03:00
|
|
|
error = true;
|
|
|
|
}
|
2020-06-13 17:09:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:18:47 +03:00
|
|
|
if error {
|
|
|
|
return Err(());
|
|
|
|
}
|
2020-06-13 17:09:45 +03:00
|
|
|
|
2020-08-12 16:18:47 +03:00
|
|
|
Ok(())
|
|
|
|
})
|
2020-06-13 17:09:45 +03:00
|
|
|
})
|
|
|
|
.expect("failed to process a module");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pass() {
|
|
|
|
let _ = pretty_env_logger::try_init();
|
|
|
|
|
|
|
|
let args: Vec<_> = env::args().collect();
|
|
|
|
let mut tests = Vec::new();
|
|
|
|
reference_tests(&mut tests, false).unwrap();
|
|
|
|
test_main(&args, tests, Some(Options::new()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn errors() {
|
|
|
|
let _ = pretty_env_logger::try_init();
|
|
|
|
|
|
|
|
let args: Vec<_> = env::args().collect();
|
|
|
|
let mut tests = Vec::new();
|
|
|
|
reference_tests(&mut tests, true).unwrap();
|
|
|
|
test_main(&args, tests, Some(Options::new()));
|
|
|
|
}
|
2020-10-02 05:07:40 +03:00
|
|
|
|
|
|
|
struct Hook;
|
|
|
|
|
|
|
|
impl swc_bundler::Hook for Hook {
|
2020-11-02 09:22:21 +03:00
|
|
|
fn get_import_meta_props(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
module_record: &ModuleRecord,
|
|
|
|
) -> Result<Vec<KeyValueProp>, Error> {
|
|
|
|
Ok(vec![
|
|
|
|
KeyValueProp {
|
|
|
|
key: PropName::Ident(Ident::new(js_word!("url"), span)),
|
|
|
|
value: Box::new(Expr::Lit(Lit::Str(Str {
|
|
|
|
span,
|
|
|
|
value: module_record.file_name.to_string().into(),
|
|
|
|
has_escape: false,
|
2020-12-21 22:27:18 +03:00
|
|
|
kind: Default::default(),
|
2020-11-02 09:22:21 +03:00
|
|
|
}))),
|
|
|
|
},
|
|
|
|
KeyValueProp {
|
|
|
|
key: PropName::Ident(Ident::new(js_word!("main"), span)),
|
|
|
|
value: Box::new(if module_record.is_entry {
|
|
|
|
Expr::Member(MemberExpr {
|
|
|
|
span,
|
|
|
|
obj: ExprOrSuper::Expr(Box::new(Expr::MetaProp(MetaPropExpr {
|
|
|
|
meta: Ident::new(js_word!("import"), span),
|
|
|
|
prop: Ident::new(js_word!("meta"), span),
|
|
|
|
}))),
|
|
|
|
prop: Box::new(Expr::Ident(Ident::new(js_word!("main"), span))),
|
|
|
|
computed: false,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Expr::Lit(Lit::Bool(Bool { span, value: false }))
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
])
|
2020-10-02 05:07:40 +03:00
|
|
|
}
|
|
|
|
}
|