quickenv 0.5.0

An unintrusive environment manager
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
use std::fs::{create_dir_all, write};

use anyhow::Error;
use which::which;

mod acceptance_helpers;
use acceptance_helpers::{assert_cmd, set_executable, setup};

#[test]
fn test_basic() -> Result<(), Error> {
    let harness = setup()?;
    write(harness.join(".envrc"), "export PATH=bogus:$PATH\n")?;
    create_dir_all(harness.join("bogus"))?;
    write(harness.join("bogus/hello"), "#!/bin/sh\necho hello world")?;
    set_executable(harness.join("bogus/hello"))?;

    assert_cmd!(harness, quickenv "reload",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] 1 unshimmed commands (1 new). Use 'quickenv shim' to make them available.
    Set QUICKENV_NO_SHIM_WARNINGS=1 to silence this message.
    "###);
    harness.which("hello").unwrap_err();
    assert_cmd!(harness, quickenv "shim" "hello",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);
    harness.which("hello")?;
    assert_cmd!(harness, hello,  @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    hello world

    ----- stderr -----
    "###);
    assert_cmd!(harness, quickenv "unshim" "hello",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Removed 1 shims from [scrubbed $HOME]/.quickenv/bin/.
    Use 'quickenv shim <command>' to add them again
    "###);
    which("hello").unwrap_err();

    assert_cmd!(harness, quickenv "reload",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] 1 unshimmed commands. Use 'quickenv shim' to make them available.
    Set QUICKENV_NO_SHIM_WARNINGS=1 to silence this message.
    "###);
    Ok(())
}

#[test]
fn test_shadowed() -> Result<(), Error> {
    let mut harness = setup()?;
    harness.prepend_path(harness.join("bogus"));
    create_dir_all(harness.join("bogus"))?;
    write(harness.join("bogus/hello"), "#!/bin/sh\necho hello world")?;
    set_executable(harness.join("bogus/hello"))?;
    assert_cmd!(harness, hello,  @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    hello world

    ----- stderr -----
    "###);
    assert_cmd!(harness, quickenv "shim" "hello",  @r###"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    [ERROR quickenv] [scrubbed $HOME]/.quickenv/bin/hello is shadowed by an executable of the same name at [scrubbed $HOME]/project/bogus/hello
    "###);
    Ok(())
}

#[test]
fn test_shadowing() -> Result<(), Error> {
    let harness = setup()?;
    assert_cmd!(harness, quickenv "shim" "true",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);
    Ok(())
}

#[test]
fn test_shim_self() -> Result<(), Error> {
    let harness = setup()?;
    assert_cmd!(harness, quickenv "unshim" "quickenv",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] not unshimming own binary
    Removed 0 shims from [scrubbed $HOME]/.quickenv/bin/.
    Use 'quickenv shim <command>' to add them again
    "###);
    assert_cmd!(harness, quickenv "shim" "quickenv",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] not shimming own binary
    created no new shims.
    "###);
    assert_cmd!(harness, quickenv "unshim" "quickenv",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] not unshimming own binary
    Removed 0 shims from [scrubbed $HOME]/.quickenv/bin/.
    Use 'quickenv shim <command>' to add them again
    "###);
    Ok(())
}

#[test]
fn test_verbosity() -> Result<(), Error> {
    let mut harness = setup()?;
    assert_cmd!(harness, quickenv "vars",  @r###"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    [ERROR quickenv] failed to find .envrc in current or any parent directory
    "###);
    harness.set_var("QUICKENV_LOG", "debug");
    assert_cmd!(harness, quickenv "vars",  @r###"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    [DEBUG quickenv] argv[0] is "[scrubbed $HOME]/.quickenv/quickenv_bin/quickenv"
    [DEBUG quickenv] own program name is quickenv, so no shim running
    [ERROR quickenv] failed to find .envrc in current or any parent directory
    "###);
    Ok(())
}

#[test]
fn test_vars_quiet() -> Result<(), Error> {
    let harness = setup()?;
    assert_cmd!(harness, quickenv "vars" "-q",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    "###);
    assert_cmd!(harness, quickenv "vars" "--quiet",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    "###);
    Ok(())
}

#[test]
fn test_script_failure() -> Result<(), Error> {
    let harness = setup()?;
    write(harness.join(".envrc"), "exit 1")?;
    assert_cmd!(harness, quickenv "reload",  @r###"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    [ERROR quickenv] .envrc exited with status exit status: 1
    "###);
    Ok(())
}

#[test]
fn test_eating_own_tail() -> Result<(), Error> {
    let harness = setup()?;
    assert_cmd!(harness, quickenv "shim" "bash",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);
    write(
        harness.join(".envrc"),
        "bash -c 'echo hello world'; export PATH=bogus:$PATH",
    )?;
    assert_cmd!(harness, quickenv "reload",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    hello world

    ----- stderr -----
    "###);
    create_dir_all(harness.join("bogus"))?;
    write(harness.join("bogus/hello"), "#!/bin/sh\necho hello world")?;
    set_executable(harness.join("bogus/hello"))?;
    assert_cmd!(harness, quickenv "shim" "hello",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);
    assert_cmd!(harness, hello,  @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    hello world

    ----- stderr -----
    "###);
    Ok(())
}

#[test]
fn test_eating_own_tail2() -> Result<(), Error> {
    let harness = setup()?;
    assert_cmd!(harness, quickenv "shim" "bash",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);
    write(
        harness.join(".envrc"),
        "echo the value is $MYVALUE\nexport MYVALUE=canary",
    )?;
    assert_cmd!(harness, quickenv "reload",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    the value is

    ----- stderr -----
    "###);
    // assert that during reloading, we're not shimming bash and accidentally sourcing the old
    // envvar values. canary should not appear during reload.
    assert_cmd!(harness, quickenv "reload",  @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    the value is

    ----- stderr -----
    "###);
    Ok(())
}

#[test]
fn test_exec() -> Result<(), Error> {
    let harness = setup()?;

    write(harness.join(".envrc"), "export PATH=bogus:$PATH\n")?;
    create_dir_all(harness.join("bogus"))?;
    write(harness.join("bogus/hello"), "#!/bin/sh\necho hello world")?;
    set_executable(harness.join("bogus/hello"))?;

    assert_cmd!(harness, quickenv "reload", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] 1 unshimmed commands (1 new). Use 'quickenv shim' to make them available.
    Set QUICKENV_NO_SHIM_WARNINGS=1 to silence this message.
    "###);

    harness.which("hello").unwrap_err();
    assert_cmd!(harness, quickenv "exec" "hello", @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    hello world

    ----- stderr -----
    "###);
    Ok(())
}

#[test]
fn test_shim_creating_shims() -> Result<(), Error> {
    let harness = setup()?;

    write(harness.join(".envrc"), "export PATH=bogus:$PATH\n")?;
    assert_cmd!(harness, quickenv "reload", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    "###);

    create_dir_all(harness.join("bogus"))?;
    write(harness.join("bogus/hello"), "#!/bin/sh\necho hello world")?;
    set_executable(harness.join("bogus/hello"))?;

    // there is a command. it does not create more commands. quickenv should not amend any output
    assert_cmd!(harness, quickenv "exec" "hello", @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    hello world

    ----- stderr -----
    "###);

    // shimming the command should work
    assert_cmd!(harness, quickenv "shim" "--yes", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Found these unshimmed commands in your .envrc:

    hello

    Quickenv will create this new shim binary in [scrubbed $HOME]/.quickenv/bin/.
    Inside of [scrubbed $HOME]/project, those commands will run with .envrc enabled.
    Outside, they will run normally.
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    Use 'quickenv shim <command>' to run additional commands with .envrc enabled.
    "###);

    // change the command such that it creates another command, and run it
    write(
        harness.join("bogus/hello"),
        "#!/bin/sh\necho 'echo hello world' > bogus/hello2 && chmod +x bogus/hello2",
    )?;
    set_executable(harness.join("bogus/hello"))?;

    // quickenv should warn that more commands need shimming now
    assert_cmd!(harness, quickenv "exec" "hello", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] 1 unshimmed commands (1 new). Use 'quickenv shim' to make them available.
    Set QUICKENV_NO_SHIM_WARNINGS=1 to silence this message.
    "###);

    // quickenv shim should find the new command
    assert_cmd!(harness, quickenv "shim" "--yes", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Found these unshimmed commands in your .envrc:

    hello2

    Quickenv will create this new shim binary in [scrubbed $HOME]/.quickenv/bin/.
    Inside of [scrubbed $HOME]/project, those commands will run with .envrc enabled.
    Outside, they will run normally.
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    Use 'quickenv shim <command>' to run additional commands with .envrc enabled.
    "###);

    Ok(())
}

#[test]
fn test_auto_shimming() -> Result<(), Error> {
    let harness = setup()?;

    write(harness.join(".envrc"), "export PATH=bogus:$PATH\n")?;
    create_dir_all(harness.join("bogus"))?;
    write(harness.join("bogus/hello"), "#!/bin/sh\necho hello world")?;
    set_executable(harness.join("bogus/hello"))?;

    assert_cmd!(harness, quickenv "reload", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] 1 unshimmed commands (1 new). Use 'quickenv shim' to make them available.
    Set QUICKENV_NO_SHIM_WARNINGS=1 to silence this message.
    "###);

    assert_cmd!(harness, quickenv "shim" "-y", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Found these unshimmed commands in your .envrc:

    hello

    Quickenv will create this new shim binary in [scrubbed $HOME]/.quickenv/bin/.
    Inside of [scrubbed $HOME]/project, those commands will run with .envrc enabled.
    Outside, they will run normally.
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    Use 'quickenv shim <command>' to run additional commands with .envrc enabled.
    "###);

    assert_cmd!(harness, quickenv "shim" "-y", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    created no new shims.
    Use 'quickenv shim <command>' to run additional commands with .envrc enabled.
    "###);

    Ok(())
}

#[test]
fn test_no_envrc_context() -> Result<(), Error> {
    let harness = setup()?;
    assert_cmd!(harness, quickenv "shim" "echo", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);
    assert_cmd!(harness, echo "hello world", @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    hello world

    ----- stderr -----
    "###);
    Ok(())
}

#[test]
fn test_eating_own_tail3() -> Result<(), Error> {
    // regression: we removed our own PATH from the PATH envvar, but:
    // 1) the path is actually duplicated, so we didn't remove all copies, and we recurse into
    //    the shim
    // 2) the shim re-adds the PATH through its envrc cache
    let mut harness = setup()?;
    harness.prepend_path(std::fs::canonicalize(harness.join("../.quickenv/bin")).unwrap());
    write(harness.join(".envrc"), "export PATH=hello:$PATH:")?;
    assert_cmd!(harness, quickenv "reload", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    "###);
    assert_cmd!(harness, quickenv "shim" "hello", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);
    harness.set_var("QUICKENV_LOG", "debug");
    assert_cmd!(harness, hello, @r###"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    [DEBUG quickenv] argv[0] is "[scrubbed $HOME]/.quickenv/bin/hello"
    [DEBUG quickenv] attempting to launch shim for "[scrubbed $HOME]/.quickenv/bin/hello"
    [DEBUG quickenv] loading [scrubbed $HOME]/project/.envrc
    [DEBUG quickenv] removing own entry from PATH: [scrubbed $HOME]/.quickenv/bin
    [DEBUG quickenv] removing own entry from PATH: [scrubbed $HOME]/.quickenv/bin
    [ERROR quickenv] failed to run shimmed command

    Caused by:
        0: failed to run hello
        1: failed to find actual binary
        2: failed to find hello
        3: cannot find binary path
    "###);
    Ok(())
}

#[test]
fn test_which() -> Result<(), Error> {
    let harness = setup()?;

    write(harness.join(".envrc"), "export PATH=bogus:$PATH\n")?;
    create_dir_all(harness.join("bogus"))?;
    write(harness.join("bogus/hello"), "#!/bin/sh\necho hello world")?;
    set_executable(harness.join("bogus/hello"))?;

    assert_cmd!(harness, quickenv "reload", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] 1 unshimmed commands (1 new). Use 'quickenv shim' to make them available.
    Set QUICKENV_NO_SHIM_WARNINGS=1 to silence this message.
    "###);
    assert_cmd!(harness, quickenv "which" "hello", @r###"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    [ERROR quickenv] cannot find binary path
    "###);
    assert_cmd!(harness, quickenv "shim" "hello", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);
    assert_cmd!(harness, quickenv "which" "hello", @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    bogus/hello

    ----- stderr -----
    "###);
    Ok(())
}

#[test]
fn test_which_pretend_shimmed() -> Result<(), Error> {
    let harness = setup()?;

    write(harness.join(".envrc"), "export PATH=bogus:$PATH\n")?;
    create_dir_all(harness.join("bogus"))?;

    assert_cmd!(harness, quickenv "which" "bash", @r###"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    [ERROR quickenv] "bash" is not shimmed by quickenv
    "###);

    assert_cmd!(harness, quickenv "reload", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    "###);

    assert_cmd!(harness, quickenv "which" "bash", @r###"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    [ERROR quickenv] "bash" is not shimmed by quickenv
    "###);

    write(harness.join("bogus/bash"), "#!/bin/sh\necho hello world")?;
    set_executable(harness.join("bogus/bash"))?;

    assert_cmd!(harness, quickenv "which" "bash", @r###"
    success: false
    exit_code: 1
    ----- stdout -----

    ----- stderr -----
    [ERROR quickenv] "bash" is not shimmed by quickenv
    "###);

    assert_cmd!(harness, quickenv "which" "bash" "--pretend-shimmed", @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    bogus/bash

    ----- stderr -----
    "###);
    Ok(())
}

#[test]
fn test_shim_ctrl_c_handling() -> Result<(), Error> {
    let harness = setup()?;

    // Create a test script that sets up a signal handler
    write(harness.join(".envrc"), "export PATH=test_bin:$PATH\n")?;
    create_dir_all(harness.join("test_bin"))?;

    // Create a test script that demonstrates the signal handling behavior
    write(
        harness.join("test_bin/signal_test"),
        r#"#!/bin/sh
myexit() { 
    echo "shutdown"
    sleep 0.1
    echo "bye"
}
trap myexit SIGINT
sleep 1
"#,
    )?;
    set_executable(harness.join("test_bin/signal_test"))?;

    assert_cmd!(harness, quickenv "reload", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    [WARN quickenv] 1 unshimmed commands (1 new). Use 'quickenv shim' to make them available.
    Set QUICKENV_NO_SHIM_WARNINGS=1 to silence this message.
    "###);

    assert_cmd!(harness, quickenv "shim" "signal_test", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);

    // Test that the shim properly handles SIGINT by using timeout
    // This reproduces the exact issue from GitHub issue #12
    let harness_modified = harness;

    // Use the same repro case as in the issue
    let timeout_cmd = format!(
        "timeout -s SIGINT 0.5 {} exec sh -c 'myexit() {{ echo shutdown; sleep 1; echo bye; }}; trap myexit INT; sleep 10'; echo new_prompt",
        harness_modified.which("quickenv")?.display()
    );

    let output = std::process::Command::new("sh")
        .arg("-c")
        .arg(&timeout_cmd)
        .current_dir(&harness_modified.cwd)
        .envs(&harness_modified.env)
        .output()?;

    let stdout = String::from_utf8_lossy(&output.stdout);

    // verify that the signal was handled properly. we should see:
    // 1. "shutdown"
    // 2. "bye"
    // 3. "new_prompt"
    assert!(
        stdout.contains("shutdown"),
        "Expected 'shutdown' in output: {stdout}"
    );
    assert!(stdout.contains("bye"), "Expected 'bye' in output: {stdout}");
    assert!(
        stdout.contains("new_prompt"),
        "Expected 'new_prompt' in output: {stdout}"
    );

    // "bye" must come before "new_prompt"
    // If signal handling is not done correctly, quickenv exits immediately and "new_prompt"
    // appears before "bye"
    let bye_pos = stdout.find("bye").expect("Expected 'bye' in output");
    let new_prompt_pos = stdout
        .find("new_prompt")
        .expect("Expected 'new_prompt' in output");
    assert!(bye_pos < new_prompt_pos,
        "CTRL-C handling bug detected: 'bye' should come before 'new_prompt'. Output order indicates quickenv exited early and orphaned the child process. Actual output: {stdout}");

    Ok(())
}

#[test]
fn test_direnv_lib_loading() -> Result<(), Error> {
    let harness = setup()?;

    // Create a fake direnv lib directory and file
    let direnv_lib_dir = harness.join(".config/direnv/lib");
    create_dir_all(&direnv_lib_dir)?;

    // Create a lib file that exports a custom function and variable
    write(
        direnv_lib_dir.join("test_lib.sh"),
        r#"#!/bin/bash
# Test direnv lib file
export DIRENV_LIB_LOADED=true
my_custom_function() {
    echo "custom function from direnv lib"
}
"#,
    )?;

    // Create .envrc that uses the function and variable from the lib
    write(
        harness.join(".envrc"),
        r#"my_custom_function
echo "DIRENV_LIB_LOADED is: $DIRENV_LIB_LOADED"
export PATH=test_bin:$PATH
"#,
    )?;

    // Create a test binary
    create_dir_all(harness.join("test_bin"))?;
    write(
        harness.join("test_bin/hello"),
        "#!/bin/sh\necho hello from test bin",
    )?;
    set_executable(harness.join("test_bin/hello"))?;

    // Set XDG_CONFIG_HOME to point to our test config
    let mut test_harness = harness;
    test_harness.set_var("XDG_CONFIG_HOME", test_harness.join(".config"));

    // Test that lib loading works during reload
    assert_cmd!(test_harness, quickenv "reload", @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    custom function from direnv lib
    DIRENV_LIB_LOADED is: true

    ----- stderr -----
    [WARN quickenv] 1 unshimmed commands (1 new). Use 'quickenv shim' to make them available.
    Set QUICKENV_NO_SHIM_WARNINGS=1 to silence this message.
    "###);

    // Test that lib loading works when executing shimmed commands
    assert_cmd!(test_harness, quickenv "shim" "hello", @r###"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Created 1 shims in [scrubbed $HOME]/.quickenv/bin/, updated 0.
    Use 'quickenv unshim <command>' to remove them again.
    "###);

    // The environment should be loaded properly when running the shimmed command
    assert_cmd!(test_harness, hello, @r###"
    success: true
    exit_code: 0
    ----- stdout -----
    hello from test bin

    ----- stderr -----
    "###);

    Ok(())
}