Skip to content

Commit 1f4ea6d

Browse files
committed
Test in-workspace git branch detection
1 parent dd2d2d8 commit 1f4ea6d

File tree

2 files changed

+85
-37
lines changed

2 files changed

+85
-37
lines changed

src/getgitbranch.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ const execFile = nodeUtil.promisify(child_process.execFile);
99
export default async function getCurrentGitBranch(
1010
docUri: vscode.Uri,
1111
): Promise<string | undefined> {
12-
return (
12+
const branch =
1313
getCurrentGitBranchFromVscode(docUri) ||
14-
(await getCurrentGitBranchFromGit(docUri))
15-
);
14+
(await getCurrentGitBranchFromGit(docUri));
15+
16+
if (docUri.scheme == "file" && !branch) {
17+
console.warn("No Git branch found for document", docUri);
18+
}
19+
20+
return branch;
1621
}
1722

1823
function getCurrentGitBranchFromVscode(docUri: vscode.Uri): string | undefined {
@@ -34,19 +39,19 @@ function getCurrentGitBranchFromVscode(docUri: vscode.Uri): string | undefined {
3439
const git = extension.exports.getAPI(1);
3540
const repository = git.getRepository(docUri);
3641
if (!repository) {
37-
console.warn("No Git repository for current document", docUri);
42+
console.debug("No vscode Git repository for current document", docUri);
3843
return undefined;
3944
}
4045

4146
const currentBranch = repository.state.HEAD;
4247
if (!currentBranch) {
43-
console.warn("No HEAD branch for current document", docUri);
48+
console.debug("No vscode HEAD branch for current document", docUri);
4449
return undefined;
4550
}
4651

4752
const branchName = currentBranch.name;
4853
if (!branchName) {
49-
console.warn("Current branch has no name", docUri, currentBranch);
54+
console.warn("Current vscode branch has no name", docUri, currentBranch);
5055
return undefined;
5156
}
5257

@@ -74,14 +79,18 @@ async function getCurrentGitBranchFromGit(
7479

7580
const branchName = stdout.trim();
7681
if (!branchName) {
77-
console.warn("No git branch found", docUri, branchName);
82+
console.warn(
83+
`No git branch found in ${docDirectory}`,
84+
docUri,
85+
branchName,
86+
);
7887
return undefined;
7988
}
8089

8190
console.debug("Git: Current branch name", branchName);
8291
return branchName;
8392
} catch (e) {
84-
console.warn("Git invocation failed", docUri, e);
93+
console.warn(`Git invocation failed in ${docDirectory}`, docUri, e);
8594
return undefined;
8695
}
8796
}

src/test/suite/extensionIntegration.test.ts

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,77 @@ import * as os from "os";
77
import { execSync } from "child_process";
88

99
suite("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

Comments
 (0)