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