From cf421ee6696ed871339f2fbcb86e5f9464827a42 Mon Sep 17 00:00:00 2001 From: saul Date: Tue, 28 Mar 2023 12:16:42 +1300 Subject: [PATCH 01/15] Add path joining utility. --- src/utils/path-join.js | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/utils/path-join.js diff --git a/src/utils/path-join.js b/src/utils/path-join.js new file mode 100644 index 0000000..52f1b3b --- /dev/null +++ b/src/utils/path-join.js @@ -0,0 +1,4 @@ +const s = process?.platform === 'win32' ? '\\' : '/' +const reg = new RegExp(`((?<=\\${s})\\${s}+)|(^\\.\\${s})|((?<=\\${s})\\.\\${s})`, 'g') + +export default (...paths) => paths.join(s).replace(reg, '') From 7ce1257cf90c3e42d121aee1ea90ae9d57de9886 Mon Sep 17 00:00:00 2001 From: saul Date: Tue, 28 Mar 2023 12:55:11 +1300 Subject: [PATCH 02/15] Add posix and win32 variations to path join. --- src/utils/path-join.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/utils/path-join.js b/src/utils/path-join.js index 52f1b3b..28f2c11 100644 --- a/src/utils/path-join.js +++ b/src/utils/path-join.js @@ -1,4 +1,11 @@ -const s = process?.platform === 'win32' ? '\\' : '/' -const reg = new RegExp(`((?<=\\${s})\\${s}+)|(^\\.\\${s})|((?<=\\${s})\\.\\${s})`, 'g') +const posixReg = /((?<=\/)\/+)|(^\.\/)|((?<=\/)\.\/)/g +const win32Reg = /((?<=\\)\\+)|(^\.\\)|((?<=\\)\.\\)/g -export default (...paths) => paths.join(s).replace(reg, '') +const createJoin = isWin => (...paths) => paths.join(isWin ? '\\' : '/').replace(isWin ? win32Reg : posixReg, '') + +export const join = createJoin(process?.platform === 'win32') + +export const posixJoin = createJoin(false) +export const win32Join = createJoin(true) + +export default join From 06a68bbe1cca7592ec3074ddc5d6baf485b94646 Mon Sep 17 00:00:00 2001 From: saul Date: Tue, 28 Mar 2023 12:55:15 +1300 Subject: [PATCH 03/15] Replace usages of path with path join util. --- src/OrbitDB.js | 4 ++-- src/address.js | 4 ++-- src/database.js | 8 ++++---- src/db/keyvalue-persisted.js | 4 ++-- src/identities/identities.js | 4 ++-- src/manifest.js | 5 ++--- src/sync.js | 4 ++-- 7 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/OrbitDB.js b/src/OrbitDB.js index a41e0e5..77c1c34 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -9,7 +9,7 @@ import OrbitDBAddress, { isValidAddress } from './address.js' import DBManifest from './manifest.js' import { createId, isDefined } from './utils/index.js' // import Logger from 'logplease' -import path from 'path' +import pathJoin from './utils/path-join.js' import * as Block from 'multiformats/block' import * as dagCbor from '@ipld/dag-cbor' import { sha256 } from 'multiformats/hashes/sha2' @@ -46,7 +46,7 @@ const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => { id = id || await createId() const { id: peerId } = await ipfs.id() directory = directory || './orbitdb' - keystore = keystore || await KeyStore({ path: path.join(directory, './keystore') }) + keystore = keystore || await KeyStore({ path: pathJoin(directory, './keystore') }) const identities = await Identities({ ipfs, keystore }) identity = identity || await identities.createIdentity({ id }) diff --git a/src/address.js b/src/address.js index 365e78f..74eb5a7 100644 --- a/src/address.js +++ b/src/address.js @@ -1,6 +1,6 @@ -import * as Path from 'path' import { CID } from 'multiformats/cid' import { base58btc } from 'multiformats/bases/base58' +import { posixJoin } from './utils/path-join.js' const isValidAddress = (address) => { address = address.toString() @@ -45,7 +45,7 @@ const OrbitDBAddress = (address) => { const path = address.replace('/orbitdb/', '').replace('\\orbitdb\\', '') const toString = () => { - return (Path.posix || Path).join('/', protocol, '/', path) + return posixJoin('/', protocol, path) } return { diff --git a/src/database.js b/src/database.js index 5b7af4b..c23fd39 100644 --- a/src/database.js +++ b/src/database.js @@ -1,8 +1,8 @@ import { EventEmitter } from 'events' import PQueue from 'p-queue' -import Path from 'path' import Sync from './sync.js' import { ComposedStorage, LRUStorage, IPFSBlockStorage, LevelStorage } from './storage/index.js' +import pathJoin from './utils/path-join.js' const defaultPointerCount = 0 const defaultCacheSize = 1000 @@ -10,7 +10,7 @@ const defaultCacheSize = 1000 const Database = async ({ OpLog, ipfs, identity, address, name, accessController, directory, meta, headsStorage, entryStorage, indexStorage, pointerCount, syncAutomatically }) => { const { Log, Entry } = OpLog - directory = Path.join(directory || './orbitdb', `./${address}/`) + directory = pathJoin(directory || './orbitdb', `./${address}/`) meta = meta || {} pointerCount = pointerCount || defaultPointerCount @@ -21,12 +21,12 @@ const Database = async ({ OpLog, ipfs, identity, address, name, accessController headsStorage = headsStorage || await ComposedStorage( await LRUStorage({ size: defaultCacheSize }), - await LevelStorage({ path: Path.join(directory, '/log/_heads/') }) + await LevelStorage({ path: pathJoin(directory, '/log/_heads/') }) ) indexStorage = indexStorage || await ComposedStorage( await LRUStorage({ size: defaultCacheSize }), - await LevelStorage({ path: Path.join(directory, '/log/_index/') }) + await LevelStorage({ path: pathJoin(directory, '/log/_index/') }) ) const log = await Log(identity, { logId: address, access: accessController, entryStorage, headsStorage, indexStorage }) diff --git a/src/db/keyvalue-persisted.js b/src/db/keyvalue-persisted.js index 948378c..e487a1d 100644 --- a/src/db/keyvalue-persisted.js +++ b/src/db/keyvalue-persisted.js @@ -1,7 +1,7 @@ import LevelStorage from '../storage/level.js' import { KeyValue } from './index.js' +import pathJoin from '../utils/path-join.js' import PQueue from 'p-queue' -import path from 'path' const valueEncoding = 'json' @@ -11,7 +11,7 @@ const KeyValuePersisted = async ({ OpLog, Database, ipfs, identity, address, nam const queue = new PQueue({ concurrency: 1 }) - directory = path.join(directory || './orbitdb', `./${address}/_index/`) + directory = pathJoin(directory || './orbitdb', `./${address}/_index/`) const index = await LevelStorage({ path: directory, valueEncoding }) let latestOplogHash diff --git a/src/identities/identities.js b/src/identities/identities.js index 5095c72..762f44b 100644 --- a/src/identities/identities.js +++ b/src/identities/identities.js @@ -4,10 +4,10 @@ import OrbitDBIdentityProvider from './providers/orbitdb.js' // import EthIdentityProvider from './identity-providers/ethereum.js' import KeyStore, { signMessage, verifyMessage } from '../key-store.js' import { LRUStorage, IPFSBlockStorage, MemoryStorage, ComposedStorage } from '../storage/index.js' -import Path from 'path' +import pathJoin from '../utils/path-join.js' const DefaultProviderType = 'orbitdb' -const DefaultIdentityKeysPath = Path.join('./orbitdb', 'identities') +const DefaultIdentityKeysPath = pathJoin('./orbitdb', 'identities') const supportedTypes = { orbitdb: OrbitDBIdentityProvider diff --git a/src/manifest.js b/src/manifest.js index c956539..2925ba5 100644 --- a/src/manifest.js +++ b/src/manifest.js @@ -1,5 +1,4 @@ -import path from 'path' - +import pathJoin from './utils/path-join.js' import * as Block from 'multiformats/block' import * as dagCbor from '@ipld/dag-cbor' import { sha256 } from 'multiformats/hashes/sha2' @@ -20,7 +19,7 @@ export default async (storage, name, type, accessControllerAddress, { meta } = { { name, type, - accessController: (path.posix || path).join('/ipfs', accessControllerAddress) + accessController: pathJoin('/ipfs', accessControllerAddress) }, // meta field is only added to manifest if meta parameter is defined meta !== undefined ? { meta } : {} diff --git a/src/sync.js b/src/sync.js index 74f5aa5..6766749 100644 --- a/src/sync.js +++ b/src/sync.js @@ -1,8 +1,8 @@ import { pipe } from 'it-pipe' import PQueue from 'p-queue' -import Path from 'path' import { EventEmitter } from 'events' import { TimeoutController } from 'timeout-abort-controller' +import pathJoin from './utils/path-join.js' const DefaultTimeout = 30000 // 30 seconds @@ -46,7 +46,7 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => { if (!log) throw new Error('An instance of log is required.') const address = log.id - const headsSyncAddress = Path.join('/orbitdb/heads/', address) + const headsSyncAddress = pathJoin('/orbitdb/heads/', address) const queue = new PQueue({ concurrency: 1 }) const peers = new Set() From 3c79588778741057cef1cea64f5a774817678051 Mon Sep 17 00:00:00 2001 From: saul Date: Tue, 28 Mar 2023 13:04:08 +1300 Subject: [PATCH 04/15] Fix process undefined error in browsers. --- src/utils/path-join.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/path-join.js b/src/utils/path-join.js index 28f2c11..651ef0c 100644 --- a/src/utils/path-join.js +++ b/src/utils/path-join.js @@ -3,7 +3,7 @@ const win32Reg = /((?<=\\)\\+)|(^\.\\)|((?<=\\)\.\\)/g const createJoin = isWin => (...paths) => paths.join(isWin ? '\\' : '/').replace(isWin ? win32Reg : posixReg, '') -export const join = createJoin(process?.platform === 'win32') +export const join = createJoin(typeof process !== 'undefined' && process?.platform === 'win32') export const posixJoin = createJoin(false) export const win32Join = createJoin(true) From 1b03b58d148824ffe30b19af1a6394e94e874db3 Mon Sep 17 00:00:00 2001 From: saul Date: Tue, 28 Mar 2023 13:38:20 +1300 Subject: [PATCH 05/15] Remove redundant webpack configs. --- conf/webpack.config.js | 15 +-------------- conf/webpack.debug.config.js | 15 +-------------- conf/webpack.tests.config.js | 5 ----- 3 files changed, 2 insertions(+), 33 deletions(-) diff --git a/conf/webpack.config.js b/conf/webpack.config.js index a360614..9e064ab 100644 --- a/conf/webpack.config.js +++ b/conf/webpack.config.js @@ -27,24 +27,11 @@ export default (env, argv) => { fs: '{ existsSync: () => true }', mkdirp: '{}' }, - plugins: [ - new webpack.ProvidePlugin({ - process: 'process/browser', - Buffer: ['buffer', 'Buffer'] - }) - ], resolve: { modules: [ 'node_modules', path.resolve(__dirname, '../node_modules') - ], - fallback: { - path: require.resolve('path-browserify'), - os: false, - fs: false, - constants: false, - stream: false - } + ] }, resolveLoader: { modules: [ diff --git a/conf/webpack.debug.config.js b/conf/webpack.debug.config.js index fa65139..14f184e 100644 --- a/conf/webpack.debug.config.js +++ b/conf/webpack.debug.config.js @@ -28,24 +28,11 @@ export default (env, argv) => { fs: '{ existsSync: () => true }', mkdirp: '{}' }, - plugins: [ - new webpack.ProvidePlugin({ - process: 'process/browser', - Buffer: ['buffer', 'Buffer'] - }) - ], resolve: { modules: [ 'node_modules', path.resolve(__dirname, '../node_modules') - ], - fallback: { - path: require.resolve('path-browserify'), - os: false, - fs: false, - constants: false, - stream: false - } + ] }, resolveLoader: { modules: [ diff --git a/conf/webpack.tests.config.js b/conf/webpack.tests.config.js index 1591561..683a620 100644 --- a/conf/webpack.tests.config.js +++ b/conf/webpack.tests.config.js @@ -27,7 +27,6 @@ export default (env, argv) => { }, plugins: [ new webpack.ProvidePlugin({ - process: 'process/browser', Buffer: ['buffer', 'Buffer'] }) ], @@ -38,10 +37,6 @@ export default (env, argv) => { ], fallback: { path: require.resolve('path-browserify'), - os: false, - fs: false, - constants: false, - stream: false } }, resolveLoader: { From 34238dd852186283c944fedf0147c6c85ab94ca1 Mon Sep 17 00:00:00 2001 From: saul Date: Tue, 28 Mar 2023 13:44:07 +1300 Subject: [PATCH 06/15] Fix linter errors. --- conf/webpack.config.js | 3 --- conf/webpack.debug.config.js | 3 --- conf/webpack.tests.config.js | 2 +- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/conf/webpack.config.js b/conf/webpack.config.js index 9e064ab..da904fd 100644 --- a/conf/webpack.config.js +++ b/conf/webpack.config.js @@ -1,10 +1,7 @@ import path from 'path' -import webpack from 'webpack' import { fileURLToPath } from 'url' -import { createRequire } from 'module' export default (env, argv) => { - const require = createRequire(import.meta.url) const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) diff --git a/conf/webpack.debug.config.js b/conf/webpack.debug.config.js index 14f184e..8705468 100644 --- a/conf/webpack.debug.config.js +++ b/conf/webpack.debug.config.js @@ -1,10 +1,7 @@ import path from 'path' -import webpack from 'webpack' import { fileURLToPath } from 'url' -import { createRequire } from 'module' export default (env, argv) => { - const require = createRequire(import.meta.url) const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) diff --git a/conf/webpack.tests.config.js b/conf/webpack.tests.config.js index 683a620..2d96510 100644 --- a/conf/webpack.tests.config.js +++ b/conf/webpack.tests.config.js @@ -36,7 +36,7 @@ export default (env, argv) => { path.resolve(__dirname, '../node_modules') ], fallback: { - path: require.resolve('path-browserify'), + path: require.resolve('path-browserify') } }, resolveLoader: { From 07ddc998583a2b02ff2d17a7e53e7d795c1445da Mon Sep 17 00:00:00 2001 From: saul Date: Tue, 28 Mar 2023 13:52:44 +1300 Subject: [PATCH 07/15] Add process back to webpack test config. --- conf/webpack.tests.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/webpack.tests.config.js b/conf/webpack.tests.config.js index 2d96510..427fce4 100644 --- a/conf/webpack.tests.config.js +++ b/conf/webpack.tests.config.js @@ -27,6 +27,7 @@ export default (env, argv) => { }, plugins: [ new webpack.ProvidePlugin({ + process: 'process/browser', Buffer: ['buffer', 'Buffer'] }) ], From 33a53cf765645f74b59388f53c951a4d8e0b197b Mon Sep 17 00:00:00 2001 From: saul Date: Tue, 28 Mar 2023 14:07:43 +1300 Subject: [PATCH 08/15] Fallback webpack test config process var. --- conf/webpack.tests.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/conf/webpack.tests.config.js b/conf/webpack.tests.config.js index 427fce4..9fe2388 100644 --- a/conf/webpack.tests.config.js +++ b/conf/webpack.tests.config.js @@ -37,7 +37,8 @@ export default (env, argv) => { path.resolve(__dirname, '../node_modules') ], fallback: { - path: require.resolve('path-browserify') + path: require.resolve('path-browserify'), + process: false } }, resolveLoader: { From d3162d898f073a6762514fa43e35e428fcbcd978 Mon Sep 17 00:00:00 2001 From: saul Date: Wed, 29 Mar 2023 09:57:48 +1300 Subject: [PATCH 09/15] Add path join tests. --- test/path-join.spec.js | 99 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 test/path-join.spec.js diff --git a/test/path-join.spec.js b/test/path-join.spec.js new file mode 100644 index 0000000..b2c39ad --- /dev/null +++ b/test/path-join.spec.js @@ -0,0 +1,99 @@ +import { equal } from 'assert' +import Path from 'path' +import { join, posixJoin, win32Join } from '../src/utils/path-join.js' + +const platform = process.platform + +const createTestData = s => [ + [s], + [s, 'test'], + ['test'], + ['test', s], + [`test${s}`, s], + [s, `${s}test`], + [`test${s}`, s, `${s}${s}`, `${s}a`], + [`test${s}`, '.', `${s}a`], + [`test${s}.${s}a`], + ['test', s, '.', s] +] + +const posixTestData = createTestData('/') +const win32TestData = createTestData('\\') + +describe('Path posix join', () => { + it('gives the same results as Path using posix join on posix paths', () => { + for (const data of posixTestData) { + equal(Path.posix.join(...data), posixJoin(...data)) + } + }) + + it('gives the same results as Path using posix join on win32 paths', () => { + for (const data of win32TestData) { + equal(Path.posix.join(...data), posixJoin(...data)) + } + }) +}) + +describe('Path win32 join', () => { + it('gives the same results as Path using win32 join on posix paths', () => { + for (const data of posixTestData) { + equal(Path.win32.join(...data), win32Join(...data)) + } + }) + + it('gives the same results as Path using win32 join on win32 paths', () => { + for (const data of win32TestData) { + equal(Path.win32.join(...data), win32Join(...data)) + } + }) +}) + +describe('Path join on win32', () => { + let join; + + before(async () => { + Object.defineProperty(process, 'platform', { value: 'win32', writable: true }) + join = (await import('../src/utils/path-join.js?win32')).join + }); + + after(() => { + Object.defineProperty(process, 'platform', { value: platform, writable: false }) + }); + + it('gives the same results as Path using posix paths', () => { + for (const data of posixTestData) { + equal(Path.win32.join(...data), join(...data)) + } + }) + + it('gives the same results as Path using win32 paths', () => { + for (const data of win32TestData) { + equal(Path.win32.join(...data), join(...data)) + } + }) +}) + +describe('Path join on posix', () => { + let join; + + before(async () => { + Object.defineProperty(process, 'platform', { value: 'linux', writable: true }) + join = (await import('../src/utils/path-join.js?linux')).join + }); + + after(() => { + Object.defineProperty(process, 'platform', { value: platform, writable: false }) + }); + + it('gives the same results as Path using posix paths', () => { + for (const data of posixTestData) { + equal(Path.posix.join(...data), join(...data)) + } + }) + + it('gives the same results as Path using win32 paths', () => { + for (const data of win32TestData) { + equal(Path.posix.join(...data), join(...data)) + } + }) +}) From 194ecafb0ac4a9584bc510758f5470003d284cb7 Mon Sep 17 00:00:00 2001 From: saul Date: Wed, 29 Mar 2023 10:13:19 +1300 Subject: [PATCH 10/15] Fix issue using posix paths with win32 join. --- src/utils/path-join.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/path-join.js b/src/utils/path-join.js index 651ef0c..6652bf0 100644 --- a/src/utils/path-join.js +++ b/src/utils/path-join.js @@ -1,7 +1,9 @@ const posixReg = /((?<=\/)\/+)|(^\.\/)|((?<=\/)\.\/)/g const win32Reg = /((?<=\\)\\+)|(^\.\\)|((?<=\\)\.\\)/g -const createJoin = isWin => (...paths) => paths.join(isWin ? '\\' : '/').replace(isWin ? win32Reg : posixReg, '') +const createJoin = isWin => (...paths) => isWin ? + paths.join('\\').replace(/\//g, '\\').replace(win32Reg, '') : + paths.join('/').replace(posixReg, '') export const join = createJoin(typeof process !== 'undefined' && process?.platform === 'win32') From ed28fb4136dfb1c87e0938ff9cb0cc37ccd9b69b Mon Sep 17 00:00:00 2001 From: saul Date: Wed, 29 Mar 2023 10:37:10 +1300 Subject: [PATCH 11/15] Expand test with harder tests. --- test/path-join.spec.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/test/path-join.spec.js b/test/path-join.spec.js index b2c39ad..40e7a7d 100644 --- a/test/path-join.spec.js +++ b/test/path-join.spec.js @@ -1,11 +1,13 @@ import { equal } from 'assert' import Path from 'path' -import { join, posixJoin, win32Join } from '../src/utils/path-join.js' +import { posixJoin, win32Join } from '../src/utils/path-join.js' const platform = process.platform const createTestData = s => [ - [s], + [], + [''], + [s], [s, 'test'], ['test'], ['test', s], @@ -14,7 +16,11 @@ const createTestData = s => [ [`test${s}`, s, `${s}${s}`, `${s}a`], [`test${s}`, '.', `${s}a`], [`test${s}.${s}a`], - ['test', s, '.', s] + ['test', s, '.', s], + ['/test', '\\', 'mixed'], + ['\\test', '/', 'mixed'], + ['test', '/', 'mixed', '\\', 'data'], + ['test', '\\', 'mixed', '/', 'data'] ] const posixTestData = createTestData('/') @@ -62,13 +68,13 @@ describe('Path join on win32', () => { it('gives the same results as Path using posix paths', () => { for (const data of posixTestData) { - equal(Path.win32.join(...data), join(...data)) + equal(Path.join(...data), join(...data)) } }) it('gives the same results as Path using win32 paths', () => { for (const data of win32TestData) { - equal(Path.win32.join(...data), join(...data)) + equal(Path.join(...data), join(...data)) } }) }) @@ -87,13 +93,13 @@ describe('Path join on posix', () => { it('gives the same results as Path using posix paths', () => { for (const data of posixTestData) { - equal(Path.posix.join(...data), join(...data)) + equal(Path.join(...data), join(...data)) } }) it('gives the same results as Path using win32 paths', () => { for (const data of win32TestData) { - equal(Path.posix.join(...data), join(...data)) + equal(Path.join(...data), join(...data)) } }) }) From 9ca240845327e55e7ba67dc87fcc541eea421dd2 Mon Sep 17 00:00:00 2001 From: saul Date: Wed, 29 Mar 2023 10:39:54 +1300 Subject: [PATCH 12/15] Change default join to posix. --- src/utils/path-join.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/utils/path-join.js b/src/utils/path-join.js index 6652bf0..880cc21 100644 --- a/src/utils/path-join.js +++ b/src/utils/path-join.js @@ -1,13 +1,12 @@ -const posixReg = /((?<=\/)\/+)|(^\.\/)|((?<=\/)\.\/)/g -const win32Reg = /((?<=\\)\\+)|(^\.\\)|((?<=\\)\.\\)/g +export const posixJoin = (...paths) => paths + .join('/') + .replace(/((?<=\/)\/+)|(^\.\/)|((?<=\/)\.\/)/g, '') || '.' -const createJoin = isWin => (...paths) => isWin ? - paths.join('\\').replace(/\//g, '\\').replace(win32Reg, '') : - paths.join('/').replace(posixReg, '') +export const win32Join = (...paths) => paths + .join('\\') + .replace(/\//g, '\\') + .replace(/((?<=\\)\\+)|(^\.\\)|((?<=\\)\.\\)/g, '') || '.' -export const join = createJoin(typeof process !== 'undefined' && process?.platform === 'win32') +export const join = posixJoin -export const posixJoin = createJoin(false) -export const win32Join = createJoin(true) - -export default join +export default posixJoin From 3335c8b69801c07e49ea1b62a348070dc5cab9f1 Mon Sep 17 00:00:00 2001 From: saul Date: Wed, 29 Mar 2023 10:47:38 +1300 Subject: [PATCH 13/15] Cleanup path join tests. --- test/path-join.spec.js | 54 +++++++----------------------------------- 1 file changed, 8 insertions(+), 46 deletions(-) diff --git a/test/path-join.spec.js b/test/path-join.spec.js index 40e7a7d..8029279 100644 --- a/test/path-join.spec.js +++ b/test/path-join.spec.js @@ -1,8 +1,6 @@ import { equal } from 'assert' import Path from 'path' -import { posixJoin, win32Join } from '../src/utils/path-join.js' - -const platform = process.platform +import { join, posixJoin, win32Join } from '../src/utils/path-join.js' const createTestData = s => [ [], @@ -27,13 +25,13 @@ const posixTestData = createTestData('/') const win32TestData = createTestData('\\') describe('Path posix join', () => { - it('gives the same results as Path using posix join on posix paths', () => { + it('gives the same results as \'path\' using posix join on posix paths', () => { for (const data of posixTestData) { equal(Path.posix.join(...data), posixJoin(...data)) } }) - it('gives the same results as Path using posix join on win32 paths', () => { + it('gives the same results as \'path\' using posix join on win32 paths', () => { for (const data of win32TestData) { equal(Path.posix.join(...data), posixJoin(...data)) } @@ -41,63 +39,27 @@ describe('Path posix join', () => { }) describe('Path win32 join', () => { - it('gives the same results as Path using win32 join on posix paths', () => { + it('gives the same results as \'path\' using win32 join on posix paths', () => { for (const data of posixTestData) { equal(Path.win32.join(...data), win32Join(...data)) } }) - it('gives the same results as Path using win32 join on win32 paths', () => { + it('gives the same results as \'path\' using win32 join on win32 paths', () => { for (const data of win32TestData) { equal(Path.win32.join(...data), win32Join(...data)) } }) }) -describe('Path join on win32', () => { - let join; - - before(async () => { - Object.defineProperty(process, 'platform', { value: 'win32', writable: true }) - join = (await import('../src/utils/path-join.js?win32')).join - }); - - after(() => { - Object.defineProperty(process, 'platform', { value: platform, writable: false }) - }); - - it('gives the same results as Path using posix paths', () => { +describe('Path join', () => { + it('gives the same results as \'path\' on posix paths', () => { for (const data of posixTestData) { equal(Path.join(...data), join(...data)) } }) - it('gives the same results as Path using win32 paths', () => { - for (const data of win32TestData) { - equal(Path.join(...data), join(...data)) - } - }) -}) - -describe('Path join on posix', () => { - let join; - - before(async () => { - Object.defineProperty(process, 'platform', { value: 'linux', writable: true }) - join = (await import('../src/utils/path-join.js?linux')).join - }); - - after(() => { - Object.defineProperty(process, 'platform', { value: platform, writable: false }) - }); - - it('gives the same results as Path using posix paths', () => { - for (const data of posixTestData) { - equal(Path.join(...data), join(...data)) - } - }) - - it('gives the same results as Path using win32 paths', () => { + it('gives the same results as \'path\' in win32 paths', () => { for (const data of win32TestData) { equal(Path.join(...data), join(...data)) } From ce5e17781355988d2b55b7ba95fa42d458e880a4 Mon Sep 17 00:00:00 2001 From: saul Date: Wed, 29 Mar 2023 11:10:09 +1300 Subject: [PATCH 14/15] Skip path join tests in environments where it is undefined. --- test/path-join.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/path-join.spec.js b/test/path-join.spec.js index 8029279..b3e50b8 100644 --- a/test/path-join.spec.js +++ b/test/path-join.spec.js @@ -24,7 +24,7 @@ const createTestData = s => [ const posixTestData = createTestData('/') const win32TestData = createTestData('\\') -describe('Path posix join', () => { +;(Path.posix != null ? describe : describe.skip)('Path posix join', () => { it('gives the same results as \'path\' using posix join on posix paths', () => { for (const data of posixTestData) { equal(Path.posix.join(...data), posixJoin(...data)) @@ -38,7 +38,7 @@ describe('Path posix join', () => { }) }) -describe('Path win32 join', () => { +;(Path.win32 != null ? describe : describe.skip)('Path win32 join', () => { it('gives the same results as \'path\' using win32 join on posix paths', () => { for (const data of posixTestData) { equal(Path.win32.join(...data), win32Join(...data)) From 9c1af3deb846e19c2194243455fa68f74dfad91d Mon Sep 17 00:00:00 2001 From: saul Date: Wed, 29 Mar 2023 11:15:44 +1300 Subject: [PATCH 15/15] Fix test descriptions. --- test/path-join.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/path-join.spec.js b/test/path-join.spec.js index b3e50b8..b4fdd30 100644 --- a/test/path-join.spec.js +++ b/test/path-join.spec.js @@ -53,13 +53,13 @@ const win32TestData = createTestData('\\') }) describe('Path join', () => { - it('gives the same results as \'path\' on posix paths', () => { + it('gives the same results as \'path\' using join on posix paths', () => { for (const data of posixTestData) { equal(Path.join(...data), join(...data)) } }) - it('gives the same results as \'path\' in win32 paths', () => { + it('gives the same results as \'path\' using join on win32 paths', () => { for (const data of win32TestData) { equal(Path.join(...data), join(...data)) }