From ad70360fdaaa3aac0c1d8949b44125c853ce9716 Mon Sep 17 00:00:00 2001
From: Michael Kriese <michael.kriese@visualon.de>
Date: Thu, 14 Jul 2022 17:33:28 +0200
Subject: [PATCH] fix(utils/fs): make assertions windows compatible (#16583)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 lib/util/fs/index.spec.ts |  8 ++++----
 lib/util/fs/util.spec.ts  | 21 +++++++++++----------
 lib/util/fs/util.ts       |  8 +++-----
 3 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/lib/util/fs/index.spec.ts b/lib/util/fs/index.spec.ts
index 71aa1257a7..0f221e33e1 100644
--- a/lib/util/fs/index.spec.ts
+++ b/lib/util/fs/index.spec.ts
@@ -1,7 +1,7 @@
 import _findUp from 'find-up';
 import fs from 'fs-extra';
 import tmp, { DirectoryResult } from 'tmp-promise';
-import { join } from 'upath';
+import { join, resolve } from 'upath';
 import { mockedFunction } from '../../../test/util';
 import { GlobalConfig } from '../../config/global';
 import {
@@ -246,7 +246,7 @@ describe('util/fs/index', () => {
       expect(result).toMatchSnapshot();
 
       await writeLocalFile('Cargo.lock', '');
-      await writeLocalFile('/test/subdir/Cargo.lock', '');
+      await writeLocalFile('test/subdir/Cargo.lock', '');
 
       const resultWithAdditionalFiles = await readLocalDirectory('test');
       expect(resultWithAdditionalFiles).not.toBeNull();
@@ -291,13 +291,13 @@ describe('util/fs/index', () => {
     });
 
     it('returns false for directory', async () => {
-      const path = `${localDir}/foobar`;
+      const path = resolve(`${localDir}/foobar`);
       await fs.mkdir(path);
       expect(await localPathIsFile(path)).toBeFalse();
     });
 
     it('returns false for non-existing path', async () => {
-      expect(await localPathIsFile(`${localDir}/foobar`)).toBeFalse();
+      expect(await localPathIsFile(resolve(`${localDir}/foobar`))).toBeFalse();
     });
   });
 
diff --git a/lib/util/fs/util.spec.ts b/lib/util/fs/util.spec.ts
index ae8c7d4c5b..f62fff1625 100644
--- a/lib/util/fs/util.spec.ts
+++ b/lib/util/fs/util.spec.ts
@@ -1,20 +1,20 @@
+import upath from 'upath';
 import { GlobalConfig } from '../../config/global';
 import { FILE_ACCESS_VIOLATION_ERROR } from '../../constants/error-messages';
 import { ensureCachePath, ensureLocalPath } from './util';
 
 describe('util/fs/util', () => {
-  const localDir = '/foo';
-  const cacheDir = '/bar';
+  const localDir = upath.resolve('/foo');
+  const cacheDir = upath.resolve('/bar');
 
   beforeAll(() => {
     GlobalConfig.set({ localDir, cacheDir });
   });
 
   test.each`
-    path      | fullPath
-    ${''}     | ${`${localDir}`}
-    ${'baz'}  | ${`${localDir}/baz`}
-    ${'/baz'} | ${`${localDir}/baz`}
+    path     | fullPath
+    ${''}    | ${`${localDir}`}
+    ${'baz'} | ${`${localDir}/baz`}
   `(`ensureLocalPath('$path', '$fullPath')`, ({ path, fullPath }) => {
     expect(ensureLocalPath(path)).toBe(fullPath);
   });
@@ -25,15 +25,15 @@ describe('util/fs/util', () => {
     ${'../etc/passwd'}
     ${'/foo/../bar'}
     ${'/foo/../../etc/passwd'}
+    ${'/baz'}
   `(`ensureLocalPath('$path', '${localDir}') - throws`, ({ path }) => {
     expect(() => ensureLocalPath(path)).toThrow(FILE_ACCESS_VIOLATION_ERROR);
   });
 
   test.each`
-    path      | fullPath
-    ${''}     | ${`${cacheDir}`}
-    ${'baz'}  | ${`${cacheDir}/baz`}
-    ${'/baz'} | ${`${cacheDir}/baz`}
+    path     | fullPath
+    ${''}    | ${`${cacheDir}`}
+    ${'baz'} | ${`${cacheDir}/baz`}
   `(`ensureCachePath('$path', '$fullPath')`, ({ path, fullPath }) => {
     expect(ensureCachePath(path)).toBe(fullPath);
   });
@@ -44,6 +44,7 @@ describe('util/fs/util', () => {
     ${'../etc/passwd'}
     ${'/bar/../foo'}
     ${'/bar/../../etc/passwd'}
+    ${'/baz'}
   `(`ensureCachePath('$path', '${cacheDir}') - throws`, ({ path }) => {
     expect(() => ensureCachePath(path)).toThrow(FILE_ACCESS_VIOLATION_ERROR);
   });
diff --git a/lib/util/fs/util.ts b/lib/util/fs/util.ts
index f63226bf37..72844a7025 100644
--- a/lib/util/fs/util.ts
+++ b/lib/util/fs/util.ts
@@ -15,11 +15,9 @@ function assertBaseDir(path: string, baseDir: string): void {
 
 function ensurePath(path: string, key: 'localDir' | 'cacheDir'): string {
   const baseDir = upath.resolve(GlobalConfig.get(key)!);
-  let fullPath = path;
-  if (fullPath.startsWith(baseDir)) {
-    fullPath = fullPath.replace(baseDir, '');
-  }
-  fullPath = upath.resolve(upath.join(baseDir, fullPath));
+  const fullPath = upath.resolve(
+    upath.isAbsolute(path) ? path : upath.join(baseDir, path)
+  );
   assertBaseDir(fullPath, baseDir);
   return fullPath;
 }
-- 
GitLab