1+ # Requires AutoHotkey v2.0
2+
3+ ; This script is an integration test for the following scenario:
4+ ; A Git hook spawns a background process that outputs some text
5+ ; to the console even after Git has exited.
6+
7+ ; At some point in time, the Cygwin/MSYS2 runtime left the console
8+ ; in a state where it was not possible to navigate the history via
9+ ; CursorUp/Down, as reported in https://github.com/microsoft/git/issues/730.
10+ ; This was fixed in the Cygwin/MSYS2 runtime, but then regressed again.
11+ ; This test is meant to verify that the issue is fixed and remains so.
12+
13+ ; First, set the worktree path; This path will be reused
14+ ; for the `.log` file).
15+ if A_Args.Length > 0
16+ workTree := A_Args[1 ]
17+ else
18+ {
19+ ; Create a unique worktree path in the TEMP directory.
20+ workTree := EnvGet ('TEMP') . '\git- test- background - hook'
21+ if FileExist (workTree)
22+ {
23+ counter := 0
24+ while FileExist (workTree '- ' counter)
25+ counter++
26+ workTree := workTree '- ' counter
27+ }
28+ }
29+
30+ Info (text ) {
31+ FileAppend text '`n', workTree '.log'
32+ }
33+
34+ closeWindow := false
35+ childPid := 0
36+ ExitWithError (error ) {
37+ Info 'Error : ' error
38+ if closeWindow
39+ WinClose " A"
40+ else if childPid ! = 0
41+ ProcessClose childPid
42+ ExitApp 1
43+ }
44+
45+ RunWaitOne (command ) {
46+ shell := ComObject(" WScript.Shell" )
47+ ; Execute a single command via cmd.exe
48+ exec := shell.Exec(A_ComSpec " /C " command)
49+ ; Read and return the command's output
50+ return exec.StdOut.ReadAll()
51+ }
52+
53+ SetWorkingDir (EnvGet ('TEMP'))
54+ Info 'uname: ' RunWaitOne('uname - a')
55+ Info RunWaitOne('git version -- build- options')
56+
57+ RunWait ('git init " ' workTree '" ', '', 'Hide ')
58+ if A_LastError
59+ ExitWithError 'Could not initialize Git worktree at: ' workTree
60+
61+ SetWorkingDir (workTree)
62+ if A_LastError
63+ ExitWithError 'Could not set working directory to: ' workTree
64+
65+ if not FileExist ('.git/ hooks') and not DirCreate('.git/ hooks')
66+ ExitWithError 'Could not create hooks directory: ' workTree
67+
68+ FileAppend ("#!/bin/sh`npowershell -command 'for ($i = 0; $i -lt 50; $i++ ) { echo $i; sleep -milliseconds 10 }' &`n", '.git/hooks/pre-commit')
69+ if A_LastError
70+ ExitWithError 'Could not create pre- commit hook: ' A_LastError
71+
72+ Run 'wt.exe - d . ' A_ComSpec ' / d', , , & childPid
73+ if A_LastError
74+ ExitWithError 'Error launching CMD: ' A_LastError
75+ Info 'Launched CMD: ' childPid
76+ if not WinWait (A_ComSpec, , 9 )
77+ ExitWithError 'CMD window did not appear'
78+ Info 'Got window'
79+ WinActivate
80+ CloseWindow := true
81+ WinMove 0 , 0
82+ Info 'Moved window to top left (so that the bottom is not cut off )'
83+
84+ CaptureText () {
85+ ControlGetPos & cx, & cy, & cw, & ch, 'Windows.UI.Composition.DesktopWindowContentBridge1', " A"
86+ titleBarHeight := 54
87+ scrollBarWidth := 28
88+ pad := 8
89+
90+ SavedClipboard := ClipboardAll
91+ A_Clipboard := ''
92+ SendMode ('Event')
93+ MouseMove cx + pad, cy + titleBarHeight + pad
94+ MouseClickDrag 'Left ', , , cx + cw - scrollBarWidth, cy + ch - pad, , ''
95+ MouseClick 'Right '
96+ ClipWait ()
97+ Result := A_Clipboard
98+ Clipboard := SavedClipboard
99+ return Result
100+ }
101+
102+ Info('Setting committer identity')
103+ Send ('git config user.
name Test{
Enter }git config user.email
[email protected] {
Enter }')
104+
105+ Info('Committing')
106+ Send ('git commit -- allow- empty - m zOMG{Enter }')
107+ ; Wait for the hook to finish printing
108+ While not RegExMatch (CaptureText(), '`n49$')
109+ {
110+ Sleep 100
111+ if A_Index > 1000
112+ ExitWithError 'Timed out waiting for commit to finish'
113+ MouseClick 'WheelDown ', , , 20
114+ }
115+ Info('Hook finished')
116+
117+ ; Verify that CursorUp shows the previous command
118+ Send ('{Up }')
119+ Sleep 150
120+ Text := CaptureText()
121+ if not RegExMatch (Text , 'git commit -- allow- empty - m zOMG * $')
122+ ExitWithError 'Cursor Up did not work: ' Text
123+ Info('Match! ')
124+
125+ Send ('^ C')
126+ Send ('exit{Enter }')
127+ Sleep 50
128+ SetWorkingDir (EnvGet ('TEMP'))
129+ DirDelete(workTree, true )
0 commit comments