strigil 0.2.0

A minimal, dependency-free clone of grep, written in Rust.
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
//! Integration tests that spawn the compiled `strigil` binary.

use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, Output, Stdio};

const THREE_LINES: &str = "The quick brown fox\njumps over the lazy dog\nthe end\n";

fn temp_file() -> PathBuf {
    // Thread id keeps parallel tests from racing on a shared fixture file.
    let path = std::env::temp_dir().join(format!(
        "strigil_cli_test_{}_{:?}.txt",
        std::process::id(),
        std::thread::current().id()
    ));
    fs::write(&path, THREE_LINES).expect("writes a temp test file");
    path
}

fn run(args: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_strigil"))
        .args(args)
        .output()
        .expect("spawns strigil")
}

/// Runs strigil with the given arguments and a piped standard input.
fn run_with_stdin(args: &[&str], input: &str) -> Output {
    let mut child = Command::new(env!("CARGO_BIN_EXE_strigil"))
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawns strigil");
    child
        .stdin
        .take()
        .expect("stdin is piped")
        .write_all(input.as_bytes())
        .expect("writes test input");
    child.wait_with_output().expect("waits for strigil")
}

fn exit_code(output: &Output) -> i32 {
    output.status.code().expect("process exits normally")
}

#[test]
fn prints_matching_lines_with_numbers_and_exits_zero() {
    let file = temp_file();
    let out = run(&["fox", file.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("1:The quick brown fox"));
    assert!(!stdout.contains("2:"));
}

#[test]
fn exits_one_when_no_lines_match() {
    let file = temp_file();
    let out = run(&["zebra", file.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 1);
    assert!(out.stdout.is_empty());
}

#[test]
fn matching_is_case_sensitive_by_default() {
    let file = temp_file();
    let out = run(&["the", file.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    assert!(String::from_utf8_lossy(&out.stdout).contains("3:the end"));
}

#[test]
fn ignore_case_flag_matches_case_insensitively() {
    let file = temp_file();
    let out = run(&["THE", file.to_str().unwrap(), "--ignore-case"]);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("1:The quick brown fox"));
    assert!(stdout.contains("3:the end"));
}

#[test]
fn ignore_case_flag_accepted_before_the_positionals() {
    let file = temp_file();
    let out = run(&["--ignore-case", "THE", file.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("1:The quick brown fox"));
    assert!(stdout.contains("3:the end"));
}

#[test]
fn ignore_case_flag_accepted_between_the_positionals() {
    let file = temp_file();
    let out = run(&["THE", "--ignore-case", file.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    assert!(String::from_utf8_lossy(&out.stdout).contains("1:The quick brown fox"));
}

#[test]
fn multiple_files_are_searched_with_a_filename_prefix() {
    let dir = std::env::temp_dir().join(format!("strigil_cli_multi_{}", std::process::id()));
    fs::create_dir_all(&dir).expect("creates a temp directory");
    let a = dir.join("a.txt");
    let b = dir.join("b.txt");
    fs::write(&a, THREE_LINES).expect("writes a.txt");
    fs::write(&b, "nothing here\n").expect("writes b.txt");
    let out = run(&["fox", a.to_str().unwrap(), b.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("a.txt:1:The quick brown fox"),
        "stdout was: {stdout}"
    );
    assert!(!stdout.contains("b.txt:"), "stdout was: {stdout}");
}

#[test]
fn a_missing_file_among_others_does_not_hide_matches() {
    let file = temp_file();
    let out = run(&["fox", file.to_str().unwrap(), "definitely-missing.txt"]);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("1:The quick brown fox"),
        "stdout was: {stdout}"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("definitely-missing.txt"),
        "stderr was: {stderr}"
    );
}

#[test]
fn every_file_missing_exits_three() {
    let out = run(&["fox", "missing-a.txt", "missing-b.txt"]);
    assert_eq!(exit_code(&out), 3);
}

#[test]
fn count_with_multiple_files_prefixes_each_count() {
    let dir = std::env::temp_dir().join(format!("strigil_cli_count_{}", std::process::id()));
    fs::create_dir_all(&dir).expect("creates a temp directory");
    let a = dir.join("a.txt");
    let b = dir.join("b.txt");
    fs::write(&a, THREE_LINES).expect("writes a.txt");
    fs::write(&b, "nothing here\n").expect("writes b.txt");
    let out = run(&["-c", "fox", a.to_str().unwrap(), b.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("a.txt:1"), "stdout was: {stdout}");
    assert!(stdout.contains("b.txt:0"), "stdout was: {stdout}");
}

#[test]
fn recursive_flag_searches_an_entire_directory_tree() {
    let dir = std::env::temp_dir().join(format!("strigil_cli_tree_{}", std::process::id()));
    let nested = dir.join("sub");
    fs::create_dir_all(&nested).expect("creates a temp tree");
    fs::write(nested.join("nested.txt"), THREE_LINES).expect("writes nested.txt");
    let out = run(&["-r", "fox", dir.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("nested.txt:1:The quick brown fox"),
        "stdout was: {stdout}"
    );
}

#[test]
fn double_dash_allows_filenames_starting_with_a_dash() {
    let dir = std::env::temp_dir().join(format!("strigil_cli_dash_{}", std::process::id()));
    fs::create_dir_all(&dir).expect("creates a temp directory");
    let dashed = dir.join("-dashed.txt");
    fs::write(&dashed, THREE_LINES).expect("writes -dashed.txt");
    let out = run(&["fox", "--", dashed.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    assert!(String::from_utf8_lossy(&out.stdout).contains("1:The quick brown fox"));
}

#[test]
fn directory_without_recursive_is_an_io_error() {
    let dir = std::env::temp_dir().join(format!("strigil_cli_dir_{}", std::process::id()));
    fs::create_dir_all(&dir).expect("creates a temp directory");
    let out = run(&["fox", dir.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 3);
}

#[test]
fn missing_file_is_an_io_error_exit_three() {
    let out = run(&["fox", "definitely-not-a-real-file.txt"]);
    assert_eq!(exit_code(&out), 3);
}

#[test]
fn wrong_argument_count_is_a_usage_error_exit_two() {
    let out = run(&[]);
    assert_eq!(exit_code(&out), 2);
    assert!(String::from_utf8_lossy(&out.stderr).contains("Usage:"));
}

#[test]
fn unknown_third_flag_is_a_usage_error_exit_two() {
    let file = temp_file();
    let out = run(&["fox", file.to_str().unwrap(), "--wat"]);
    assert_eq!(exit_code(&out), 2);
}

#[test]
fn color_always_highlights_the_first_match_in_ansi_red() {
    let file = temp_file();
    let out = Command::new(env!("CARGO_BIN_EXE_strigil"))
        .args(["fox", file.to_str().unwrap()])
        .env("COLOR", "always")
        .output()
        .expect("spawns strigil");
    assert_eq!(exit_code(&out), 0);
    assert!(String::from_utf8_lossy(&out.stdout).contains("\x1b[31mfox\x1b[0m"));
}

#[test]
fn color_never_disables_highlighting() {
    let file = temp_file();
    let out = Command::new(env!("CARGO_BIN_EXE_strigil"))
        .args(["fox", file.to_str().unwrap()])
        .env("COLOR", "never")
        .output()
        .expect("spawns strigil");
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("1:The quick brown fox"));
    assert!(!stdout.contains('\u{1b}'));
}

#[test]
fn no_color_overrides_color_always() {
    let file = temp_file();
    let out = Command::new(env!("CARGO_BIN_EXE_strigil"))
        .args(["fox", file.to_str().unwrap()])
        .env("COLOR", "always")
        .env("NO_COLOR", "1")
        .output()
        .expect("spawns strigil");
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("1:The quick brown fox"));
    assert!(!stdout.contains('\u{1b}'));
}

#[test]
fn plain_output_contains_no_ansi_escape_codes() {
    let file = temp_file();
    let out = run(&["fox", file.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    assert!(!String::from_utf8_lossy(&out.stdout).contains('\u{1b}'));
}

#[test]
fn reads_pattern_from_standard_input_when_no_file_is_given() {
    let out = run_with_stdin(&["fox"], THREE_LINES);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("1:The quick brown fox"));
    assert!(!stdout.contains("2:"));
}

#[test]
fn dash_as_the_file_reads_standard_input() {
    let out = run_with_stdin(&["fox", "-"], THREE_LINES);
    assert_eq!(exit_code(&out), 0);
    assert!(String::from_utf8_lossy(&out.stdout).contains("1:The quick brown fox"));
}

#[test]
fn empty_standard_input_exits_zero() {
    let out = run_with_stdin(&["fox"], "");
    assert_eq!(exit_code(&out), 0);
    assert!(out.stdout.is_empty());
}

#[test]
fn help_flag_prints_usage_and_exits_zero() {
    let out = run(&["--help"]);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("Usage: strigil <pattern> [<file>]"));
    assert!(stdout.contains("--ignore-case"));

    // --help wins even when positionals are present.
    let out = run(&["fox", "ignored.txt", "--help"]);
    assert_eq!(exit_code(&out), 0);
}

#[test]
fn version_flag_prints_the_version_and_exits_zero() {
    let out = run(&["--version"]);
    assert_eq!(exit_code(&out), 0);
    assert_eq!(
        String::from_utf8_lossy(&out.stdout).trim(),
        format!("strigil {}", env!("CARGO_PKG_VERSION"))
    );
}

#[test]
fn short_i_flag_aliases_ignore_case() {
    let file = temp_file();
    let out = run(&["THE", file.to_str().unwrap(), "-i"]);
    assert_eq!(exit_code(&out), 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("1:The quick brown fox"));
    assert!(stdout.contains("3:the end"));
}

#[test]
fn short_i_flag_accepted_before_the_positionals() {
    let file = temp_file();
    let out = run(&["-i", "THE", file.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    assert!(String::from_utf8_lossy(&out.stdout).contains("1:The quick brown fox"));
}

#[test]
fn capital_v_flag_prints_the_version() {
    let out = run(&["-V"]);
    assert_eq!(exit_code(&out), 0);
    assert_eq!(
        String::from_utf8_lossy(&out.stdout).trim(),
        format!("strigil {}", env!("CARGO_PKG_VERSION"))
    );
}

#[test]
fn count_flag_prints_only_the_number_of_matching_lines() {
    let file = temp_file();
    let out = run(&["the", file.to_str().unwrap(), "-c"]);
    assert_eq!(exit_code(&out), 0);
    assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "2");
}

#[test]
fn count_flag_combines_with_ignore_case() {
    let file = temp_file();
    let out = run(&["-i", "the", "-c", file.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "3");
}

#[test]
fn count_on_empty_input_prints_zero_and_exits_zero() {
    let out = run_with_stdin(&["fox", "-c"], "");
    assert_eq!(exit_code(&out), 0);
    assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "0");
}

#[test]
fn invert_flag_prints_only_non_matching_lines() {
    let file = temp_file();
    let out = run(&["the", file.to_str().unwrap(), "-v"]);
    assert_eq!(exit_code(&out), 0);
    // Only line 1 ("The quick brown fox") lacks a lowercase "the".
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("1:The quick brown fox"));
    assert!(!stdout.contains("2:"));
    assert!(!stdout.contains("3:"));
}

#[test]
fn invert_flag_exits_one_when_every_line_matches() {
    let file = temp_file();
    let out = run(&["e", file.to_str().unwrap(), "-v"]);
    assert_eq!(exit_code(&out), 1);
    assert!(out.stdout.is_empty());
}

#[test]
fn count_and_invert_combine() {
    let file = temp_file();
    let out = run(&["-v", "-c", "e", file.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 1); // every line contains 'e', so none inverted
    assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "0");
}

#[test]
fn binary_input_with_a_match_prints_binary_file_matches() {
    let path = std::env::temp_dir().join(format!(
        "strigil_cli_test_{}_{:?}.bin",
        std::process::id(),
        std::thread::current().id()
    ));
    fs::write(&path, b"hello\x00world\n").expect("writes a binary temp file");
    let out = run(&["hello", path.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 0);
    assert!(String::from_utf8_lossy(&out.stdout).contains("binary file matches"));
}

#[test]
fn binary_input_without_a_match_exits_one() {
    let path = std::env::temp_dir().join(format!(
        "strigil_cli_test_{}_{:?}_nope.bin",
        std::process::id(),
        std::thread::current().id()
    ));
    fs::write(&path, b"hello\x00world\n").expect("writes a binary temp file");
    let out = run(&["zebra", path.to_str().unwrap()]);
    assert_eq!(exit_code(&out), 1);
    assert!(out.stdout.is_empty());
}