Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit b3c7986

Browse files
committed
test: fix tests on contextignore and context
1 parent 75cbdef commit b3c7986

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

linters/typescript/src/__tests__/context_linter.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,34 @@ describe('ContextLinter', () => {
1414
`);
1515
fs.writeFileSync(path.join(testDir, '.context.md'), `---
1616
module-name: test-module
17+
related-modules: []
18+
version: 1.0.0
1719
description: A test module
20+
diagrams: []
21+
technologies: ['TypeScript', 'Jest']
22+
conventions: ['Use camelCase for variables']
23+
directives: []
24+
architecture:
25+
style: 'Modular'
26+
components: ['Component A', 'Component B']
27+
data-flow: ['Component A -> Component B']
28+
development:
29+
setup-steps: ['Install dependencies', 'Configure environment']
30+
build-command: 'npm run build'
31+
test-command: 'npm test'
32+
business-requirements:
33+
key-features: ['Feature 1', 'Feature 2']
34+
target-audience: 'Developers'
35+
success-metrics: ['Code coverage', 'Performance']
36+
quality-assurance:
37+
testing-frameworks: ['Jest']
38+
coverage-threshold: '80%'
39+
performance-benchmarks: ['Load time < 1s']
40+
deployment:
41+
platform: 'AWS'
42+
cicd-pipeline: 'GitHub Actions'
43+
staging-environment: 'staging.example.com'
44+
production-environment: 'production.example.com'
1845
---
1946
# Test Module
2047

linters/typescript/src/__tests__/contextignore_linter.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('ContextignoreLinter', () => {
3333
const content = await fs.promises.readFile(path.join(testDir, '.contextignore'), 'utf-8');
3434
const result = await linter.lintContextignoreFile(content, path.join(testDir, '.contextignore'));
3535
expect(result).toBe(true);
36+
expect(linter.getErrorMessages()).toHaveLength(0);
3637
});
3738

3839
it('should detect invalid patterns', async () => {
@@ -44,6 +45,7 @@ describe('ContextignoreLinter', () => {
4445
`;
4546
const result = await linter.lintContextignoreFile(content, 'test/.contextignore');
4647
expect(result).toBe(false);
48+
expect(linter.getErrorMessages()).toContain('Error: Invalid pattern at line 4: invalid**pattern');
4749
});
4850

4951
it('should detect attempts to ignore critical files', async () => {
@@ -54,6 +56,19 @@ describe('ContextignoreLinter', () => {
5456
`;
5557
const result = await linter.lintContextignoreFile(content, 'test/.contextignore');
5658
expect(result).toBe(false);
59+
expect(linter.getErrorMessages()).toContain('Error: Pattern at line 3 ignores critical file: .context.md');
60+
});
61+
62+
it('should not produce errors for valid patterns', async () => {
63+
const content = `
64+
node_modules/
65+
*.log
66+
/build
67+
!important.log
68+
`;
69+
const result = await linter.lintContextignoreFile(content, 'test/.contextignore');
70+
expect(result).toBe(true);
71+
expect(linter.getErrorMessages()).toHaveLength(0);
5772
});
5873
});
5974

linters/typescript/src/contextignore_linter.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ export class ContextignoreLinter {
1111
private criticalPatterns: Set<string>;
1212
// Cache of ignore instances for each directory
1313
private ignoreCache: Map<string, ReturnType<typeof ignore>>;
14+
// Store error messages
15+
private errorMessages: string[];
1416

1517
constructor() {
1618
this.criticalPatterns = new Set(['.context.md', '.context.yaml', '.context.json', '.contextdocs.md', '.contextignore']);
1719
this.ignoreCache = new Map();
20+
this.errorMessages = [];
1821
}
1922

2023
/**
@@ -25,6 +28,7 @@ export class ContextignoreLinter {
2528
*/
2629
public async lintContextignoreFile(content: string, filePath: string): Promise<boolean> {
2730
try {
31+
this.errorMessages = []; // Reset error messages
2832
const relativePath = path.relative(process.cwd(), filePath);
2933
console.log(`\nLinting file: ${relativePath}`);
3034
console.log('- Validating .contextignore format');
@@ -39,7 +43,7 @@ export class ContextignoreLinter {
3943

4044
// Updated regex to catch more invalid patterns, including "invalid**pattern"
4145
if (!/^!?(?:[\w\-./]+|\*(?!\*)|\?+|\[.+\])+$/.test(line)) {
42-
console.error(` Error: Invalid pattern at line ${i + 1}: ${line}`);
46+
this.errorMessages.push(`Error: Invalid pattern at line ${i + 1}: ${line}`);
4347
isValid = false;
4448
}
4549

@@ -61,7 +65,7 @@ export class ContextignoreLinter {
6165
console.log(''); // Add a blank line for better readability
6266
return isValid;
6367
} catch (error) {
64-
console.error(`Error linting .contextignore file ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
68+
this.errorMessages.push(`Error linting .contextignore file ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
6569
return false;
6670
}
6771
}
@@ -75,7 +79,7 @@ export class ContextignoreLinter {
7579
private validateCriticalPattern(pattern: string, lineNumber: number): boolean {
7680
for (const criticalPattern of this.criticalPatterns) {
7781
if (pattern.endsWith(criticalPattern) || pattern.includes(`/${criticalPattern}`)) {
78-
console.error(` Error: Pattern at line ${lineNumber + 1} ignores critical file: ${criticalPattern}`);
82+
this.errorMessages.push(`Error: Pattern at line ${lineNumber + 1} ignores critical file: ${criticalPattern}`);
7983
return false;
8084
}
8185
}
@@ -92,7 +96,7 @@ export class ContextignoreLinter {
9296
for (let j = i + 1; j < patterns.length; j++) {
9397
if (patterns[i].startsWith('!') && patterns[j] === patterns[i].slice(1) ||
9498
patterns[j].startsWith('!') && patterns[i] === patterns[j].slice(1)) {
95-
console.error(` Error: Conflicting patterns found: ${patterns[i]} and ${patterns[j]}`);
99+
this.errorMessages.push(`Error: Conflicting patterns found: ${patterns[i]} and ${patterns[j]}`);
96100
return false;
97101
}
98102
}
@@ -110,7 +114,7 @@ export class ContextignoreLinter {
110114
const ig = ignore().add(patterns);
111115
this.ignoreCache.set(path.dirname(filePath), ig);
112116
} catch (error) {
113-
console.error(`Error updating ignore cache for ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
117+
this.errorMessages.push(`Error updating ignore cache for ${filePath}: ${error instanceof Error ? error.message : String(error)}`);
114118
}
115119
}
116120

@@ -137,7 +141,7 @@ export class ContextignoreLinter {
137141

138142
return false;
139143
} catch (error) {
140-
console.error(`Error checking if file ${filePath} is ignored: ${error instanceof Error ? error.message : String(error)}`);
144+
this.errorMessages.push(`Error checking if file ${filePath} is ignored: ${error instanceof Error ? error.message : String(error)}`);
141145
return false;
142146
}
143147
}
@@ -170,7 +174,7 @@ export class ContextignoreLinter {
170174
return ig.ignores(relativePath);
171175
});
172176
} catch (error) {
173-
console.error(`Error getting ignored files for directory ${directoryPath}: ${error instanceof Error ? error.message : String(error)}`);
177+
this.errorMessages.push(`Error getting ignored files for directory ${directoryPath}: ${error instanceof Error ? error.message : String(error)}`);
174178
return [];
175179
}
176180
}
@@ -198,4 +202,12 @@ export class ContextignoreLinter {
198202
walk(directoryPath);
199203
return files;
200204
}
205+
206+
/**
207+
* Get the error messages
208+
* @returns An array of error messages
209+
*/
210+
public getErrorMessages(): string[] {
211+
return this.errorMessages;
212+
}
201213
}

0 commit comments

Comments
 (0)