Skip to content

Commit 26eb483

Browse files
committed
refactor(test): hoist getDefaultLogger() calls to reduce overhead
Hoist repeated getDefaultLogger() calls to const logger at the function level in binary test suite. This reduces function call overhead and improves test performance. Changes: - buildBinary(): hoist to const logger - beforeAll(): hoist to const logger
1 parent 8c0b343 commit 26eb483

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

packages/cli/test/integration/binary/binary-test-suite.test.mts

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,23 @@ const BINARIES = {
7373
async function buildBinary(
7474
binaryType: keyof typeof BINARIES,
7575
): Promise<boolean> {
76+
const logger = getDefaultLogger()
7677
const binary = BINARIES[binaryType]
7778

7879
if (!binary.buildCommand) {
7980
return false
8081
}
8182

82-
getDefaultLogger().log(`Building ${binary.name}...`)
83-
getDefaultLogger().log(`Running: ${binary.buildCommand.join(' ')}`)
83+
logger.log(`Building ${binary.name}...`)
84+
logger.log(`Running: ${binary.buildCommand.join(' ')}`)
8485

8586
if (binaryType === 'smol') {
86-
getDefaultLogger().log(
87+
logger.log(
8788
'Note: smol build may take 30-60 minutes on first build',
8889
)
89-
getDefaultLogger().log(' (subsequent builds are faster with caching)')
90+
logger.log(' (subsequent builds are faster with caching)')
9091
}
91-
getDefaultLogger().log('')
92+
logger.log('')
9293

9394
try {
9495
const result = await spawn(
@@ -101,14 +102,14 @@ async function buildBinary(
101102
)
102103

103104
if (result.code !== 0) {
104-
getDefaultLogger().error(`Failed to build ${binary.name}`)
105+
logger.error(`Failed to build ${binary.name}`)
105106
return false
106107
}
107108

108-
getDefaultLogger().log(`Successfully built ${binary.name}`)
109+
logger.log(`Successfully built ${binary.name}`)
109110
return true
110111
} catch (e) {
111-
getDefaultLogger().error(`Error building ${binary.name}:`, e)
112+
logger.error(`Error building ${binary.name}:`, e)
112113
return false
113114
}
114115
}
@@ -128,22 +129,30 @@ function runBinaryTestSuite(binaryType: keyof typeof BINARIES) {
128129
let binaryExists = false
129130

130131
beforeAll(async () => {
132+
const logger = getDefaultLogger()
133+
131134
// Check if binary exists.
132135
binaryExists = existsSync(binary.path)
133136

134137
if (!binaryExists) {
135-
getDefaultLogger().log('')
136-
getDefaultLogger().warn(`Binary not found: ${binary.path}`)
138+
logger.log('')
139+
logger.warn(`Binary not found: ${binary.path}`)
137140

138141
// In CI: Skip building (rely on cache).
139142
if (process.env.CI) {
140-
getDefaultLogger().log(
143+
logger.log(
141144
'Running in CI - skipping build (binary not in cache)',
142145
)
143-
getDefaultLogger().log(
144-
'To prime cache, run: gh workflow run publish-socketbin.yml --field dry-run=true',
145-
)
146-
getDefaultLogger().log('')
146+
if (binaryType === 'sea') {
147+
logger.log(
148+
'To build SEA binaries, run: gh workflow run build-sea.yml',
149+
)
150+
} else if (binaryType === 'smol') {
151+
logger.log(
152+
'To build smol binaries, run: gh workflow run build-smol.yml',
153+
)
154+
}
155+
logger.log('')
147156
return
148157
}
149158

@@ -155,60 +164,60 @@ function runBinaryTestSuite(binaryType: keyof typeof BINARIES) {
155164
})
156165

157166
if (!shouldBuild) {
158-
getDefaultLogger().log('Skipping build. Tests will be skipped.')
159-
getDefaultLogger().log(
167+
logger.log('Skipping build. Tests will be skipped.')
168+
logger.log(
160169
`To build manually, run: ${binary.buildCommand.join(' ')}`,
161170
)
162-
getDefaultLogger().log('')
171+
logger.log('')
163172
return
164173
}
165174

166-
getDefaultLogger().log('Building binary...')
175+
logger.log('Building binary...')
167176
const buildSuccess = await buildBinary(binaryType)
168177

169178
if (buildSuccess) {
170179
binaryExists = existsSync(binary.path)
171180
}
172181

173182
if (!binaryExists) {
174-
getDefaultLogger().log('')
175-
getDefaultLogger().error(
183+
logger.log('')
184+
logger.error(
176185
`Failed to build ${binary.name}. Tests will be skipped.`,
177186
)
178-
getDefaultLogger().log('To build this binary manually, run:')
179-
getDefaultLogger().log(` ${binary.buildCommand.join(' ')}`)
180-
getDefaultLogger().log('')
187+
logger.log('To build this binary manually, run:')
188+
logger.log(` ${binary.buildCommand.join(' ')}`)
189+
logger.log('')
181190
return
182191
}
183192

184-
getDefaultLogger().log(`Binary built successfully: ${binary.path}`)
185-
getDefaultLogger().log('')
193+
logger.log(`Binary built successfully: ${binary.path}`)
194+
logger.log('')
186195
}
187196

188197
// Check authentication.
189198
if (ENV.RUN_INTEGRATION_TESTS) {
190199
const apiToken = await getDefaultApiToken()
191200
hasAuth = !!apiToken
192201
if (!apiToken && !process.env.CI) {
193-
getDefaultLogger().log('')
194-
getDefaultLogger().warn(
202+
logger.log('')
203+
logger.warn(
195204
'Integration tests require Socket authentication.',
196205
)
197-
getDefaultLogger().log('Please run one of the following:')
198-
getDefaultLogger().log(
206+
logger.log('Please run one of the following:')
207+
logger.log(
199208
' 1. socket login (to authenticate with Socket)',
200209
)
201-
getDefaultLogger().log(
210+
logger.log(
202211
' 2. Set SOCKET_SECURITY_API_KEY environment variable',
203212
)
204-
getDefaultLogger().log(
213+
logger.log(
205214
' 3. Skip integration tests by not setting RUN_INTEGRATION_TESTS',
206215
)
207-
getDefaultLogger().log('')
208-
getDefaultLogger().log(
216+
logger.log('')
217+
logger.log(
209218
'Integration tests will be skipped due to missing authentication.',
210219
)
211-
getDefaultLogger().log('')
220+
logger.log('')
212221
}
213222
}
214223
})

0 commit comments

Comments
 (0)