From 3606ee2f3681b08a2c2a30d5db024638de42eca8 Mon Sep 17 00:00:00 2001 From: Martin Fillafer Date: Mon, 20 Mar 2023 15:28:36 +0100 Subject: [PATCH] revert: `getBasePath()` logic to original --- __tests__/utils/path-utils.test.ts | 21 +++++++++++++++++++++ src/utils/path-utils.ts | 22 +++++++++------------- 2 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 __tests__/utils/path-utils.test.ts diff --git a/__tests__/utils/path-utils.test.ts b/__tests__/utils/path-utils.test.ts new file mode 100644 index 0000000..8b6038a --- /dev/null +++ b/__tests__/utils/path-utils.test.ts @@ -0,0 +1,21 @@ +import {getBasePath} from '../../src/utils/path-utils' + +describe('getBasePath', () => { + it('tracked file in path', () => { + const path = 'C:/Users/Michal/Workspace/dorny/test-check/reports/jest/__tests__/main.test.js' + const trackedFiles = ['__tests__/main.test.js', '__tests__/second.test.js', 'lib/main.js'] + + const result = getBasePath(path, trackedFiles) + + expect(result).toBe('C:/Users/Michal/Workspace/dorny/test-check/reports/jest/') + }) + + // it('tracked file contains part of the path', () => { + // const path = 'mnt/extra-addons/product_changes/tests/first_test.py' + // const trackedFiles = ['addons/product_changes/tests/first_test.py'] + // + // const result = getBasePath(path, trackedFiles) + // + // expect(result).toBe('addons/product_changes/tests/first_test.py') + // }) +}) diff --git a/src/utils/path-utils.ts b/src/utils/path-utils.ts index e0dbbc8..91f1784 100644 --- a/src/utils/path-utils.ts +++ b/src/utils/path-utils.ts @@ -23,21 +23,17 @@ export function getBasePath(path: string, trackedFiles: string[]): string | unde return '' } + let max = '' for (const file of trackedFiles) { - const pathParts = path.split('/') - const originalLength = pathParts.length - const fileParts = file.split('/') - - while (pathParts.length && pathParts.slice(-1)[0] === fileParts.slice(-1)[0]) { - pathParts.pop() - fileParts.pop() - } - - // we found some matching path parts - if (pathParts.length !== originalLength) { - return pathParts.join('/') + if (path.endsWith(file) && file.length > max.length) { + max = file } } - return undefined + if (max === '') { + return undefined + } + + const base = path.substr(0, path.length - max.length) + return base }