@@ -13,7 +13,6 @@ import type { DirResult } from "tmp";
13
13
import { dirSync } from "tmp" ;
14
14
import { ensureDirSync , symlinkSync , writeFileSync } from "fs-extra" ;
15
15
import "../../matchers/toEqualPath" ;
16
- import { homedir } from "os" ;
17
16
18
17
describe ( "files" , ( ) => {
19
18
const dataDir = join ( __dirname , "../../data" ) ;
@@ -462,47 +461,39 @@ describe("walkDirectory", () => {
462
461
} ) ;
463
462
464
463
describe ( "findCommonParentDir" , ( ) => {
465
- const dataDir = join ( __dirname , "../../data" ) ;
466
- const rootDir = parse ( homedir ( ) ) . root ;
467
- const sourceDir = join ( dataDir , "../.." ) ;
464
+ const rootDir = parse ( process . cwd ( ) ) . root ;
468
465
469
- it ( "should find the common parent dir for multiple relative paths with common parent" , ( ) => {
466
+ it ( "should fail if not all paths are not absolute" , async ( ) => {
470
467
const paths = [
471
468
join ( "foo" , "bar" , "baz" ) ,
472
- join ( "foo" , "bar" , "qux" ) ,
473
- join ( "foo" , "bar" , "quux" ) ,
469
+ join ( "/ foo" , "bar" , "qux" ) ,
470
+ join ( "/ foo" , "bar" , "quux" ) ,
474
471
] ;
475
472
476
- const commonDir = findCommonParentDir ( ...paths ) ;
477
-
478
- expect ( commonDir ) . toEqualPath ( join ( "foo" , "bar" ) ) ;
473
+ expect ( ( ) => findCommonParentDir ( ...paths ) ) . toThrow (
474
+ "All paths must be absolute" ,
475
+ ) ;
479
476
} ) ;
480
477
481
- it ( "should return empty path if relative paths have no common parent" , ( ) => {
482
- const paths = [
483
- join ( "foo" , "bar" , "baz" ) ,
484
- join ( "qux" , "quux" , "corge" ) ,
485
- join ( "grault" , "garply" ) ,
486
- ] ;
487
-
488
- const commonDir = findCommonParentDir ( ...paths ) ;
489
-
490
- expect ( commonDir ) . toEqualPath ( "" ) ;
478
+ it ( "should fail if no path are provided" , async ( ) => {
479
+ expect ( ( ) => findCommonParentDir ( ) ) . toThrow (
480
+ "At least one path must be provided" ,
481
+ ) ;
491
482
} ) ;
492
483
493
- it ( "should find the common parent dir for multiple absolute paths with common parent" , ( ) => {
484
+ it ( "should find the common parent dir for multiple paths with common parent" , ( ) => {
494
485
const paths = [
495
- join ( dataDir , " foo", "bar" , "baz" ) ,
496
- join ( dataDir , " foo", "bar" , "qux" ) ,
497
- join ( dataDir , " foo", "bar" , "quux" ) ,
486
+ join ( "/ foo", "bar" , "baz" ) ,
487
+ join ( "/ foo", "bar" , "qux" ) ,
488
+ join ( "/ foo", "bar" , "quux" ) ,
498
489
] ;
499
490
500
491
const commonDir = findCommonParentDir ( ...paths ) ;
501
492
502
- expect ( commonDir ) . toEqualPath ( join ( dataDir , " foo", "bar" ) ) ;
493
+ expect ( commonDir ) . toEqualPath ( join ( "/ foo", "bar" ) ) ;
503
494
} ) ;
504
495
505
- it ( "should return the root if absolute paths have no common parent other than root " , ( ) => {
496
+ it ( "should return empty path if paths have no common parent" , ( ) => {
506
497
const paths = [
507
498
join ( "/foo" , "bar" , "baz" ) ,
508
499
join ( "/qux" , "quux" , "corge" ) ,
@@ -514,91 +505,79 @@ describe("findCommonParentDir", () => {
514
505
expect ( commonDir ) . toEqualPath ( rootDir ) ;
515
506
} ) ;
516
507
517
- it ( "should handle a mix of absolute and relative paths" , async ( ) => {
518
- const paths = [
519
- join ( dataDir , "foo" , "bar" , "baz" ) ,
520
- join ( "foo" , "bar" , "qux" ) ,
521
- join ( dataDir , "foo" , "bar" , "quux" ) ,
522
- ] ;
523
-
524
- const commonDir = findCommonParentDir ( ...paths ) ;
525
-
526
- expect ( commonDir ) . toEqualPath ( sourceDir ) ;
527
- } ) ;
528
-
529
508
it ( "should handle a mix of dirs and files" , async ( ) => {
530
509
const paths = [
531
- join ( dataDir , " foo", "bar" , "baz" ) ,
532
- join ( dataDir , " foo", "bar" , "qux.ql" ) ,
533
- join ( dataDir , " foo", "bar" , "quux" ) ,
510
+ join ( "/ foo", "bar" , "baz" ) ,
511
+ join ( "/ foo", "bar" , "qux.ql" ) ,
512
+ join ( "/ foo", "bar" , "quux" ) ,
534
513
] ;
535
514
536
515
const commonDir = findCommonParentDir ( ...paths ) ;
537
516
538
- expect ( commonDir ) . toEqualPath ( join ( dataDir , " foo", "bar" ) ) ;
517
+ expect ( commonDir ) . toEqualPath ( join ( "/ foo", "bar" ) ) ;
539
518
} ) ;
540
519
541
520
it ( "should handle dirs that have the same name" , async ( ) => {
542
521
const paths = [
543
- join ( "foo" , "foo" , "bar" ) ,
544
- join ( "foo" , "foo" , "baz" ) ,
545
- join ( "foo" , "foo" ) ,
522
+ join ( "/ foo" , "foo" , "bar" ) ,
523
+ join ( "/ foo" , "foo" , "baz" ) ,
524
+ join ( "/ foo" , "foo" ) ,
546
525
] ;
547
526
548
527
const commonDir = findCommonParentDir ( ...paths ) ;
549
528
550
- expect ( commonDir ) . toEqualPath ( join ( "foo" , "foo" ) ) ;
529
+ expect ( commonDir ) . toEqualPath ( join ( "/ foo" , "foo" ) ) ;
551
530
} ) ;
552
531
553
532
it ( "should handle dirs that have the same subdir structure but different base path" , async ( ) => {
554
533
const paths = [
555
- join ( "foo" , "bar" ) ,
556
- join ( "bar" , "foo" , "bar" ) ,
557
- join ( "foo" , "foo" , "bar" ) ,
534
+ join ( "/ foo" , "bar" ) ,
535
+ join ( "/ bar" , "foo" , "bar" ) ,
536
+ join ( "/ foo" , "foo" , "bar" ) ,
558
537
] ;
559
538
560
539
const commonDir = findCommonParentDir ( ...paths ) ;
561
540
562
- expect ( commonDir ) . toEqualPath ( "" ) ;
541
+ expect ( commonDir ) . toEqualPath ( rootDir ) ;
563
542
} ) ;
564
543
565
544
it ( "should handle a single path" , async ( ) => {
566
- const paths = [ join ( "foo" , "bar" , "baz" ) ] ;
545
+ const paths = [ join ( "/ foo" , "bar" , "baz" ) ] ;
567
546
568
547
const commonDir = findCommonParentDir ( ...paths ) ;
569
548
570
- expect ( commonDir ) . toEqualPath ( join ( "foo" , "bar" , "baz" ) ) ;
549
+ expect ( commonDir ) . toEqualPath ( join ( "/ foo" , "bar" , "baz" ) ) ;
571
550
} ) ;
572
551
573
552
it ( "should return the same path if all paths are identical" , ( ) => {
574
553
const paths = [
575
- join ( "foo" , "bar" , "baz" ) ,
576
- join ( "foo" , "bar" , "baz" ) ,
577
- join ( "foo" , "bar" , "baz" ) ,
554
+ join ( "/ foo" , "bar" , "baz" ) ,
555
+ join ( "/ foo" , "bar" , "baz" ) ,
556
+ join ( "/ foo" , "bar" , "baz" ) ,
578
557
] ;
579
558
580
559
const commonDir = findCommonParentDir ( ...paths ) ;
581
560
582
- expect ( commonDir ) . toEqualPath ( join ( "foo" , "bar" , "baz" ) ) ;
561
+ expect ( commonDir ) . toEqualPath ( join ( "/ foo" , "bar" , "baz" ) ) ;
583
562
} ) ;
584
563
585
564
it ( "should return the directory path if paths only differ by the file extension" , ( ) => {
586
565
const paths = [
587
- join ( "foo" , "bar" , "baz.txt" ) ,
588
- join ( "foo" , "bar" , "baz.jpg" ) ,
589
- join ( "foo" , "bar" , "baz.pdf" ) ,
566
+ join ( "/ foo" , "bar" , "baz.txt" ) ,
567
+ join ( "/ foo" , "bar" , "baz.jpg" ) ,
568
+ join ( "/ foo" , "bar" , "baz.pdf" ) ,
590
569
] ;
591
570
592
571
const commonDir = findCommonParentDir ( ...paths ) ;
593
572
594
- expect ( commonDir ) . toEqualPath ( join ( "foo" , "bar" ) ) ;
573
+ expect ( commonDir ) . toEqualPath ( join ( "/ foo" , "bar" ) ) ;
595
574
} ) ;
596
575
597
576
it ( "should handle empty paths" , ( ) => {
598
- const paths = [ "" , "" , "" ] ;
577
+ const paths = [ "/ " , "/ " , "/ " ] ;
599
578
600
579
const commonDir = findCommonParentDir ( ...paths ) ;
601
580
602
- expect ( commonDir ) . toEqualPath ( "" ) ;
581
+ expect ( commonDir ) . toEqualPath ( rootDir ) ;
603
582
} ) ;
604
583
} ) ;
0 commit comments