Skip to content

[Fix] Infinite while loop when called on root folder, add tests #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
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 || parentDirectoryPath === '.') {
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
38 changes: 32 additions & 6 deletions test/functions/getFirstExistingParentPath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,47 @@ 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 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 dependencies = getDependencies(parentPath)

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

test('unix: get first parent of root is root', async t => {
const parentPath = '/'
const dependencies = getDependencies(parentPath)

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

test('win32: Gets parent to C:\\Alex', async t => {
// note that 'C:/' will fail on UNIX os's as normalize(C:/Alex/..) = C: not C:/
const parentPath = '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('C:/Alex', dependencies), parentPath)
})

test('win32: Returns root folder when called on root folder', async t => {
const parentPath = '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 = 'C:/'
const dependencies = getDependencies(drivePathThatExists)
const drivePathThatDoesNotExist = 'Z:/'
t.is(await getFirstExistingParentPath(drivePathThatDoesNotExist, dependencies), '')
})