fzf-make 0.75.0

A command line tool that executes commands using fuzzy finder with preview window for make, pnpm, yarn, just, and task.
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
use crate::model::{
    command::{self, CommandWithPreview},
    file_util,
    runner_type::RunnerType,
};
use anyhow::{Result, anyhow, bail};
use std::{
    fs::{self},
    path::PathBuf,
    process,
};
use tree_sitter::Parser;

const JUSTFILE_EXTENSION: &str = "just";
const JUSTFILE_NAME_MOD_JUST: &str = "mod.just";
const JUSTFILE_NAME_JUSTFILE: &str = "justfile";
const JUSTFILE_NAME_DOT_JUSTFILE: &str = ".justfile";

#[derive(Debug, Clone, PartialEq)]
pub struct Just {
    // path represents the path to the justfile.
    path: PathBuf,
    // Just can declare modules to import by `mod` directive.
    // ref: https://github.com/casey/just#modules1190
    modules: Vec<Module>,
    commands: Vec<command::CommandWithPreview>,
}

#[derive(Debug, Clone, PartialEq)]
struct Module {
    mod_name: String,
    content: Just,
}

#[derive(PartialEq, Debug)]
struct PossiblePaths {
    current: PathBuf,
    child: Vec<PathBuf>,
}

impl Just {
    pub fn new(current_dir: PathBuf) -> Result<Just> {
        let justfile_path = match Just::find_justfile(current_dir.clone()) {
            Some(path) => path,
            None => bail!("justfile not found"),
        };
        let source_code = fs::read_to_string(&justfile_path)?;

        match Just::parse_justfile(current_dir, justfile_path.clone(), source_code) {
            Some(c) => Ok(c),
            None => Err(anyhow!("failed to parse justfile")),
        }
    }

    pub fn to_commands(&self) -> Vec<command::CommandWithPreview> {
        let mut result = self.commands.clone();

        // Add commands from modules with module name prefix (recursively)
        for module in &self.modules {
            // Get all commands from the module (including nested modules)
            let module_commands = module.content.to_commands();
            for cmd in module_commands {
                result.push(CommandWithPreview::new(
                    RunnerType::Just,
                    format!("{}::{}", module.mod_name, cmd.args),
                    cmd.file_path.clone(),
                    cmd.line_number,
                ));
            }
        }

        result
    }

    pub fn path(&self) -> PathBuf {
        self.path.clone()
    }

    pub fn command_to_run(&self, command: &command::CommandForExec) -> Result<String, anyhow::Error> {
        Ok(format!("just {}", command.args))
    }

    pub fn execute(&self, command: &command::CommandForExec) -> Result<(), anyhow::Error> {
        let child = process::Command::new("just")
            .stdin(process::Stdio::inherit())
            .args(command.args.split_whitespace())
            .spawn();

        match child {
            Ok(mut child) => match child.wait() {
                Ok(_) => Ok(()),
                Err(e) => Err(anyhow!("failed to run: {}", e)),
            },
            Err(e) => Err(anyhow!("failed to spawn: {}", e)),
        }
    }

    fn find_justfile(current_dir: PathBuf) -> Option<PathBuf> {
        file_util::find_file_in_ancestors(current_dir, vec!["justfile", ".justfile"])
    }

    fn parse_justfile(current_dir: PathBuf, justfile_path: PathBuf, source_code: String) -> Option<Just> {
        let mut parser = Parser::new();
        parser.set_language(&tree_sitter_just::language()).unwrap();
        let tree = parser.parse(&source_code, None).unwrap();
        // source_file
        // ├── shebang
        // │   └── language
        // ├── module
        //     └── name: identifier
        //     └── string
        // └── recipe (multiple))
        //     ├── recipe_header
        //     │   └── name: identifier
        //     ├── recipe_body
        //     │   └── recipe_line
        //     │       └── text
        //     └── attribute (multiple, optional)
        //         ├── identifier
        //         └── argument: string
        let mut commands = vec![];
        let mut modules = vec![];

        // At first, it seemed that it is more readable if we can use `Node#children_by_field_name` instead of `Node#children`.
        // But the elements wanted to be extracted here do not have names.
        // So we had no choice but to use `Node#children`.
        'recipe: for recipes_and_its_siblings in tree.root_node().named_children(&mut tree.walk()) {
            if recipes_and_its_siblings.kind() == "module" {
                // Parse `mod` directive.
                // Its format is like `(module name: (identifier) (string))`.
                let mut mod_name = String::new();
                let mut mod_path: Option<PathBuf> = None;
                if mod_name.is_empty() {
                    for recipe_child in recipes_and_its_siblings.named_children(&mut tree.walk()) {
                        match recipe_child.kind() {
                            "identifier" => {
                                mod_name = source_code[recipe_child.byte_range()].to_string();
                            }
                            "string" => {
                                let raw_path = &source_code[recipe_child.byte_range()];
                                // Remove surrounding quotes
                                mod_path =
                                    Some(PathBuf::from(raw_path.trim_matches(|c| c == '\'' || c == '"').to_string()));
                            }
                            _ => {}
                        }
                    }
                }

                // Retrieve the justfiles for the modules recursively.
                if let Some(path) = Just::get_mod_file_path(current_dir.clone(), mod_name.clone(), mod_path) {
                    // Ensure the path is absolute
                    let absolute_path = fs::canonicalize(&path).unwrap_or(path);
                    if let Ok(mod_source_code) = fs::read_to_string(&absolute_path)
                        && let Some(parsed) = Just::parse_justfile(current_dir.clone(), absolute_path, mod_source_code)
                    {
                        modules.push(Module {
                            mod_name: mod_name.clone(),
                            content: parsed,
                        });
                    }
                }
            }

            // Retrieve recipe names.
            if recipes_and_its_siblings.kind() == "recipe" {
                let mut should_skip = false;
                recipes_and_its_siblings.children(&mut tree.walk()).for_each(|attr| {
                    let attr_name = &source_code[attr.byte_range()];
                    if attr_name.contains("private") {
                        should_skip = true;
                    }
                });
                if should_skip {
                    continue;
                }
                for recipe_child in recipes_and_its_siblings.named_children(&mut tree.walk()) {
                    if recipe_child.kind() == "recipe_header" {
                        // `recipe_name` has format like: `fmt:`
                        let recipe_name = &source_code[recipe_child.byte_range()];
                        let trimmed = recipe_name.split(':').collect::<Vec<&str>>();
                        if let Some(r) = trimmed.first() {
                            // If recipe includes an argument, it will be like `run arg:`.
                            // So we need to split it by space and take the first element.
                            let command_name = r.split_whitespace().next().unwrap_or("").to_string();

                            commands.push(CommandWithPreview::new(
                                RunnerType::Just,
                                command_name,
                                justfile_path.clone(),
                                recipe_child.start_position().row as u32 + 1,
                            ))
                        };
                        continue 'recipe;
                    };
                }
            }
        }

        if commands.is_empty() {
            None
        } else {
            Some(Just {
                path: justfile_path,
                modules,
                commands,
            })
        }
    }

    fn calc_possible_justfile_path_from_mod_info(
        current_dir: PathBuf,
        mod_name: String,
        mod_path: &Option<PathBuf>,
    ) -> PossiblePaths {
        // Helper function to convert a path to absolute path
        let to_absolute = |path: PathBuf| -> PathBuf {
            if path.is_absolute() {
                path
            } else {
                let joined = current_dir.join(&path);
                fs::canonicalize(&joined).unwrap_or(joined)
            }
        };

        match mod_path {
            Some(mod_path) => {
                // Both mod_name and mod_path are specified. e.g. mod backend "./api"
                let base_path = to_absolute(PathBuf::from(&mod_path));
                PossiblePaths {
                    current: base_path.clone(),
                    child: vec![
                        base_path.join(JUSTFILE_NAME_MOD_JUST),
                        base_path.join(JUSTFILE_NAME_JUSTFILE),
                        base_path.join(JUSTFILE_NAME_DOT_JUSTFILE),
                    ],
                }
            }
            None => {
                // Only mod_name is specified. e.g. `mod backend`
                let base_path = to_absolute(PathBuf::from(&mod_name));
                let current_with_ext = base_path.with_extension(JUSTFILE_EXTENSION);
                PossiblePaths {
                    current: fs::canonicalize(&current_with_ext).unwrap_or(current_with_ext),
                    child: vec![
                        base_path.join(JUSTFILE_NAME_MOD_JUST),
                        base_path.join(JUSTFILE_NAME_JUSTFILE),
                        base_path.join(JUSTFILE_NAME_DOT_JUSTFILE),
                    ],
                }
            }
        }
    }

    // get_mod_file_path retrieves the justfile path for the specified module.
    //
    // If PATH is specified(e.g. mod foo ./bar), justfile path will be like below:
    // 1. {PATH}(if it's a file path)
    // 2. {PATH}/mod.just
    // 3. {PATH}/justfile
    // 4. {PATH}/.justfile
    //
    // If PATH is not specified(e.g. mod foo), justfile path will be like below:
    // 1. ./foo.just
    // 2. ./foo/mod.just
    // 3. ./foo/justfile
    // 4. ./foo/.justfile
    fn get_mod_file_path(current_dir: PathBuf, mod_name: String, mod_path: Option<PathBuf>) -> Option<PathBuf> {
        let possible_paths =
            Just::calc_possible_justfile_path_from_mod_info(current_dir.clone(), mod_name.clone(), &mod_path);

        // Check if possible_paths.current is an existing file.
        // This is for the 1. mentioned above in both patterns.
        if let Ok(metadata) = fs::metadata(&possible_paths.current)
            && metadata.is_file()
        {
            return Some(possible_paths.current);
        }

        // This is for the 2. - 4. mentioned above in both patterns.
        let target_path = match mod_path.clone() {
            // When PATH is specified.
            Some(mod_path) => {
                let joined = current_dir.join(mod_path);
                fs::canonicalize(&joined).unwrap_or(joined)
            }
            // When PATH is not specified.
            None => {
                let joined = current_dir.join(&mod_name);
                fs::canonicalize(&joined).unwrap_or(joined)
            }
        };
        if let Ok(metadata) = fs::metadata(&target_path) {
            // metadata should be a directory.
            if metadata.is_dir() {
                // If it's a directory, search for justfile inside
                if let Ok(elements) = fs::read_dir(target_path.clone()) {
                    for e in elements {
                        // justfile and .justfile are case-insensitive.
                        // Though mod.just is case-sensitive, it is not problem because it is
                        // already lowercase.
                        let original_file_name = e.unwrap().file_name();
                        let lower_file_name_string = original_file_name.to_str().unwrap().to_lowercase();
                        for possible_path in &possible_paths.child {
                            if lower_file_name_string == possible_path.file_name().unwrap().to_str().unwrap() {
                                let found_path = target_path.join(original_file_name);
                                return Some(fs::canonicalize(&found_path).unwrap_or(found_path));
                            }
                        }
                    }
                }
            }
        }
        None
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use pretty_assertions::assert_eq;
    use uuid::Uuid;

    #[test]
    fn test_find_justfile() {
        // cleanup before test
        let test_root_dir = std::env::temp_dir().join("fzf_make_test");
        // error will be returned if the directory does not exist.
        let _ = std::fs::remove_dir_all(&test_root_dir);
        std::fs::create_dir(&test_root_dir).unwrap();

        // justfile exists in temp_dir
        {
            let test_target_dir = test_root_dir.join(Uuid::new_v4().to_string());
            std::fs::create_dir(&test_target_dir).unwrap();

            let justfile_path = test_target_dir.join("justfile");
            std::fs::File::create(&justfile_path).unwrap();
            assert_eq!(Just::find_justfile(test_target_dir), Some(justfile_path));
        }

        // .justfile exists in temp_dir
        {
            let test_target_dir = test_root_dir.join(Uuid::new_v4().to_string());
            std::fs::create_dir(&test_target_dir).unwrap();

            let justfile_path = test_target_dir.join(".justfile");
            std::fs::File::create(&justfile_path).unwrap();
            assert_eq!(Just::find_justfile(test_target_dir), Some(justfile_path));
        }

        // justfile exists in the one of ancestors of temp_dir
        {
            let parent = test_root_dir.join(Uuid::new_v4().to_string());
            let test_target_dir = parent.join("child_dir");
            std::fs::create_dir_all(&test_target_dir).unwrap();

            let justfile_path = parent.join("justfile");
            std::fs::File::create(&justfile_path).unwrap();
            assert_eq!(Just::find_justfile(test_target_dir), Some(justfile_path));
        }

        // no justfile exists
        {
            let parent = test_root_dir.join(Uuid::new_v4().to_string());
            let test_target_dir = parent.join("child_dir");
            std::fs::create_dir_all(&test_target_dir).unwrap();

            assert_eq!(Just::find_justfile(test_target_dir), None);
        }

        let _ = std::fs::remove_dir_all(&test_root_dir);
    }

    #[test]
    fn test_parse_justfile() {
        struct Case {
            name: &'static str,
            source_code: &'static str,
            expected: Option<Just>,
        }
        let cases = vec![
            Case {
                name: "empty justfile",
                source_code: "",
                expected: None,
            },
            Case {
                name: "justfile with multiple recipes",
                source_code: r#"
#!/usr/bin/env -S just --justfile

test:
  cargo test --all

[group: 'misc']
run:
  echo run

[group: 'misc']
build:
  echo build

[group: 'misc']
fmt : # https://example.com
  echo fmt

[group: 'misc']
[private ]
fmt-private:
  echo fmt

# everyone's favorite animate paper clip
[group: 'check']
clippy:
  echo clippy
        "#,
                expected: Some(Just {
                    path: PathBuf::from("justfile"),
                    modules: vec![],
                    commands: vec![
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "test".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 4,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "run".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 8,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "build".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 12,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "fmt".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 16,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "clippy".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 26,
                        },
                    ],
                }),
            },
            Case {
                name: "justfile with recipes including a recipe with argument",
                source_code: r#"#!/usr/bin/env -S just --justfile

[group: 'misc']
run arg:
  echo "run {{arg}}"

[group: 'misc']
build:
  echo build
        "#,
                expected: Some(Just {
                    path: PathBuf::from("justfile"),
                    modules: vec![],
                    commands: vec![
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "run".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 4,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "build".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 8,
                        },
                    ],
                }),
            },
        ];

        for case in cases {
            assert_eq!(
                Just::parse_justfile(PathBuf::from("path"), PathBuf::from("justfile"), case.source_code.to_string()),
                case.expected,
                "{}",
                case.name
            );
        }
    }

    #[test]
    fn test_parse_justfile_retrieve_mod_recursively() {
        struct Case {
            name: &'static str,
            source_code: &'static str,
            expected: Option<Just>,
        }
        let cases = vec![
            Case {
                name: "empty justfile",
                source_code: "",
                expected: None,
            },
            Case {
                name: "justfile with multiple recipes",
                source_code: r#"
#!/usr/bin/env -S just --justfile

test:
  cargo test --all

[group: 'misc']
run:
  echo run

[group: 'misc']
build:
  echo build

[group: 'misc']
fmt : # https://example.com
  echo fmt

[group: 'misc']
[private ]
fmt-private:
  echo fmt

# everyone's favorite animate paper clip
[group: 'check']
clippy:
  echo clippy
        "#,
                expected: Some(Just {
                    path: PathBuf::from("justfile"),
                    modules: vec![],
                    commands: vec![
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "test".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 4,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "run".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 8,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "build".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 12,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "fmt".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 16,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "clippy".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 26,
                        },
                    ],
                }),
            },
            Case {
                name: "justfile with recipes including a recipe with argument",
                source_code: r#"#!/usr/bin/env -S just --justfile

[group: 'misc']
run arg:
  echo "run {{arg}}"

[group: 'misc']
build:
  echo build
        "#,
                expected: Some(Just {
                    path: PathBuf::from("justfile"),
                    modules: vec![],
                    commands: vec![
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "run".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 4,
                        },
                        CommandWithPreview {
                            runner_type: RunnerType::Just,
                            args: "build".to_string(),
                            file_path: PathBuf::from("justfile"),
                            line_number: 8,
                        },
                    ],
                }),
            },
        ];

        for case in cases {
            assert_eq!(
                Just::parse_justfile(PathBuf::from("path"), PathBuf::from("justfile"), case.source_code.to_string()),
                case.expected,
                "{}",
                case.name
            );
        }
    }

    #[test]
    fn test_calc_justfile_path_from_mod_info() {
        struct Case {
            name: &'static str,
            current_dir: &'static str,
            mod_name: &'static str,
            mod_path: Option<PathBuf>,
            expected: PossiblePaths,
        }

        let cases = vec![
            Case {
                name: "mod_path == None",
                current_dir: "/tmp/test_dir",
                mod_name: "backend",
                mod_path: None,
                expected: PossiblePaths {
                    current: PathBuf::from("/tmp/test_dir/backend.just"),
                    child: vec![
                        PathBuf::from("/tmp/test_dir/backend/mod.just"),
                        PathBuf::from("/tmp/test_dir/backend/justfile"),
                        PathBuf::from("/tmp/test_dir/backend/.justfile"),
                    ],
                },
            },
            Case {
                name: "mod_path == Some(rel_path)",
                current_dir: "/tmp/test_dir",
                mod_name: "backend",
                mod_path: Some(PathBuf::from("./backend")),
                expected: PossiblePaths {
                    current: PathBuf::from("/tmp/test_dir/backend"),
                    child: vec![
                        PathBuf::from("/tmp/test_dir/backend/mod.just"),
                        PathBuf::from("/tmp/test_dir/backend/justfile"),
                        PathBuf::from("/tmp/test_dir/backend/.justfile"),
                    ],
                },
            },
            Case {
                name: "mod_path == Some(abs_path)",
                current_dir: "/tmp/test_dir",
                mod_name: "backend",
                mod_path: Some(PathBuf::from("/Users/user/backend")),
                expected: PossiblePaths {
                    current: PathBuf::from("/Users/user/backend"),
                    child: vec![
                        PathBuf::from("/Users/user/backend/mod.just"),
                        PathBuf::from("/Users/user/backend/justfile"),
                        PathBuf::from("/Users/user/backend/.justfile"),
                    ],
                },
            },
        ];

        for case in cases {
            assert_eq!(
                Just::calc_possible_justfile_path_from_mod_info(
                    PathBuf::from(case.current_dir),
                    case.mod_name.to_string(),
                    &case.mod_path,
                ),
                case.expected,
                "{}",
                case.name
            );
        }
    }

    #[test]
    fn test_get_mod_file_path() {
        // Setup test directory
        let test_root_dir = std::env::temp_dir().join("fzf_make_test_get_mod_file_path");
        let _ = std::fs::remove_dir_all(&test_root_dir);
        std::fs::create_dir_all(&test_root_dir).unwrap();

        // Test case 1: mod_path is None, and backend.just exists
        {
            let backend_just = test_root_dir.join("backend.just");
            std::fs::File::create(&backend_just).unwrap();

            let result = Just::get_mod_file_path(test_root_dir.clone(), "backend".to_string(), None);
            let expected = fs::canonicalize(&backend_just).unwrap_or(backend_just);
            assert_eq!(result, Some(expected));
        }

        // Test case 2: mod_path is None, backend.just doesn't exist, but backend/mod.just exists
        {
            let test_dir = test_root_dir.join("test_case_2");
            std::fs::create_dir_all(&test_dir).unwrap();

            let backend_dir = test_dir.join("backend");
            std::fs::create_dir_all(&backend_dir).unwrap();
            let mod_just = backend_dir.join("mod.just");
            std::fs::File::create(&mod_just).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), None);
            let expected = fs::canonicalize(&mod_just).unwrap_or(mod_just);
            assert_eq!(result, Some(expected));
        }

        // Test case 3: mod_path is None, backend.just doesn't exist, but backend/justfile exists
        {
            let test_dir = test_root_dir.join("test_case_3");
            std::fs::create_dir_all(&test_dir).unwrap();

            let backend_dir = test_dir.join("backend");
            std::fs::create_dir_all(&backend_dir).unwrap();
            let justfile = backend_dir.join("justfile");
            std::fs::File::create(&justfile).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), None);
            let expected = fs::canonicalize(&justfile).unwrap_or(justfile);
            assert_eq!(result, Some(expected));
        }

        // Test case 4: mod_path is None, backend.just doesn't exist, but backend/.justfile exists
        {
            let test_dir = test_root_dir.join("test_case_4");
            std::fs::create_dir_all(&test_dir).unwrap();

            let backend_dir = test_dir.join("backend");
            std::fs::create_dir_all(&backend_dir).unwrap();
            let dot_justfile = backend_dir.join(".justfile");
            std::fs::File::create(&dot_justfile).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), None);
            let expected = fs::canonicalize(&dot_justfile).unwrap_or(dot_justfile);
            assert_eq!(result, Some(expected));
        }

        // Test case 5: mod_path is Some(relative path), and the path is a file
        {
            let test_dir = test_root_dir.join("test_case_5");
            std::fs::create_dir_all(&test_dir).unwrap();

            let api_just = test_dir.join("api.just");
            std::fs::File::create(&api_just).unwrap();

            let result =
                Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), Some(PathBuf::from("./api.just")));
            assert_eq!(result, Some(fs::canonicalize(&api_just).unwrap()));
        }

        // Test case 6: mod_path is Some(relative path), and the path is a directory with mod.just
        {
            let test_dir = test_root_dir.join("test_case_6");
            std::fs::create_dir_all(&test_dir).unwrap();

            let api_dir = test_dir.join("api");
            std::fs::create_dir_all(&api_dir).unwrap();
            let mod_just = api_dir.join("mod.just");
            std::fs::File::create(&mod_just).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), Some(PathBuf::from("./api")));
            let expected = fs::canonicalize(&mod_just).unwrap_or(mod_just);
            assert_eq!(result, Some(expected));
        }

        // Test case 7: mod_path is Some(relative path), and the path is a directory with justfile
        {
            let test_dir = test_root_dir.join("test_case_7");
            std::fs::create_dir_all(&test_dir).unwrap();

            let api_dir = test_dir.join("api");
            std::fs::create_dir_all(&api_dir).unwrap();
            let justfile = api_dir.join("justfile");
            std::fs::File::create(&justfile).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), Some(PathBuf::from("./api")));
            let expected = fs::canonicalize(&justfile).unwrap_or(justfile);
            assert_eq!(result, Some(expected));
        }

        // Test case 8: mod_path is Some(relative path), and the path is a directory with .justfile
        {
            let test_dir = test_root_dir.join("test_case_8");
            std::fs::create_dir_all(&test_dir).unwrap();

            let api_dir = test_dir.join("api");
            std::fs::create_dir_all(&api_dir).unwrap();
            let dot_justfile = api_dir.join(".justfile");
            std::fs::File::create(&dot_justfile).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), Some(PathBuf::from("./api")));
            let expected = fs::canonicalize(&dot_justfile).unwrap_or(dot_justfile);
            assert_eq!(result, Some(expected));
        }

        // Test case 9: case-insensitive filename - Justfile (capital J)
        {
            let test_dir = test_root_dir.join("test_case_9");
            std::fs::create_dir_all(&test_dir).unwrap();

            let backend_dir = test_dir.join("backend");
            std::fs::create_dir_all(&backend_dir).unwrap();
            let justfile_capital = backend_dir.join("Justfile");
            std::fs::File::create(&justfile_capital).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), None);
            let expected = fs::canonicalize(&justfile_capital).unwrap_or(justfile_capital);
            assert_eq!(result, Some(expected));
        }

        // Test case 10: case-insensitive filename - .Justfile (capital J)
        {
            let test_dir = test_root_dir.join("test_case_10");
            std::fs::create_dir_all(&test_dir).unwrap();

            let backend_dir = test_dir.join("backend");
            std::fs::create_dir_all(&backend_dir).unwrap();
            let dot_justfile_capital = backend_dir.join(".Justfile");
            std::fs::File::create(&dot_justfile_capital).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), None);
            let expected = fs::canonicalize(&dot_justfile_capital).unwrap_or(dot_justfile_capital);
            assert_eq!(result, Some(expected));
        }

        // Test case 11: mod_path is Some(absolute path), and the path is a file
        {
            let test_dir = test_root_dir.join("test_case_11");
            std::fs::create_dir_all(&test_dir).unwrap();

            let api_just = test_dir.join("api.just");
            std::fs::File::create(&api_just).unwrap();
            let absolute_path = fs::canonicalize(&api_just).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), Some(absolute_path.clone()));
            assert_eq!(result, Some(absolute_path));
        }

        // Test case 12: mod_path is Some(absolute path), and the path is a directory with mod.just
        {
            let test_dir = test_root_dir.join("test_case_12");
            std::fs::create_dir_all(&test_dir).unwrap();

            let api_dir = test_dir.join("api");
            std::fs::create_dir_all(&api_dir).unwrap();
            let mod_just = api_dir.join("mod.just");
            std::fs::File::create(&mod_just).unwrap();
            let absolute_path = fs::canonicalize(&api_dir).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), Some(absolute_path));
            let expected = fs::canonicalize(&mod_just).unwrap_or(mod_just);
            assert_eq!(result, Some(expected));
        }

        // Test case 13: no matching file exists
        {
            let test_dir = test_root_dir.join("test_case_13");
            std::fs::create_dir_all(&test_dir).unwrap();

            let result = Just::get_mod_file_path(test_dir.clone(), "backend".to_string(), None);
            assert_eq!(result, None);
        }

        // Cleanup
        let _ = std::fs::remove_dir_all(&test_root_dir);
    }

    #[test]
    fn test_to_commands() {
        // Test case 1: Just with only root level commands (no modules)
        {
            let just = Just {
                path: PathBuf::from("justfile"),
                modules: vec![],
                commands: vec![
                    CommandWithPreview::new(RunnerType::Just, "test".to_string(), PathBuf::from("justfile"), 1),
                    CommandWithPreview::new(RunnerType::Just, "build".to_string(), PathBuf::from("justfile"), 5),
                ],
            };

            let result = just.to_commands();
            assert_eq!(result.len(), 2);
            assert_eq!(result[0].args, "test");
            assert_eq!(result[1].args, "build");
        }

        // Test case 2: Just with modules
        {
            let just = Just {
                path: PathBuf::from("justfile"),
                modules: vec![Module {
                    mod_name: "backend".to_string(),
                    content: Just {
                        path: PathBuf::from("backend.just"),
                        modules: vec![],
                        commands: vec![
                            CommandWithPreview::new(
                                RunnerType::Just,
                                "start".to_string(),
                                PathBuf::from("backend.just"),
                                1,
                            ),
                            CommandWithPreview::new(
                                RunnerType::Just,
                                "stop".to_string(),
                                PathBuf::from("backend.just"),
                                5,
                            ),
                        ],
                    },
                }],
                commands: vec![CommandWithPreview::new(
                    RunnerType::Just,
                    "test".to_string(),
                    PathBuf::from("justfile"),
                    1,
                )],
            };

            let result = just.to_commands();
            assert_eq!(result.len(), 3);
            assert_eq!(result[0].args, "test");
            assert_eq!(result[0].file_path, PathBuf::from("justfile"));
            assert_eq!(result[1].args, "backend::start");
            assert_eq!(result[1].file_path, PathBuf::from("backend.just"));
            assert_eq!(result[2].args, "backend::stop");
            assert_eq!(result[2].file_path, PathBuf::from("backend.just"));
        }

        // Test case 3: Just with multiple modules
        {
            let just = Just {
                path: PathBuf::from("justfile"),
                modules: vec![
                    Module {
                        mod_name: "backend".to_string(),
                        content: Just {
                            path: PathBuf::from("backend.just"),
                            modules: vec![],
                            commands: vec![CommandWithPreview::new(
                                RunnerType::Just,
                                "start".to_string(),
                                PathBuf::from("backend.just"),
                                1,
                            )],
                        },
                    },
                    Module {
                        mod_name: "frontend".to_string(),
                        content: Just {
                            path: PathBuf::from("frontend.just"),
                            modules: vec![],
                            commands: vec![CommandWithPreview::new(
                                RunnerType::Just,
                                "serve".to_string(),
                                PathBuf::from("frontend.just"),
                                1,
                            )],
                        },
                    },
                ],
                commands: vec![CommandWithPreview::new(
                    RunnerType::Just,
                    "test".to_string(),
                    PathBuf::from("justfile"),
                    1,
                )],
            };

            let result = just.to_commands();
            assert_eq!(result.len(), 3);
            assert_eq!(result[0].args, "test");
            assert_eq!(result[1].args, "backend::start");
            assert_eq!(result[2].args, "frontend::serve");
        }

        // Test case 4: Nested modules
        {
            let just = Just {
                path: PathBuf::from("justfile"),
                modules: vec![Module {
                    mod_name: "backend".to_string(),
                    content: Just {
                        path: PathBuf::from("backend.just"),
                        modules: vec![Module {
                            mod_name: "api".to_string(),
                            content: Just {
                                path: PathBuf::from("backend/api.just"),
                                modules: vec![],
                                commands: vec![CommandWithPreview::new(
                                    RunnerType::Just,
                                    "deploy".to_string(),
                                    PathBuf::from("backend/api.just"),
                                    1,
                                )],
                            },
                        }],
                        commands: vec![CommandWithPreview::new(
                            RunnerType::Just,
                            "start".to_string(),
                            PathBuf::from("backend.just"),
                            1,
                        )],
                    },
                }],
                commands: vec![],
            };

            let result = just.to_commands();
            // backend::start (1) + backend::api::deploy (1) = 2
            // But since backend module's to_commands() is called recursively,
            // it returns both "start" and "api::deploy", so we get:
            // - backend::start
            // - backend::api::deploy
            assert_eq!(result.len(), 2);
            assert_eq!(result[0].args, "backend::start");
            assert_eq!(result[1].args, "backend::api::deploy");
        }
    }
}