1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00
David Sherret 2023-03-30 17:47:53 -04:00 committed by Matt Mastracci
parent 5949321c55
commit 5d20c36eaf
12 changed files with 813 additions and 247 deletions

View file

@ -11,7 +11,6 @@ use deno_core::serde::Deserialize;
use deno_core::serde::Serialize; use deno_core::serde::Serialize;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::url::Url; use deno_core::url::Url;
use log::error;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
@ -42,7 +41,7 @@ fn base_url_to_filename(url: &Url) -> Option<PathBuf> {
} }
"data" | "blob" => (), "data" | "blob" => (),
scheme => { scheme => {
error!("Don't know how to create cache name for scheme: {}", scheme); log::debug!("Don't know how to create cache name for scheme: {}", scheme);
return None; return None;
} }
}; };

View file

@ -485,6 +485,31 @@ impl Config {
.unwrap_or_else(|| self.settings.workspace.enable) .unwrap_or_else(|| self.settings.workspace.enable)
} }
/// Gets the root directories or file paths based on the workspace config.
pub fn enabled_root_urls(&self) -> Vec<Url> {
let mut urls: Vec<Url> = Vec::new();
if !self.settings.workspace.enable && self.enabled_paths.is_empty() {
// do not return any urls when disabled
return urls;
}
for (workspace, enabled_paths) in &self.enabled_paths {
if !enabled_paths.is_empty() {
urls.extend(enabled_paths.iter().cloned());
} else {
urls.push(workspace.clone());
}
}
if urls.is_empty() {
if let Some(root_dir) = &self.root_uri {
urls.push(root_dir.clone())
}
}
sort_and_remove_non_leaf_urls(&mut urls);
urls
}
pub fn specifier_code_lens_test(&self, specifier: &ModuleSpecifier) -> bool { pub fn specifier_code_lens_test(&self, specifier: &ModuleSpecifier) -> bool {
let value = self let value = self
.settings .settings
@ -621,6 +646,21 @@ impl Config {
} }
} }
/// Removes any URLs that are a descendant of another URL in the collection.
fn sort_and_remove_non_leaf_urls(dirs: &mut Vec<Url>) {
if dirs.is_empty() {
return;
}
dirs.sort();
for i in (0..dirs.len() - 1).rev() {
let prev = &dirs[i + 1];
if prev.as_str().starts_with(dirs[i].as_str()) {
dirs.remove(i + 1);
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -785,4 +825,69 @@ mod tests {
WorkspaceSettings::default() WorkspaceSettings::default()
); );
} }
#[test]
fn test_sort_and_remove_non_leaf_urls() {
fn run_test(dirs: Vec<&str>, expected_output: Vec<&str>) {
let mut dirs = dirs
.into_iter()
.map(|dir| Url::parse(dir).unwrap())
.collect();
sort_and_remove_non_leaf_urls(&mut dirs);
let dirs: Vec<_> = dirs.iter().map(|dir| dir.as_str()).collect();
assert_eq!(dirs, expected_output);
}
run_test(
vec![
"file:///test/asdf/test/asdf/",
"file:///test/asdf/",
"file:///test/asdf/",
"file:///testing/456/893/",
"file:///testing/456/893/test/",
],
vec!["file:///test/asdf/", "file:///testing/456/893/"],
);
run_test(vec![], vec![]);
}
#[test]
fn config_enabled_root_urls() {
let mut config = Config::new();
let root_dir = Url::parse("file:///example/").unwrap();
config.root_uri = Some(root_dir.clone());
config.settings.workspace.enable = false;
config.settings.workspace.enable_paths = Vec::new();
assert_eq!(config.enabled_root_urls(), vec![]);
config.settings.workspace.enable = true;
assert_eq!(config.enabled_root_urls(), vec![root_dir]);
config.settings.workspace.enable = false;
let root_dir1 = Url::parse("file:///root1/").unwrap();
let root_dir2 = Url::parse("file:///root2/").unwrap();
let root_dir3 = Url::parse("file:///root3/").unwrap();
config.enabled_paths = HashMap::from([
(
root_dir1.clone(),
vec![
root_dir1.join("sub_dir").unwrap(),
root_dir1.join("sub_dir/other").unwrap(),
root_dir1.join("test.ts").unwrap(),
],
),
(root_dir2.clone(), vec![root_dir2.join("other.ts").unwrap()]),
(root_dir3.clone(), vec![]),
]);
assert_eq!(
config.enabled_root_urls(),
vec![
root_dir1.join("sub_dir").unwrap(),
root_dir1.join("test.ts").unwrap(),
root_dir2.join("other.ts").unwrap(),
root_dir3
]
);
}
} }

View file

@ -43,11 +43,13 @@ use deno_runtime::deno_node::NodeResolutionMode;
use deno_runtime::deno_node::PackageJson; use deno_runtime::deno_node::PackageJson;
use deno_runtime::permissions::PermissionsContainer; use deno_runtime::permissions::PermissionsContainer;
use indexmap::IndexMap; use indexmap::IndexMap;
use lsp::Url;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet; use std::collections::HashSet;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::fs; use std::fs;
use std::fs::ReadDir;
use std::ops::Range; use std::ops::Range;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
@ -775,18 +777,6 @@ impl FileSystemDocuments {
self.docs.insert(specifier.clone(), doc.clone()); self.docs.insert(specifier.clone(), doc.clone());
Some(doc) Some(doc)
} }
pub fn refresh_dependencies(
&mut self,
resolver: &dyn deno_graph::source::Resolver,
) {
for doc in self.docs.values_mut() {
if let Some(new_doc) = doc.maybe_with_new_resolver(resolver) {
*doc = new_doc;
}
}
self.dirty = true;
}
} }
fn get_document_path( fn get_document_path(
@ -1166,6 +1156,7 @@ impl Documents {
pub fn update_config( pub fn update_config(
&mut self, &mut self,
root_urls: Vec<Url>,
maybe_import_map: Option<Arc<import_map::ImportMap>>, maybe_import_map: Option<Arc<import_map::ImportMap>>,
maybe_config_file: Option<&ConfigFile>, maybe_config_file: Option<&ConfigFile>,
maybe_package_json: Option<&PackageJson>, maybe_package_json: Option<&PackageJson>,
@ -1173,11 +1164,18 @@ impl Documents {
npm_resolution: NpmResolution, npm_resolution: NpmResolution,
) { ) {
fn calculate_resolver_config_hash( fn calculate_resolver_config_hash(
root_urls: &[Url],
maybe_import_map: Option<&import_map::ImportMap>, maybe_import_map: Option<&import_map::ImportMap>,
maybe_jsx_config: Option<&JsxImportSourceConfig>, maybe_jsx_config: Option<&JsxImportSourceConfig>,
maybe_package_json_deps: Option<&PackageJsonDeps>, maybe_package_json_deps: Option<&PackageJsonDeps>,
) -> u64 { ) -> u64 {
let mut hasher = FastInsecureHasher::default(); let mut hasher = FastInsecureHasher::default();
hasher.write_hashable(&{
// ensure these are sorted (they should be, but this is a safeguard)
let mut root_urls = root_urls.to_vec();
root_urls.sort_unstable();
root_urls
});
if let Some(import_map) = maybe_import_map { if let Some(import_map) = maybe_import_map {
hasher.write_str(&import_map.to_json()); hasher.write_str(&import_map.to_json());
hasher.write_str(import_map.base_url().as_str()); hasher.write_str(import_map.base_url().as_str());
@ -1193,6 +1191,7 @@ impl Documents {
let maybe_jsx_config = let maybe_jsx_config =
maybe_config_file.and_then(|cf| cf.to_maybe_jsx_import_source_config()); maybe_config_file.and_then(|cf| cf.to_maybe_jsx_import_source_config());
let new_resolver_config_hash = calculate_resolver_config_hash( let new_resolver_config_hash = calculate_resolver_config_hash(
&root_urls,
maybe_import_map.as_deref(), maybe_import_map.as_deref(),
maybe_jsx_config.as_ref(), maybe_jsx_config.as_ref(),
maybe_package_json_deps.as_ref(), maybe_package_json_deps.as_ref(),
@ -1232,21 +1231,51 @@ impl Documents {
// only refresh the dependencies if the underlying configuration has changed // only refresh the dependencies if the underlying configuration has changed
if self.resolver_config_hash != new_resolver_config_hash { if self.resolver_config_hash != new_resolver_config_hash {
self.refresh_dependencies(); self.refresh_dependencies(root_urls);
self.resolver_config_hash = new_resolver_config_hash; self.resolver_config_hash = new_resolver_config_hash;
} }
self.dirty = true; self.dirty = true;
} }
fn refresh_dependencies(&mut self) { fn refresh_dependencies(&mut self, root_urls: Vec<Url>) {
let resolver = self.resolver.as_graph_resolver(); let resolver = self.resolver.as_graph_resolver();
for doc in self.open_docs.values_mut() { for doc in self.open_docs.values_mut() {
if let Some(new_doc) = doc.maybe_with_new_resolver(resolver) { if let Some(new_doc) = doc.maybe_with_new_resolver(resolver) {
*doc = new_doc; *doc = new_doc;
} }
} }
self.file_system_docs.lock().refresh_dependencies(resolver);
// update the file system documents
let mut fs_docs = self.file_system_docs.lock();
let mut not_found_docs =
fs_docs.docs.keys().cloned().collect::<HashSet<_>>();
let open_docs = &mut self.open_docs;
for specifier in PreloadDocumentFinder::from_root_urls(&root_urls) {
// mark this document as having been found
not_found_docs.remove(&specifier);
if !open_docs.contains_key(&specifier)
&& !fs_docs.docs.contains_key(&specifier)
{
fs_docs.refresh_document(&self.cache, resolver, &specifier);
} else {
// update the existing entry to have the new resolver
if let Some(doc) = fs_docs.docs.get_mut(&specifier) {
if let Some(new_doc) = doc.maybe_with_new_resolver(resolver) {
*doc = new_doc;
}
}
}
}
// clean up and remove any documents that weren't found
for uri in not_found_docs {
fs_docs.docs.remove(&uri);
}
fs_docs.dirty = true;
} }
/// Iterate through the documents, building a map where the key is a unique /// Iterate through the documents, building a map where the key is a unique
@ -1478,12 +1507,150 @@ fn analyze_module(
} }
} }
/// Iterator that finds documents that can be preloaded into
/// the LSP on startup.
struct PreloadDocumentFinder {
pending_dirs: Vec<PathBuf>,
pending_files: Vec<PathBuf>,
current_entries: Option<ReadDir>,
}
impl PreloadDocumentFinder {
pub fn from_root_urls(root_urls: &Vec<Url>) -> Self {
let mut finder = PreloadDocumentFinder {
pending_dirs: Default::default(),
pending_files: Default::default(),
current_entries: Default::default(),
};
for root_url in root_urls {
if let Ok(path) = root_url.to_file_path() {
if path.is_dir() {
finder.pending_dirs.push(path);
} else {
finder.pending_files.push(path);
}
}
}
finder
}
fn read_next_file_entry(&mut self) -> Option<ModuleSpecifier> {
fn is_discoverable_dir(dir_path: &Path) -> bool {
if let Some(dir_name) = dir_path.file_name() {
let dir_name = dir_name.to_string_lossy().to_lowercase();
!matches!(dir_name.as_str(), "node_modules" | ".git")
} else {
false
}
}
if let Some(mut entries) = self.current_entries.take() {
while let Some(entry) = entries.next() {
if let Ok(entry) = entry {
let path = entry.path();
if let Ok(file_type) = entry.file_type() {
if file_type.is_dir() && is_discoverable_dir(&path) {
self.pending_dirs.push(path);
} else if file_type.is_file() {
if let Some(specifier) = Self::get_valid_specifier(&path) {
// restore the next entries for next time
self.current_entries = Some(entries);
return Some(specifier);
}
}
}
}
}
}
None
}
fn get_valid_specifier(path: &Path) -> Option<ModuleSpecifier> {
fn is_discoverable_file(file_path: &Path) -> bool {
// Don't auto-discover minified files as they are likely to be very large
// and likely not to have dependencies on code outside them that would
// be useful in the LSP
if let Some(file_name) = file_path.file_name() {
let file_name = file_name.to_string_lossy().to_lowercase();
!file_name.as_str().contains(".min.")
} else {
false
}
}
fn is_discoverable_media_type(media_type: MediaType) -> bool {
match media_type {
MediaType::JavaScript
| MediaType::Jsx
| MediaType::Mjs
| MediaType::Cjs
| MediaType::TypeScript
| MediaType::Mts
| MediaType::Cts
| MediaType::Dts
| MediaType::Dmts
| MediaType::Dcts
| MediaType::Tsx => true,
MediaType::Json // ignore because json never depends on other files
| MediaType::Wasm
| MediaType::SourceMap
| MediaType::TsBuildInfo
| MediaType::Unknown => false,
}
}
let media_type = MediaType::from_path(path);
if is_discoverable_media_type(media_type) && is_discoverable_file(path) {
if let Ok(specifier) = ModuleSpecifier::from_file_path(path) {
return Some(specifier);
}
}
None
}
fn queue_next_file_entries(&mut self) {
debug_assert!(self.current_entries.is_none());
while let Some(dir_path) = self.pending_dirs.pop() {
if let Ok(entries) = fs::read_dir(&dir_path) {
self.current_entries = Some(entries);
break;
}
}
}
}
impl Iterator for PreloadDocumentFinder {
type Item = ModuleSpecifier;
fn next(&mut self) -> Option<Self::Item> {
// drain the pending files
while let Some(path) = self.pending_files.pop() {
if let Some(specifier) = Self::get_valid_specifier(&path) {
return Some(specifier);
}
}
// then go through the current entries and directories
while !self.pending_dirs.is_empty() || self.current_entries.is_some() {
match self.read_next_file_entry() {
Some(entry) => return Some(entry),
None => {
self.queue_next_file_entries();
}
}
}
None
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::npm::NpmResolution; use crate::npm::NpmResolution;
use super::*; use super::*;
use import_map::ImportMap; use import_map::ImportMap;
use pretty_assertions::assert_eq;
use test_util::TempDir; use test_util::TempDir;
fn setup(temp_dir: &TempDir) -> (Documents, PathBuf) { fn setup(temp_dir: &TempDir) -> (Documents, PathBuf) {
@ -1616,6 +1783,7 @@ console.log(b, "hello deno");
.unwrap(); .unwrap();
documents.update_config( documents.update_config(
vec![],
Some(Arc::new(import_map)), Some(Arc::new(import_map)),
None, None,
None, None,
@ -1655,6 +1823,7 @@ console.log(b, "hello deno");
.unwrap(); .unwrap();
documents.update_config( documents.update_config(
vec![],
Some(Arc::new(import_map)), Some(Arc::new(import_map)),
None, None,
None, None,
@ -1676,4 +1845,64 @@ console.log(b, "hello deno");
); );
} }
} }
#[test]
pub fn test_pre_load_document_finder() {
let temp_dir = TempDir::new();
temp_dir.create_dir_all("root1/node_modules/");
temp_dir.write("root1/node_modules/mod.ts", ""); // no, node_modules
temp_dir.create_dir_all("root1/sub_dir");
temp_dir.create_dir_all("root1/.git");
temp_dir.create_dir_all("root1/file.ts"); // no, directory
temp_dir.write("root1/mod1.ts", ""); // yes
temp_dir.write("root1/mod2.js", ""); // yes
temp_dir.write("root1/mod3.tsx", ""); // yes
temp_dir.write("root1/mod4.d.ts", ""); // yes
temp_dir.write("root1/mod5.jsx", ""); // yes
temp_dir.write("root1/mod6.mjs", ""); // yes
temp_dir.write("root1/mod7.mts", ""); // yes
temp_dir.write("root1/mod8.d.mts", ""); // yes
temp_dir.write("root1/other.json", ""); // no, json
temp_dir.write("root1/other.txt", ""); // no, text file
temp_dir.write("root1/other.wasm", ""); // no, don't load wasm
temp_dir.write("root1/sub_dir/mod.ts", ""); // yes
temp_dir.write("root1/sub_dir/data.min.ts", ""); // no, minified file
temp_dir.write("root1/.git/main.ts", ""); // no, .git folder
temp_dir.create_dir_all("root2/folder");
temp_dir.write("root2/file1.ts", ""); // yes, provided
temp_dir.write("root2/file2.ts", ""); // no, not provided
temp_dir.write("root2/folder/main.ts", ""); // yes, provided
temp_dir.create_dir_all("root3/");
temp_dir.write("root3/mod.ts", ""); // no, not provided
let mut urls = PreloadDocumentFinder::from_root_urls(&vec![
temp_dir.uri().join("root1/").unwrap(),
temp_dir.uri().join("root2/file1.ts").unwrap(),
temp_dir.uri().join("root2/folder/").unwrap(),
])
.collect::<Vec<_>>();
// order doesn't matter
urls.sort();
assert_eq!(
urls,
vec![
temp_dir.uri().join("root1/mod1.ts").unwrap(),
temp_dir.uri().join("root1/mod2.js").unwrap(),
temp_dir.uri().join("root1/mod3.tsx").unwrap(),
temp_dir.uri().join("root1/mod4.d.ts").unwrap(),
temp_dir.uri().join("root1/mod5.jsx").unwrap(),
temp_dir.uri().join("root1/mod6.mjs").unwrap(),
temp_dir.uri().join("root1/mod7.mts").unwrap(),
temp_dir.uri().join("root1/mod8.d.mts").unwrap(),
temp_dir.uri().join("root1/sub_dir/mod.ts").unwrap(),
temp_dir.uri().join("root2/file1.ts").unwrap(),
temp_dir.uri().join("root2/folder/main.ts").unwrap(),
]
);
}
} }

View file

@ -1138,6 +1138,7 @@ impl Inner {
fn refresh_documents_config(&mut self) { fn refresh_documents_config(&mut self) {
self.documents.update_config( self.documents.update_config(
self.config.enabled_root_urls(),
self.maybe_import_map.clone(), self.maybe_import_map.clone(),
self.maybe_config_file.as_ref(), self.maybe_config_file.as_ref(),
self.maybe_package_json.as_ref(), self.maybe_package_json.as_ref(),

View file

@ -2803,9 +2803,9 @@ fn op_respond(state: &mut OpState, args: Response) -> bool {
fn op_script_names(state: &mut OpState) -> Vec<String> { fn op_script_names(state: &mut OpState) -> Vec<String> {
let state = state.borrow_mut::<State>(); let state = state.borrow_mut::<State>();
let documents = &state.state_snapshot.documents; let documents = &state.state_snapshot.documents;
let open_docs = documents.documents(DocumentsFilter::OpenDiagnosable); let all_docs = documents.documents(DocumentsFilter::AllDiagnosable);
let mut result = Vec::new();
let mut seen = HashSet::new(); let mut seen = HashSet::new();
let mut result = Vec::new();
if documents.has_injected_types_node_package() { if documents.has_injected_types_node_package() {
// ensure this is first so it resolves the node types first // ensure this is first so it resolves the node types first
@ -2822,23 +2822,17 @@ fn op_script_names(state: &mut OpState) -> Vec<String> {
} }
// finally include the documents and all their dependencies // finally include the documents and all their dependencies
for doc in &open_docs { for doc in &all_docs {
let specifier = doc.specifier(); let specifiers = std::iter::once(doc.specifier()).chain(
if seen.insert(specifier.as_str()) { doc
result.push(specifier.to_string()); .dependencies()
} .values()
} .filter_map(|dep| dep.get_type().or_else(|| dep.get_code())),
);
// and then all their dependencies (do this after to avoid exists calls) for specifier in specifiers {
for doc in &open_docs { if seen.insert(specifier.as_str()) && documents.exists(specifier) {
for dep in doc.dependencies().values() { // only include dependencies we know to exist otherwise typescript will error
if let Some(specifier) = dep.get_type().or_else(|| dep.get_code()) { result.push(specifier.to_string());
if seen.insert(specifier.as_str()) {
// only include dependencies we know to exist otherwise typescript will error
if documents.exists(specifier) {
result.push(specifier.to_string());
}
}
} }
} }
} }

View file

@ -11,21 +11,21 @@ use std::fs;
use std::process::Stdio; use std::process::Stdio;
use test_util::deno_cmd_with_deno_dir; use test_util::deno_cmd_with_deno_dir;
use test_util::env_vars_for_npm_tests; use test_util::env_vars_for_npm_tests;
use test_util::lsp::LspClientBuilder;
use test_util::testdata_path; use test_util::testdata_path;
use test_util::TestContextBuilder; use test_util::TestContextBuilder;
use tower_lsp::lsp_types as lsp; use tower_lsp::lsp_types as lsp;
#[test] #[test]
fn lsp_startup_shutdown() { fn lsp_startup_shutdown() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.shutdown(); client.shutdown();
} }
#[test] #[test]
fn lsp_init_tsconfig() { fn lsp_init_tsconfig() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
@ -58,7 +58,7 @@ fn lsp_init_tsconfig() {
#[test] #[test]
fn lsp_tsconfig_types() { fn lsp_tsconfig_types() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
@ -98,7 +98,8 @@ fn lsp_tsconfig_types() {
#[test] #[test]
fn lsp_tsconfig_bad_config_path() { fn lsp_tsconfig_bad_config_path() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder builder
.set_config("bad_tsconfig.json") .set_config("bad_tsconfig.json")
@ -123,7 +124,7 @@ fn lsp_tsconfig_bad_config_path() {
#[test] #[test]
fn lsp_triple_slash_types() { fn lsp_triple_slash_types() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
let a_dts = "// deno-lint-ignore-file no-var\ndeclare var a: string;"; let a_dts = "// deno-lint-ignore-file no-var\ndeclare var a: string;";
temp_dir.write("a.d.ts", a_dts); temp_dir.write("a.d.ts", a_dts);
@ -146,7 +147,7 @@ fn lsp_triple_slash_types() {
#[test] #[test]
fn lsp_import_map() { fn lsp_import_map() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
let import_map = r#"{ let import_map = r#"{
"imports": { "imports": {
@ -205,7 +206,7 @@ fn lsp_import_map() {
#[test] #[test]
fn lsp_import_map_data_url() { fn lsp_import_map_data_url() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.set_import_map("data:application/json;utf8,{\"imports\": { \"example\": \"https://deno.land/x/example/mod.ts\" }}"); builder.set_import_map("data:application/json;utf8,{\"imports\": { \"example\": \"https://deno.land/x/example/mod.ts\" }}");
@ -230,7 +231,7 @@ fn lsp_import_map_data_url() {
#[test] #[test]
fn lsp_import_map_config_file() { fn lsp_import_map_config_file() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
"deno.import_map.jsonc", "deno.import_map.jsonc",
@ -297,7 +298,7 @@ fn lsp_import_map_config_file() {
#[test] #[test]
fn lsp_import_map_embedded_in_config_file() { fn lsp_import_map_embedded_in_config_file() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
"deno.embedded_import_map.jsonc", "deno.embedded_import_map.jsonc",
@ -358,7 +359,7 @@ fn lsp_import_map_embedded_in_config_file() {
#[test] #[test]
fn lsp_deno_task() { fn lsp_deno_task() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
"deno.jsonc", "deno.jsonc",
@ -393,7 +394,7 @@ fn lsp_deno_task() {
#[test] #[test]
fn lsp_import_assertions() { fn lsp_import_assertions() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.set_import_map("data:application/json;utf8,{\"imports\": { \"example\": \"https://deno.land/x/example/mod.ts\" }}"); builder.set_import_map("data:application/json;utf8,{\"imports\": { \"example\": \"https://deno.land/x/example/mod.ts\" }}");
@ -509,7 +510,7 @@ fn lsp_import_assertions() {
#[test] #[test]
fn lsp_import_map_import_completions() { fn lsp_import_map_import_completions() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
"import-map.json", "import-map.json",
@ -649,7 +650,8 @@ fn lsp_import_map_import_completions() {
#[test] #[test]
fn lsp_hover() { fn lsp_hover() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -690,7 +692,8 @@ fn lsp_hover() {
#[test] #[test]
fn lsp_hover_asset() { fn lsp_hover_asset() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -747,7 +750,7 @@ fn lsp_hover_asset() {
#[test] #[test]
fn lsp_hover_disabled() { fn lsp_hover_disabled() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.set_deno_enable(false); builder.set_deno_enable(false);
@ -779,7 +782,7 @@ fn lsp_hover_disabled() {
#[test] #[test]
fn lsp_inlay_hints() { fn lsp_inlay_hints() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.enable_inlay_hints(); builder.enable_inlay_hints();
@ -882,7 +885,8 @@ fn lsp_inlay_hints() {
#[test] #[test]
fn lsp_inlay_hints_not_enabled() { fn lsp_inlay_hints_not_enabled() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -929,11 +933,11 @@ fn lsp_inlay_hints_not_enabled() {
#[test] #[test]
fn lsp_workspace_enable_paths() { fn lsp_workspace_enable_paths() {
fn run_test(use_trailing_slash: bool) { fn run_test(use_trailing_slash: bool) {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
// we aren't actually writing anything to the tempdir in this test, but we
// just need a legitimate file path on the host system so that logic that
// tries to convert to and from the fs paths works on all env
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.create_dir_all("worker");
temp_dir.write("worker/shared.ts", "export const a = 1");
temp_dir.write("worker/other.ts", "import { a } from './shared.ts';\na;");
let root_specifier = temp_dir.uri(); let root_specifier = temp_dir.uri();
@ -985,7 +989,11 @@ fn lsp_workspace_enable_paths() {
"uri": root_specifier.join("./worker/file.ts").unwrap(), "uri": root_specifier.join("./worker/file.ts").unwrap(),
"languageId": "typescript", "languageId": "typescript",
"version": 1, "version": 1,
"text": "console.log(Date.now());\n" "text": concat!(
"console.log(Date.now());\n",
"import { a } from './shared.ts';\n",
"a;\n",
),
} }
})); }));
@ -1072,6 +1080,56 @@ fn lsp_workspace_enable_paths() {
}) })
); );
// check that the file system documents were auto-discovered
// via the enabled paths
let res = client.write_request(
"textDocument/references",
json!({
"textDocument": {
"uri": root_specifier.join("./worker/file.ts").unwrap(),
},
"position": { "line": 2, "character": 0 },
"context": {
"includeDeclaration": true
}
}),
);
assert_eq!(
res,
json!([{
"uri": root_specifier.join("./worker/file.ts").unwrap(),
"range": {
"start": { "line": 1, "character": 9 },
"end": { "line": 1, "character": 10 }
}
}, {
"uri": root_specifier.join("./worker/file.ts").unwrap(),
"range": {
"start": { "line": 2, "character": 0 },
"end": { "line": 2, "character": 1 }
}
}, {
"uri": root_specifier.join("./worker/shared.ts").unwrap(),
"range": {
"start": { "line": 0, "character": 13 },
"end": { "line": 0, "character": 14 }
}
}, {
"uri": root_specifier.join("./worker/other.ts").unwrap(),
"range": {
"start": { "line": 0, "character": 9 },
"end": { "line": 0, "character": 10 }
}
}, {
"uri": root_specifier.join("./worker/other.ts").unwrap(),
"range": {
"start": { "line": 1, "character": 0 },
"end": { "line": 1, "character": 1 }
}
}])
);
client.shutdown(); client.shutdown();
} }
@ -1081,7 +1139,8 @@ fn lsp_workspace_enable_paths() {
#[test] #[test]
fn lsp_hover_unstable_disabled() { fn lsp_hover_unstable_disabled() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -1120,7 +1179,8 @@ fn lsp_hover_unstable_disabled() {
#[test] #[test]
fn lsp_hover_unstable_enabled() { fn lsp_hover_unstable_enabled() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.set_unstable(true); builder.set_unstable(true);
}); });
@ -1163,7 +1223,8 @@ fn lsp_hover_unstable_enabled() {
#[test] #[test]
fn lsp_hover_change_mbc() { fn lsp_hover_change_mbc() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -1228,7 +1289,7 @@ fn lsp_hover_change_mbc() {
#[test] #[test]
fn lsp_hover_closed_document() { fn lsp_hover_closed_document() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write("a.ts", r#"export const a = "a";"#); temp_dir.write("a.ts", r#"export const a = "a";"#);
temp_dir.write("b.ts", r#"export * from "./a.ts";"#); temp_dir.write("b.ts", r#"export * from "./a.ts";"#);
@ -1320,7 +1381,10 @@ fn lsp_hover_closed_document() {
#[test] #[test]
fn lsp_hover_dependency() { fn lsp_hover_dependency() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
@ -1466,7 +1530,8 @@ fn lsp_hover_dependency() {
// unable to resolve dependencies when there was an invalid syntax in the module // unable to resolve dependencies when there was an invalid syntax in the module
#[test] #[test]
fn lsp_hover_deps_preserved_when_invalid_parse() { fn lsp_hover_deps_preserved_when_invalid_parse() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -1557,7 +1622,10 @@ fn lsp_hover_deps_preserved_when_invalid_parse() {
#[test] #[test]
fn lsp_hover_typescript_types() { fn lsp_hover_typescript_types() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
@ -1610,7 +1678,8 @@ fn lsp_hover_typescript_types() {
#[test] #[test]
fn lsp_hover_jsdoc_symbol_link() { fn lsp_hover_jsdoc_symbol_link() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -1660,7 +1729,8 @@ fn lsp_hover_jsdoc_symbol_link() {
#[test] #[test]
fn lsp_goto_type_definition() { fn lsp_goto_type_definition() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -1702,7 +1772,8 @@ fn lsp_goto_type_definition() {
#[test] #[test]
fn lsp_call_hierarchy() { fn lsp_call_hierarchy() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -1831,7 +1902,8 @@ fn lsp_call_hierarchy() {
#[test] #[test]
fn lsp_large_doc_changes() { fn lsp_large_doc_changes() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let large_file_text = let large_file_text =
fs::read_to_string(testdata_path().join("lsp").join("large_file.txt")) fs::read_to_string(testdata_path().join("lsp").join("large_file.txt"))
@ -1932,7 +2004,8 @@ fn lsp_large_doc_changes() {
#[test] #[test]
fn lsp_document_symbol() { fn lsp_document_symbol() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -2131,7 +2204,8 @@ fn lsp_document_symbol() {
#[test] #[test]
fn lsp_folding_range() { fn lsp_folding_range() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -2177,7 +2251,8 @@ fn lsp_folding_range() {
#[test] #[test]
fn lsp_rename() { fn lsp_rename() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -2229,7 +2304,8 @@ fn lsp_rename() {
#[test] #[test]
fn lsp_selection_range() { fn lsp_selection_range() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -2306,7 +2382,8 @@ fn lsp_selection_range() {
#[test] #[test]
fn lsp_semantic_tokens() { fn lsp_semantic_tokens() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -2365,7 +2442,8 @@ fn lsp_semantic_tokens() {
#[test] #[test]
fn lsp_code_lens() { fn lsp_code_lens() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -2539,7 +2617,8 @@ fn lsp_code_lens() {
#[test] #[test]
fn lsp_code_lens_impl() { fn lsp_code_lens_impl() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -2685,7 +2764,8 @@ fn lsp_code_lens_impl() {
#[test] #[test]
fn lsp_code_lens_test() { fn lsp_code_lens_test() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.disable_testing_api().set_code_lens(None); builder.disable_testing_api().set_code_lens(None);
}); });
@ -2940,7 +3020,8 @@ fn lsp_code_lens_test() {
#[test] #[test]
fn lsp_code_lens_test_disabled() { fn lsp_code_lens_test_disabled() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.disable_testing_api().set_code_lens(Some(json!({ builder.disable_testing_api().set_code_lens(Some(json!({
"implementations": true, "implementations": true,
@ -2980,7 +3061,8 @@ fn lsp_code_lens_test_disabled() {
#[test] #[test]
fn lsp_code_lens_non_doc_nav_tree() { fn lsp_code_lens_non_doc_nav_tree() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -3037,7 +3119,8 @@ fn lsp_code_lens_non_doc_nav_tree() {
#[test] #[test]
fn lsp_nav_tree_updates() { fn lsp_nav_tree_updates() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -3177,7 +3260,8 @@ fn lsp_nav_tree_updates() {
#[test] #[test]
fn lsp_find_references() { fn lsp_find_references() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -3285,7 +3369,8 @@ fn lsp_find_references() {
#[test] #[test]
fn lsp_signature_help() { fn lsp_signature_help() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -3405,7 +3490,8 @@ fn lsp_signature_help() {
#[test] #[test]
fn lsp_code_actions() { fn lsp_code_actions() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -3592,7 +3678,8 @@ fn lsp_code_actions() {
#[test] #[test]
fn lsp_code_actions_deno_cache() { fn lsp_code_actions_deno_cache() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let diagnostics = client.did_open(json!({ let diagnostics = client.did_open(json!({
"textDocument": { "textDocument": {
@ -3681,7 +3768,8 @@ fn lsp_code_actions_deno_cache() {
#[test] #[test]
fn lsp_code_actions_deno_cache_npm() { fn lsp_code_actions_deno_cache_npm() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let diagnostics = client.did_open(json!({ let diagnostics = client.did_open(json!({
"textDocument": { "textDocument": {
@ -3765,7 +3853,8 @@ fn lsp_code_actions_deno_cache_npm() {
#[test] #[test]
fn lsp_code_actions_imports() { fn lsp_code_actions_imports() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -4005,7 +4094,8 @@ export class DuckConfig {
#[test] #[test]
fn lsp_code_actions_refactor() { fn lsp_code_actions_refactor() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -4214,7 +4304,8 @@ fn lsp_code_actions_refactor() {
#[test] #[test]
fn lsp_code_actions_refactor_no_disabled_support() { fn lsp_code_actions_refactor_no_disabled_support() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.with_capabilities(|c| { builder.with_capabilities(|c| {
let doc = c.text_document.as_mut().unwrap(); let doc = c.text_document.as_mut().unwrap();
@ -4283,7 +4374,8 @@ fn lsp_code_actions_refactor_no_disabled_support() {
#[test] #[test]
fn lsp_code_actions_deadlock() { fn lsp_code_actions_deadlock() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let large_file_text = let large_file_text =
fs::read_to_string(testdata_path().join("lsp").join("large_file.txt")) fs::read_to_string(testdata_path().join("lsp").join("large_file.txt"))
@ -4406,7 +4498,8 @@ fn lsp_code_actions_deadlock() {
#[test] #[test]
fn lsp_completions() { fn lsp_completions() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -4464,7 +4557,8 @@ fn lsp_completions() {
#[test] #[test]
fn lsp_completions_private_fields() { fn lsp_completions_private_fields() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -4488,7 +4582,8 @@ fn lsp_completions_private_fields() {
#[test] #[test]
fn lsp_completions_optional() { fn lsp_completions_optional() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -4570,7 +4665,8 @@ fn lsp_completions_optional() {
#[test] #[test]
fn lsp_completions_auto_import() { fn lsp_completions_auto_import() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -4654,7 +4750,8 @@ fn lsp_completions_auto_import() {
#[test] #[test]
fn lsp_completions_snippet() { fn lsp_completions_snippet() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
json!({ json!({
@ -4748,7 +4845,8 @@ fn lsp_completions_snippet() {
#[test] #[test]
fn lsp_completions_no_snippet() { fn lsp_completions_no_snippet() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.with_capabilities(|c| { builder.with_capabilities(|c| {
let doc = c.text_document.as_mut().unwrap(); let doc = c.text_document.as_mut().unwrap();
@ -4802,7 +4900,10 @@ fn lsp_completions_no_snippet() {
#[test] #[test]
fn lsp_completions_npm() { fn lsp_completions_npm() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
@ -4938,7 +5039,10 @@ fn lsp_completions_npm() {
#[test] #[test]
fn lsp_npm_specifier_unopened_file() { fn lsp_npm_specifier_unopened_file() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
@ -5017,7 +5121,10 @@ fn lsp_npm_specifier_unopened_file() {
#[test] #[test]
fn lsp_completions_node_specifier() { fn lsp_completions_node_specifier() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let diagnostics = client.did_open(json!({ let diagnostics = client.did_open(json!({
@ -5238,7 +5345,10 @@ fn lsp_completions_node_specifier() {
#[test] #[test]
fn lsp_completions_registry() { fn lsp_completions_registry() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.add_test_server_suggestions(); builder.add_test_server_suggestions();
@ -5301,7 +5411,10 @@ fn lsp_completions_registry() {
#[test] #[test]
fn lsp_completions_registry_empty() { fn lsp_completions_registry_empty() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder.add_test_server_suggestions(); builder.add_test_server_suggestions();
@ -5361,7 +5474,10 @@ fn lsp_completions_registry_empty() {
#[test] #[test]
fn lsp_auto_discover_registry() { fn lsp_auto_discover_registry() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
@ -5394,7 +5510,10 @@ fn lsp_auto_discover_registry() {
#[test] #[test]
fn lsp_cache_location() { fn lsp_cache_location() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
@ -5483,7 +5602,10 @@ fn lsp_cache_location() {
/// and cache files. /// and cache files.
#[test] #[test]
fn lsp_tls_cert() { fn lsp_tls_cert() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize(|builder| { client.initialize(|builder| {
builder builder
@ -5570,7 +5692,10 @@ fn lsp_tls_cert() {
#[test] #[test]
fn lsp_diagnostics_warn_redirect() { fn lsp_diagnostics_warn_redirect() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
@ -5646,7 +5771,10 @@ fn lsp_diagnostics_warn_redirect() {
#[test] #[test]
fn lsp_redirect_quick_fix() { fn lsp_redirect_quick_fix() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open( client.did_open(
@ -5730,7 +5858,8 @@ fn lsp_redirect_quick_fix() {
#[test] #[test]
fn lsp_diagnostics_deprecated() { fn lsp_diagnostics_deprecated() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let diagnostics = client.did_open(json!({ let diagnostics = client.did_open(json!({
"textDocument": { "textDocument": {
@ -5776,7 +5905,8 @@ fn lsp_diagnostics_deprecated() {
#[test] #[test]
fn lsp_diagnostics_deno_types() { fn lsp_diagnostics_deno_types() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let diagnostics = client let diagnostics = client
.did_open(json!({ .did_open(json!({
@ -5803,7 +5933,8 @@ fn lsp_diagnostics_deno_types() {
#[test] #[test]
fn lsp_diagnostics_refresh_dependents() { fn lsp_diagnostics_refresh_dependents() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -5891,7 +6022,8 @@ struct PerformanceAverages {
#[test] #[test]
fn lsp_performance() { fn lsp_performance() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -5943,7 +6075,8 @@ fn lsp_performance() {
#[test] #[test]
fn lsp_format_no_changes() { fn lsp_format_no_changes() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -5972,7 +6105,8 @@ fn lsp_format_no_changes() {
#[test] #[test]
fn lsp_format_error() { fn lsp_format_error() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -6000,7 +6134,8 @@ fn lsp_format_error() {
#[test] #[test]
fn lsp_format_mbc() { fn lsp_format_mbc() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -6055,7 +6190,7 @@ fn lsp_format_mbc() {
#[test] #[test]
fn lsp_format_exclude_with_config() { fn lsp_format_exclude_with_config() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
@ -6108,7 +6243,7 @@ fn lsp_format_exclude_with_config() {
#[test] #[test]
fn lsp_format_exclude_default_config() { fn lsp_format_exclude_default_config() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
@ -6161,7 +6296,8 @@ fn lsp_format_exclude_default_config() {
#[test] #[test]
fn lsp_format_json() { fn lsp_format_json() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -6216,7 +6352,8 @@ fn lsp_format_json() {
#[test] #[test]
fn lsp_json_no_diagnostics() { fn lsp_json_no_diagnostics() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -6253,7 +6390,8 @@ fn lsp_json_no_diagnostics() {
#[test] #[test]
fn lsp_format_markdown() { fn lsp_format_markdown() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -6300,7 +6438,7 @@ fn lsp_format_markdown() {
#[test] #[test]
fn lsp_format_with_config() { fn lsp_format_with_config() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
"deno.fmt.jsonc", "deno.fmt.jsonc",
@ -6407,7 +6545,8 @@ fn lsp_format_with_config() {
#[test] #[test]
fn lsp_markdown_no_diagnostics() { fn lsp_markdown_no_diagnostics() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -6444,7 +6583,10 @@ fn lsp_markdown_no_diagnostics() {
#[test] #[test]
fn lsp_configuration_did_change() { fn lsp_configuration_did_change() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
@ -6537,7 +6679,8 @@ fn lsp_configuration_did_change() {
#[test] #[test]
fn lsp_workspace_symbol() { fn lsp_workspace_symbol() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -6631,7 +6774,8 @@ fn lsp_workspace_symbol() {
#[test] #[test]
fn lsp_code_actions_ignore_lint() { fn lsp_code_actions_ignore_lint() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -6754,7 +6898,8 @@ fn lsp_code_actions_ignore_lint() {
/// This test exercises updating an existing deno-lint-ignore-file comment. /// This test exercises updating an existing deno-lint-ignore-file comment.
#[test] #[test]
fn lsp_code_actions_update_ignore_lint() { fn lsp_code_actions_update_ignore_lint() {
let mut client = LspClientBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
"textDocument": { "textDocument": {
@ -6879,7 +7024,7 @@ console.log(snake_case);
#[test] #[test]
fn lsp_lint_with_config() { fn lsp_lint_with_config() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
@ -6920,7 +7065,7 @@ fn lsp_lint_with_config() {
#[test] #[test]
fn lsp_lint_exclude_with_config() { fn lsp_lint_exclude_with_config() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
temp_dir.write( temp_dir.write(
@ -6961,7 +7106,10 @@ fn lsp_lint_exclude_with_config() {
#[test] #[test]
fn lsp_jsx_import_source_pragma() { fn lsp_jsx_import_source_pragma() {
let context = TestContextBuilder::new().use_http_server().build(); let context = TestContextBuilder::new()
.use_http_server()
.use_temp_cwd()
.build();
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
client.did_open(json!({ client.did_open(json!({
@ -7058,7 +7206,7 @@ struct TestRunResponseParams {
#[test] #[test]
fn lsp_testing_api() { fn lsp_testing_api() {
let context = TestContextBuilder::new().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
let contents = r#" let contents = r#"
@ -7216,3 +7364,56 @@ Deno.test({
client.shutdown(); client.shutdown();
} }
#[test]
fn lsp_closed_file_find_references() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir();
temp_dir.write("./mod.ts", "export const a = 5;");
temp_dir.write(
"./mod.test.ts",
"import { a } from './mod.ts'; console.log(a);",
);
let temp_dir_url = temp_dir.uri();
let mut client = context.new_lsp_command().build();
client.initialize_default();
client.did_open(json!({
"textDocument": {
"uri": temp_dir_url.join("mod.ts").unwrap(),
"languageId": "typescript",
"version": 1,
"text": r#"export const a = 5;"#
}
}));
let res = client.write_request(
"textDocument/references",
json!({
"textDocument": {
"uri": temp_dir_url.join("mod.ts").unwrap(),
},
"position": { "line": 0, "character": 13 },
"context": {
"includeDeclaration": false
}
}),
);
assert_eq!(
res,
json!([{
"uri": temp_dir_url.join("mod.test.ts").unwrap(),
"range": {
"start": { "line": 0, "character": 9 },
"end": { "line": 0, "character": 10 }
}
}, {
"uri": temp_dir_url.join("mod.test.ts").unwrap(),
"range": {
"start": { "line": 0, "character": 42 },
"end": { "line": 0, "character": 43 }
}
}])
);
client.shutdown();
}

View file

@ -131,24 +131,43 @@ fn pty_complete_expression() {
#[test] #[test]
fn pty_complete_imports() { fn pty_complete_imports() {
util::with_pty(&["repl", "-A"], |mut console| { let context = TestContextBuilder::default().use_temp_cwd().build();
// single quotes let temp_dir = context.temp_dir();
console.write_line_raw("import './run/001_hel\t'"); temp_dir.create_dir_all("subdir");
console.expect("Hello World"); temp_dir.write("./subdir/my_file.ts", "");
// double quotes temp_dir.create_dir_all("run");
console.write_line_raw("import { output } from \"./run/045_out\t\""); temp_dir.write("./run/hello.ts", "console.log('Hello World');");
console.expect("\"./run/045_output.ts\""); temp_dir.write(
console.write_line_raw("output('testing output');"); "./run/output.ts",
console.expect("testing output"); r#"export function output(text: string) {
}); console.log(text);
}
"#,
);
context
.new_command()
.args_vec(["repl", "-A"])
.with_pty(|mut console| {
// single quotes
console.write_line_raw("import './run/hel\t'");
console.expect("Hello World");
// double quotes
console.write_line_raw("import { output } from \"./run/out\t\"");
console.expect("\"./run/output.ts\"");
console.write_line_raw("output('testing output');");
console.expect("testing output");
});
// ensure when the directory changes that the suggestions come from the cwd // ensure when the directory changes that the suggestions come from the cwd
util::with_pty(&["repl", "-A"], |mut console| { context
console.write_line("Deno.chdir('./subdir');"); .new_command()
console.expect("undefined"); .args_vec(["repl", "-A"])
console.write_line_raw("import '../run/001_hel\t'"); .with_pty(|mut console| {
console.expect("Hello World"); console.write_line("Deno.chdir('./subdir');");
}); console.expect("undefined");
console.write_line_raw("import '../run/he\t'");
console.expect("Hello World");
});
} }
#[test] #[test]
@ -283,10 +302,15 @@ fn let_redeclaration() {
#[test] #[test]
fn repl_cwd() { fn repl_cwd() {
util::with_pty(&["repl", "-A"], |mut console| { let context = TestContextBuilder::default().use_temp_cwd().build();
console.write_line("Deno.cwd()"); let temp_dir = context.temp_dir();
console.expect("testdata"); context
}); .new_command()
.args_vec(["repl", "-A"])
.with_pty(|mut console| {
console.write_line("Deno.cwd()");
console.expect(temp_dir.path().file_name().unwrap().to_str().unwrap());
});
} }
#[test] #[test]
@ -384,18 +408,30 @@ fn multiline() {
#[test] #[test]
fn import() { fn import() {
util::with_pty(&["repl", "-A"], |mut console| { let context = TestContextBuilder::default()
console.write_line("import('./subdir/auto_print_hello.ts')"); .use_copy_temp_dir("./subdir")
console.expect("hello!"); .build();
}); context
.new_command()
.args_vec(["repl", "-A"])
.with_pty(|mut console| {
console.write_line("import('./subdir/auto_print_hello.ts')");
console.expect("hello!");
});
} }
#[test] #[test]
fn import_declarations() { fn import_declarations() {
util::with_pty(&["repl", "-A"], |mut console| { let context = TestContextBuilder::default()
console.write_line("import './subdir/auto_print_hello.ts'"); .use_copy_temp_dir("./subdir")
console.expect("hello!"); .build();
}); context
.new_command()
.args_vec(["repl", "-A"])
.with_pty(|mut console| {
console.write_line("import './subdir/auto_print_hello.ts'");
console.expect("hello!");
});
} }
#[test] #[test]

View file

@ -14,6 +14,7 @@ use trust_dns_client::serialize::txt::Lexer;
use trust_dns_client::serialize::txt::Parser; use trust_dns_client::serialize::txt::Parser;
use util::assert_contains; use util::assert_contains;
use util::env_vars_for_npm_tests_no_sync_download; use util::env_vars_for_npm_tests_no_sync_download;
use util::TestContext;
use util::TestContextBuilder; use util::TestContextBuilder;
itest!(stdout_write_all { itest!(stdout_write_all {
@ -571,9 +572,10 @@ itest!(_089_run_allow_list {
#[test] #[test]
fn _090_run_permissions_request() { fn _090_run_permissions_request() {
util::with_pty( TestContext::default()
&["run", "--quiet", "run/090_run_permissions_request.ts"], .new_command()
|mut console| { .args_vec(["run", "--quiet", "run/090_run_permissions_request.ts"])
.with_pty(|mut console| {
console.expect(concat!( console.expect(concat!(
"┌ ⚠️ Deno requests run access to \"ls\".\r\n", "┌ ⚠️ Deno requests run access to \"ls\".\r\n",
"├ Requested by `Deno.permissions.query()` API.\r\n", "├ Requested by `Deno.permissions.query()` API.\r\n",
@ -592,15 +594,15 @@ fn _090_run_permissions_request() {
console.expect("Denied run access to \"cat\"."); console.expect("Denied run access to \"cat\".");
console.expect("granted"); console.expect("granted");
console.expect("denied"); console.expect("denied");
}, });
);
} }
#[test] #[test]
fn _090_run_permissions_request_sync() { fn _090_run_permissions_request_sync() {
util::with_pty( TestContext::default()
&["run", "--quiet", "run/090_run_permissions_request_sync.ts"], .new_command()
|mut console| { .args_vec(["run", "--quiet", "run/090_run_permissions_request_sync.ts"])
.with_pty(|mut console| {
console.expect(concat!( console.expect(concat!(
"┌ ⚠️ Deno requests run access to \"ls\".\r\n", "┌ ⚠️ Deno requests run access to \"ls\".\r\n",
"├ Requested by `Deno.permissions.query()` API.\r\n", "├ Requested by `Deno.permissions.query()` API.\r\n",
@ -619,15 +621,15 @@ fn _090_run_permissions_request_sync() {
console.expect("Denied run access to \"cat\"."); console.expect("Denied run access to \"cat\".");
console.expect("granted"); console.expect("granted");
console.expect("denied"); console.expect("denied");
}, });
);
} }
#[test] #[test]
fn permissions_prompt_allow_all() { fn permissions_prompt_allow_all() {
util::with_pty( TestContext::default()
&["run", "--quiet", "run/permissions_prompt_allow_all.ts"], .new_command()
|mut console| { .args_vec(["run", "--quiet", "run/permissions_prompt_allow_all.ts"])
.with_pty(|mut console| {
// "run" permissions // "run" permissions
console.expect(concat!( console.expect(concat!(
"┌ ⚠️ Deno requests run access to \"FOO\".\r\n", "┌ ⚠️ Deno requests run access to \"FOO\".\r\n",
@ -697,9 +699,10 @@ fn permissions_prompt_allow_all() {
#[test] #[test]
fn permissions_prompt_allow_all_2() { fn permissions_prompt_allow_all_2() {
util::with_pty( TestContext::default()
&["run", "--quiet", "run/permissions_prompt_allow_all_2.ts"], .new_command()
|mut console| { .args_vec(["run", "--quiet", "run/permissions_prompt_allow_all_2.ts"])
.with_pty(|mut console| {
// "env" permissions // "env" permissions
console.expect(concat!( console.expect(concat!(
"┌ ⚠️ Deno requests env access to \"FOO\".\r\n", "┌ ⚠️ Deno requests env access to \"FOO\".\r\n",
@ -728,15 +731,15 @@ fn permissions_prompt_allow_all_2() {
)); ));
console.write_line_raw("A"); console.write_line_raw("A");
console.expect("✅ Granted all read access."); console.expect("✅ Granted all read access.");
}, });
);
} }
#[test] #[test]
fn permissions_prompt_allow_all_lowercase_a() { fn permissions_prompt_allow_all_lowercase_a() {
util::with_pty( TestContext::default()
&["run", "--quiet", "run/permissions_prompt_allow_all.ts"], .new_command()
|mut console| { .args_vec(["run", "--quiet", "run/permissions_prompt_allow_all.ts"])
.with_pty(|mut console| {
// "run" permissions // "run" permissions
console.expect(concat!( console.expect(concat!(
"┌ ⚠️ Deno requests run access to \"FOO\".\r\n", "┌ ⚠️ Deno requests run access to \"FOO\".\r\n",
@ -746,8 +749,7 @@ fn permissions_prompt_allow_all_lowercase_a() {
)); ));
console.write_line_raw("a"); console.write_line_raw("a");
console.expect("Unrecognized option."); console.expect("Unrecognized option.");
}, });
);
} }
itest!(_091_use_define_for_class_fields { itest!(_091_use_define_for_class_fields {
@ -2141,6 +2143,7 @@ fn dont_cache_on_check_fail() {
mod permissions { mod permissions {
use test_util as util; use test_util as util;
use util::TestContext;
// TODO(bartlomieju): remove --unstable once Deno.Command is stabilized // TODO(bartlomieju): remove --unstable once Deno.Command is stabilized
#[test] #[test]
@ -2503,9 +2506,10 @@ mod permissions {
#[test] #[test]
fn _061_permissions_request() { fn _061_permissions_request() {
util::with_pty( TestContext::default()
&["run", "--quiet", "run/061_permissions_request.ts"], .new_command()
|mut console| { .args_vec(["run", "--quiet", "run/061_permissions_request.ts"])
.with_pty(|mut console| {
console.expect(concat!( console.expect(concat!(
"┌ ⚠️ Deno requests read access to \"foo\".\r\n", "┌ ⚠️ Deno requests read access to \"foo\".\r\n",
"├ Requested by `Deno.permissions.query()` API.\r\n", "├ Requested by `Deno.permissions.query()` API.\r\n",
@ -2523,15 +2527,15 @@ mod permissions {
console.expect("granted"); console.expect("granted");
console.expect("prompt"); console.expect("prompt");
console.expect("denied"); console.expect("denied");
}, });
);
} }
#[test] #[test]
fn _061_permissions_request_sync() { fn _061_permissions_request_sync() {
util::with_pty( TestContext::default()
&["run", "--quiet", "run/061_permissions_request_sync.ts"], .new_command()
|mut console| { .args_vec(["run", "--quiet", "run/061_permissions_request_sync.ts"])
.with_pty(|mut console| {
console.expect(concat!( console.expect(concat!(
"┌ ⚠️ Deno requests read access to \"foo\".\r\n", "┌ ⚠️ Deno requests read access to \"foo\".\r\n",
"├ Requested by `Deno.permissions.query()` API.\r\n", "├ Requested by `Deno.permissions.query()` API.\r\n",
@ -2549,15 +2553,15 @@ mod permissions {
console.expect("granted"); console.expect("granted");
console.expect("prompt"); console.expect("prompt");
console.expect("denied"); console.expect("denied");
}, });
);
} }
#[test] #[test]
fn _062_permissions_request_global() { fn _062_permissions_request_global() {
util::with_pty( TestContext::default()
&["run", "--quiet", "run/062_permissions_request_global.ts"], .new_command()
|mut console| { .args_vec(["run", "--quiet", "run/062_permissions_request_global.ts"])
.with_pty(|mut console| {
console.expect(concat!( console.expect(concat!(
"┌ ⚠️ Deno requests read access.\r\n", "┌ ⚠️ Deno requests read access.\r\n",
"├ Requested by `Deno.permissions.query()` API.\r\n", "├ Requested by `Deno.permissions.query()` API.\r\n",
@ -2571,19 +2575,15 @@ mod permissions {
.expect("PermissionStatus { state: \"granted\", onchange: null }"); .expect("PermissionStatus { state: \"granted\", onchange: null }");
console console
.expect("PermissionStatus { state: \"granted\", onchange: null }"); .expect("PermissionStatus { state: \"granted\", onchange: null }");
}, });
);
} }
#[test] #[test]
fn _062_permissions_request_global_sync() { fn _062_permissions_request_global_sync() {
util::with_pty( TestContext::default()
&[ .new_command()
"run", .args_vec(["run", "--quiet", "run/062_permissions_request_global_sync.ts"])
"--quiet", .with_pty(|mut console| {
"run/062_permissions_request_global_sync.ts",
],
|mut console| {
console.expect(concat!( console.expect(concat!(
"┌ ⚠️ Deno requests read access.\r\n", "┌ ⚠️ Deno requests read access.\r\n",
"├ Requested by `Deno.permissions.query()` API.\r\n", "├ Requested by `Deno.permissions.query()` API.\r\n",
@ -2597,8 +2597,7 @@ mod permissions {
.expect("PermissionStatus { state: \"granted\", onchange: null }"); .expect("PermissionStatus { state: \"granted\", onchange: null }");
console console
.expect("PermissionStatus { state: \"granted\", onchange: null }"); .expect("PermissionStatus { state: \"granted\", onchange: null }");
}, });
);
} }
itest!(_063_permissions_revoke { itest!(_063_permissions_revoke {
@ -2623,9 +2622,10 @@ mod permissions {
#[test] #[test]
fn _066_prompt() { fn _066_prompt() {
util::with_pty( TestContext::default()
&["run", "--quiet", "--unstable", "run/066_prompt.ts"], .new_command()
|mut console| { .args_vec(["run", "--quiet", "--unstable", "run/066_prompt.ts"])
.with_pty(|mut console| {
console.expect("What is your name? [Jane Doe] "); console.expect("What is your name? [Jane Doe] ");
console.write_line_raw("John Doe"); console.write_line_raw("John Doe");
console.expect("Your name is John Doe."); console.expect("Your name is John Doe.");
@ -2658,8 +2658,7 @@ mod permissions {
console.expect("What is EOF? "); console.expect("What is EOF? ");
console.write_line(""); console.write_line("");
console.expect("Your answer is null"); console.expect("Your answer is null");
}, });
);
} }
itest!(dynamic_import_permissions_remote_remote { itest!(dynamic_import_permissions_remote_remote {
@ -2715,28 +2714,31 @@ itest!(byte_order_mark {
#[test] #[test]
fn issue9750() { fn issue9750() {
util::with_pty(&["run", "--prompt", "run/issue9750.js"], |mut console| { TestContext::default()
console.expect("Enter 'yy':"); .new_command()
console.write_line_raw("yy"); .args_vec(["run", "--prompt", "run/issue9750.js"])
console.expect(concat!( .with_pty(|mut console| {
"┌ ⚠️ Deno requests env access.\r\n", console.expect("Enter 'yy':");
"├ Requested by `Deno.permissions.query()` API.\r\n", console.write_line_raw("yy");
"├ Run again with --allow-env to bypass this prompt.\r\n", console.expect(concat!(
"└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions)", "┌ ⚠️ Deno requests env access.\r\n",
)); "├ Requested by `Deno.permissions.query()` API.\r\n",
console.write_line_raw("n"); "├ Run again with --allow-env to bypass this prompt.\r\n",
console.expect("Denied env access."); "└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions)",
console.expect(concat!( ));
"┌ ⚠️ Deno requests env access to \"SECRET\".\r\n", console.write_line_raw("n");
"├ Run again with --allow-env to bypass this prompt.\r\n", console.expect("Denied env access.");
"└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions)", console.expect(concat!(
)); "┌ ⚠️ Deno requests env access to \"SECRET\".\r\n",
console.write_line_raw("n"); "├ Run again with --allow-env to bypass this prompt.\r\n",
console.expect_all(&[ "└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions)",
"Denied env access to \"SECRET\".", ));
"PermissionDenied: Requires env access to \"SECRET\", run again with the --allow-env flag", console.write_line_raw("n");
]); console.expect_all(&[
}); "Denied env access to \"SECRET\".",
"PermissionDenied: Requires env access to \"SECRET\", run again with the --allow-env flag",
]);
});
} }
// Regression test for https://github.com/denoland/deno/issues/11451. // Regression test for https://github.com/denoland/deno/issues/11451.

View file

@ -4,6 +4,7 @@
// These tests are intended to only test integration. // These tests are intended to only test integration.
use test_util::env_vars_for_npm_tests; use test_util::env_vars_for_npm_tests;
use test_util::TestContext;
itest!(task_no_args { itest!(task_no_args {
args: "task -q --config task/deno_json/deno.json", args: "task -q --config task/deno_json/deno.json",
@ -53,12 +54,12 @@ itest!(task_non_existent {
#[test] #[test]
fn task_emoji() { fn task_emoji() {
// this bug only appears when using a pty/tty // this bug only appears when using a pty/tty
test_util::with_pty( TestContext::default()
&["task", "--config", "task/deno_json/deno.json", "echo_emoji"], .new_command()
|mut console| { .args_vec(["task", "--config", "task/deno_json/deno.json", "echo_emoji"])
.with_pty(|mut console| {
console.expect("Task echo_emoji echo 🔥\r\n🔥"); console.expect("Task echo_emoji echo 🔥\r\n🔥");
}, });
);
} }
itest!(task_boolean_logic { itest!(task_boolean_logic {

View file

@ -406,10 +406,9 @@ fn file_protocol() {
.unwrap() .unwrap()
.to_string(); .to_string();
let context = TestContext::default(); TestContext::default()
context
.new_command() .new_command()
.args_vec(vec!["test".to_string(), file_url]) .args_vec(["test", file_url.as_str()])
.run() .run()
.assert_matches_file("test/file_protocol.out"); .assert_matches_file("test/file_protocol.out");
} }

View file

@ -228,8 +228,11 @@ impl TestCommandBuilder {
self self
} }
pub fn args_vec(&mut self, args: Vec<String>) -> &mut Self { pub fn args_vec<T: AsRef<str>, I: IntoIterator<Item = T>>(
self.args_vec = args; &mut self,
args: I,
) -> &mut Self {
self.args_vec = args.into_iter().map(|a| a.as_ref().to_string()).collect();
self self
} }

View file

@ -2093,8 +2093,7 @@ impl<'a> CheckOutputIntegrationTest<'a> {
command_builder.args(self.args); command_builder.args(self.args);
} }
if !self.args_vec.is_empty() { if !self.args_vec.is_empty() {
command_builder command_builder.args_vec(self.args_vec.clone());
.args_vec(self.args_vec.iter().map(|a| a.to_string()).collect());
} }
if let Some(input) = &self.input { if let Some(input) = &self.input {
command_builder.stdin(input); command_builder.stdin(input);
@ -2167,11 +2166,8 @@ pub fn pattern_match(pattern: &str, s: &str, wildcard: &str) -> bool {
} }
pub fn with_pty(deno_args: &[&str], action: impl FnMut(Pty)) { pub fn with_pty(deno_args: &[&str], action: impl FnMut(Pty)) {
let context = TestContextBuilder::default().build(); let context = TestContextBuilder::default().use_temp_cwd().build();
context context.new_command().args_vec(deno_args).with_pty(action);
.new_command()
.args_vec(deno_args.iter().map(ToString::to_string).collect())
.with_pty(action);
} }
pub struct WrkOutput { pub struct WrkOutput {