Skip to content

Commit 42b73d0

Browse files
authored
feat: support to choose different atest bin path (#776)
Co-authored-by: rick <[email protected]>
1 parent 22978c4 commit 42b73d0

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

console/atest-desktop/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838
<input name="download-timeout" id="download-timeout" type="text"/>
3939
</td>
4040
</tr>
41+
<tr>
42+
<td>
43+
<label for="bin-location">Bin Location</label>
44+
</td>
45+
<td>
46+
<input name="bin-location" id="bin-location" type="text"/>
47+
</td>
48+
</tr>
4149
<tr>
4250
<td>Log</td>
4351
<td>
@@ -118,10 +126,16 @@
118126
window.electronAPI.setDownloadTimeout(downloadTimeout.value)
119127
});
120128

129+
const binLocation = document.getElementById('bin-location');
130+
binLocation.addEventListener("input", function(e) {
131+
window.electronAPI.setBinLocation(binLocation.value)
132+
});
133+
121134
(async function() {
122135
portInput.value = await window.electronAPI.getPort()
123136
extensionRegistry.value = await window.electronAPI.getExtensionRegistry()
124137
downloadTimeout.value = await window.electronAPI.getDownloadTimeout()
138+
binLocation.value = await window.electronAPI.getBinLocation()
125139

126140
document.getElementById('address').innerText = await window.electronAPI.getHomePage();
127141
})();

console/atest-desktop/main.js

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ let serverProcess;
120120
let serverPort = 7788;
121121
let extensionRegistry = "ghcr.io";
122122
let downloadTimeout = "1m";
123+
let mainProcessLocation = "built-in";
123124

124125
// This method will be called when Electron has finished
125126
// initialization and is ready to create browser windows.
@@ -156,6 +157,12 @@ app.whenReady().then(() => {
156157
})
157158
ipcMain.handle('getHomePage', server.getHomePage)
158159
ipcMain.handle('getHealthzUrl', server.getHealthzUrl)
160+
ipcMain.handle('getMainProcessLocation', () => {
161+
return mainProcessLocation
162+
})
163+
ipcMain.handle('setMainProcessLocation', (e, location) => {
164+
mainProcessLocation = location
165+
})
159166

160167
startServer()
161168
createWindow()
@@ -178,28 +185,21 @@ const startServer = () => {
178185
recursive: true
179186
})
180187

181-
// try to find the atest file first
182-
const serverFile = process.platform === "win32" ? "atest.exe" : "atest"
183-
const atestFromHome = path.join(homeBin, serverFile)
184-
const atestFromPkg = path.join(__dirname, serverFile)
185-
186-
const data = fs.readFileSync(atestFromPkg)
187-
log.info('start to write file with length', data.length)
188-
189-
try {
190-
if (process.platform === "win32") {
191-
const file = fs.openSync(atestFromHome, 'w');
192-
fs.writeSync(file, data, 0, data.length, 0);
193-
fs.closeSync(file);
194-
}else{
195-
fs.writeFileSync(atestFromHome, data);
188+
let atestBinPath
189+
switch (mainProcessLocation) {
190+
case "built-in":
191+
atestBinPath = locateBinPath()
192+
break;
193+
case "system-path":
194+
const which = require('which');
195+
atestBinPath = process.platform === "win32" ? which.sync('atest.exe') : which.sync('atest')
196+
break;
197+
case "home-path":
198+
atestBinPath = locateBinPath(false)
199+
break;
196200
}
197-
} catch (e) {
198-
log.error('Error Code:', e.code);
199-
}
200-
fs.chmodSync(atestFromHome, 0o755);
201201

202-
serverProcess = spawn(atestFromHome, [
202+
serverProcess = spawn(atestBinPath, [
203203
"server",
204204
`--http-port=${serverPort}`,
205205
"--port=0",
@@ -223,6 +223,33 @@ const startServer = () => {
223223
log.info(serverProcess.spawnargs)
224224
}
225225

226+
const locateBinPath = (overwrite = true) => {
227+
// try to find the atest file first
228+
const serverFile = process.platform === "win32" ? "atest.exe" : "atest"
229+
const atestFromHome = path.join(homeBin, serverFile)
230+
if (!overwrite) {
231+
return atestFromHome
232+
}
233+
234+
const atestFromPkg = path.join(__dirname, serverFile)
235+
const data = fs.readFileSync(atestFromPkg)
236+
log.info('start to write file with length', data.length)
237+
238+
try {
239+
if (process.platform === "win32") {
240+
const file = fs.openSync(atestFromHome, 'w');
241+
fs.writeSync(file, data, 0, data.length, 0);
242+
fs.closeSync(file);
243+
}else{
244+
fs.writeFileSync(atestFromHome, data);
245+
}
246+
} catch (e) {
247+
log.error('Error Code:', e.code);
248+
}
249+
fs.chmodSync(atestFromHome, 0o755);
250+
return atestFromHome
251+
}
252+
226253
const stopServer = () => {
227254
if (serverProcess) {
228255
serverProcess.kill()

console/atest-desktop/preload.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2024 API Testing Authors.
2+
Copyright 2024-2025 API Testing Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -41,6 +41,8 @@ contextBridge.exposeInMainWorld('electronAPI', {
4141
setExtensionRegistry: (registry) => ipcRenderer.invoke('setExtensionRegistry', registry),
4242
getExtensionRegistry: () => ipcRenderer.invoke('getExtensionRegistry'),
4343
getDownloadTimeout: () => ipcRenderer.invoke('getDownloadTimeout'),
44-
setDownloadTimeout: (timeout) => ipcRenderer.invoke('setDownloadTimeout', timeout),
44+
setDownloadTimeout: (e) => ipcRenderer.invoke('setDownloadTimeout', e),
45+
getMainProcessLocation: () => ipcRenderer.invoke('getMainProcessLocation'),
46+
setMainProcessLocation: (e) => ipcRenderer.invoke('setMainProcessLocation', e),
4547
getHealthzUrl: () => ipcRenderer.invoke('getHealthzUrl'),
4648
})

0 commit comments

Comments
 (0)