luff 0.2.1

Print files with formatting
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
//! End-to-end tests for luff CLI
//!
//! These tests execute the binary and verify its behavior in
//! realistic scenarios.
//!
//! Requires the `cli` feature (for the `luff` binary target).
#![cfg(feature = "cli")]

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;

/// Helper to create a test command
fn luff_cmd() -> Command {
    Command::new(assert_cmd::cargo::cargo_bin!("luff"))
}

#[test]
fn test_help_flag() {
    luff_cmd()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Print files"));
}

#[test]
fn test_version_flag() {
    luff_cmd()
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains(env!("CARGO_PKG_VERSION")));
}

#[test]
fn test_process_single_file() {
    let temp = TempDir::new().unwrap();
    let file_path = temp.path().join("test.txt");
    fs::write(&file_path, "Hello, World!").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .arg("--files")
        .arg(file_path.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("Hello, World!"))
        .stdout(predicate::str::contains("```txt"));
}

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

    let file1 = temp.path().join("file1.txt");
    fs::write(&file1, "Content 1").unwrap();

    let file2 = temp.path().join("file2.txt");
    fs::write(&file2, "Content 2").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .arg("--files")
        .arg(file1.to_str().unwrap())
        .arg(file2.to_str().unwrap())
        .assert()
        .success()
        .stdout(predicate::str::contains("Content 1"))
        .stdout(predicate::str::contains("Content 2"));
}

#[test]
fn test_nonexistent_file_error() {
    // When all specified files are invalid, the CLI should exit with error
    luff_cmd()
        .arg("--files")
        .arg("/nonexistent/file.txt")
        .assert()
        .failure() // Correctly expect failure when no valid files
        .stderr(predicate::str::contains(
            "All 1 specified file(s) were invalid",
        ));
}

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

    // Create test structure
    fs::create_dir(temp.path().join("src")).unwrap();
    fs::write(temp.path().join("src/main.rs"), "fn main() {}").unwrap();
    fs::write(temp.path().join("README.md"), "# Test").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("main.rs"))
        .stdout(predicate::str::contains("README.md"));
}

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

    // Create .gitignore
    fs::write(temp.path().join(".gitignore"), "ignored.txt\n").unwrap();

    // Create files
    fs::write(temp.path().join("included.txt"), "include").unwrap();
    fs::write(temp.path().join("ignored.txt"), "ignore").unwrap();

    // Default behavior: should respect .gitignore
    luff_cmd()
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("included.txt"))
        .stdout(predicate::str::contains("ignored.txt").not());
}

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

    // Create .gitignore
    fs::write(temp.path().join(".gitignore"), "ignored.txt\n").unwrap();

    // Create files
    fs::write(temp.path().join("included.txt"), "include").unwrap();
    fs::write(temp.path().join("ignored.txt"), "ignore").unwrap();

    // With --add flag: should include .gitignore files
    luff_cmd()
        .current_dir(temp.path())
        .arg("--add")
        .assert()
        .success()
        .stdout(predicate::str::contains("included.txt"))
        .stdout(predicate::str::contains("ignored.txt"));
}

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

    // Create .gitignore
    fs::write(temp.path().join(".gitignore"), "secret.txt\n").unwrap();

    // Create files
    fs::write(temp.path().join("public.txt"), "public").unwrap();
    fs::write(temp.path().join("secret.txt"), "secret").unwrap();

    // Test short form -a
    luff_cmd()
        .current_dir(temp.path())
        .arg("-a")
        .assert()
        .success()
        .stdout(predicate::str::contains("public.txt"))
        .stdout(predicate::str::contains("secret.txt"));
}

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

    // Create files
    fs::write(temp.path().join("keep.txt"), "keep").unwrap();
    fs::write(temp.path().join("skip.log"), "skip").unwrap();
    fs::create_dir(temp.path().join("target")).unwrap();
    fs::write(temp.path().join("target/build.rs"), "build").unwrap();

    // Test --ignore with globs
    luff_cmd()
        .current_dir(temp.path())
        .arg("--ignore")
        .arg("*.log")
        .arg("--ignore")
        .arg("target/**")
        .assert()
        .success()
        .stdout(predicate::str::contains("keep.txt"))
        .stdout(predicate::str::contains("skip.log").not())
        .stdout(predicate::str::contains("build.rs").not());
}

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

    // Create .gitignore with wildcard pattern
    fs::write(temp.path().join(".gitignore"), "*.log\ntarget/\n").unwrap();

    // Create directory structure
    fs::create_dir(temp.path().join("target")).unwrap();
    fs::write(temp.path().join("app.txt"), "app").unwrap();
    fs::write(temp.path().join("debug.log"), "log").unwrap();
    fs::write(temp.path().join("target/build.txt"), "build").unwrap();

    // Default: should exclude *.log and target/
    luff_cmd()
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("app.txt"))
        .stdout(predicate::str::contains("debug.log").not())
        .stdout(predicate::str::contains("target/build.txt").not());
}

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

    fs::create_dir(temp.path().join(".hidden")).unwrap();
    fs::write(temp.path().join(".hidden/file.txt"), "hidden").unwrap();
    fs::write(temp.path().join("visible.txt"), "visible").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("visible.txt"))
        .stdout(predicate::str::contains(".hidden").not());
}

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

    fs::create_dir(temp.path().join(".hidden")).unwrap();
    fs::write(temp.path().join(".hidden/file.txt"), "hidden").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .arg("--dotfiles")
        .assert()
        .success()
        .stdout(predicate::str::contains(".hidden/file.txt"));
}

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

    let png_file = temp.path().join("image.png");
    fs::write(&png_file, vec![0u8; 100]).unwrap();

    let txt_file = temp.path().join("text.txt");
    fs::write(&txt_file, "text content").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("text.txt"))
        .stdout(predicate::str::contains("image.png").not());
}

#[test]
fn test_verbose_logging() {
    let temp = TempDir::new().unwrap();
    fs::write(temp.path().join("test.txt"), "test").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .arg("--verbose")
        .assert()
        .success();
    // Note: We can't easily test stderr in this setup
    // but we verify it doesn't crash with verbose logging
}

#[test]
fn test_output_format_markdown() {
    let temp = TempDir::new().unwrap();
    fs::write(temp.path().join("test.txt"), "content").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .arg("--format")
        .arg("markdown")
        .assert()
        .success()
        .stdout(predicate::str::contains("```"));
}

#[test]
fn test_output_format_tree() {
    let temp = TempDir::new().unwrap();
    fs::write(temp.path().join("test.txt"), "content").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .arg("--format")
        .arg("tree")
        .assert()
        .success()
        .stdout(predicate::str::contains("."))
        .stdout(predicate::str::contains("└── test.txt"));
}

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

    fs::create_dir_all(temp.path().join("a/b/c")).unwrap();
    fs::write(temp.path().join("a/file1.txt"), "1").unwrap();
    fs::write(temp.path().join("a/b/file2.txt"), "2").unwrap();
    fs::write(temp.path().join("a/b/c/file3.txt"), "3").unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .arg("--max-depth")
        .arg("2")
        .assert()
        .success()
        .stdout(predicate::str::contains("file1.txt"))
        .stdout(predicate::str::contains("file2.txt"))
        .stdout(predicate::str::contains("file3.txt").not());
}

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

    // Create a directory with a file and an empty subdirectory
    fs::create_dir_all(temp.path().join("parent/child")).unwrap();
    fs::write(temp.path().join("parent/file.txt"), "content").unwrap();
    fs::create_dir(temp.path().join("parent/empty")).unwrap();

    luff_cmd()
        .current_dir(temp.path())
        .arg("--format")
        .arg("tree")
        .assert()
        .success()
        .stdout(predicate::str::contains("parent"))
        .stdout(predicate::str::contains("empty"))
        .stdout(predicate::str::contains("child"));
}

#[test]
fn test_file_snapshot_consistency() {
    // This test verifies that DirectoryWalker takes a consistent snapshot
    // of files at construction time. Files that exist when the walker is
    // created WILL be included, even if they're output files.
    let temp = TempDir::new().unwrap();

    // Create initial files
    fs::write(temp.path().join("file1.txt"), "content1").unwrap();
    fs::write(temp.path().join("file2.txt"), "content2").unwrap();

    // First run - capture output
    let output = luff_cmd().current_dir(temp.path()).output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();

    // Verify initial files are in output
    assert!(stdout.contains("file1.txt"));
    assert!(stdout.contains("file2.txt"));

    // Write output to a new file in the same directory
    let output_file = temp.path().join("output.txt");
    fs::write(&output_file, &stdout).unwrap();

    // Second run - output.txt now exists at walker construction time
    let output2 = luff_cmd().current_dir(temp.path()).output().unwrap();
    let stdout2 = String::from_utf8(output2.stdout).unwrap();

    // All files including output.txt should be in the second run
    assert!(stdout2.contains("file1.txt"));
    assert!(stdout2.contains("file2.txt"));
    assert!(
        stdout2.contains("output.txt"),
        "Files existing at walker construction should be included"
    );

    // The second output should be longer (includes output.txt)
    assert!(
        stdout2.len() > stdout.len(),
        "Second run should include the newly created output.txt"
    );
}

#[test]
fn test_iteration_isolation() {
    // This test verifies that files created DURING iteration by external
    // processes would not be discovered (though we can't easily test this
    // in a single-threaded test). The upfront collection ensures the
    // walker has a consistent view of files.
    let temp = TempDir::new().unwrap();

    fs::write(temp.path().join("file1.txt"), "content1").unwrap();
    fs::write(temp.path().join("file2.txt"), "content2").unwrap();

    // Run luff - it collects files upfront
    let output = luff_cmd().current_dir(temp.path()).output().unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();

    // Verify only the expected files are included
    assert!(stdout.contains("file1.txt"));
    assert!(stdout.contains("file2.txt"));

    // Count occurrences of "```" to verify we have exactly 2 code blocks
    let code_block_count = stdout.matches("```txt").count();
    assert_eq!(code_block_count, 2, "Should have exactly 2 files processed");
}

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

    // Create a file with content - should be included
    fs::write(temp.path().join("with_content.txt"), "some content").unwrap();

    // Create an empty file immediately before running - should be skipped
    // This simulates the shell's behavior when redirecting output
    // (e.g., luff > output.txt creates output.txt before luff runs)
    fs::write(temp.path().join("empty_recent.txt"), "").unwrap();

    // Run luff immediately (within the 2-second threshold)
    // We set a high threshold (5s) to ensure the test doesn't flake on slow machines/CI
    // where process startup might take longer than the default 10ms threshold.
    let output = luff_cmd()
        .current_dir(temp.path())
        .env("LUFF_OUTPUT_PROTECTION_MS", "5000")
        .output()
        .unwrap();

    let stdout = String::from_utf8(output.stdout).unwrap();

    // Should include the file with content
    assert!(
        stdout.contains("with_content.txt"),
        "Should include files with content"
    );

    // Should NOT include the recently created empty file
    assert!(
        !stdout.contains("empty_recent.txt"),
        "Should skip recently created empty files (shell redirection protection)"
    );
}

#[test]
fn test_streaming_output_file_exclusion() {
    // This test simulates `luff > output.txt` where output.txt is in the scanned directory.
    // It verifies that luff detects this file is the output target and excludes it,
    // preventing infinite loops or duplication.
    let temp = TempDir::new().unwrap();

    // Create some content
    fs::write(temp.path().join("file1.txt"), "content1").unwrap();

    let output_path = temp.path().join("output.txt");
    let output_file = std::fs::File::create(&output_path).unwrap();

    // Use std::process::Command to handle redirection properly
    // This makes luff's stdout actually point to the file on disk
    let status = std::process::Command::new(assert_cmd::cargo::cargo_bin!("luff"))
        .current_dir(temp.path())
        .stdout(output_file)
        .status()
        .unwrap();

    assert!(status.success());

    // Read the output file
    let output_content = fs::read_to_string(&output_path).unwrap();

    // It should contain "file1.txt"
    assert!(output_content.contains("file1.txt"));

    // It should NOT contain "output.txt" (itself)
    // If it does, it means luff read the file while writing to it
    assert!(
        !output_content.contains("output.txt"),
        "Output file should not contain itself"
    );
}