From 7bc81880e1455d08a51c26e306cd78f3da7ab268 Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Sat, 17 Aug 2024 04:22:13 -0700 Subject: [PATCH] Windows: Fix `handleCustomProtocols` test (#10884) --- .../customProtocols/handleCustomProtocols.test.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/app-desktop/utils/customProtocols/handleCustomProtocols.test.ts b/packages/app-desktop/utils/customProtocols/handleCustomProtocols.test.ts index 7c56c2453..48d34ee36 100644 --- a/packages/app-desktop/utils/customProtocols/handleCustomProtocols.test.ts +++ b/packages/app-desktop/utils/customProtocols/handleCustomProtocols.test.ts @@ -42,8 +42,12 @@ const setUpProtocolHandler = () => { return { protocolHandler, onRequestListener }; }; +// Although none of the paths in this test suite point to real files, file paths must be in +// a certain format on Windows to avoid invalid path exceptions. +const toPlatformPath = (path: string) => process.platform === 'win32' ? `C:/${path}` : path; + const expectPathToBeBlocked = async (onRequestListener: ProtocolHandler, filePath: string) => { - const url = `joplin-content://note-viewer/${filePath}`; + const url = `joplin-content://note-viewer/${toPlatformPath(filePath)}`; await expect( async () => await onRequestListener(new Request(url)), @@ -51,7 +55,9 @@ const expectPathToBeBlocked = async (onRequestListener: ProtocolHandler, filePat }; const expectPathToBeUnblocked = async (onRequestListener: ProtocolHandler, filePath: string) => { - const handleRequestResult = await onRequestListener(new Request(`joplin-content://note-viewer/${filePath}`)); + const handleRequestResult = await onRequestListener( + new Request(`joplin-content://note-viewer/${toPlatformPath(filePath)}`), + ); expect(handleRequestResult.body).toBeTruthy(); }; @@ -70,7 +76,7 @@ describe('handleCustomProtocols', () => { await expectPathToBeBlocked(onRequestListener, '/test/path'); await expectPathToBeBlocked(onRequestListener, '/'); - protocolHandler.allowReadAccessToDirectory('/test/path/'); + protocolHandler.allowReadAccessToDirectory(toPlatformPath('/test/path/')); await expectPathToBeUnblocked(onRequestListener, '/test/path'); await expectPathToBeUnblocked(onRequestListener, '/test/path/a.txt'); await expectPathToBeUnblocked(onRequestListener, '/test/path/b.txt'); @@ -79,7 +85,7 @@ describe('handleCustomProtocols', () => { await expectPathToBeBlocked(onRequestListener, '/test/path2'); await expectPathToBeBlocked(onRequestListener, '/test/path/../a.txt'); - protocolHandler.allowReadAccessToDirectory('/another/path/here'); + protocolHandler.allowReadAccessToDirectory(toPlatformPath('/another/path/here')); await expectPathToBeBlocked(onRequestListener, '/another/path/here2'); await expectPathToBeUnblocked(onRequestListener, '/another/path/here');