yek 0.25.5

A tool to serialize a repository into chunks of text files
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
#[cfg(test)]
mod e2e_tests {
    use assert_cmd::Command;
    use predicates::prelude::*;
    use std::fs;

    use tempfile::tempdir;

    #[test]
    fn test_empty_dir() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        Command::cargo_bin("yek")?
            .arg(temp_dir.path())
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_single_file() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("test.txt"), "Test content")?;

        let mut cmd = Command::cargo_bin("yek")?;
        cmd.arg(temp_dir.path()).assert().success();
        Ok(())
    }

    #[test]
    fn test_multiple_files() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("test1.txt"), "Test content 1")?;
        fs::write(temp_dir.path().join("test2.txt"), "Test content 2")?;

        Command::cargo_bin("yek")?
            .arg(temp_dir.path())
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_ignore_patterns() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("test.log"), "Log content")?;
        fs::write(temp_dir.path().join("test.txt"), "Test content")?;

        Command::cargo_bin("yek")?
            .arg(temp_dir.path())
            .arg("--ignore-patterns")
            .arg("*.log")
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_priority_rules() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        // Create the 'src' directory
        fs::create_dir(temp_dir.path().join("src"))?;
        fs::write(temp_dir.path().join("src/test.rs"), "Test content")?;
        fs::write(temp_dir.path().join("test.txt"), "Test content")?;

        let config_content = r#"
            input_paths = ["."]
            [[priority_rules]]
            pattern = "src/.*\\.rs"
            score = 100
        "#;
        fs::write(temp_dir.path().join("yek.toml"), config_content)?;

        let mut cmd = Command::cargo_bin("yek")?;
        cmd.arg("--config-file")
            .arg(temp_dir.path().join("yek.toml"))
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_binary_files() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("image.jpg"), [0xFF, 0xD8, 0xFF])?; // Mock JPEG header

        let mut cmd = Command::cargo_bin("yek")?;
        cmd.arg(temp_dir.path()).assert().success();
        Ok(())
    }

    #[test]
    fn test_output_dir() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        let output_dir = temp_dir.path().join("output");

        // Create a simple Rust file
        fs::write(temp_dir.path().join("main.rs"), "fn main() {}")?;

        // Run Yek with output_dir
        let mut cmd = Command::cargo_bin("yek")?;
        let output = cmd
            .current_dir(temp_dir.path())
            .env("TERM", "xterm") // Ensure terminal mode
            .env("FORCE_TTY", "1")
            .arg(temp_dir.path())
            .arg("--output-dir")
            .arg(&output_dir)
            .arg("--debug")
            .output()?;

        assert!(output.status.success());

        // Ensure output dir is printed in stdout
        let stdout = String::from_utf8(output.stdout)?;
        assert!(
            stdout.contains(&output_dir.display().to_string()),
            "Expected output directory `{}` to be printed in stdout, but it was {}",
            output_dir.display(),
            stdout
        );

        // Ensure the directory exists
        assert!(
            output_dir.exists(),
            "Expected output directory `{}` to exist, but it does not",
            output_dir.display()
        );

        // Ensure at least one output file exists inside the directory
        let output_files = fs::read_dir(&output_dir)?
            .filter_map(|e| match e {
                Ok(entry) => Some(entry),
                Err(err) => {
                    eprintln!("Warning: Failed to read directory entry: {}", err);
                    None
                }
            })
            .collect::<Vec<_>>();

        assert!(
            !output_files.is_empty(),
            "Expected output directory `{}` to contain files, but it is empty",
            output_dir.display()
        );

        Ok(())
    }

    #[test]
    fn test_max_size() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("test.txt"), "Test content")?;

        Command::cargo_bin("yek")?
            .arg(temp_dir.path())
            .arg("--max-size")
            .arg("1KB")
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_tokens_mode() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("test.txt"), "Test content")?;

        let mut cmd = Command::cargo_bin("yek")?;
        cmd.arg(temp_dir.path())
            .arg("--tokens")
            .arg("100")
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_git_integration() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        // Initialize a Git repo
        std::process::Command::new("git")
            .args(["init"])
            .current_dir(temp_dir.path())
            .output()?;

        fs::write(temp_dir.path().join("test.txt"), "Test content")?;
        std::process::Command::new("git")
            .args(["add", "test.txt"])
            .current_dir(temp_dir.path())
            .output()?;
        std::process::Command::new("git")
            .args(["commit", "-m", "Initial commit"])
            .current_dir(temp_dir.path())
            .output()?;

        Command::cargo_bin("yek")?
            .arg(temp_dir.path())
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_multiple_input_dirs() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir1 = tempdir()?;
        let temp_dir2 = tempdir()?;
        fs::write(temp_dir1.path().join("test1.txt"), "Test content 1")?;
        fs::write(temp_dir2.path().join("test2.txt"), "Test content 2")?;

        Command::cargo_bin("yek")?
            .arg(temp_dir1.path())
            .arg(temp_dir2.path())
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_glob_pattern() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("test.txt"), "Test content")?;

        let output = Command::cargo_bin("yek")?
            .current_dir(temp_dir.path())
            .arg("*.txt")
            .output()?;
        let stdout = String::from_utf8(output.stdout)?;
        assert!(output.status.success());
        assert!(stdout.contains("Test content"));
        Ok(())
    }

    #[test]
    fn test_mix_of_files_and_dirs() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("test.txt"), "Test content")?;
        fs::write(temp_dir.path().join("test2.txt"), "Test content 2")?;
        let dir = temp_dir.path().join("dir");
        fs::create_dir(&dir)?;
        fs::write(dir.join("test3"), "Test content 3")?;

        Command::cargo_bin("yek")?
            .current_dir(temp_dir.path())
            .arg("*.txt")
            .assert()
            .success();

        let output = Command::cargo_bin("yek")?
            .current_dir(temp_dir.path())
            .arg("*.txt")
            .output()?;
        let stdout = String::from_utf8(output.stdout)?;
        assert!(stdout.contains("Test content"));
        assert!(stdout.contains("Test content 2"));
        assert!(!stdout.contains("Test content 3"));
        Ok(())
    }

    #[test]
    fn test_mix_of_files_and_dirs_with_glob_pattern() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("test.txt"), "Test content")?;
        fs::write(temp_dir.path().join("test2.txt"), "Test content 2")?;
        fs::write(temp_dir.path().join("code.rs"), "use std::fs;")?;
        let dir = temp_dir.path().join("dir");
        fs::create_dir(&dir)?;
        fs::write(dir.join("test4"), "Test content 4")?;

        Command::cargo_bin("yek")?
            .current_dir(temp_dir.path())
            .args(["*.txt", "code.rs"])
            .assert()
            .success();

        let output = Command::cargo_bin("yek")?
            .current_dir(temp_dir.path())
            .args(["*.txt", "code.rs"])
            .output()?;
        let stdout = String::from_utf8(output.stdout)?;
        assert!(stdout.contains("Test content"));
        assert!(stdout.contains("Test content 2"));
        assert!(!stdout.contains("Test content 4"));
        assert!(stdout.contains("use std::fs;"));
        Ok(())
    }

    #[test]
    fn test_config_file() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        let config_content = r#"
            max_size = "1KB"
            input_paths = ["."]
        "#;
        fs::write(temp_dir.path().join("yek.toml"), config_content)?;

        let mut cmd = Command::cargo_bin("yek")?;
        cmd.arg("--config-file")
            .arg(temp_dir.path().join("yek.toml"))
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_streaming_mode() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("test.rs"), "Test content")?;

        let mut cmd = Command::cargo_bin("yek")?;
        cmd.arg(temp_dir.path())
            .assert()
            .success()
            .stdout(predicate::str::contains("Test content"));
        Ok(())
    }

    #[test]
    fn test_gitignore_respected() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join(".gitignore"), "*.log")?;
        fs::write(temp_dir.path().join("test.log"), "Log content")?;
        fs::write(temp_dir.path().join("test.txt"), "Test content")?;

        Command::cargo_bin("yek")?
            .arg(temp_dir.path())
            .assert()
            .success();

        Ok(())
    }

    #[test]
    fn test_hidden_files_included() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join(".hidden.txt"), "Hidden content")?;

        Command::cargo_bin("yek")?
            .arg(temp_dir.path())
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_binary_file_extension_config() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("data.bin"), [0, 1, 2, 3])?;

        let config_content = r#"
            input_paths = ["."]
            binary_extensions = ["bin"]
        "#;
        fs::write(temp_dir.path().join("yek.toml"), config_content)?;

        Command::cargo_bin("yek")?
            .arg("--config-file")
            .arg(temp_dir.path().join("yek.toml"))
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_git_boost_config() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        let config_content = r#"
            input_paths = ["."]
            git_boost_max = 50
        "#;
        fs::write(temp_dir.path().join("yek.toml"), config_content)?;

        // Initialize a Git repo
        std::process::Command::new("git")
            .args(["init"])
            .current_dir(temp_dir.path())
            .output()?;

        fs::write(temp_dir.path().join("file.txt"), "content")?;
        std::process::Command::new("git")
            .args(["add", "file.txt"])
            .current_dir(temp_dir.path())
            .output()?;
        std::process::Command::new("git")
            .args(["commit", "-m", "Initial commit"])
            .current_dir(temp_dir.path())
            .output()?;

        let mut cmd = Command::cargo_bin("yek")?;
        cmd.arg("--config-file")
            .arg(temp_dir.path().join("yek.toml"))
            .assert()
            .success();
        Ok(())
    }

    #[test]
    fn test_default_ignore_license_no_config() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("LICENSE"), "License content")?;

        let mut cmd = Command::cargo_bin("yek")?;
        let output = cmd.arg(temp_dir.path()).output()?;

        // Assert that the command was successful
        assert!(output.status.success());

        // Convert stdout bytes to a string
        let stdout = String::from_utf8(output.stdout)?;

        // Assert that the output does not contain "License content"
        assert!(
            !stdout.contains("License content"),
            "Output should not contain 'License content'"
        );

        Ok(())
    }

    #[test]
    fn test_default_ignore_license_empty_config() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("LICENSE"), "License content")?;
        fs::write(
            temp_dir.path().join("yek.yaml"),
            "ignore_patterns: []\n", // Empty ignore_patterns
        )?;

        let mut cmd = Command::cargo_bin("yek")?;
        let output = cmd
            .arg("--config-file")
            .arg(temp_dir.path().join("yek.yaml"))
            .arg(temp_dir.path())
            .output()?;

        assert!(output.status.success());
        let stdout = String::from_utf8(output.stdout)?;
        assert!(
            !stdout.contains("License content"),
            "Output should not contain 'License content' even with empty config"
        );

        Ok(())
    }

    #[test]
    fn test_gitignore_allowlist() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("LICENSE"), "License content")?;
        fs::write(temp_dir.path().join(".gitignore"), "!LICENSE\n")?;

        let mut cmd = Command::cargo_bin("yek")?;
        let output = cmd.arg(temp_dir.path()).output()?;

        assert!(output.status.success());
        let stdout = String::from_utf8(output.stdout)?;

        assert!(
            stdout.contains("License content"),
            "Output should contain 'License content' because .gitignore allowlists it"
        );

        Ok(())
    }

    #[test]
    fn test_windows_path_normalization() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("LICENSE"), "License content")?;
        // TODO:
        // Use a path with mixed slashes to simulate potential Windows issues
        // let windows_path = format!(
        //     "{}\\LICENSE",
        //     temp_dir.path().to_string_lossy().replace("/", "\\")
        // );

        let mut cmd = Command::cargo_bin("yek")?;
        let output = cmd.arg(temp_dir.path()).output()?;

        assert!(output.status.success());
        let stdout = String::from_utf8(output.stdout)?;

        assert!(
            !stdout.contains("License content"),
            "Output should not contain 'License content' even with Windows-style paths"
        );

        Ok(())
    }
}