Skip to content

Commit 80576df

Browse files
🧪 Add dependency injection and tests for MustGetExe (#103)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent fd55f27 commit 80576df

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

.github/workflows/ahk-lint-format-compile.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,22 @@ jobs:
1919
- uses: actions/checkout@v6
2020

2121
- name: Install AutoHotkey v1.1 (Portable)
22-
run: choco install autohotkey.portable --version=1.1.36.01 -y
22+
run: |
23+
for ($i=0; $i -lt 3; $i++) {
24+
choco install autohotkey.portable --version=1.1.36.01 -y
25+
if ($LASTEXITCODE -eq 0) { break }
26+
Start-Sleep -Seconds 10
27+
}
28+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
2329
shell: pwsh
2430
- name: Install AutoHotkey v2
25-
run: choco install autohotkey -y
31+
run: |
32+
for ($i=0; $i -lt 3; $i++) {
33+
choco install autohotkey -y
34+
if ($LASTEXITCODE -eq 0) { break }
35+
Start-Sleep -Seconds 10
36+
}
37+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
2638
shell: pwsh
2739
- name: Verify Installations
2840
run: |

Lib/v2/AHK_Common.ahk

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,20 @@ FindExe(name, fallbacks := []) {
7474
return ""
7575
}
7676

77-
MustGetExe(name, fallbacks := []) {
77+
MustGetExe(name, fallbacks := [], mockMsgBox := "", mockExitApp := "") {
7878
exe := FindExe(name, fallbacks)
7979
if exe = ""
8080
{
81-
MsgBox("Required executable not found: " . name . "`nChecked PATH and fallbacks.")
82-
ExitApp(1)
81+
msg := "Required executable not found: " . name . "`nChecked PATH and fallbacks."
82+
if mockMsgBox !== ""
83+
mockMsgBox(msg)
84+
else
85+
MsgBox(msg)
86+
87+
if mockExitApp !== ""
88+
mockExitApp(1)
89+
else
90+
ExitApp(1)
8391
}
8492
return exe
8593
}

tests/FindExe_Test.ahk

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ try {
5959
EnvSet("PATH", ";;" . mockPath . ";;")
6060
AssertEqual(testBaseDir . "\PathDir2\tool.exe", FindExe("tool.exe"), "Should handle empty entries in PATH")
6161

62+
; MustGetExe Tests
63+
64+
; Test 6: MustGetExe success path
65+
AssertEqual(directPath, MustGetExe(directPath), "MustGetExe should return path if found")
66+
67+
; Test 7: MustGetExe failure path
68+
mockState := Map("msgBoxCalled", false, "exitAppCalled", false, "exitCode", "")
69+
70+
mockMsgBox := (msg) => (mockState["msgBoxCalled"] := true)
71+
mockExitApp := (code) => (mockState["exitAppCalled"] := true, mockState["exitCode"] := code)
72+
73+
; Attempt to find a non-existent file with no fallbacks
74+
try {
75+
MustGetExe("definitely_nonexistent.exe", [], mockMsgBox, mockExitApp)
76+
}
77+
78+
AssertEqual(1, mockState["msgBoxCalled"], "MustGetExe should call MsgBox on failure")
79+
AssertEqual(1, mockState["exitAppCalled"], "MustGetExe should call ExitApp on failure")
80+
AssertEqual(1, mockState["exitCode"], "MustGetExe should exit with code 1 on failure")
81+
6282
; Final Results
6383
stdout.WriteLine("---")
6484
stdout.WriteLine("Tests Passed: " . testsPassed)

0 commit comments

Comments
 (0)