1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor: Remove call sites of "deno_core::resolve_url_or_path" (#18169)

These call sites didn't need to use "resolve_url_or_path".

Towards landing https://github.com/denoland/deno/pull/15454
This commit is contained in:
Bartek Iwańczuk 2023-03-13 19:31:03 -04:00 committed by GitHub
parent a35c8e6588
commit c4771356f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 64 additions and 43 deletions

View file

@ -742,7 +742,6 @@ mod tests {
use super::*;
use deno_core::error::get_custom_error_class;
use deno_core::resolve_url;
use deno_core::resolve_url_or_path;
use deno_core::url::Url;
use deno_runtime::deno_fetch::create_http_client;
use deno_runtime::deno_web::Blob;
@ -827,7 +826,8 @@ mod tests {
async fn test_fetch_local_encoded(charset: &str, expected: String) {
let p = test_util::testdata_path().join(format!("encoding/{charset}.ts"));
let specifier = resolve_url_or_path(p.to_str().unwrap()).unwrap();
let specifier =
ModuleSpecifier::from_file_path(p.to_str().unwrap()).unwrap();
let (file, _) = test_fetch(&specifier).await;
assert_eq!(&*file.source, expected);
}
@ -845,7 +845,7 @@ mod tests {
];
for (specifier, is_ok, expected) in fixtures {
let specifier = resolve_url_or_path(specifier).unwrap();
let specifier = ModuleSpecifier::parse(specifier).unwrap();
let actual = get_validated_scheme(&specifier);
assert_eq!(actual.is_ok(), is_ok);
if is_ok {
@ -1021,7 +1021,7 @@ mod tests {
];
for (specifier, maybe_content_type, media_type, maybe_charset) in fixtures {
let specifier = resolve_url_or_path(specifier).unwrap();
let specifier = ModuleSpecifier::parse(specifier).unwrap();
assert_eq!(
map_content_type(&specifier, maybe_content_type.as_ref()),
(media_type, maybe_charset)
@ -1034,7 +1034,8 @@ mod tests {
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
let local = temp_dir.path().join("a.ts");
let specifier =
resolve_url_or_path(local.as_os_str().to_str().unwrap()).unwrap();
ModuleSpecifier::from_file_path(local.as_os_str().to_str().unwrap())
.unwrap();
let file = File {
local,
maybe_types: None,
@ -1143,7 +1144,7 @@ mod tests {
let (file_fetcher_01, _) = setup(CacheSetting::Use, Some(temp_dir.clone()));
let (file_fetcher_02, _) = setup(CacheSetting::Use, Some(temp_dir.clone()));
let specifier =
resolve_url_or_path("http://localhost:4545/subdir/mod2.ts").unwrap();
ModuleSpecifier::parse("http://localhost:4545/subdir/mod2.ts").unwrap();
let result = file_fetcher
.fetch(&specifier, PermissionsContainer::allow_all())
@ -1587,8 +1588,7 @@ mod tests {
async fn test_fetch_local_bypasses_file_cache() {
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
let fixture_path = temp_dir.path().join("mod.ts");
let specifier =
resolve_url_or_path(&fixture_path.to_string_lossy()).unwrap();
let specifier = ModuleSpecifier::from_file_path(&fixture_path).unwrap();
fs::write(fixture_path.clone(), r#"console.log("hello deno");"#).unwrap();
let result = file_fetcher
.fetch(&specifier, PermissionsContainer::allow_all())
@ -1690,7 +1690,8 @@ mod tests {
#[tokio::test]
async fn test_fetch_remote_javascript_with_types() {
let specifier =
resolve_url_or_path("http://127.0.0.1:4545/xTypeScriptTypes.js").unwrap();
ModuleSpecifier::parse("http://127.0.0.1:4545/xTypeScriptTypes.js")
.unwrap();
let (file, _) = test_fetch_remote(&specifier).await;
assert_eq!(
file.maybe_types,
@ -1701,7 +1702,7 @@ mod tests {
#[tokio::test]
async fn test_fetch_remote_jsx_with_types() {
let specifier =
resolve_url_or_path("http://127.0.0.1:4545/xTypeScriptTypes.jsx")
ModuleSpecifier::parse("http://127.0.0.1:4545/xTypeScriptTypes.jsx")
.unwrap();
let (file, _) = test_fetch_remote(&specifier).await;
assert_eq!(file.media_type, MediaType::Jsx,);
@ -1714,7 +1715,8 @@ mod tests {
#[tokio::test]
async fn test_fetch_remote_typescript_with_types() {
let specifier =
resolve_url_or_path("http://127.0.0.1:4545/xTypeScriptTypes.ts").unwrap();
ModuleSpecifier::parse("http://127.0.0.1:4545/xTypeScriptTypes.ts")
.unwrap();
let (file, _) = test_fetch_remote(&specifier).await;
assert_eq!(file.maybe_types, None);
}

View file

@ -565,7 +565,8 @@ impl ProcState {
// but sadly that's not the case due to missing APIs in V8.
let is_repl = matches!(self.options.sub_command(), DenoSubcommand::Repl(_));
let referrer = if referrer.is_empty() && is_repl {
deno_core::resolve_url_or_path("./$deno$repl.ts")?
let cwd = std::env::current_dir().context("Unable to get CWD")?;
deno_core::resolve_path("./$deno$repl.ts", &cwd)?
} else {
deno_core::resolve_url_or_path(referrer)?
};

View file

@ -658,6 +658,6 @@ Deno.test(
p.close();
p.stdout.close();
assertStrictEquals(code, 1);
assertStringIncludes(stderr, "invalid module path");
assertStringIncludes(stderr, "Unable to get CWD");
},
);

View file

@ -12,7 +12,9 @@ use crate::proc_state::ProcState;
use crate::tsc::get_types_declaration_file_text;
use deno_ast::MediaType;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::resolve_path;
use deno_core::resolve_url_or_path;
use deno_doc as doc;
use deno_graph::ModuleSpecifier;
@ -60,11 +62,12 @@ pub async fn print_docs(
doc_parser.parse_module(&source_file_specifier)?.definitions
}
DocSourceFileFlag::Path(source_file) => {
let cwd = std::env::current_dir().context("Unable to get CWD")?;
let module_specifier = resolve_url_or_path(&source_file)?;
// If the root module has external types, the module graph won't redirect it,
// so instead create a dummy file which exports everything from the actual file being documented.
let root_specifier = resolve_url_or_path("./$deno$doc.ts").unwrap();
let root_specifier = resolve_path("./$deno$doc.ts", &cwd).unwrap();
let root = File {
local: PathBuf::from("./$deno$doc.ts"),
maybe_types: None,

View file

@ -5,8 +5,9 @@ use crate::args::ReplFlags;
use crate::colors;
use crate::proc_state::ProcState;
use crate::worker::create_main_worker;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::resolve_url_or_path;
use deno_core::resolve_path;
use deno_runtime::permissions::Permissions;
use deno_runtime::permissions::PermissionsContainer;
use rustyline::error::ReadlineError;
@ -80,7 +81,8 @@ async fn read_eval_file(
}
pub async fn run(flags: Flags, repl_flags: ReplFlags) -> Result<i32, AnyError> {
let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap();
let cwd = std::env::current_dir().context("Unable to get CWD")?;
let main_module = resolve_path("./$deno$repl.ts", &cwd).unwrap();
let ps = ProcState::build(flags).await?;
let mut worker = create_main_worker(
&ps,

View file

@ -11,6 +11,7 @@ use deno_ast::swc::visit::VisitWith;
use deno_ast::DiagnosticsError;
use deno_ast::ImportsNotUsedAsValues;
use deno_ast::ModuleSpecifier;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::futures::channel::mpsc::UnboundedReceiver;
use deno_core::futures::FutureExt;
@ -143,7 +144,8 @@ impl ReplSession {
}
assert_ne!(context_id, 0);
let referrer = deno_core::resolve_url_or_path("./$deno$repl.ts").unwrap();
let cwd = std::env::current_dir().context("Unable to get CWD")?;
let referrer = deno_core::resolve_path("./$deno$repl.ts", &cwd).unwrap();
let mut repl_session = ReplSession {
proc_state,

View file

@ -5,7 +5,9 @@ use std::sync::Arc;
use deno_ast::MediaType;
use deno_ast::ModuleSpecifier;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::resolve_path;
use deno_core::resolve_url_or_path;
use deno_graph::npm::NpmPackageReqReference;
use deno_runtime::permissions::Permissions;
@ -67,7 +69,8 @@ To grant permissions, set them before the script argument. For example:
pub async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
let ps = ProcState::build(flags).await?;
let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap();
let cwd = std::env::current_dir().context("Unable to get CWD")?;
let main_module = resolve_path("./$deno$stdin.ts", &cwd).unwrap();
let mut worker = create_main_worker(
&ps,
main_module.clone(),
@ -139,8 +142,9 @@ pub async fn eval_command(
) -> Result<i32, AnyError> {
// deno_graph works off of extensions for local files to determine the media
// type, and so our "fake" specifier needs to have the proper extension.
let cwd = std::env::current_dir().context("Unable to get CWD")?;
let main_module =
resolve_url_or_path(&format!("./$deno$eval.{}", eval_flags.ext))?;
resolve_path(&format!("./$deno$eval.{}", eval_flags.ext), &cwd)?;
let ps = ProcState::build(flags).await?;
let permissions = PermissionsContainer::new(Permissions::from_options(
&ps.options.permissions_options(),

View file

@ -928,7 +928,7 @@ mod tests {
maybe_tsbuildinfo: Option<String>,
) -> OpState {
let specifier = maybe_specifier
.unwrap_or_else(|| resolve_url_or_path("file:///main.ts").unwrap());
.unwrap_or_else(|| ModuleSpecifier::parse("file:///main.ts").unwrap());
let hash_data = maybe_hash_data.unwrap_or_else(|| vec![b"".to_vec()]);
let fixtures = test_util::testdata_path().join("tsc2");
let mut loader = MockLoader { fixtures };
@ -1050,7 +1050,7 @@ mod tests {
("file:///.tsbuildinfo", MediaType::Unknown),
];
for (specifier, media_type) in fixtures {
let specifier = resolve_url_or_path(specifier).unwrap();
let specifier = ModuleSpecifier::parse(specifier).unwrap();
assert_eq!(get_tsc_media_type(&specifier), media_type);
}
}
@ -1076,7 +1076,7 @@ mod tests {
#[tokio::test]
async fn test_load() {
let mut state = setup(
Some(resolve_url_or_path("https://deno.land/x/mod.ts").unwrap()),
Some(ModuleSpecifier::parse("https://deno.land/x/mod.ts").unwrap()),
None,
Some("some content".to_string()),
)
@ -1107,7 +1107,7 @@ mod tests {
#[tokio::test]
async fn test_load_asset() {
let mut state = setup(
Some(resolve_url_or_path("https://deno.land/x/mod.ts").unwrap()),
Some(ModuleSpecifier::parse("https://deno.land/x/mod.ts").unwrap()),
None,
Some("some content".to_string()),
)
@ -1128,7 +1128,7 @@ mod tests {
#[tokio::test]
async fn test_load_tsbuildinfo() {
let mut state = setup(
Some(resolve_url_or_path("https://deno.land/x/mod.ts").unwrap()),
Some(ModuleSpecifier::parse("https://deno.land/x/mod.ts").unwrap()),
None,
Some("some content".to_string()),
)
@ -1169,7 +1169,7 @@ mod tests {
#[tokio::test]
async fn test_resolve() {
let mut state = setup(
Some(resolve_url_or_path("https://deno.land/x/a.ts").unwrap()),
Some(ModuleSpecifier::parse("https://deno.land/x/a.ts").unwrap()),
None,
None,
)
@ -1191,7 +1191,7 @@ mod tests {
#[tokio::test]
async fn test_resolve_empty() {
let mut state = setup(
Some(resolve_url_or_path("https://deno.land/x/a.ts").unwrap()),
Some(ModuleSpecifier::parse("https://deno.land/x/a.ts").unwrap()),
None,
None,
)
@ -1253,7 +1253,7 @@ mod tests {
#[tokio::test]
async fn test_exec_basic() {
let specifier = resolve_url_or_path("https://deno.land/x/a.ts").unwrap();
let specifier = ModuleSpecifier::parse("https://deno.land/x/a.ts").unwrap();
let actual = test_exec(&specifier)
.await
.expect("exec should not have errored");
@ -1264,7 +1264,7 @@ mod tests {
#[tokio::test]
async fn test_exec_reexport_dts() {
let specifier = resolve_url_or_path("file:///reexports.ts").unwrap();
let specifier = ModuleSpecifier::parse("file:///reexports.ts").unwrap();
let actual = test_exec(&specifier)
.await
.expect("exec should not have errored");
@ -1275,7 +1275,7 @@ mod tests {
#[tokio::test]
async fn fix_lib_ref() {
let specifier = resolve_url_or_path("file:///libref.ts").unwrap();
let specifier = ModuleSpecifier::parse("file:///libref.ts").unwrap();
let actual = test_exec(&specifier)
.await
.expect("exec should not have errored");

View file

@ -728,6 +728,7 @@ fn create_web_worker_callback(
#[cfg(test)]
mod tests {
use super::*;
use deno_core::resolve_path;
use deno_core::resolve_url_or_path;
use deno_core::FsModuleLoader;
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
@ -735,7 +736,8 @@ mod tests {
use deno_runtime::permissions::Permissions;
fn create_test_worker() -> MainWorker {
let main_module = resolve_url_or_path("./hello.js").unwrap();
let main_module =
resolve_path("./hello.js", &std::env::current_dir().unwrap()).unwrap();
let permissions = PermissionsContainer::new(Permissions::default());
let options = WorkerOptions {
@ -802,7 +804,7 @@ mod tests {
.parent()
.unwrap()
.join("tests/circular1.js");
let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
let mut worker = create_test_worker();
let result = worker.execute_main_module(&module_specifier).await;
if let Err(err) = result {
@ -817,7 +819,9 @@ mod tests {
async fn execute_mod_resolve_error() {
// "foo" is not a valid module specifier so this should return an error.
let mut worker = create_test_worker();
let module_specifier = resolve_url_or_path("does-not-exist").unwrap();
let module_specifier =
resolve_path("./does-not-exist", &std::env::current_dir().unwrap())
.unwrap();
let result = worker.execute_main_module(&module_specifier).await;
assert!(result.is_err());
}
@ -828,7 +832,7 @@ mod tests {
// tests).
let mut worker = create_test_worker();
let p = test_util::testdata_path().join("run/001_hello.js");
let module_specifier = resolve_url_or_path(&p.to_string_lossy()).unwrap();
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
let result = worker.execute_main_module(&module_specifier).await;
assert!(result.is_ok());
}

View file

@ -2550,7 +2550,6 @@ pub fn create_child_permissions(
#[cfg(test)]
mod tests {
use super::*;
use deno_core::resolve_url_or_path;
use deno_core::serde_json::json;
use prompter::tests::*;
@ -2854,27 +2853,31 @@ mod tests {
let mut fixtures = vec![
(
resolve_url_or_path("http://localhost:4545/mod.ts").unwrap(),
ModuleSpecifier::parse("http://localhost:4545/mod.ts").unwrap(),
true,
),
(
resolve_url_or_path("http://deno.land/x/mod.ts").unwrap(),
ModuleSpecifier::parse("http://deno.land/x/mod.ts").unwrap(),
false,
),
(
resolve_url_or_path("data:text/plain,Hello%2C%20Deno!").unwrap(),
ModuleSpecifier::parse("data:text/plain,Hello%2C%20Deno!").unwrap(),
true,
),
];
if cfg!(target_os = "windows") {
fixtures
.push((resolve_url_or_path("file:///C:/a/mod.ts").unwrap(), true));
fixtures
.push((resolve_url_or_path("file:///C:/b/mod.ts").unwrap(), false));
.push((ModuleSpecifier::parse("file:///C:/a/mod.ts").unwrap(), true));
fixtures.push((
ModuleSpecifier::parse("file:///C:/b/mod.ts").unwrap(),
false,
));
} else {
fixtures.push((resolve_url_or_path("file:///a/mod.ts").unwrap(), true));
fixtures.push((resolve_url_or_path("file:///b/mod.ts").unwrap(), false));
fixtures
.push((ModuleSpecifier::parse("file:///a/mod.ts").unwrap(), true));
fixtures
.push((ModuleSpecifier::parse("file:///b/mod.ts").unwrap(), false));
}
for (specifier, expected) in fixtures {
@ -2898,7 +2901,7 @@ mod tests {
for url in test_cases {
assert!(perms
.check_specifier(&resolve_url_or_path(url).unwrap())
.check_specifier(&ModuleSpecifier::parse(url).unwrap())
.is_err());
}
}