From 27df70f351c45d73193eccee2e1cb448977074f3 Mon Sep 17 00:00:00 2001 From: Roger Qiu Date: Thu, 11 Aug 2022 14:48:53 +1000 Subject: [PATCH] fix: default node path should be using `path.join` for cross platform path construction --- src/utils/utils.ts | 10 +++++----- tests/utils.test.ts | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 0a1519d19..46e7a131c 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -12,20 +12,20 @@ function getDefaultNodePath(): string | undefined { const homeDir = os.homedir(); const dataDir = process.env.XDG_DATA_HOME; if (dataDir != null) { - p = `${dataDir}/${prefix}`; + p = path.join(dataDir, prefix); } else { - p = `${homeDir}/.local/share/${prefix}`; + p = path.join(homeDir, '.local', 'share', prefix); } } else if (platform === 'darwin') { const homeDir = os.homedir(); - p = `${homeDir}/Library/Application Support/${prefix}`; + p = path.join(homeDir, 'Library', 'Application Support', prefix); } else if (platform === 'win32') { const homeDir = os.homedir(); const appDataDir = process.env.LOCALAPPDATA; if (appDataDir != null) { - p = `${appDataDir}/${prefix}`; + p = path.join(appDataDir, prefix); } else { - p = `${homeDir}/AppData/Local/${prefix}`; + p = path.join(homeDir, 'AppData', 'Local', prefix); } } else { return; diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 1896fbedc..a4de7648b 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -1,16 +1,32 @@ import os from 'os'; +import path from 'path'; +import process from 'process'; import * as utils from '@/utils'; describe('utils', () => { test('getting default node path', () => { const homeDir = os.homedir(); + const prefix = 'polykey'; const p = utils.getDefaultNodePath(); + expect(p).toBeDefined(); if (process.platform === 'linux') { - expect(p).toBe(`${homeDir}/.local/share/polykey`); + const dataDir = process.env.XDG_DATA_HOME; + if (dataDir != null) { + expect(p).toBe(path.join(dataDir, prefix)); + } else { + expect(p).toBe(path.join(homeDir, '.local', 'share', prefix)); + } } else if (process.platform === 'darwin') { - expect(p).toBe(`${homeDir}/Library/Application Support/polykey`); + expect(p).toBe( + path.join(homeDir, 'Library', 'Application Support', 'polykey'), + ); } else if (process.platform === 'win32') { - expect(p).toBe(`${homeDir}/AppData/Local/polykey`); + const appDataDir = process.env.LOCALAPPDATA; + if (appDataDir != null) { + expect(p).toBe(path.join(appDataDir, prefix)); + } else { + expect(p).toBe(path.join(homeDir, 'AppData', 'Local', prefix)); + } } }); });