@@ -7,38 +7,77 @@ import * as os from "os";
77import { execSync } from "child_process" ;
88
99suite ( "Extension Integration" , ( ) => {
10+ test ( "Git branch detected in in-workspace file" , async ( ) => {
11+ const expectedBranch = execSync ( "git branch --show-current" , {
12+ cwd : __dirname ,
13+ } )
14+ . toString ( )
15+ . trim ( ) ;
16+
17+ const commitMessage = path . join ( __dirname , "COMMIT_EDITMSG" ) ;
18+ fs . writeFileSync ( commitMessage , "For testing\n\nThese are some words.\n" ) ;
19+
20+ try {
21+ // Open the file in an editor
22+ const doc = await vscode . workspace . openTextDocument (
23+ vscode . Uri . file ( commitMessage ) ,
24+ ) ;
25+ await vscode . window . showTextDocument ( doc ) ;
26+
27+ // Wait for extension activation to complete
28+ const ext = vscode . extensions . getExtension (
29+ "walles.git-commit-message-plus" ,
30+ ) ! ;
31+ await ext . activate ( ) ;
32+
33+ // Wait (up to ~2s) for async branch detection to populate gitBranch
34+ const deadline = Date . now ( ) + 2000 ;
35+ let actualBranch : string | undefined = ext . exports . gitBranch ;
36+ while ( actualBranch != expectedBranch && Date . now ( ) < deadline ) {
37+ await new Promise ( ( r ) => globalThis . setTimeout ( r , 50 ) ) ;
38+ actualBranch = ext . exports . gitBranch ;
39+ }
40+
41+ assert . strictEqual ( actualBranch , expectedBranch ) ;
42+ } finally {
43+ fs . rmSync ( commitMessage ) ;
44+ }
45+ } ) ;
46+
1047 test ( "Git branch detected in not-in-workspace file" , async ( ) => {
1148 const tmpDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "git-commit-test-" ) ) ;
12- const commitMessage = path . join ( tmpDir , "COMMIT_EDITMSG" ) ;
13- fs . writeFileSync ( commitMessage , "Initial commit\n\nDetails here.\n" ) ;
14-
15- // Create a repo and a branch that we can detect
16- execSync ( "git init" , { cwd : tmpDir } ) ;
17- execSync ( "git checkout -b test-branch" , { cwd : tmpDir } ) ;
18-
19- // Open the file in an editor
20- const doc = await vscode . workspace . openTextDocument (
21- vscode . Uri . file ( commitMessage ) ,
22- ) ;
23- await vscode . window . showTextDocument ( doc ) ;
24-
25- // Wait for extension activation to complete
26- const ext = vscode . extensions . getExtension (
27- "walles.git-commit-message-plus" ,
28- ) ! ;
29- await ext . activate ( ) ;
30-
31- // Wait (up to ~2s) for async branch detection to populate gitBranch
32- const deadline = Date . now ( ) + 2000 ;
33- let actualBranch : string | undefined = ext . exports . gitBranch ;
34- while ( ! actualBranch && Date . now ( ) < deadline ) {
35- await new Promise ( ( r ) => globalThis . setTimeout ( r , 50 ) ) ;
36- actualBranch = ext . exports . gitBranch ;
37- }
3849
39- assert . strictEqual ( actualBranch , "test-branch" ) ;
50+ try {
51+ const commitMessage = path . join ( tmpDir , "COMMIT_EDITMSG" ) ;
52+ fs . writeFileSync ( commitMessage , "Initial commit\n\nDetails here.\n" ) ;
53+
54+ // Create a repo and a branch that we can detect
55+ execSync ( "git init" , { cwd : tmpDir } ) ;
56+ execSync ( "git checkout -b test-branch" , { cwd : tmpDir } ) ;
57+
58+ // Open the file in an editor
59+ const doc = await vscode . workspace . openTextDocument (
60+ vscode . Uri . file ( commitMessage ) ,
61+ ) ;
62+ await vscode . window . showTextDocument ( doc ) ;
4063
41- // Clean up
42- fs . rmSync ( tmpDir , { recursive : true , force : true } ) ;
64+ // Wait for extension activation to complete
65+ const ext = vscode . extensions . getExtension (
66+ "walles.git-commit-message-plus" ,
67+ ) ! ;
68+ await ext . activate ( ) ;
69+
70+ // Wait (up to ~2s) for async branch detection to populate gitBranch
71+ const deadline = Date . now ( ) + 2000 ;
72+ let actualBranch : string | undefined = ext . exports . gitBranch ;
73+ while ( actualBranch != "test-branch" && Date . now ( ) < deadline ) {
74+ await new Promise ( ( r ) => globalThis . setTimeout ( r , 50 ) ) ;
75+ actualBranch = ext . exports . gitBranch ;
76+ }
77+
78+ assert . strictEqual ( actualBranch , "test-branch" ) ;
79+ } finally {
80+ fs . rmSync ( tmpDir , { recursive : true , force : true } ) ;
81+ }
4382 } ) ;
4483} ) ;
0 commit comments