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