-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathutils.test.ts
More file actions
168 lines (143 loc) · 6.79 KB
/
utils.test.ts
File metadata and controls
168 lines (143 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fse from 'fs-extra'
import * as path from 'path'
import {buildBabelExtensibilityArgs} from './babel/utils'
import {ApplicationExtensionEntry, ApplicationExtensionEntryTuple} from '../types'
import {LOCAL_EXTENSIONS_DIR, OVERRIDABLE_FILE_NAME, NODE_MODULES_FOLDER} from './constants'
import {getOverridesFromFile, getForceOverridesFilePaths} from './utils'
import e from 'express'
jest.mock('fs-extra', () => ({
...jest.requireActual('fs-extra'),
realpathSync: jest.fn(),
readFileSync: jest.fn()
}))
const EXTENSIONS: ApplicationExtensionEntryTuple[] = [
['@salesforce/extension-this', {enabled: true}],
['@salesforce/extension-that', {enabled: true}]
]
const CONFIG = {
app: {extensions: EXTENSIONS}
}
const normalizePath = (posixPath: string) => posixPath.split(path.posix.sep).join(path.sep)
describe('buildBabelExtensibilityArgs', () => {
const realpathSyncMock = jest.spyOn(fse, 'realpathSync') as jest.Mock
beforeEach(() => {
realpathSyncMock.mockImplementation((filePath: string) => {
if (filePath.includes('build-remote-server.js')) {
return normalizePath('/absolute/path/to/build-remote-server.js')
}
if (filePath.includes('application-extensions.js')) {
return normalizePath('/absolute/path/to/application-extensions.js')
}
if (filePath.includes(normalizePath('@salesforce/extension-this/src'))) {
return normalizePath('/absolute/path/to/@salesforce/extension-this/src')
}
if (filePath.includes(normalizePath('@salesforce/extension-that/src'))) {
return normalizePath('/absolute/path/to/@salesforce/extension-that/src')
}
throw new Error(`Unexpected path: ${filePath}`)
})
})
afterEach(() => {
jest.clearAllMocks()
})
test('should return the correct Babel arguments string', () => {
const expectedArgs = `--only "app${path.sep}**,${path.sep}absolute${path.sep}path${path.sep}to${path.sep}build-remote-server.js,${path.sep}absolute${path.sep}path${path.sep}to${path.sep}application-extensions.js,${path.sep}absolute${path.sep}path${path.sep}to${path.sep}@salesforce${path.sep}extension-this${path.sep}src${path.sep}**,${path.sep}absolute${path.sep}path${path.sep}to${path.sep}@salesforce${path.sep}extension-that${path.sep}src${path.sep}**"`
const result = buildBabelExtensibilityArgs(CONFIG)
expect(result).toBe(expectedArgs)
})
test('should call realpathSync with the correct paths for each extension', () => {
buildBabelExtensibilityArgs(CONFIG)
// Validate that realpathSync was called with the correct paths
expect(fse.realpathSync).toHaveBeenCalledWith(
path.resolve(
'node_modules/@salesforce/pwa-kit-runtime/ssr/server/build-remote-server.js'
)
)
expect(fse.realpathSync).toHaveBeenCalledWith(
path.resolve(
'node_modules/@salesforce/pwa-kit-extension-sdk/express/placeholders/application-extensions.js'
)
)
expect(fse.realpathSync).toHaveBeenCalledWith(
path.resolve('node_modules/@salesforce/extension-this/src')
)
expect(fse.realpathSync).toHaveBeenCalledWith(
path.resolve('node_modules/@salesforce/extension-that/src')
)
})
test('should handle an empty list of configured extensions', () => {
const expectedArgs = `--only "app${path.sep}**,${path.sep}absolute${path.sep}path${path.sep}to${path.sep}build-remote-server.js,${path.sep}absolute${path.sep}path${path.sep}to${path.sep}application-extensions.js"`
const result = buildBabelExtensibilityArgs({app: {extensions: []}})
expect(result).toBe(expectedArgs)
const result2 = buildBabelExtensibilityArgs({app: {}})
expect(result2).toBe(expectedArgs)
})
})
describe('getOverridesFromFile', () => {
const readFileSyncMock = jest.spyOn(fse, 'readFileSync') as jest.Mock
test('returns parsed override entries, skipping comments and blank lines', () => {
readFileSyncMock.mockReturnValue(`
// This is a comment
override1
override2 // inline comment
// Another comment
override3
`)
const result = getOverridesFromFile('mock/path/.force_overrides')
expect(result).toEqual(['override1', 'override2', 'override3'])
})
test('returns an empty array if file not found (ENOENT)', () => {
const error = new Error('File not found') as any
error.code = 'ENOENT'
readFileSyncMock.mockImplementation(() => {
throw error
})
const result = getOverridesFromFile('nonexistent/path')
expect(result).toEqual([])
})
test('logs warning for other read errors and returns empty array', () => {
const error = new Error('Permission denied') as any
error.code = 'EACCES'
const warnSpy = jest.spyOn(console, 'warn').mockImplementation()
readFileSyncMock.mockImplementation(() => {
throw error
})
const result = getOverridesFromFile('bad/path')
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('Error reading override file at'),
error
)
expect(result).toEqual([])
warnSpy.mockRestore()
})
})
describe('getForceOverridesFilePaths', () => {
const projectDir = '/project'
const extensions: ApplicationExtensionEntry[] = [
['ext-a', {enabled: true}],
['ext-b', {enabled: true}],
['ext-c', {enabled: true}]
]
test('returns all potential override paths', () => {
const result = getForceOverridesFilePaths(projectDir, extensions)
expect(result).toEqual([
path.join(projectDir, OVERRIDABLE_FILE_NAME),
path.join(projectDir, LOCAL_EXTENSIONS_DIR, 'ext-a', OVERRIDABLE_FILE_NAME),
path.join(projectDir, NODE_MODULES_FOLDER, 'ext-a', OVERRIDABLE_FILE_NAME),
path.join(projectDir, LOCAL_EXTENSIONS_DIR, 'ext-b', OVERRIDABLE_FILE_NAME),
path.join(projectDir, NODE_MODULES_FOLDER, 'ext-b', OVERRIDABLE_FILE_NAME),
path.join(projectDir, LOCAL_EXTENSIONS_DIR, 'ext-c', OVERRIDABLE_FILE_NAME),
path.join(projectDir, NODE_MODULES_FOLDER, 'ext-c', OVERRIDABLE_FILE_NAME)
])
})
test('handles an empty extensions array', () => {
const result = getForceOverridesFilePaths(projectDir, [])
expect(result).toEqual([path.join(projectDir, OVERRIDABLE_FILE_NAME)])
})
})