lx-ls 0.6.1

The file lister with personality! 🌟
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
//! Tests for the [theme] config section and --theme flag.

mod support;

use std::fs;
use predicates::prelude::*;
use support::lx_no_colour;
use tempfile::tempdir;


/// Helper: run lx with colour enabled and a given config.
fn lx_with_theme(config_content: &str) -> (tempfile::TempDir, assert_cmd::Command) {
    let dir = tempdir().expect("failed to create tempdir");
    let config_path = dir.path().join("config.toml");
    fs::write(&config_path, config_content).unwrap();

    let mut cmd = assert_cmd::Command::cargo_bin("lx").expect("binary lx not found");
    cmd.env("LX_CONFIG", config_path)
       .env("HOME", "/nonexistent")
       .env_remove("LS_COLORS")
       .env_remove("LX_COLORS")
       .arg("--colour=always");
    (dir, cmd)
}


// ── UI element overrides ─────────────────────────────────────────

#[test]
fn theme_directory_colour() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.test]
        directory = "bold red"
    "#);

    let work = tempdir().expect("failed to create workdir");
    fs::create_dir(work.path().join("mydir")).unwrap();

    cmd.args(["--theme=test", "-1"])
        .arg(work.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;31m"));
}

#[test]
fn theme_date_colour() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.test]
        date = "bold cyan"
    "#);

    cmd.args(["--theme=test", "-l", "Cargo.toml"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;36m"));
}

#[test]
fn theme_x11_colour() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.test]
        date = "tomato"
    "#);

    cmd.args(["--theme=test", "-l", "Cargo.toml"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[38;2;255;99;71m"));
}

#[test]
fn theme_hex_colour() {
    let (_dir, mut cmd) = lx_with_theme(
        "version = \"0.3\"\n[theme.test]\ndate = \"#ff8700\"\n"
    );

    cmd.args(["--theme=test", "-l", "Cargo.toml"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[38;2;255;135;0m"));
}


// ── Style set overrides ─────────────────────────────────────────

#[test]
fn theme_extension_colour() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.test]
        use-style = "myexts"
        [style.myexts]
        "*.txt" = "bold magenta"
    "#);

    let work = tempdir().expect("failed to create workdir");
    fs::write(work.path().join("readme.txt"), "").unwrap();

    cmd.args(["--theme=test", "-1"])
        .arg(work.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;35m"));
}

#[test]
fn theme_filename_colour() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.test]
        use-style = "mynames"
        [style.mynames]
        Makefile = "bold underline yellow"
    "#);

    let work = tempdir().expect("failed to create workdir");
    fs::write(work.path().join("Makefile"), "").unwrap();

    cmd.args(["--theme=test", "-1"])
        .arg(work.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;4;33m"));
}


// ── Personality integration ──────────────────────────────────────

#[test]
fn theme_via_personality() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [personality.lx]
        theme = "ocean"

        [theme.ocean]
        date = "bold cyan"
    "#);

    // The lx personality should activate the ocean theme.
    cmd.args(["-l", "Cargo.toml"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;36m"));
}

#[test]
fn theme_inherited_through_personality() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [personality.default]
        theme = "ocean"

        [personality.myview]
        inherits = "default"
        format = "long"

        [theme.ocean]
        date = "bold cyan"
    "#);

    cmd.args(["-pmyview", "Cargo.toml"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;36m"));
}

#[test]
fn theme_cli_overrides_personality() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [personality.lx]
        theme = "ocean"

        [theme.ocean]
        date = "bold cyan"

        [theme.warm]
        date = "bold red"
    "#);

    // --theme=warm should override the personality's theme = "ocean"
    cmd.args(["--theme=warm", "-l", "Cargo.toml"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;31m"));
}


// ── Precedence over env vars ─────────────────────────────────────

#[test]
fn theme_overrides_env() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.test]
        date = "bold red"
    "#);

    cmd.env("LX_COLORS", "da=32")
        .args(["--theme=test", "-l", "Cargo.toml"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;31m"));
}


// ── Class references in styles ────────────────────────────────────

#[test]
fn style_class_reference() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"

        [class]
        testclass = ["*.xyz"]

        [theme.test]
        inherits = "exa"
        use-style = "mystyle"

        [style.mystyle]
        class.testclass = "bold magenta"
    "#);

    let work = tempdir().expect("failed to create workdir");
    fs::write(work.path().join("data.xyz"), "").unwrap();

    // Bold magenta = 1;35
    cmd.args(["--theme=test", "-1"])
        .arg(work.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;35m"));
}

#[test]
fn style_class_overrides_exa_default() {
    // User style with a class reference should override the
    // compiled-in exa style for the same files.
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"

        [theme.test]
        inherits = "exa"
        use-style = "custom"

        [style.custom]
        class.compressed = "bold cyan"
    "#);

    let work = tempdir().expect("failed to create workdir");
    fs::write(work.path().join("archive.zip"), "").unwrap();

    // Bold cyan = 1;36 (not red, which is the exa default)
    cmd.args(["--theme=test", "-1"])
        .arg(work.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;36m"));
}

#[test]
fn style_quoted_pattern_and_class() {
    // A style can mix class references and quoted file patterns.
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"

        [class]
        data = ["*.csv"]

        [theme.test]
        inherits = "exa"
        use-style = "mixed"

        [style.mixed]
        class.data = "bold green"
        "Makefile" = "bold red"
    "#);

    let work = tempdir().expect("failed to create workdir");
    fs::write(work.path().join("results.csv"), "").unwrap();
    fs::write(work.path().join("Makefile"), "").unwrap();

    // Both should be coloured
    cmd.args(["--theme=test", "-1"])
        .arg(work.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;32m"))   // bold green
        .stdout(predicate::str::contains("\x1b[1;31m"));   // bold red
}

#[test]
fn user_class_overrides_compiled_in() {
    // A user-defined [class] entry overrides the compiled-in one.
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"

        [class]
        compressed = ["*.myarc"]

        [theme.test]
        inherits = "exa"
        use-style = "exa"
    "#);

    let work = tempdir().expect("failed to create workdir");
    fs::write(work.path().join("data.myarc"), "").unwrap();
    // .zip should NOT match compressed anymore (user redefined it)
    fs::write(work.path().join("stuff.zip"), "").unwrap();

    // .myarc gets the exa compressed colour (red = 31)
    cmd.args(["--theme=test", "-1"])
        .arg(work.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[31m"));
}


// ── Theme inheritance ────────────────────────────────────────────

#[test]
fn theme_inherits_exa() {
    // A theme inheriting from "exa" should get the compiled-in
    // defaults, then override specific keys.
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.custom]
        inherits = "exa"
        date = "bold red"
    "#);

    // date is bold red (overridden), but directory should still be
    // bold blue (inherited from exa).
    cmd.args(["--theme=custom", "-l", "src"])
        .assert()
        .success()
        // Bold blue directory (from exa): 1;34
        .stdout(predicate::str::contains("\x1b[1;34m"))
        // Bold red date (from custom): 1;31
        .stdout(predicate::str::contains("\x1b[1;31m"));
}

#[test]
fn theme_without_inherits_is_blank() {
    // A theme without inherits starts from plain — only its own
    // keys apply.
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.bare]
        date = "bold red"
    "#);

    // date is bold red, but directory should NOT be bold blue
    // (no exa defaults).
    cmd.args(["--theme=bare", "-l", "src"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;31m"))
        // No bold blue (1;34m) — directories are unstyled.
        .stdout(predicate::str::contains("\x1b[1;34m").not());
}

#[test]
fn theme_inherits_custom() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.base]
        inherits = "exa"
        date = "bold cyan"

        [theme.child]
        inherits = "base"
        directory = "bold red"
    "#);

    // child: directory=bold red, date=bold cyan (from base),
    // everything else from exa.
    cmd.args(["--theme=child", "-l", "src"])
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[1;31m"))   // bold red dir
        .stdout(predicate::str::contains("\x1b[1;36m"));   // bold cyan date
}

#[test]
fn theme_inheritance_cycle_detected() {
    let (_dir, mut cmd) = lx_with_theme(r#"
        version = "0.3"
        [theme.a]
        inherits = "b"
        [theme.b]
        inherits = "a"
    "#);

    // Should still work (warning emitted), just no theme applied.
    cmd.args(["--theme=a", "-1", "Cargo.toml"])
        .assert()
        .success();
}


// ── No theme is fine ─────────────────────────────────────────────

// ── Default theme smoke tests ────────────────────────────────────

#[test]
fn no_theme_works() {
    lx_no_colour()
        .args(["-1", "Cargo.toml"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Cargo.toml"));
}

#[test]
fn default_theme_produces_colour() {
    // Without any config, the compiled-in exa theme (via the
    // default personality) should produce coloured output.
    // Bold blue (1;34) for directories is the exa default.
    let mut cmd = assert_cmd::Command::cargo_bin("lx").expect("binary lx not found");
    cmd.env("LX_CONFIG", "/nonexistent")
       .env("HOME", "/nonexistent")
       .env_remove("LS_COLORS")
       .env_remove("LX_COLORS")
       .arg("--colour=always")
       .args(["-l", "src"])
       .assert()
       .success()
       // Bold blue directory (from exa theme): 1;34
       .stdout(predicate::str::contains("\x1b[1;34m"))
       // Blue date (from exa theme): 34
       .stdout(predicate::str::contains("\x1b[34m"));
}

#[test]
fn default_theme_colours_filetypes() {
    // The compiled-in exa style should colour compressed files red.
    let mut cmd = assert_cmd::Command::cargo_bin("lx").expect("binary lx not found");
    cmd.env("LX_CONFIG", "/nonexistent")
       .env("HOME", "/nonexistent")
       .env_remove("LS_COLORS")
       .env_remove("LX_COLORS")
       .arg("--colour=always");

    let work = tempdir().expect("failed to create workdir");
    fs::write(work.path().join("archive.zip"), "").unwrap();

    // Red (31) for compressed files (from exa style).
    cmd.args(["-1"])
        .arg(work.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("\x1b[31m"));
}

#[test]
fn init_config_preserves_default_colours() {
    // After --init-config, output should look identical to no-config.
    // This is design invariant #2.
    let dir = tempdir().expect("failed to create tempdir");

    // Generate config.
    let mut init = assert_cmd::Command::cargo_bin("lx").expect("binary lx not found");
    init.args(["--init-config"])
        .env("HOME", dir.path())
        .env("LX_CONFIG", "/nonexistent")
        .assert()
        .success();

    let config_path = dir.path().join(".lxconfig.toml");
    assert!(config_path.exists());

    // Run with generated config — should have bold blue directories.
    let mut cmd = assert_cmd::Command::cargo_bin("lx").expect("binary lx not found");
    cmd.env("LX_CONFIG", &config_path)
       .env("HOME", dir.path())
       .env_remove("LS_COLORS")
       .env_remove("LX_COLORS")
       .arg("--colour=always")
       .args(["-l", "src"])
       .assert()
       .success()
       .stdout(predicate::str::contains("\x1b[1;34m"));
}