Skip to content

Commit bbcdda7

Browse files
authored
refactor: use path.join from stdlib instead of custom join (#1861)
1 parent d361301 commit bbcdda7

File tree

5 files changed

+36
-95
lines changed

5 files changed

+36
-95
lines changed

__tests__/test-utils-join.js

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const path = require('path').posix || require('path')
44
const { join } = require('isomorphic-git/internal-apis')
55

66
describe('utils/join', () => {
7-
it('should join "good" paths the same as path.join', async () => {
7+
describe('when "internal join" generates paths the same as "path.join"', () => {
8+
// Tests adapted from path-browserify
89
const fixtures = [
910
['/foo/bar', 'baz'],
1011
['foo/bar', 'baz'],
@@ -18,60 +19,39 @@ describe('utils/join', () => {
1819
['/', '.'],
1920
['/', '.git'],
2021
['.', '.git'],
22+
[],
23+
['foo/x', './bar'],
24+
['foo/x/', './bar'],
25+
['foo/x/', '.', 'bar'],
26+
['.', '.', '.'],
27+
['.', './', '.'],
28+
['.', '/./', '.'],
29+
['.', '/////./', '.'],
30+
['.'],
31+
['', '.'],
32+
['foo', '/bar'],
33+
['foo', ''],
34+
['foo', '', '/bar'],
35+
['/'],
36+
['/', '.'],
37+
[''],
38+
['', ''],
39+
['', 'foo'],
40+
['', '', 'foo'],
41+
[' /foo'],
42+
[' ', 'foo'],
43+
[' ', '.'],
44+
[' ', ''],
45+
['/', '/foo'],
46+
['/', '//foo'],
47+
['/', '', '/foo'],
2148
]
22-
for (const fixture of fixtures) {
23-
expect(join(...fixture)).toEqual(path.join(...fixture))
24-
}
25-
})
26-
it('should join degenerate paths the same as path.join in these cases', async () => {
27-
// Tests adapted from path-browserify
28-
const fixtures = [
29-
[[], '.'],
30-
[['foo/x', './bar'], 'foo/x/bar'],
31-
[['foo/x/', './bar'], 'foo/x/bar'],
32-
[['foo/x/', '.', 'bar'], 'foo/x/bar'],
33-
[['.', '.', '.'], '.'],
34-
[['.', './', '.'], '.'],
35-
[['.', '/./', '.'], '.'],
36-
[['.', '/////./', '.'], '.'],
37-
[['.'], '.'],
38-
[['', '.'], '.'],
39-
[['foo', '/bar'], 'foo/bar'],
40-
[['foo', ''], 'foo'],
41-
[['foo', '', '/bar'], 'foo/bar'],
42-
[['/'], '/'],
43-
[['/', '.'], '/'],
44-
[[''], '.'],
45-
[['', ''], '.'],
46-
[['', 'foo'], 'foo'],
47-
[['', '', 'foo'], 'foo'],
48-
[[' /foo'], ' /foo'],
49-
[[' ', 'foo'], ' /foo'],
50-
[[' ', '.'], ' '],
51-
[[' ', ''], ' '],
52-
[['/', '/foo'], '/foo'],
53-
[['/', '//foo'], '/foo'],
54-
[['/', '', '/foo'], '/foo'],
55-
]
56-
for (const [args, result] of fixtures) {
57-
expect(join(...args)).toEqual(result)
58-
expect(join(...args)).toEqual(path.join(...args))
59-
}
60-
})
61-
it('should join degenerate paths differently from path.join in these cases', async () => {
62-
// Tests adapted from path-browserify
63-
const disagreeFixtures = [
64-
[['./'], '.'],
65-
[['.', './'], '.'],
66-
[['', '/foo'], 'foo'],
67-
[['', '', '/foo'], 'foo'],
68-
[['foo/', ''], 'foo'],
69-
[['', '/', 'foo'], 'foo'],
70-
[['', '/', '/foo'], 'foo'],
71-
]
72-
for (const [args, result] of disagreeFixtures) {
73-
expect(join(...args)).toEqual(result)
74-
expect(join(...args)).not.toEqual(path.join(...args))
75-
}
49+
fixtures.forEach(fixture => {
50+
it(`"${JSON.stringify(fixture)}" should join to "${path.join(
51+
...fixture
52+
)}"`, () => {
53+
expect(join(...fixture)).toEqual(path.join(...fixture))
54+
})
55+
})
7656
})
7757
})

src/internal-apis.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export * from './utils/mergeFile'
4141
export * from './utils/mergeTree'
4242
export * from './utils/modified'
4343
export * from './utils/padHex'
44-
export * from './utils/path'
4544
export * from './utils/pkg'
4645
export * from './utils/resolveTree'
4746
export * from './utils/shasum'

src/utils/join.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
// For some reason path.posix.join is undefined in webpack
2-
// Also, this is just much smaller
3-
import { normalizePath } from './normalizePath.js'
4-
5-
export function join(...parts) {
6-
return normalizePath(parts.map(normalizePath).join('/'))
7-
}
1+
export { join } from 'path'

src/utils/normalizePath.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/utils/path.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)