bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use super::*;

// ============================================================
// Issue #85: Rule code support in .bashrsignore
// ============================================================

#[test]
fn test_issue_85_rule_codes_parsed() {
    // Issue #85: .bashrsignore should support rule codes like SEC010, SC2031
    let content = r#"
SEC010
SC2031
SC2032
SC2046
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // Rule codes should be stored separately from file patterns
    assert!(ignore.should_ignore_rule("SEC010"));
    assert!(ignore.should_ignore_rule("SC2031"));
    assert!(ignore.should_ignore_rule("SC2032"));
    assert!(ignore.should_ignore_rule("SC2046"));
    assert!(!ignore.should_ignore_rule("SEC001")); // Not in file
}

#[test]
fn test_issue_85_rule_codes_case_insensitive() {
    let content = "sec010\nSC2031";
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // Rule codes should be case-insensitive
    assert!(ignore.should_ignore_rule("SEC010"));
    assert!(ignore.should_ignore_rule("sec010"));
    assert!(ignore.should_ignore_rule("SC2031"));
    assert!(ignore.should_ignore_rule("sc2031"));
}

#[test]
fn test_issue_85_mixed_content() {
    // .bashrsignore can contain both file patterns and rule codes
    let content = r#"
# Ignore vendor scripts
vendor/**/*.sh

# Ignore specific rules (Issue #85)
SEC010
SC2031

# Exclude specific file
scripts/record-metric.sh
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // File patterns work
    assert!(matches!(
        ignore.should_ignore(Path::new("vendor/foo.sh")),
        IgnoreResult::Ignored(_)
    ));

    // Rule codes work
    assert!(ignore.should_ignore_rule("SEC010"));
    assert!(ignore.should_ignore_rule("SC2031"));
    assert!(!ignore.should_ignore_rule("DET001"));
}

#[test]
fn test_issue_85_rule_code_patterns() {
    // Test various rule code formats that should be recognized
    let content = r#"
SEC001
SEC010
SC2031
SC2046
DET001
DET002
IDEM001
IDEM002
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    assert!(ignore.should_ignore_rule("SEC001"));
    assert!(ignore.should_ignore_rule("SEC010"));
    assert!(ignore.should_ignore_rule("SC2031"));
    assert!(ignore.should_ignore_rule("SC2046"));
    assert!(ignore.should_ignore_rule("DET001"));
    assert!(ignore.should_ignore_rule("DET002"));
    assert!(ignore.should_ignore_rule("IDEM001"));
    assert!(ignore.should_ignore_rule("IDEM002"));
}

#[test]
fn test_issue_85_get_ignored_rules() {
    let content = "SEC010\nSC2031\nDET001";
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    let rules = ignore.ignored_rules();
    assert_eq!(rules.len(), 3);
    assert!(rules.contains(&"SEC010".to_string()));
    assert!(rules.contains(&"SC2031".to_string()));
    assert!(rules.contains(&"DET001".to_string()));
}

// ============================================================
// Issue #109: Line-specific and file-specific rule ignoring
// ============================================================

#[test]
fn test_issue_109_file_specific_ignore() {
    // Issue #109: Ignore rule only in specific file
    let content = "SEC010:scripts/install.sh";
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // Should be ignored in the specific file
    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("scripts/install.sh"), 1));

    // Should NOT be ignored in other files
    assert!(!ignore.should_ignore_rule_at("SEC010", Path::new("other/file.sh"), 1));

    // Global check should return false
    assert!(!ignore.should_ignore_rule("SEC010"));
}

#[test]
fn test_issue_109_line_specific_ignore() {
    // Issue #109: Ignore rule only on specific line
    let content = "DET001:scripts/metrics.sh:42";
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // Should be ignored on line 42
    assert!(ignore.should_ignore_rule_at("DET001", Path::new("scripts/metrics.sh"), 42));

    // Should NOT be ignored on other lines
    assert!(!ignore.should_ignore_rule_at("DET001", Path::new("scripts/metrics.sh"), 43));

    // Should NOT be ignored in other files
    assert!(!ignore.should_ignore_rule_at("DET001", Path::new("other/file.sh"), 42));
}

#[test]
fn test_issue_109_global_still_works() {
    // Issue #109: Global ignore should still work
    let content = "SEC010";
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // Global ignore applies everywhere
    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("any/file.sh"), 1));
    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("other/path.sh"), 999));
}

#[test]
fn test_issue_109_mixed_ignores() {
    // Mix of global, file-specific, and line-specific
    let content = r#"
SEC010
SC2031:scripts/install.sh
DET001:scripts/metrics.sh:42
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // SEC010 is global - applies everywhere
    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("any/file.sh"), 1));

    // SC2031 is file-specific - only in scripts/install.sh
    assert!(ignore.should_ignore_rule_at("SC2031", Path::new("scripts/install.sh"), 1));
    assert!(!ignore.should_ignore_rule_at("SC2031", Path::new("other.sh"), 1));

    // DET001 is line-specific - only on line 42 of scripts/metrics.sh
    assert!(ignore.should_ignore_rule_at("DET001", Path::new("scripts/metrics.sh"), 42));
    assert!(!ignore.should_ignore_rule_at("DET001", Path::new("scripts/metrics.sh"), 41));
}

#[test]
fn test_issue_109_path_normalization() {
    // Paths should be normalized (./prefix removed)
    let content = "SEC010:./scripts/install.sh";
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // Both with and without ./ should match
    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("scripts/install.sh"), 1));
    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("./scripts/install.sh"), 1));
}

#[test]
fn test_issue_109_case_insensitive_rule() {
    let content = "sec010:scripts/install.sh";
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // Rule codes should be case-insensitive
    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("scripts/install.sh"), 1));
    assert!(ignore.should_ignore_rule_at("sec010", Path::new("scripts/install.sh"), 1));
}

#[test]
fn test_issue_109_multiple_rules_same_file() {
    // Multiple rules can be ignored in the same file
    let content = r#"
SEC010:scripts/install.sh
SC2031:scripts/install.sh
DET001:scripts/install.sh
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("scripts/install.sh"), 1));
    assert!(ignore.should_ignore_rule_at("SC2031", Path::new("scripts/install.sh"), 1));
    assert!(ignore.should_ignore_rule_at("DET001", Path::new("scripts/install.sh"), 1));

    // Other rules should NOT be ignored
    assert!(!ignore.should_ignore_rule_at("IDEM001", Path::new("scripts/install.sh"), 1));
}

#[test]
fn test_issue_109_multiple_lines_same_file() {
    // Multiple line-specific ignores in the same file
    let content = r#"
SEC010:scripts/install.sh:10
SEC010:scripts/install.sh:20
SEC010:scripts/install.sh:30
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("scripts/install.sh"), 10));
    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("scripts/install.sh"), 20));
    assert!(ignore.should_ignore_rule_at("SEC010", Path::new("scripts/install.sh"), 30));

    // Other lines should NOT be ignored
    assert!(!ignore.should_ignore_rule_at("SEC010", Path::new("scripts/install.sh"), 15));
}

// ============================================================
// Original tests
// ============================================================

#[test]
fn test_empty_ignore_file() {
    let ignore = IgnoreFile::empty();
    assert!(!ignore.has_patterns());
    assert_eq!(
        ignore.should_ignore(Path::new("any/file.sh")),
        IgnoreResult::NotIgnored
    );
}

#[test]
fn test_parse_simple_pattern() {
    let content = "vendor/*.sh";
    let ignore = IgnoreFile::parse(content).expect("valid pattern");

    assert!(ignore.has_patterns());
    assert_eq!(ignore.pattern_count(), 1);

    assert!(matches!(
        ignore.should_ignore(Path::new("vendor/foo.sh")),
        IgnoreResult::Ignored(_)
    ));
    assert_eq!(
        ignore.should_ignore(Path::new("src/main.sh")),
        IgnoreResult::NotIgnored
    );
}

#[test]
fn test_parse_comments_and_empty_lines() {
    let content = r#"
# This is a comment
vendor/*.sh

# Another comment

**/generated/*.sh
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // Should have 2 patterns (comments and empty lines ignored)
    assert_eq!(ignore.pattern_count(), 2);
}

#[test]
fn test_negation_pattern() {
    let content = r#"
vendor/*.sh
!vendor/important.sh
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    // vendor/foo.sh should be ignored
    assert!(matches!(
        ignore.should_ignore(Path::new("vendor/foo.sh")),
        IgnoreResult::Ignored(_)
    ));

    // vendor/important.sh should NOT be ignored (negation)
    assert_eq!(
        ignore.should_ignore(Path::new("vendor/important.sh")),
        IgnoreResult::NotIgnored
    );
}

#[test]
fn test_double_star_pattern() {
    let content = "**/generated/*.sh";
    let ignore = IgnoreFile::parse(content).expect("valid pattern");

    assert!(matches!(
        ignore.should_ignore(Path::new("src/generated/foo.sh")),
        IgnoreResult::Ignored(_)
    ));
    assert!(matches!(
        ignore.should_ignore(Path::new("deep/path/generated/bar.sh")),
        IgnoreResult::Ignored(_)
    ));
    assert_eq!(
        ignore.should_ignore(Path::new("src/main.sh")),
        IgnoreResult::NotIgnored
    );
}

#[test]
fn test_exact_file_match() {
    let content = "scripts/record-metric.sh";
    let ignore = IgnoreFile::parse(content).expect("valid pattern");

    assert!(matches!(
        ignore.should_ignore(Path::new("scripts/record-metric.sh")),
        IgnoreResult::Ignored(_)
    ));
    assert_eq!(
        ignore.should_ignore(Path::new("scripts/other.sh")),
        IgnoreResult::NotIgnored
    );
}

#[test]
fn test_issue_58_record_metric_script() {
    // Issue #58: .bashrsignore should allow excluding record-metric.sh
    let content = r#"
# Metrics recording script from paiml-mcp-agent-toolkit
# Rationale: DET002 (timestamps) and SEC010 (paths) are false positives
scripts/record-metric.sh
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    assert!(matches!(
        ignore.should_ignore(Path::new("scripts/record-metric.sh")),
        IgnoreResult::Ignored(_)
    ));
}

#[test]
fn test_invalid_pattern_error() {
    // Invalid glob pattern (unclosed bracket)
    let content = "vendor/[invalid";
    let result = IgnoreFile::parse(content);

    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.contains("Invalid pattern"));
}

#[test]
fn test_load_nonexistent_file() {
    let result = IgnoreFile::load(Path::new("/nonexistent/.bashrsignore"));
    assert!(result.is_ok());
    assert!(result.unwrap().is_none());
}

#[test]
fn test_multiple_patterns() {
    let content = r#"
# Exclude vendor and generated
vendor/**/*.sh
generated/**/*.sh

# But keep important ones
!vendor/critical.sh
"#;
    let ignore = IgnoreFile::parse(content).expect("valid patterns");

    assert_eq!(ignore.pattern_count(), 3); // 2 include + 1 exclude

    // vendor/foo.sh - ignored
    assert!(matches!(
        ignore.should_ignore(Path::new("vendor/foo.sh")),
        IgnoreResult::Ignored(_)
    ));

    // vendor/critical.sh - NOT ignored (negation)
    assert_eq!(
        ignore.should_ignore(Path::new("vendor/critical.sh")),
        IgnoreResult::NotIgnored
    );

    // generated/output.sh - ignored
    assert!(matches!(
        ignore.should_ignore(Path::new("generated/output.sh")),
        IgnoreResult::Ignored(_)
    ));

    // src/main.sh - not matched
    assert_eq!(
        ignore.should_ignore(Path::new("src/main.sh")),
        IgnoreResult::NotIgnored
    );
}