Skip to content

Commit bfe1ecd

Browse files
committed
test(language_server): add tests for WorkspaceWorker::init_watchers (#14516)
Working on the watcher for `.oxfmtrc.json`, so some tests would be not wrong ;)
1 parent ea5838e commit bfe1ecd

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": [
3+
"./lint.json"
4+
]
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

crates/oxc_language_server/src/worker.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,153 @@ mod tests {
430430
);
431431
}
432432
}
433+
434+
#[cfg(test)]
435+
mod test_init_watchers {
436+
use tower_lsp_server::{
437+
UriExt,
438+
lsp_types::{GlobPattern, OneOf, RelativePattern, Uri},
439+
};
440+
441+
use crate::{linter::options::LintOptions, options::Options, worker::WorkspaceWorker};
442+
443+
struct Tester {
444+
pub worker: WorkspaceWorker,
445+
}
446+
447+
impl Tester {
448+
pub fn new(relative_root_dir: &'static str, options: &Options) -> Self {
449+
let absolute_path =
450+
std::env::current_dir().expect("could not get current dir").join(relative_root_dir);
451+
let uri =
452+
Uri::from_file_path(absolute_path).expect("could not convert current dir to uri");
453+
454+
let worker = tokio::runtime::Runtime::new()
455+
.unwrap()
456+
.block_on(async { Self::create_workspace_worker(uri, options).await });
457+
458+
Self { worker }
459+
}
460+
461+
async fn create_workspace_worker(absolute_path: Uri, options: &Options) -> WorkspaceWorker {
462+
let worker = WorkspaceWorker::new(absolute_path);
463+
worker.start_worker(options).await;
464+
465+
worker
466+
}
467+
468+
fn init_watchers(&self) -> Vec<tower_lsp_server::lsp_types::FileSystemWatcher> {
469+
tokio::runtime::Runtime::new()
470+
.unwrap()
471+
.block_on(async { self.worker.init_watchers().await })
472+
}
473+
}
474+
475+
#[test]
476+
fn test_default_options() {
477+
let tester = Tester::new("fixtures/watcher/default", &Options::default());
478+
let watchers = tester.init_watchers();
479+
480+
assert_eq!(watchers.len(), 1);
481+
assert_eq!(
482+
watchers[0].glob_pattern,
483+
GlobPattern::Relative(RelativePattern {
484+
base_uri: OneOf::Right(tester.worker.get_root_uri().clone()),
485+
pattern: "**/.oxlintrc.json".to_string(),
486+
})
487+
);
488+
}
489+
490+
#[test]
491+
fn test_custom_config_path() {
492+
let tester = Tester::new(
493+
"fixtures/watcher/default",
494+
&Options {
495+
lint: LintOptions {
496+
config_path: Some("configs/lint.json".to_string()),
497+
..Default::default()
498+
},
499+
..Default::default()
500+
},
501+
);
502+
let watchers = tester.init_watchers();
503+
504+
assert_eq!(watchers.len(), 1);
505+
assert_eq!(
506+
watchers[0].glob_pattern,
507+
GlobPattern::Relative(RelativePattern {
508+
base_uri: OneOf::Right(tester.worker.get_root_uri().clone()),
509+
pattern: "configs/lint.json".to_string(),
510+
})
511+
);
512+
}
513+
514+
#[test]
515+
fn test_linter_extends_configs() {
516+
let tester = Tester::new("fixtures/watcher/linter_extends", &Options::default());
517+
let watchers = tester.init_watchers();
518+
519+
// The root `.oxlintrc.json` extends `./lint.json -> 2 watchers
520+
// The nested configs are enabled, so it finds `.oxlintrc.json` a second time -> 3 watchers
521+
assert_eq!(watchers.len(), 3);
522+
523+
// nested configs pattern
524+
assert_eq!(
525+
watchers[0].glob_pattern,
526+
GlobPattern::Relative(RelativePattern {
527+
base_uri: OneOf::Right(tester.worker.get_root_uri().clone()),
528+
pattern: "**/.oxlintrc.json".to_string(),
529+
})
530+
);
531+
532+
// nested config extends
533+
assert_eq!(
534+
watchers[1].glob_pattern,
535+
GlobPattern::Relative(RelativePattern {
536+
base_uri: OneOf::Right(tester.worker.get_root_uri().clone()),
537+
pattern: "lint.json".to_string(),
538+
})
539+
);
540+
541+
// base config extends
542+
// TODO: filter duplicates
543+
assert_eq!(
544+
watchers[2].glob_pattern,
545+
GlobPattern::Relative(RelativePattern {
546+
base_uri: OneOf::Right(tester.worker.get_root_uri().clone()),
547+
pattern: "lint.json".to_string(),
548+
})
549+
);
550+
}
551+
552+
#[test]
553+
fn test_linter_extends_custom_config_path() {
554+
let tester = Tester::new(
555+
"fixtures/watcher/linter_extends",
556+
&Options {
557+
lint: LintOptions {
558+
config_path: Some(".oxlintrc.json".to_string()),
559+
..Default::default()
560+
},
561+
..Default::default()
562+
},
563+
);
564+
let watchers = tester.init_watchers();
565+
566+
assert_eq!(watchers.len(), 2);
567+
assert_eq!(
568+
watchers[0].glob_pattern,
569+
GlobPattern::Relative(RelativePattern {
570+
base_uri: OneOf::Right(tester.worker.get_root_uri().clone()),
571+
pattern: ".oxlintrc.json".to_string(),
572+
})
573+
);
574+
assert_eq!(
575+
watchers[1].glob_pattern,
576+
GlobPattern::Relative(RelativePattern {
577+
base_uri: OneOf::Right(tester.worker.get_root_uri().clone()),
578+
pattern: "lint.json".to_string(),
579+
})
580+
);
581+
}
582+
}

0 commit comments

Comments
 (0)