basefmt 0.1.0

A formatter that applies universal formatting rules to any text file
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;

fn basefmt() -> Command {
    Command::new(env!("CARGO_BIN_EXE_basefmt"))
}

/// Helper function to recursively copy a directory tree
fn copy_dir_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
    fs::create_dir_all(dst)?;
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let file_type = entry.file_type()?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());

        if file_type.is_dir() {
            copy_dir_recursive(&src_path, &dst_path)?;
        } else {
            fs::copy(&src_path, &dst_path)?;
        }
    }
    Ok(())
}

fn setup_test_file(temp_dir: &TempDir, fixture_name: &str) -> PathBuf {
    let input_path = PathBuf::from("tests/fixtures/input").join(fixture_name);
    let temp_file = temp_dir.path().join(fixture_name);
    fs::copy(&input_path, &temp_file).unwrap();
    temp_file
}

// Helper function to create a .editorconfig file with all rules enabled
fn create_default_editorconfig(dir: &TempDir) {
    let config_path = dir.path().join(".editorconfig");
    fs::write(
        config_path,
        r#"root = true

[*]
insert_final_newline = true
trim_trailing_whitespace = true
trim_leading_newlines = true
"#,
    )
    .unwrap();
}

fn read_expected(fixture_name: &str) -> String {
    let expected_path = PathBuf::from("tests/fixtures/expected").join(fixture_name);
    fs::read_to_string(expected_path).unwrap()
}

#[test]
fn test_format_single_files() {
    let test_cases = [
        "leading_newlines.txt",
        "no_final_newline.txt",
        "trailing_space.txt",
        "multiple_final_newlines.txt",
    ];

    for fixture_name in test_cases {
        let temp_dir = TempDir::new().unwrap();
        create_default_editorconfig(&temp_dir);
        let test_file = setup_test_file(&temp_dir, fixture_name);

        let status = basefmt().arg(test_file.to_str().unwrap()).status().unwrap();
        assert!(status.success(), "Failed to format {fixture_name}");

        let actual = fs::read_to_string(&test_file).unwrap();
        let expected = read_expected(fixture_name);
        assert_eq!(
            actual, expected,
            "File {fixture_name} was not formatted correctly"
        );
    }
}

#[test]
fn test_format_directory() {
    let temp_dir = TempDir::new().unwrap();
    create_default_editorconfig(&temp_dir);
    setup_test_file(&temp_dir, "leading_newlines.txt");
    setup_test_file(&temp_dir, "no_final_newline.txt");
    setup_test_file(&temp_dir, "trailing_space.txt");
    setup_test_file(&temp_dir, "multiple_final_newlines.txt");

    let status = basefmt()
        .arg(temp_dir.path().to_str().unwrap())
        .status()
        .unwrap();
    assert!(status.success());

    // Verify all files were formatted correctly
    for fixture_name in [
        "leading_newlines.txt",
        "no_final_newline.txt",
        "trailing_space.txt",
        "multiple_final_newlines.txt",
    ] {
        let actual = fs::read_to_string(temp_dir.path().join(fixture_name)).unwrap();
        let expected = read_expected(fixture_name);
        assert_eq!(
            actual, expected,
            "File {fixture_name} was not formatted correctly"
        );
    }
}

#[test]
fn test_check_mode_clean_file() {
    let temp_dir = TempDir::new().unwrap();
    create_default_editorconfig(&temp_dir);
    let expected_path = PathBuf::from("tests/fixtures/expected/leading_newlines.txt");
    let test_file = temp_dir.path().join("leading_newlines.txt");
    fs::copy(&expected_path, &test_file).unwrap();

    let status = basefmt()
        .arg("--check")
        .arg(test_file.to_str().unwrap())
        .status()
        .unwrap();
    assert!(status.success());
}

#[test]
fn test_check_mode_dirty_file() {
    let temp_dir = TempDir::new().unwrap();
    create_default_editorconfig(&temp_dir);
    let test_file = setup_test_file(&temp_dir, "leading_newlines.txt");
    let original_content = fs::read_to_string(&test_file).unwrap();

    let status = basefmt()
        .arg("--check")
        .arg(test_file.to_str().unwrap())
        .status()
        .unwrap();
    assert!(!status.success());

    // Verify file was not modified
    let after_check = fs::read_to_string(&test_file).unwrap();
    assert_eq!(original_content, after_check);
}
#[test]
fn test_format_skips_binary_file() {
    let temp_dir = TempDir::new().unwrap();
    let binary_file = temp_dir.path().join("binary.bin");
    // Write invalid UTF-8 bytes
    fs::write(&binary_file, &[0xFF, 0xFE, 0xFD]).unwrap();

    let status = basefmt()
        .arg(binary_file.to_str().unwrap())
        .status()
        .unwrap();
    // Binary files should be silently skipped, exit code 0
    assert!(status.success());
    assert_eq!(status.code(), Some(0));

    // Verify file was not modified
    let content = fs::read(&binary_file).unwrap();
    assert_eq!(content, vec![0xFF, 0xFE, 0xFD]);
}

#[test]
fn test_format_directory_with_binary_file() {
    let temp_dir = TempDir::new().unwrap();

    // Create a text file that needs formatting
    let text_file = temp_dir.path().join("text.txt");
    fs::write(&text_file, "\n\ntest content  \n\n").unwrap();

    // Create a binary file
    let binary_file = temp_dir.path().join("binary.bin");
    fs::write(&binary_file, &[0xFF, 0xFE, 0xFD]).unwrap();

    let status = basefmt()
        .arg(temp_dir.path().to_str().unwrap())
        .status()
        .unwrap();
    // Binary file should be skipped, text file formatted successfully, exit code 0
    assert!(status.success());
    assert_eq!(status.code(), Some(0));

    // Text file should be formatted correctly
    let text_content = fs::read_to_string(&text_file).unwrap();
    assert_eq!(text_content, "test content\n");

    // Binary file should not be modified
    let binary_content = fs::read(&binary_file).unwrap();
    assert_eq!(binary_content, vec![0xFF, 0xFE, 0xFD]);
}

#[test]
fn test_check_skips_binary_file() {
    let temp_dir = TempDir::new().unwrap();
    let binary_file = temp_dir.path().join("binary.bin");
    // Write invalid UTF-8 bytes
    fs::write(&binary_file, &[0xFF, 0xFE, 0xFD]).unwrap();

    let status = basefmt()
        .arg("--check")
        .arg(binary_file.to_str().unwrap())
        .status()
        .unwrap();
    // Binary files should be silently skipped, exit code 0
    assert!(status.success());
    assert_eq!(status.code(), Some(0));

    // Verify file was not modified
    let content = fs::read(&binary_file).unwrap();
    assert_eq!(content, vec![0xFF, 0xFE, 0xFD]);
}
// ==============================================================================
// EditorConfig + exclude configuration integration tests
// ==============================================================================

/// Test that files controlled by EditorConfig and .basefmt.toml exclude patterns
/// are properly skipped during formatting
#[test]
fn test_editorconfig_and_exclude_integration() {
    let temp_dir = TempDir::new().unwrap();

    // Copy .editorconfig and .basefmt.toml from fixtures
    let fixture_src = PathBuf::from("tests/fixtures/config");
    fs::copy(
        fixture_src.join(".editorconfig"),
        temp_dir.path().join(".editorconfig"),
    )
    .unwrap();
    fs::copy(
        fixture_src.join(".basefmt.toml"),
        temp_dir.path().join(".basefmt.toml"),
    )
    .unwrap();

    // Create directory structure
    fs::create_dir_all(temp_dir.path().join("test/fixtures")).unwrap();
    fs::create_dir_all(temp_dir.path().join("vendor")).unwrap();
    fs::create_dir_all(temp_dir.path().join("generated")).unwrap();

    // Generate test files with guaranteed trailing spaces
    fs::write(
        temp_dir.path().join("normal.txt"),
        "normal file with trailing spaces  \n\n\n",
    )
    .unwrap();
    fs::write(
        temp_dir.path().join("markdown.md"),
        "# Markdown\nTrailing spaces  \n  \n\n",
    )
    .unwrap();
    fs::write(
        temp_dir.path().join("test/fixtures/data.txt"),
        "test data with trailing spaces  \n\n\n",
    )
    .unwrap();
    fs::write(
        temp_dir.path().join("vendor/lib.js"),
        "// vendor library with trailing spaces  \n\n\n",
    )
    .unwrap();
    fs::write(
        temp_dir.path().join("generated/output.rs"),
        "// generated code with trailing spaces  \n\n\n",
    )
    .unwrap();

    // Run basefmt on the entire directory
    let status = basefmt()
        .arg(temp_dir.path().to_str().unwrap())
        .status()
        .unwrap();
    assert!(status.success());

    // Test 1: normal.txt should be formatted (no exclusions apply)
    let normal_content = fs::read_to_string(temp_dir.path().join("normal.txt")).unwrap();
    assert_eq!(
        normal_content, "normal file with trailing spaces\n",
        "normal.txt should have been formatted"
    );

    // Test 2: markdown.md should keep trailing spaces (EditorConfig: trim_trailing_whitespace = false)
    let md_content = fs::read_to_string(temp_dir.path().join("markdown.md")).unwrap();
    assert!(
        md_content.ends_with("  \n"),
        "markdown.md should preserve trailing spaces due to EditorConfig. Got: {md_content:?}"
    );
    assert_eq!(
        md_content, "# Markdown\nTrailing spaces  \n  \n",
        "markdown.md formatting incorrect"
    );

    // Test 3: test/fixtures/data.txt should not be formatted (EditorConfig: unset)
    let test_fixture_content =
        fs::read_to_string(temp_dir.path().join("test/fixtures/data.txt")).unwrap();
    assert!(
        test_fixture_content.ends_with("  \n\n\n"),
        "test/fixtures/data.txt should not be formatted (EditorConfig unset)"
    );

    // Test 4: vendor/lib.js should not be formatted (EditorConfig: unset)
    let vendor_content = fs::read_to_string(temp_dir.path().join("vendor/lib.js")).unwrap();
    assert!(
        vendor_content.ends_with("  \n\n\n"),
        "vendor/lib.js should not be formatted (EditorConfig unset)"
    );

    // Test 5: generated/output.rs should not be formatted (.basefmt.toml exclude)
    let generated_content =
        fs::read_to_string(temp_dir.path().join("generated/output.rs")).unwrap();
    assert!(
        generated_content.ends_with("  \n\n\n"),
        "generated/output.rs should not be formatted (.basefmt.toml exclude)"
    );
}

/// Test that EditorConfig settings properly disable formatting rules
#[test]
fn test_editorconfig_disables_formatting() {
    let temp_dir = TempDir::new().unwrap();

    // Copy the config fixture
    let fixture_src = PathBuf::from("tests/fixtures/config");
    copy_dir_recursive(&fixture_src, temp_dir.path()).unwrap();

    // Create markdown file with trailing spaces
    let md_path = temp_dir.path().join("markdown.md");
    fs::write(&md_path, "# Test\ntrailing spaces  \n").unwrap();

    let status = basefmt().arg(md_path.to_str().unwrap()).status().unwrap();
    assert!(status.success());

    let formatted_content = fs::read_to_string(&md_path).unwrap();

    // Markdown should not have trailing spaces removed
    assert!(
        formatted_content.contains("trailing spaces  "),
        "Markdown file should preserve trailing spaces"
    );
}

/// Test that .basefmt.toml exclude has highest priority
#[test]
fn test_basefmt_exclude_overrides_editorconfig() {
    let temp_dir = TempDir::new().unwrap();

    // Copy the config fixture
    let fixture_src = PathBuf::from("tests/fixtures/config");
    copy_dir_recursive(&fixture_src, temp_dir.path()).unwrap();

    // Create generated directory and file
    fs::create_dir_all(temp_dir.path().join("generated")).unwrap();
    let generated_path = temp_dir.path().join("generated/output.rs");
    let original_content = "// generated code with trailing spaces  \n\n\n";
    fs::write(&generated_path, original_content).unwrap();

    let status = basefmt()
        .arg(temp_dir.path().to_str().unwrap())
        .status()
        .unwrap();
    assert!(status.success());

    let after_content = fs::read_to_string(&generated_path).unwrap();

    // Should not be formatted due to .basefmt.toml exclude
    assert_eq!(
        original_content, after_content,
        "generated/output.rs should not be formatted (excluded by .basefmt.toml)"
    );
}

/// Test check mode with EditorConfig and exclude patterns
#[test]
fn test_check_mode_with_config() {
    let temp_dir = TempDir::new().unwrap();

    // Copy the config fixture
    let fixture_src = PathBuf::from("tests/fixtures/config");
    copy_dir_recursive(&fixture_src, temp_dir.path()).unwrap();

    // Create a file that needs formatting
    let normal_path = temp_dir.path().join("normal.txt");
    fs::write(&normal_path, "normal file with trailing spaces  \n\n\n").unwrap();

    // Check mode should report that normal.txt needs formatting
    // but should not report excluded files as needing formatting
    let status = basefmt()
        .arg("--check")
        .arg(temp_dir.path().to_str().unwrap())
        .status()
        .unwrap();

    // Should fail because normal.txt needs formatting
    assert!(!status.success());

    // All files should remain unchanged
    let normal_content = fs::read_to_string(&normal_path).unwrap();
    assert!(
        normal_content.ends_with("  \n\n\n"),
        "check mode should not modify files"
    );
}

/// Test EditorConfig with unset values properly disables formatting
#[test]
fn test_editorconfig_unset_disables_formatting() {
    let temp_dir = TempDir::new().unwrap();

    // Copy the config fixture
    let fixture_src = PathBuf::from("tests/fixtures/config");
    copy_dir_recursive(&fixture_src, temp_dir.path()).unwrap();

    // Create vendor directory and file
    fs::create_dir_all(temp_dir.path().join("vendor")).unwrap();
    let vendor_path = temp_dir.path().join("vendor/lib.js");
    let original_content = "// vendor library with trailing spaces  \n\n\n";
    fs::write(&vendor_path, original_content).unwrap();

    let status = basefmt()
        .arg(temp_dir.path().to_str().unwrap())
        .status()
        .unwrap();
    assert!(status.success());

    let after_content = fs::read_to_string(&vendor_path).unwrap();

    // vendor/ has all formatting rules unset, so should not be formatted
    assert_eq!(
        original_content, after_content,
        "vendor/lib.js should not be formatted (EditorConfig unset)"
    );
}

/// Test that multiple exclusion rules work together correctly
#[test]
fn test_multiple_exclusion_patterns() {
    let temp_dir = TempDir::new().unwrap();

    // Copy the config fixture
    let fixture_src = PathBuf::from("tests/fixtures/config");
    copy_dir_recursive(&fixture_src, temp_dir.path()).unwrap();

    // Create test files
    fs::create_dir_all(temp_dir.path().join("test/fixtures")).unwrap();
    fs::create_dir_all(temp_dir.path().join("generated")).unwrap();

    fs::write(
        temp_dir.path().join("test/fixtures/data.txt"),
        "test data with trailing spaces  \n\n\n",
    )
    .unwrap();
    fs::write(
        temp_dir.path().join("markdown.md"),
        "# Markdown\ntrailing spaces  \n\n\n",
    )
    .unwrap();
    fs::write(
        temp_dir.path().join("generated/output.rs"),
        "// generated code with trailing spaces  \n\n\n",
    )
    .unwrap();
    fs::write(
        temp_dir.path().join("normal.txt"),
        "normal file with trailing spaces  \n\n\n",
    )
    .unwrap();

    let status = basefmt()
        .arg(temp_dir.path().to_str().unwrap())
        .status()
        .unwrap();
    assert!(status.success());

    // Verify that different exclusion mechanisms work independently:
    // 1. EditorConfig pattern-based exclusion (test/fixtures/**)
    let test_fixture = fs::read_to_string(temp_dir.path().join("test/fixtures/data.txt")).unwrap();
    assert!(
        test_fixture.ends_with("  \n\n\n"),
        "test/fixtures/** excluded by EditorConfig"
    );

    // 2. EditorConfig file extension-based rule (*.md)
    let markdown = fs::read_to_string(temp_dir.path().join("markdown.md")).unwrap();
    assert!(
        markdown.contains("trailing spaces  "),
        "*.md excluded by EditorConfig"
    );

    // 3. .basefmt.toml exclude pattern (generated/**)
    let generated = fs::read_to_string(temp_dir.path().join("generated/output.rs")).unwrap();
    assert!(
        generated.ends_with("  \n\n\n"),
        "generated/** excluded by .basefmt.toml"
    );

    // 4. Normal files should be formatted
    let normal = fs::read_to_string(temp_dir.path().join("normal.txt")).unwrap();
    assert_eq!(normal, "normal file with trailing spaces\n");
}