Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"lint:fix": "eslint --fix .",
"typecheck": "tsc --noEmit",
"test": "npm-run-all test:coverage lint typecheck",
"test:unit": "NODE_ENV=test TS_NODE_PROJECT='tsconfig.test.json' ava",
"test:unit": "cross-env NODE_ENV=test TS_NODE_PROJECT='tsconfig.test.json' ava",
"test:coverage": "nyc --reporter=lcov --reporter=text npm run test:unit --silent"
},
"ava": {
Expand Down Expand Up @@ -50,6 +50,7 @@
"@typescript-eslint/eslint-plugin": "5.59.6",
"@typescript-eslint/parser": "5.59.6",
"ava": "5.2.0",
"cross-env": "^7.0.3",
"eslint": "8.41.0",
"eslint-plugin-ava": "14.0.0",
"eslint-plugin-import": "2.27.5",
Expand Down
24 changes: 20 additions & 4 deletions src/functions/getFirstExistingParentPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,31 @@ import Dependencies from '@/src/types/dependencies'
* @param dependencies - Dependencies container
*/
async function getFirstExistingParentPath(directoryPath: string, dependencies: Dependencies): Promise<string> {
let parentDirectoryPath = directoryPath
let parentDirectoryPath = dependencies.pathNormalize(directoryPath)
let parentDirectoryFound = await isDirectoryExisting(parentDirectoryPath, dependencies)

while (!parentDirectoryFound) {
parentDirectoryPath = dependencies.pathNormalize(parentDirectoryPath + '/..')
const FAILED_TO_FIND_EXISTING_DIRECTORY_VALUE = ''

/**
* Linux max file path length is 4096 characters.
* With / separators and 1 letter folder names, this gives us a max of ~2048 folders to traverse.
* This is much less error prone than a while loop.
*/
Comment on lines +16 to +20
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This limit is not valid on every file system
https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits

So... I do not know if I want to add that to this loop.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a number we can change it to that would accomplish this goal? Or another way to avoid a while loop/provide a failsafe if we miss another edge case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately this is of course your call and I'm happy to revert to the while loop if you would like

const maxNumberOfFolders = 2048
for (let i = 0; i < maxNumberOfFolders && !parentDirectoryFound; ++i) {
const newParentDirectoryPath = dependencies.pathNormalize(parentDirectoryPath + '/..')
if (parentDirectoryPath === newParentDirectoryPath) {
return FAILED_TO_FIND_EXISTING_DIRECTORY_VALUE
}
parentDirectoryPath = newParentDirectoryPath
parentDirectoryFound = await isDirectoryExisting(parentDirectoryPath, dependencies)
}

return parentDirectoryPath
if (parentDirectoryPath !== '.') {
return parentDirectoryPath
}

return FAILED_TO_FIND_EXISTING_DIRECTORY_VALUE
}

export default getFirstExistingParentPath
4 changes: 2 additions & 2 deletions test/__helpers__/mockDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalize } from 'node:path'
import { normalize, sep } from 'node:path'
import { promisify } from 'node:util'

import Dependencies from '@/src/types/dependencies'
Expand All @@ -12,7 +12,7 @@ function mockDependencies(overrides?: Partial<Dependencies>, options?: {
release: '11.5.0',
fsAccess: () => Promise.resolve(),
pathNormalize: normalize,
pathSep: '/',
pathSep: sep,
cpExecFile: async () => {
await promisify(process.nextTick)

Expand Down
36 changes: 28 additions & 8 deletions test/functions/getFirstExistingParentPath.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import { normalize } from 'node:path'

import test from 'ava'
import { PathLike } from 'fs'

import getFirstExistingParentPath from '@/src/functions/getFirstExistingParentPath'
import mockDependencies from '@/test/__helpers__/mockDependencies'

const getDependencies = (parentPath: string) => mockDependencies({
fsAccess: async (directoryPath: PathLike) => directoryPath === parentPath ? Promise.resolve() : Promise.reject(new Error('File does not exists')),
})

test('unix: get first existing parent path', async t => {
const parentPath = '/home/Alex'
const dependencies = mockDependencies({
fsAccess: async (directoryPath: PathLike) => directoryPath === parentPath ? Promise.resolve() : Promise.reject(new Error('File does not exists')),
})
const parentPath = normalize('/home/Alex')
const dependencies = getDependencies(parentPath)

t.is(await getFirstExistingParentPath('/home/Alex/games/Some/Game', dependencies), parentPath)
})

test('unix: get first parent can be the path itself', async t => {
const parentPath = '/home/Alex'
const dependencies = mockDependencies({
fsAccess: async (directoryPath: PathLike) => directoryPath === parentPath ? Promise.resolve() : Promise.reject(new Error('File does not exists')),
})
const parentPath = normalize('/home/Alex')
const dependencies = getDependencies(parentPath)

t.is(await getFirstExistingParentPath(parentPath, dependencies), parentPath)
})

test('win32: Gets parent to C:\\HyperPlay', async t => {
const parentPath = normalize('C:\\')
const dependencies = getDependencies(parentPath)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to define the pathSep somewhere here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not following. Is this still relevant after the changes I committed?

t.is(await getFirstExistingParentPath(normalize('C:\\HyperPlay'), dependencies), parentPath)
})

test('win32: Returns root folder when called on root folder', async t => {
const parentPath = normalize('C:\\')
const dependencies = getDependencies(parentPath)
t.is(await getFirstExistingParentPath(parentPath, dependencies), parentPath)
})

test('win32: returns empty string when drive does not exist', async t => {
const drivePathThatExists = normalize('C:\\')
const dependencies = getDependencies(drivePathThatExists)
const drivePathThatDoesNotExist = normalize('Z:\\')
t.is(await getFirstExistingParentPath(drivePathThatDoesNotExist, dependencies), '')
})