Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions packages/cli-kit/src/public/node/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import {relativizePath, normalizePath, cwd, sniffForPath, commonParentDirectory} from './path.js'
import {describe, test, expect} from 'vitest'
import {
relativizePath,
normalizePath,
cwd,
sniffForPath,
commonParentDirectory,
isSubpath,
sniffForJson,
sanitizeRelativePath,
} from './path.js'
import {describe, test, expect, vi} from 'vitest'

describe('relativize', () => {
test('relativizes the path', () => {
Expand Down Expand Up @@ -93,3 +102,59 @@ describe('sniffForPath', () => {
expect(path).toStrictEqual('/path/to/project')
})
})

describe('isSubpath', () => {
test('returns true if the second path is a subpath of the first path', () => {
expect(isSubpath('/foo', '/foo/bar')).toBe(true)
expect(isSubpath('/foo', '/foo/bar/baz')).toBe(true)
})

test('returns false if the second path is not a subpath of the first path', () => {
expect(isSubpath('/foo', '/bar')).toBe(false)
expect(isSubpath('/foo/bar', '/foo')).toBe(false)
})

test('returns true if the paths are identical', () => {
expect(isSubpath('/foo', '/foo')).toBe(true)
})
})

describe('sniffForJson', () => {
test('returns true if --json is present', () => {
expect(sniffForJson(['node', 'script.js', '--json'])).toBe(true)
})

test('returns true if -j is present', () => {
expect(sniffForJson(['node', 'script.js', '-j'])).toBe(true)
})

test('returns false if neither is present', () => {
expect(sniffForJson(['node', 'script.js', '--other-flag'])).toBe(false)
})
})

describe('sanitizeRelativePath', () => {
test('does not modify standard relative paths and does not warn', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('foo/bar', warn)).toBe('foo/bar')
expect(warn).not.toHaveBeenCalled()
})

test('removes double dots and warns', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('foo/../bar', warn)).toBe('bar')
expect(warn).toHaveBeenCalledWith("Warning: path 'foo/../bar' contains '..' traversal — sanitized to 'bar'\n")
})

test('collapses nested double dots to top-level directory and warns', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('foo/../../bar', warn)).toBe('bar')
expect(warn).toHaveBeenCalledWith("Warning: path 'foo/../../bar' contains '..' traversal — sanitized to 'bar'\n")
})

test('handles single dots by collapsing them and does not warn', () => {
const warn = vi.fn()
expect(sanitizeRelativePath('foo/./bar', warn)).toBe('foo/bar')
expect(warn).not.toHaveBeenCalled()
})
})
Loading