1- import { relativizePath , normalizePath , cwd , sniffForPath } from './path.js'
1+ import { relativizePath , normalizePath , cwd , sniffForPath , commonParentDirectory } from './path.js'
22import { describe , test , expect } from 'vitest'
33
44describe ( 'relativize' , ( ) => {
@@ -25,6 +25,40 @@ describe('cwd', () => {
2525 } )
2626} )
2727
28+ describe ( 'commonParentDirectory' , ( ) => {
29+ // Parity tests with the original 'commondir' npm package (v1.0.1)
30+ test ( 'finds common parent for paths sharing a prefix' , ( ) => {
31+ expect ( commonParentDirectory ( '/foo' , '/foo/bar' ) ) . toBe ( '/foo' )
32+ expect ( commonParentDirectory ( '/foo/bar' , '/foo//bar/baz' ) ) . toBe ( '/foo/bar' )
33+ } )
34+
35+ test ( 'finds deepest common ancestor' , ( ) => {
36+ expect ( commonParentDirectory ( '/a/b/c' , '/a/b' ) ) . toBe ( '/a/b' )
37+ expect ( commonParentDirectory ( '/a/b' , '/a/b/c/d/e' ) ) . toBe ( '/a/b' )
38+ } )
39+
40+ test ( 'returns root when paths diverge at top level' , ( ) => {
41+ expect ( commonParentDirectory ( '/x/y/z/w' , '/xy/z' ) ) . toBe ( '/' )
42+ } )
43+
44+ test ( 'handles Windows-style paths' , ( ) => {
45+ expect ( commonParentDirectory ( 'X:\\foo' , 'X:\\\\foo\\bar' ) ) . toBe ( 'X:/foo' )
46+ expect ( commonParentDirectory ( 'X:\\a\\b\\c' , 'X:\\a\\b' ) ) . toBe ( 'X:/a/b' )
47+ } )
48+
49+ test ( 'returns root for completely divergent Windows paths' , ( ) => {
50+ expect ( commonParentDirectory ( 'X:\\x\\y\\z\\w' , '\\\\xy\\z' ) ) . toBe ( '/' )
51+ } )
52+
53+ test ( 'returns root for single-component paths' , ( ) => {
54+ expect ( commonParentDirectory ( '/' , '/' ) ) . toBe ( '/' )
55+ } )
56+
57+ test ( 'handles identical paths' , ( ) => {
58+ expect ( commonParentDirectory ( '/a/b/c' , '/a/b/c' ) ) . toBe ( '/a/b/c' )
59+ } )
60+ } )
61+
2862describe ( 'sniffForPath' , ( ) => {
2963 test ( 'returns the path if provided' , ( ) => {
3064 // Given
0 commit comments