rumdl 0.1.82

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
Documentation
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use rumdl_lib::config::Config;
use rumdl_lib::lint_context::LintContext;
use rumdl_lib::rule::Rule;
use rumdl_lib::rules::*;
use std::sync::{Arc, Mutex};
use std::thread;
// Duration removed - no longer needed after optimizations

#[test]
fn test_concurrent_rule_execution() {
    println!("Testing concurrent rule execution safety...");

    let test_content = r#"# Heading 1

Some content here.

## Heading 2
Missing blank line above.
Another line.

### Heading 3

More content.
"#;

    let ctx = LintContext::new(test_content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let shared_ctx = Arc::new(ctx);
    let results = Arc::new(Mutex::new(Vec::new()));

    // Test multiple rules concurrently
    let mut handles = vec![];

    for i in 0..10 {
        let ctx_clone = Arc::clone(&shared_ctx);
        let results_clone = Arc::clone(&results);

        let handle = thread::spawn(move || {
            let rule = MD022BlanksAroundHeadings::new();
            let warnings = rule.check(&ctx_clone).unwrap();

            let mut results = results_clone.lock().unwrap();
            results.push((i, warnings.len()));
        });

        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    let results = results.lock().unwrap();
    println!("Concurrent results: {:?}", *results);

    // All threads should produce the same number of warnings
    let first_result = results[0].1;
    for (thread_id, warning_count) in results.iter() {
        assert_eq!(
            *warning_count, first_result,
            "Thread {thread_id} produced different warning count: {warning_count} vs {first_result}"
        );
    }

    assert!(results.len() == 10, "Expected 10 thread results");
}

#[test]
fn test_concurrent_different_rules() {
    println!("Testing concurrent execution of different rules...");

    let test_content = r#"# Heading!

Some content with trailing spaces

## Another Heading

- List item
-Another item missing space

```
code block without language
```

Text with *emphasis * and **strong **.
"#;

    let ctx = Arc::new(LintContext::new(
        test_content,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
    ));
    let results = Arc::new(Mutex::new(Vec::new()));

    // Define different rules to test concurrently
    let rules: Vec<(String, Box<dyn Rule + Send>)> = vec![
        ("MD022".to_string(), Box::new(MD022BlanksAroundHeadings::new())),
        ("MD026".to_string(), Box::new(MD026NoTrailingPunctuation::default())),
        ("MD009".to_string(), Box::new(MD009TrailingSpaces::default())),
        ("MD018".to_string(), Box::new(MD018NoMissingSpaceAtx::new())),
        ("MD040".to_string(), Box::new(MD040FencedCodeLanguage::default())),
        ("MD037".to_string(), Box::new(MD037NoSpaceInEmphasis)),
        ("MD038".to_string(), Box::new(MD038NoSpaceInCode::default())),
    ];

    let mut handles = vec![];

    for (rule_name, rule) in rules {
        let ctx_clone = Arc::clone(&ctx);
        let results_clone = Arc::clone(&results);

        let handle = thread::spawn(move || {
            let warnings = rule.check(&ctx_clone).unwrap();

            let mut results = results_clone.lock().unwrap();
            results.push((rule_name, warnings.len()));
        });

        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    let results = results.lock().unwrap();
    println!("Rule execution results: {:?}", *results);

    // Verify all rules executed successfully
    assert!(results.len() == 7, "Expected 7 rule results");

    // Verify all rules executed (warning_count is usize, always non-negative)
    for (rule_name, _warning_count) in results.iter() {
        // Each rule executed successfully
        println!("Rule {rule_name} executed in parallel");
    }
}

#[test]
fn test_concurrent_configuration_access() {
    println!("Testing concurrent configuration access...");

    let config = Arc::new(Config::default());
    let results = Arc::new(Mutex::new(Vec::new()));

    let mut handles = vec![];

    for i in 0..5 {
        let config_clone = Arc::clone(&config);
        let results_clone = Arc::clone(&results);

        let handle = thread::spawn(move || {
            // Access configuration from multiple threads
            let all_rules = rumdl_lib::rules::all_rules(&config_clone);

            let mut results = results_clone.lock().unwrap();
            results.push((i, all_rules.len()));
        });

        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    let results = results.lock().unwrap();
    println!("Configuration access results: {:?}", *results);

    // All threads should get the same number of rules
    let first_result = results[0].1;
    for (thread_id, rule_count) in results.iter() {
        assert_eq!(
            *rule_count, first_result,
            "Thread {thread_id} got different rule count: {rule_count} vs {first_result}"
        );
    }
}

#[test]
fn test_concurrent_fix_operations() {
    println!("Testing concurrent fix operations...");

    let test_content = r#"# Heading!
Some content.
## Another Heading
More content."#;

    let ctx = Arc::new(LintContext::new(
        test_content,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
    ));
    let results = Arc::new(Mutex::new(Vec::new()));

    let mut handles = vec![];

    for i in 0..5 {
        let ctx_clone = Arc::clone(&ctx);
        let results_clone = Arc::clone(&results);

        let handle = thread::spawn(move || {
            let rule = MD026NoTrailingPunctuation::default();
            let fixed_content = rule.fix(&ctx_clone);

            let mut results = results_clone.lock().unwrap();
            results.push((i, fixed_content.is_ok()));
        });

        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    let results = results.lock().unwrap();
    println!("Fix operation results: {:?}", *results);

    // All fix operations should succeed
    for (thread_id, success) in results.iter() {
        assert!(*success, "Thread {thread_id} fix operation failed");
    }
}

#[test]
fn test_high_concurrency_stress() {
    println!("Testing high concurrency stress scenarios...");

    let test_content = r#"# Document Title

This is a test document with various issues.

## Section 1
Missing blank line above.

### Subsection 1.1

Content here.

#### Subsection 1.1.1
Another missing blank line.

## Section 2!

Content with trailing punctuation.

```
code without language
```

Text with *bad emphasis * here.
"#;

    let ctx = Arc::new(LintContext::new(
        test_content,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
    ));
    let results = Arc::new(Mutex::new(Vec::new()));

    let mut handles = vec![];
    let thread_count = 50; // High concurrency

    for i in 0..thread_count {
        let ctx_clone = Arc::clone(&ctx);
        let results_clone = Arc::clone(&results);

        let handle = thread::spawn(move || {
            // Simulate different operations
            let operation = i % 4;

            match operation {
                0 => {
                    // Check operation
                    let rule = MD022BlanksAroundHeadings::new();
                    let warnings = rule.check(&ctx_clone).unwrap();
                    let mut results = results_clone.lock().unwrap();
                    results.push((i, "check".to_string(), warnings.len()));
                }
                1 => {
                    // Fix operation
                    let rule = MD026NoTrailingPunctuation::default();
                    let fixed = rule.fix(&ctx_clone);
                    let mut results = results_clone.lock().unwrap();
                    results.push((i, "fix".to_string(), if fixed.is_ok() { 1 } else { 0 }));
                }
                2 => {
                    // Multiple rule check
                    let rule1 = MD040FencedCodeLanguage::default();
                    let rule2 = MD037NoSpaceInEmphasis;
                    let warnings1 = rule1.check(&ctx_clone).unwrap();
                    let warnings2 = rule2.check(&ctx_clone).unwrap();
                    let mut results = results_clone.lock().unwrap();
                    results.push((i, "multi".to_string(), warnings1.len() + warnings2.len()));
                }
                _ => {
                    // Configuration access
                    let config = Config::default();
                    let all_rules = rumdl_lib::rules::all_rules(&config);
                    let mut results = results_clone.lock().unwrap();
                    results.push((i, "config".to_string(), all_rules.len()));
                }
            }

            // Add small delay to increase contention
            // Removed artificial delay for test performance
        });

        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    let results = results.lock().unwrap();
    println!("High concurrency results: {} operations completed", results.len());

    // Verify all threads completed successfully
    assert_eq!(results.len(), thread_count, "Expected {thread_count} thread results");

    // Group results by operation type
    let mut operation_counts = std::collections::HashMap::new();
    for (_, operation, _) in results.iter() {
        *operation_counts.entry(operation.clone()).or_insert(0) += 1;
    }

    println!("Operation distribution: {operation_counts:?}");

    // Each operation type should have been executed
    assert!(operation_counts.contains_key("check"));
    assert!(operation_counts.contains_key("fix"));
    assert!(operation_counts.contains_key("multi"));
    assert!(operation_counts.contains_key("config"));
}

#[test]
fn test_memory_safety_concurrent_access() {
    println!("Testing memory safety under concurrent access...");

    // Use a static large content instead of dynamically generated
    let large_content = r#"# Heading 1

Content for section 1.

# Heading 2

Content for section 2.

# Heading 3

Content for section 3.

# Heading 4

Content for section 4.

# Heading 5

Content for section 5.

# Heading 6

Content for section 6.

# Heading 7

Content for section 7.

# Heading 8

Content for section 8.

# Heading 9

Content for section 9.

# Heading 10

Content for section 10.
"#;

    let ctx = Arc::new(LintContext::new(
        large_content,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
    ));
    let success_count = Arc::new(Mutex::new(0));

    let mut handles = vec![];

    for i in 0..20 {
        let ctx_clone = Arc::clone(&ctx);
        let success_count_clone = Arc::clone(&success_count);

        let handle = thread::spawn(move || {
            // Perform memory-intensive operations
            let rule = MD022BlanksAroundHeadings::new();

            for _ in 0..5 {
                match rule.check(&ctx_clone) {
                    Ok(warnings) => {
                        // Verify warnings are reasonable
                        assert!(warnings.len() < 100, "Too many warnings generated");

                        let mut count = success_count_clone.lock().unwrap();
                        *count += 1;
                    }
                    Err(_) => panic!("Rule check failed in thread {i}"),
                }

                // Small delay to allow other threads to access
                // Removed artificial delay for test performance
            }
        });

        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    let final_count = *success_count.lock().unwrap();
    println!("Successful operations: {final_count}");

    // All operations should succeed
    assert_eq!(final_count, 20 * 5, "Expected 100 successful operations");
}