2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-02-16 13:14:19 -05:00
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2022-06-14 10:05:37 -04:00
|
|
|
use std::sync::Arc;
|
2022-02-16 13:14:19 -05:00
|
|
|
|
|
|
|
use deno_ast::ModuleSpecifier;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_config::workspace::WorkspaceResolver;
|
2022-02-16 13:14:19 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::anyhow::bail;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures;
|
2023-07-14 18:10:42 -04:00
|
|
|
use deno_core::futures::FutureExt;
|
2022-02-16 13:14:19 -05:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_graph::source::LoadFuture;
|
|
|
|
use deno_graph::source::LoadResponse;
|
|
|
|
use deno_graph::source::Loader;
|
2023-10-25 18:13:22 -04:00
|
|
|
use deno_graph::DefaultModuleAnalyzer;
|
2023-06-06 17:07:46 -04:00
|
|
|
use deno_graph::GraphKind;
|
2022-02-16 13:14:19 -05:00
|
|
|
use deno_graph::ModuleGraph;
|
2022-06-14 10:05:37 -04:00
|
|
|
use import_map::ImportMap;
|
|
|
|
|
2023-07-05 12:43:22 -04:00
|
|
|
use crate::args::JsxImportSourceConfig;
|
2022-08-22 12:14:59 -04:00
|
|
|
use crate::cache::ParsedSourceCache;
|
2023-02-15 11:30:54 -05:00
|
|
|
use crate::resolver::CliGraphResolver;
|
2023-08-17 12:14:22 -04:00
|
|
|
use crate::resolver::CliGraphResolverOptions;
|
2022-02-16 13:14:19 -05:00
|
|
|
|
|
|
|
use super::build::VendorEnvironment;
|
|
|
|
|
|
|
|
// Utilities that help `deno vendor` get tested in memory.
|
|
|
|
|
|
|
|
type RemoteFileText = String;
|
|
|
|
type RemoteFileHeaders = Option<HashMap<String, String>>;
|
|
|
|
type RemoteFileResult = Result<(RemoteFileText, RemoteFileHeaders), String>;
|
|
|
|
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct TestLoader {
|
|
|
|
files: HashMap<ModuleSpecifier, RemoteFileResult>,
|
|
|
|
redirects: HashMap<ModuleSpecifier, ModuleSpecifier>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestLoader {
|
|
|
|
pub fn add(
|
|
|
|
&mut self,
|
|
|
|
path_or_specifier: impl AsRef<str>,
|
|
|
|
text: impl AsRef<str>,
|
2022-08-03 21:23:45 -04:00
|
|
|
) -> &mut Self {
|
|
|
|
self.add_result(path_or_specifier, Ok((text.as_ref().to_string(), None)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_failure(
|
|
|
|
&mut self,
|
|
|
|
path_or_specifier: impl AsRef<str>,
|
|
|
|
message: impl AsRef<str>,
|
|
|
|
) -> &mut Self {
|
|
|
|
self.add_result(path_or_specifier, Err(message.as_ref().to_string()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_result(
|
|
|
|
&mut self,
|
|
|
|
path_or_specifier: impl AsRef<str>,
|
|
|
|
result: RemoteFileResult,
|
2022-02-16 13:14:19 -05:00
|
|
|
) -> &mut Self {
|
|
|
|
if path_or_specifier
|
|
|
|
.as_ref()
|
|
|
|
.to_lowercase()
|
|
|
|
.starts_with("http")
|
|
|
|
{
|
|
|
|
self.files.insert(
|
|
|
|
ModuleSpecifier::parse(path_or_specifier.as_ref()).unwrap(),
|
2022-08-03 21:23:45 -04:00
|
|
|
result,
|
2022-02-16 13:14:19 -05:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let path = make_path(path_or_specifier.as_ref());
|
|
|
|
let specifier = ModuleSpecifier::from_file_path(path).unwrap();
|
2022-08-03 21:23:45 -04:00
|
|
|
self.files.insert(specifier, result);
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_with_headers(
|
|
|
|
&mut self,
|
|
|
|
specifier: impl AsRef<str>,
|
|
|
|
text: impl AsRef<str>,
|
|
|
|
headers: &[(&str, &str)],
|
|
|
|
) -> &mut Self {
|
|
|
|
let headers = headers
|
|
|
|
.iter()
|
|
|
|
.map(|(key, value)| (key.to_string(), value.to_string()))
|
|
|
|
.collect();
|
|
|
|
self.files.insert(
|
|
|
|
ModuleSpecifier::parse(specifier.as_ref()).unwrap(),
|
|
|
|
Ok((text.as_ref().to_string(), Some(headers))),
|
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_redirect(
|
|
|
|
&mut self,
|
|
|
|
from: impl AsRef<str>,
|
|
|
|
to: impl AsRef<str>,
|
|
|
|
) -> &mut Self {
|
|
|
|
self.redirects.insert(
|
|
|
|
ModuleSpecifier::parse(from.as_ref()).unwrap(),
|
|
|
|
ModuleSpecifier::parse(to.as_ref()).unwrap(),
|
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Loader for TestLoader {
|
|
|
|
fn load(
|
2024-04-18 20:51:16 -04:00
|
|
|
&self,
|
2022-02-16 13:14:19 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2024-02-15 14:49:35 -05:00
|
|
|
_options: deno_graph::source::LoadOptions,
|
2022-02-16 13:14:19 -05:00
|
|
|
) -> LoadFuture {
|
2024-04-18 21:43:28 -04:00
|
|
|
if let Some(redirect) = self.redirects.get(specifier) {
|
|
|
|
return Box::pin(futures::future::ready(Ok(Some(
|
|
|
|
LoadResponse::Redirect {
|
|
|
|
specifier: redirect.clone(),
|
|
|
|
},
|
|
|
|
))));
|
|
|
|
}
|
2022-02-16 13:14:19 -05:00
|
|
|
let result = self.files.get(specifier).map(|result| match result {
|
|
|
|
Ok(result) => Ok(LoadResponse::Module {
|
|
|
|
specifier: specifier.clone(),
|
2024-01-31 22:15:22 -05:00
|
|
|
content: result.0.clone().into_bytes().into(),
|
2022-02-16 13:14:19 -05:00
|
|
|
maybe_headers: result.1.clone(),
|
|
|
|
}),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
});
|
|
|
|
let result = match result {
|
|
|
|
Some(Ok(result)) => Ok(Some(result)),
|
|
|
|
Some(Err(err)) => Err(anyhow!("{}", err)),
|
|
|
|
None if specifier.scheme() == "data" => {
|
|
|
|
deno_graph::source::load_data_url(specifier)
|
|
|
|
}
|
|
|
|
None => Ok(None),
|
|
|
|
};
|
|
|
|
Box::pin(futures::future::ready(result))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct TestVendorEnvironment {
|
|
|
|
directories: RefCell<HashSet<PathBuf>>,
|
|
|
|
files: RefCell<HashMap<PathBuf, String>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VendorEnvironment for TestVendorEnvironment {
|
|
|
|
fn create_dir_all(&self, dir_path: &Path) -> Result<(), AnyError> {
|
|
|
|
let mut directories = self.directories.borrow_mut();
|
|
|
|
for path in dir_path.ancestors() {
|
|
|
|
if !directories.insert(path.to_path_buf()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:22 -05:00
|
|
|
fn write_file(&self, file_path: &Path, text: &[u8]) -> Result<(), AnyError> {
|
2022-02-16 13:14:19 -05:00
|
|
|
let parent = file_path.parent().unwrap();
|
|
|
|
if !self.directories.borrow().contains(parent) {
|
|
|
|
bail!("Directory not found: {}", parent.display());
|
|
|
|
}
|
2024-01-31 22:15:22 -05:00
|
|
|
self.files.borrow_mut().insert(
|
|
|
|
file_path.to_path_buf(),
|
|
|
|
String::from_utf8(text.to_vec()).unwrap(),
|
|
|
|
);
|
2022-02-16 13:14:19 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct VendorOutput {
|
|
|
|
pub files: Vec<(String, String)>,
|
|
|
|
pub import_map: Option<serde_json::Value>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct VendorTestBuilder {
|
|
|
|
entry_points: Vec<ModuleSpecifier>,
|
|
|
|
loader: TestLoader,
|
2024-07-03 20:54:33 -04:00
|
|
|
maybe_original_import_map: Option<ImportMap>,
|
2022-06-14 10:05:37 -04:00
|
|
|
environment: TestVendorEnvironment,
|
2023-07-05 12:43:22 -04:00
|
|
|
jsx_import_source_config: Option<JsxImportSourceConfig>,
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VendorTestBuilder {
|
|
|
|
pub fn with_default_setup() -> Self {
|
|
|
|
let mut builder = VendorTestBuilder::default();
|
|
|
|
builder.add_entry_point("/mod.ts");
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:10:42 -04:00
|
|
|
pub fn resolve_to_url(&self, path: &str) -> ModuleSpecifier {
|
|
|
|
ModuleSpecifier::from_file_path(make_path(path)).unwrap()
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:05:37 -04:00
|
|
|
pub fn new_import_map(&self, base_path: &str) -> ImportMap {
|
2023-07-14 18:10:42 -04:00
|
|
|
let base = self.resolve_to_url(base_path);
|
2022-06-14 10:05:37 -04:00
|
|
|
ImportMap::new(base)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_original_import_map(
|
|
|
|
&mut self,
|
|
|
|
import_map: ImportMap,
|
|
|
|
) -> &mut Self {
|
2024-07-03 20:54:33 -04:00
|
|
|
self.maybe_original_import_map = Some(import_map);
|
2022-06-14 10:05:37 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:14:19 -05:00
|
|
|
pub fn add_entry_point(&mut self, entry_point: impl AsRef<str>) -> &mut Self {
|
|
|
|
let entry_point = make_path(entry_point.as_ref());
|
|
|
|
self
|
|
|
|
.entry_points
|
|
|
|
.push(ModuleSpecifier::from_file_path(entry_point).unwrap());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:43:22 -04:00
|
|
|
pub fn set_jsx_import_source_config(
|
|
|
|
&mut self,
|
|
|
|
jsx_import_source_config: JsxImportSourceConfig,
|
|
|
|
) -> &mut Self {
|
|
|
|
self.jsx_import_source_config = Some(jsx_import_source_config);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:14:19 -05:00
|
|
|
pub async fn build(&mut self) -> Result<VendorOutput, AnyError> {
|
|
|
|
let output_dir = make_path("/vendor");
|
2023-07-14 18:10:42 -04:00
|
|
|
let entry_points = self.entry_points.clone();
|
2022-06-14 10:05:37 -04:00
|
|
|
let loader = self.loader.clone();
|
2023-10-25 18:13:22 -04:00
|
|
|
let parsed_source_cache = ParsedSourceCache::default();
|
2023-07-14 18:10:42 -04:00
|
|
|
let resolver = Arc::new(build_resolver(
|
2023-07-05 12:43:22 -04:00
|
|
|
self.jsx_import_source_config.clone(),
|
2024-07-03 20:54:33 -04:00
|
|
|
self.maybe_original_import_map.clone(),
|
2023-07-14 18:10:42 -04:00
|
|
|
));
|
|
|
|
super::build::build(super::build::BuildInput {
|
|
|
|
entry_points,
|
|
|
|
build_graph: {
|
|
|
|
let resolver = resolver.clone();
|
|
|
|
move |entry_points| {
|
|
|
|
async move {
|
|
|
|
Ok(
|
|
|
|
build_test_graph(
|
|
|
|
entry_points,
|
|
|
|
loader,
|
|
|
|
resolver.as_graph_resolver(),
|
2024-04-11 19:00:17 -04:00
|
|
|
&DefaultModuleAnalyzer,
|
2023-07-14 18:10:42 -04:00
|
|
|
)
|
|
|
|
.await,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.boxed_local()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
parsed_source_cache: &parsed_source_cache,
|
|
|
|
output_dir: &output_dir,
|
2024-07-03 20:54:33 -04:00
|
|
|
maybe_original_import_map: self.maybe_original_import_map.as_ref(),
|
2023-07-14 18:10:42 -04:00
|
|
|
maybe_jsx_import_source: self.jsx_import_source_config.as_ref(),
|
|
|
|
resolver: resolver.as_graph_resolver(),
|
|
|
|
environment: &self.environment,
|
|
|
|
})
|
|
|
|
.await?;
|
2022-06-14 10:05:37 -04:00
|
|
|
|
|
|
|
let mut files = self.environment.files.borrow_mut();
|
2022-02-16 13:14:19 -05:00
|
|
|
let import_map = files.remove(&output_dir.join("import_map.json"));
|
|
|
|
let mut files = files
|
|
|
|
.iter()
|
2023-01-05 14:29:50 -05:00
|
|
|
.map(|(path, text)| (path_to_string(path), text.to_string()))
|
2022-02-16 13:14:19 -05:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
files.sort_by(|a, b| a.0.cmp(&b.0));
|
|
|
|
|
|
|
|
Ok(VendorOutput {
|
|
|
|
import_map: import_map.map(|text| serde_json::from_str(&text).unwrap()),
|
|
|
|
files,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_loader(&mut self, action: impl Fn(&mut TestLoader)) -> &mut Self {
|
|
|
|
action(&mut self.loader);
|
|
|
|
self
|
|
|
|
}
|
2022-06-14 10:05:37 -04:00
|
|
|
}
|
2022-02-16 13:14:19 -05:00
|
|
|
|
2023-07-14 18:10:42 -04:00
|
|
|
fn build_resolver(
|
2023-08-17 12:14:22 -04:00
|
|
|
maybe_jsx_import_source_config: Option<JsxImportSourceConfig>,
|
2024-07-03 20:54:33 -04:00
|
|
|
maybe_original_import_map: Option<ImportMap>,
|
2023-07-14 18:10:42 -04:00
|
|
|
) -> CliGraphResolver {
|
2023-10-25 14:39:00 -04:00
|
|
|
CliGraphResolver::new(CliGraphResolverOptions {
|
|
|
|
node_resolver: None,
|
|
|
|
npm_resolver: None,
|
2023-12-06 19:03:18 -05:00
|
|
|
sloppy_imports_resolver: None,
|
2024-07-03 20:54:33 -04:00
|
|
|
workspace_resolver: Arc::new(WorkspaceResolver::new_raw(
|
|
|
|
maybe_original_import_map,
|
|
|
|
Vec::new(),
|
|
|
|
deno_config::workspace::PackageJsonDepResolution::Enabled,
|
|
|
|
)),
|
2023-10-25 14:39:00 -04:00
|
|
|
maybe_jsx_import_source_config,
|
|
|
|
maybe_vendor_dir: None,
|
|
|
|
bare_node_builtins_enabled: false,
|
|
|
|
})
|
2023-07-14 18:10:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn build_test_graph(
|
|
|
|
roots: Vec<ModuleSpecifier>,
|
2024-04-18 20:51:16 -04:00
|
|
|
loader: TestLoader,
|
2023-07-14 18:10:42 -04:00
|
|
|
resolver: &dyn deno_graph::source::Resolver,
|
2022-08-22 12:14:59 -04:00
|
|
|
analyzer: &dyn deno_graph::ModuleAnalyzer,
|
2022-06-14 10:05:37 -04:00
|
|
|
) -> ModuleGraph {
|
2023-06-06 17:07:46 -04:00
|
|
|
let mut graph = ModuleGraph::new(GraphKind::All);
|
2023-02-09 22:00:23 -05:00
|
|
|
graph
|
|
|
|
.build(
|
|
|
|
roots,
|
2024-04-18 20:51:16 -04:00
|
|
|
&loader,
|
2023-02-09 22:00:23 -05:00
|
|
|
deno_graph::BuildOptions {
|
2023-07-14 18:10:42 -04:00
|
|
|
resolver: Some(resolver),
|
2024-04-11 19:00:17 -04:00
|
|
|
module_analyzer: analyzer,
|
2023-02-09 22:00:23 -05:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
graph
|
2022-02-16 13:14:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_path(text: &str) -> PathBuf {
|
|
|
|
// This should work all in memory. We're waiting on
|
|
|
|
// https://github.com/servo/rust-url/issues/730 to provide
|
|
|
|
// a cross platform path here
|
|
|
|
assert!(text.starts_with('/'));
|
|
|
|
if cfg!(windows) {
|
2022-02-24 20:03:12 -05:00
|
|
|
PathBuf::from(format!("C:{}", text.replace('/', "\\")))
|
2022-02-16 13:14:19 -05:00
|
|
|
} else {
|
|
|
|
PathBuf::from(text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:29:50 -05:00
|
|
|
fn path_to_string<P>(path: P) -> String
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
|
|
|
let path = path.as_ref();
|
2022-02-16 13:14:19 -05:00
|
|
|
// inverse of the function above
|
|
|
|
let path = path.to_string_lossy();
|
|
|
|
if cfg!(windows) {
|
|
|
|
path.replace("C:\\", "\\").replace('\\', "/")
|
|
|
|
} else {
|
|
|
|
path.to_string()
|
|
|
|
}
|
|
|
|
}
|