Skip to content

Commit 240ec64

Browse files
authored
Add test for absolute paths in checkIgnore (#1085)
* Normalize paths returned from `git.checkIgnore` * Strip surrounding quotes from the raw `git check-ignore` response for `path.normalize` to recognise when windows paths are absolute.
1 parent d232dc5 commit 240ec64

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

.changeset/common-swans-buy.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"simple-git": minor
3+
---
4+
5+
Support for absolute paths on Windows when using `git.checkIngore`, previously Windows would report
6+
paths with duplicate separators `\\\\` between directories.
7+
8+
Following this change all paths returned from `git.checkIgnore` will be normalized through `node:path`,
9+
this should have no impact on non-windows users where the `git` binary doesn't wrap absolute paths with
10+
quotes.
11+
12+
Thanks to @Maxim-Mazurok for reporting this issue.
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { normalize } from 'node:path';
2+
13
/**
24
* Parser for the `check-ignore` command - returns each file as a string array
35
*/
46
export const parseCheckIgnore = (text: string): string[] => {
5-
return text
6-
.split(/\n/g)
7-
.map((line) => line.trim())
8-
.filter((file) => !!file);
7+
return text.split(/\n/g).map(toPath).filter(Boolean);
98
};
9+
10+
function toPath(input: string) {
11+
const path = input.trim().replace(/^["']|["']$/g, '');
12+
return path && normalize(path);
13+
}

simple-git/test/integration/check-ignore.win32.spec.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createTestContext, setUpIgnored, setUpInit, SimpleGitTestContext } from '@simple-git/test-utils';
2+
import { join } from 'node:path';
23

34
describe('checkIgnore', () => {
45

@@ -10,7 +11,7 @@ describe('checkIgnore', () => {
1011
await setUpIgnored(context, ['ignored', 'partially/untracked']);
1112
});
1213

13-
it('detects ignored files', async () => {
14+
it('detects ignored files - relative paths', async () => {
1415
const actual = await context.git.checkIgnore([
1516
'ignored/anything',
1617
'tracked/anything',
@@ -19,8 +20,33 @@ describe('checkIgnore', () => {
1920
]);
2021

2122
expect(actual).toEqual([
22-
'ignored/anything',
23-
'partially/untracked/file',
23+
join('ignored', 'anything'),
24+
join('partially','untracked', 'file'),
25+
]);
26+
});
27+
28+
it('detects ignored files - absolute paths', async () => {
29+
const paths = [
30+
join(context.root, 'ignored', 'anything'),
31+
join(context.root, 'tracked', 'anything'),
32+
join(context.root, 'partially', 'tracked'),
33+
join(context.root, 'partially', 'untracked', 'file'),
34+
];
35+
const actual = await context.git.checkIgnore(paths);
36+
37+
expect(actual).toEqual([
38+
paths[0],
39+
paths[3],
2440
]);
2541
});
42+
43+
it('detects ignored files - absolute and relative paths', async () => {
44+
const paths = [
45+
join(context.root, 'ignored', 'anything'),
46+
join('partially', 'untracked', 'file'),
47+
];
48+
const actual = await context.git.checkIgnore(paths);
49+
50+
expect(actual).toEqual(paths);
51+
});
2652
});

0 commit comments

Comments
 (0)