-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathgit-utils.test.ts
More file actions
137 lines (116 loc) · 5.09 KB
/
Copy pathgit-utils.test.ts
File metadata and controls
137 lines (116 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { describe, expect, it } from 'vitest';
import { join } from 'node:path';
import {
isInsideGitRepo,
isTestspriteIgnored,
checkTestspriteIgnored,
ensureTestspriteIgnored,
} from './git-utils.js';
describe('git-utils', () => {
describe('isInsideGitRepo', () => {
it('returns true if .git exists in the current directory', () => {
const mockExists = (p: string) => p === join('/foo/bar', '.git');
expect(isInsideGitRepo('/foo/bar', { existsSync: mockExists })).toBe(true);
});
it('returns true if .git exists in a parent directory', () => {
const mockExists = (p: string) => p === join('/foo', '.git');
expect(isInsideGitRepo('/foo/bar/baz', { existsSync: mockExists })).toBe(true);
});
it('returns false if .git is not found anywhere up the tree', () => {
const mockExists = () => false;
expect(isInsideGitRepo('/foo/bar/baz', { existsSync: mockExists })).toBe(false);
});
});
describe('isTestspriteIgnored', () => {
it('returns false for empty content', () => {
expect(isTestspriteIgnored('')).toBe(false);
});
it('returns true if .testsprite is explicitly ignored', () => {
expect(isTestspriteIgnored('.testsprite')).toBe(true);
expect(isTestspriteIgnored('.testsprite/')).toBe(true);
expect(isTestspriteIgnored('**/.testsprite')).toBe(true);
expect(isTestspriteIgnored('**/.testsprite/')).toBe(true);
});
it('returns true if .testsprite subfolders or files are ignored', () => {
expect(isTestspriteIgnored('.testsprite/*')).toBe(true);
expect(isTestspriteIgnored('**/.testsprite/runs')).toBe(true);
});
it('ignores comments and empty lines', () => {
expect(isTestspriteIgnored('# .testsprite/')).toBe(false);
expect(isTestspriteIgnored(' # comment\n.testsprite/')).toBe(true);
});
it('returns false if unrelated folders are ignored', () => {
expect(isTestspriteIgnored('node_modules/\ndist/')).toBe(false);
});
it('correctly handles negation patterns (un-ignoring)', () => {
expect(isTestspriteIgnored('.testsprite/\n!.testsprite/')).toBe(false);
expect(isTestspriteIgnored('!.testsprite/\n.testsprite/')).toBe(true);
expect(isTestspriteIgnored('**/.testsprite/\n!**/.testsprite/')).toBe(false);
expect(isTestspriteIgnored('.testsprite/*\n!.testsprite/')).toBe(false);
});
});
describe('checkTestspriteIgnored', () => {
it('returns false if .gitignore does not exist', async () => {
const mockExists = () => false;
const mockRead = async () => '';
await expect(checkTestspriteIgnored('/foo', { existsSync: mockExists, readFile: mockRead })).resolves.toBe(false);
});
it('returns true if .gitignore exists and ignores .testsprite', async () => {
const mockExists = (p: string) => p === join('/foo', '.gitignore');
const mockRead = async () => 'dist/\n.testsprite/';
await expect(checkTestspriteIgnored('/foo', { existsSync: mockExists, readFile: mockRead })).resolves.toBe(true);
});
it('returns false if .gitignore exists but does not ignore .testsprite', async () => {
const mockExists = (p: string) => p === join('/foo', '.gitignore');
const mockRead = async () => 'dist/';
await expect(checkTestspriteIgnored('/foo', { existsSync: mockExists, readFile: mockRead })).resolves.toBe(false);
});
});
describe('ensureTestspriteIgnored', () => {
it('creates .gitignore and appends .testsprite if file does not exist', async () => {
let writtenContent = '';
const mockExists = () => false;
const mockRead = async () => '';
const mockWrite = async (p: string, content: string) => {
writtenContent = content;
};
const result = await ensureTestspriteIgnored('/foo', {
existsSync: mockExists,
readFile: mockRead,
writeFile: mockWrite,
});
expect(result).toBe(true);
expect(writtenContent).toContain('.testsprite/');
});
it('appends .testsprite if .gitignore exists but does not ignore it', async () => {
let writtenContent = '';
const mockExists = () => true;
const mockRead = async () => 'node_modules/\ndist/';
const mockWrite = async (p: string, content: string) => {
writtenContent = content;
};
const result = await ensureTestspriteIgnored('/foo', {
existsSync: mockExists,
readFile: mockRead,
writeFile: mockWrite,
});
expect(result).toBe(true);
expect(writtenContent).toBe('node_modules/\ndist/\n\n# TestSprite failure artifacts\n.testsprite/\n');
});
it('does not modify .gitignore if .testsprite is already ignored', async () => {
let calledWrite = false;
const mockExists = () => true;
const mockRead = async () => 'node_modules/\n.testsprite/';
const mockWrite = async () => {
calledWrite = true;
};
const result = await ensureTestspriteIgnored('/foo', {
existsSync: mockExists,
readFile: mockRead,
writeFile: mockWrite,
});
expect(result).toBe(false);
expect(calledWrite).toBe(false);
});
});
});