cargo-e 0.3.2

e is for Example. A command-line tool for running and exploring source, examples, and binaries from Rust projects. It will run the first example, if no options are given.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
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
// src/e_findmain.rs

use crate::{
    e_target::{CargoTarget, TargetKind},
    prelude::*,
};
use toml::Value;

use crate::e_workspace::{get_workspace_member_manifest_paths, is_workspace_manifest};

/// Given an Example, attempts to locate the main file.
///
/// For **extended samples** (i.e. sample.extended is true), it first checks for a file at:
/// 1. `<manifest_dir>/src/main.rs`  
/// 2. `<manifest_dir>/main.rs`  
///    and if found returns that path.
///
/// Otherwise (or if the above do not exist), it falls back to parsing the Cargo.toml:
///   - For binaries: it looks in the `[[bin]]` section.
///   - For examples: it first checks the `[[example]]` section, and if not found, falls back to `[[bin]]`.
///     If a target matching the sample name is found, it uses the provided `"path"` (if any)
///     or defaults to `"src/main.rs"`.
///   - Returns Some(candidate) if the file exists.
pub fn find_main_file(sample: &CargoTarget) -> Option<PathBuf> {
    let manifest_path = Path::new(&sample.manifest_path);

    // Determine the base directory.
    let base = if is_workspace_manifest(manifest_path) {
        // Try to locate a workspace member manifest matching the sample name.
        match get_workspace_member_manifest_paths(manifest_path) {
            Some(members) => {
                if let Some((_, member_manifest)) = members
                    .into_iter()
                    .find(|(member_name, _)| member_name == &sample.name)
                {
                    member_manifest.parent().map(|p| p.to_path_buf())?
                } else {
                    // No matching member found; use the workspace manifest's parent.
                    manifest_path.parent().map(|p| p.to_path_buf())?
                }
            }
            None => manifest_path.parent().map(|p| p.to_path_buf())?,
        }
    } else {
        manifest_path.parent()?.to_path_buf()
    };

    // Check conventional locations for extended samples.
    let candidate_src = base.join("src").join("main.rs");
    println!("DEBUG: candidate_src: {:?}", candidate_src);
    if candidate_src.exists() {
        return Some(candidate_src);
    }
    let candidate_main = base.join("main.rs");
    println!("DEBUG: candidate_src: {:?}", candidate_main);
    if candidate_main.exists() {
        return Some(candidate_main);
    }
    let candidate_main = base.join(format!("{}.rs", sample.name));
    println!("DEBUG: candidate_src: {:?}", candidate_main);
    if candidate_main.exists() {
        return Some(candidate_main);
    }
    // Check conventional location src\bin samples.
    let candidate_src = base
        .join("src")
        .join("bin")
        .join(format!("{}.rs", sample.name));
    println!("DEBUG: candidate_src: {:?}", candidate_src);
    if candidate_src.exists() {
        return Some(candidate_src);
    }
    // If neither conventional file exists, fall through to Cargo.toml parsing.

    let contents = fs::read_to_string(manifest_path).ok()?;
    let value: Value = contents.parse().ok()?;
    let targets = if sample.kind == TargetKind::Binary {
        value.get("bin")
    } else {
        value.get("example").or_else(|| value.get("bin"))
    }?;
    if let Some(arr) = targets.as_array() {
        for target in arr {
            if let Some(name) = target.get("name").and_then(|v| v.as_str()) {
                if name == sample.name {
                    let relative = target
                        .get("path")
                        .and_then(|v| v.as_str())
                        .unwrap_or("src/main.rs");
                    let base = manifest_path.parent()?;
                    let candidate = base.join(relative);
                    if candidate.exists() {
                        return Some(candidate);
                    }
                }
            }
        }
    }
    None
}

/// Searches the given file for "fn main" and returns (line, column) where it is first found.
/// Both line and column are 1-indexed.
pub fn find_main_line(file: &Path) -> Option<(usize, usize)> {
    let content = fs::read_to_string(file).ok()?;
    for (i, line) in content.lines().enumerate() {
        if let Some(col) = line.find("fn main") {
            return Some((i + 1, col + 1));
        }
    }
    None
}

/// Computes the arguments for VSCode given a sample target.
/// Returns a tuple (folder_str, goto_arg).
/// - `folder_str` is the folder that will be opened in VSCode.
/// - `goto_arg` is an optional string of the form "\<file\>:\<line\>:\<column\>"
///   determined by searching for "fn main" in the candidate file.
///
/// For extended samples, it checks first for "src/main.rs", then "main.rs".
/// For non-extended examples, it assumes the file is at "examples/\<name\>.rs" relative to cwd.
pub fn compute_vscode_args(sample: &CargoTarget) -> (String, Option<String>) {
    let manifest_path = Path::new(&sample.manifest_path);
    // Debug print
    println!("DEBUG: manifest_path: {:?}", manifest_path);

    let candidate_file: Option<PathBuf> = find_main_file(sample).or_else(|| {
        if sample.kind == TargetKind::Binary
            || (sample.kind == TargetKind::Example && sample.extended)
        {
            // Fallback to "src/main.rs" in the manifest's folder.
            let base = manifest_path.parent()?;
            let fallback = base.join("src/main.rs");
            if fallback.exists() {
                Some(fallback)
            } else {
                None
            }
        } else if sample.kind == TargetKind::Example && !sample.extended {
            // For built-in examples, assume the file is "examples/<name>.rs" relative to the current directory.
            let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
            let fallback = cwd.join("examples").join(format!("{}.rs", sample.name));
            if fallback.exists() {
                Some(fallback)
            } else {
                None
            }
        } else {
            None
        }
    });

    println!("DEBUG: candidate_file: {:?}", candidate_file);

    let (folder, goto_arg) = if let Some(file) = candidate_file {
        let folder = file.parent().unwrap_or(&file).to_path_buf();
        let goto_arg = if let Some((line, col)) = find_main_line(&file) {
            Some(format!(
                "{}:{}:{}",
                file.to_str().unwrap_or_default(),
                line,
                col
            ))
        } else {
            Some(file.to_str().unwrap_or_default().to_string())
        };
        (folder, goto_arg)
    } else {
        (
            manifest_path
                .parent()
                .unwrap_or(manifest_path)
                .to_path_buf(),
            None,
        )
    };

    let folder_str = folder.to_str().unwrap_or_default().to_string();
    println!("DEBUG: folder_str: {}", folder_str);
    println!("DEBUG: goto_arg: {:?}", goto_arg);

    (folder_str, goto_arg)
}

/// Asynchronously opens VSCode for the given sample target.
/// It computes the VSCode arguments using `compute_vscode_args` and then launches VSCode.
pub async fn open_vscode_for_sample(sample: &CargoTarget) {
    let (folder_str, goto_arg) = compute_vscode_args(sample);

    let output = if cfg!(target_os = "windows") {
        if let Some(ref goto) = goto_arg {
            Command::new("cmd")
                .args(["/C", "code", folder_str.as_str(), "--goto", goto.as_str()])
                .output()
        } else {
            Command::new("cmd")
                .args(["/C", "code", folder_str.as_str()])
                .output()
        }
    } else {
        let mut cmd = Command::new("code");
        cmd.arg(folder_str.as_str());
        if let Some(goto) = goto_arg {
            cmd.args(["--goto", goto.as_str()]);
        }
        cmd.output()
    };

    match output {
        Ok(output) if output.status.success() => {
            // VSCode opened successfully.
            println!("DEBUG: VSCode command output: {:?}", output);
        }
        Ok(output) => {
            let msg = format!(
                "Error opening VSCode:\nstdout: {}\nstderr: {}",
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr)
            );
            error!("{}", msg);
        }
        Err(e) => {
            let msg = format!("Failed to execute VSCode command: {}", e);
            error!("{}", msg);
        }
    }
}

// /// Opens Vim for the given sample target.
// /// It computes the file and (optionally) the line and column to jump to.
// /// If the goto argument is in the format "file:line:column", it spawns Vim with a command to move the cursor.
// pub fn open_vim_for_sample(sample: &Example) {
//     let (folder_str, goto_arg) = compute_vscode_args(sample);

//     // Determine the file to open and optionally extract line and column.
//     let (file, line, col) = if let Some(goto) = goto_arg {
//         // Split into parts and convert each to an owned String.
//         let parts: Vec<String> = goto.split(':').map(|s| s.to_string()).collect();
//         if parts.len() >= 3 {
//             (parts[0].clone(), parts[1].clone(), parts[2].clone())
//         } else {
//             (goto.clone(), "1".to_string(), "1".to_string())
//         }
//     } else {
//         (folder_str.clone(), "1".to_string(), "1".to_string())
//     };

//     // Prepare Vim command arguments.
//     // We use the Vim command to jump to the given line and column:
//     //   +":call cursor(line, col)"
//     let cursor_cmd = format!("+call cursor({}, {})", line, col);
// println!("nvim {}",&file);
//     // Spawn the Vim process.
//     let output = if cfg!(target_os = "windows") {
//         let args=&["/C", "nvim", &cursor_cmd, &file];
//         println!("{:?}",args);
//         // On Windows, we might need to run via cmd.
//         Command::new("cmd")
//             .args(args)
//             .output()
//     } else {
//         let args=&[&cursor_cmd, &file];
//         println!("{:?}",args);
//         Command::new("nvim")
//             .args(args)
//             .output()
//     };

//     match output {
//         Ok(output) if output.status.success() => {
//             println!("DEBUG: Vim opened successfully.");
//         }
//         Ok(output) => {
//             let msg = format!(
//                 "Error opening Vim:\nstdout: {}\nstderr: {}",
//                 String::from_utf8_lossy(&output.stdout),
//                 String::from_utf8_lossy(&output.stderr)
//             );
//             error!("{}", msg);
//         }
//         Err(e) => {
//             let msg = format!("Failed to execute Vim command: {}", e);
//             error!("{}", msg);
//         }
//     }
// }

// /// Opens Emacs (via emacsclient) for the given sample target.
// ///
// /// It computes the file and (optionally) the line (and column) where "fn main"
// /// is located. Emacsclient supports jumping to a line with `+<line>`, so we use that.
// /// Note: column information is not used by default.
// pub fn open_emacs_for_sample(sample: &Example) {
//     let (folder_str, goto_arg) = compute_vscode_args(sample);

//     // Parse the goto argument if available.
//     let (file, line, _col) = if let Some(goto) = goto_arg {
//         // Expect the format "file:line:column". Convert each to an owned String.
//         let parts: Vec<String> = goto.split(':').map(|s| s.to_string()).collect();
//         if parts.len() >= 3 {
//             (parts[0].clone(), parts[1].clone(), parts[2].clone())
//         } else {
//             (goto.clone(), "1".to_string(), "1".to_string())
//         }
//     } else {
//         (folder_str.clone(), "1".to_string(), "1".to_string())
//     };

//     // Create the line argument for emacsclient: "+<line>"
//     let line_arg = format!("+{}", line);

//     // Spawn emacsclient to open the file at the desired line.
//     let output = if cfg!(target_os = "windows") {
//         Command::new("cmd")
//             .args(&["/C", "emacsclient", "-n", &line_arg, &file])
//             .output()
//     } else {
//         Command::new("emacsclient")
//             .args(&["-n", &line_arg, &file])
//             .output()
//     };

//     match output {
//         Ok(output) if output.status.success() => {
//             println!("DEBUG: Emacs opened successfully.");
//         }
//         Ok(output) => {
//             let msg = format!(
//                 "Error opening Emacs:\nstdout: {}\nstderr: {}",
//                 String::from_utf8_lossy(&output.stdout),
//                 String::from_utf8_lossy(&output.stderr)
//             );
//             error!("{}", msg);
//         }
//         Err(e) => {
//             let msg = format!("Failed to execute Emacs command: {}", e);
//             error!("{}", msg);
//         }
//     }
// }

#[cfg(test)]
mod tests {
    use crate::e_target::TargetOrigin;

    use super::*;
    use std::fs;
    use tempfile::tempdir;

    // Test for a non-extended sample with no explicit path in Cargo.toml (should fallback to "src/main.rs").
    #[test]
    fn test_find_main_file_default() -> Result<(), Box<dyn std::error::Error>> {
        let dir = tempdir()?;
        let manifest_path = dir.path().join("Cargo.toml");
        let main_rs = dir.path().join("src/main.rs");
        fs::create_dir_all(main_rs.parent().unwrap())?;
        fs::write(&main_rs, "fn main() {}")?;
        let toml_contents = r#"
            [package]
            name = "dummy"
            version = "0.1.0"
            edition = "2021"
            
            [[bin]]
            name = "sample1"
        "#;
        fs::write(&manifest_path, toml_contents)?;
        let sample = CargoTarget {
            name: "sample1".to_string(),
            display_name: "dummy".to_string(),
            manifest_path,
            kind: TargetKind::Binary,
            extended: false,
            toml_specified: false,
            origin: Some(TargetOrigin::Named("sample1".into())),
        };
        let found = find_main_file(&sample).expect("Should find main file");
        assert_eq!(found, main_rs);
        dir.close()?;
        Ok(())
    }

    // Test for a non-extended sample with an explicit "path" in Cargo.toml.
    #[test]
    fn test_find_main_file_with_explicit_path() -> Result<(), Box<dyn std::error::Error>> {
        let dir = tempdir()?;
        let manifest_path = dir.path().join("Cargo.toml");
        let custom_main = dir.path().join("src/main.rs");
        fs::create_dir_all(custom_main.parent().unwrap())?;
        fs::write(&custom_main, "fn main() {}")?;
        let toml_contents = format!(
            r#"
            [package]
            name = "dummy"
            version = "0.1.0"
            edition = "2021"
            
            [[bin]]
            name = "sample2"
            path = "{}"
            "#,
            custom_main
                .strip_prefix(dir.path())
                .unwrap()
                .to_str()
                .unwrap()
        );
        fs::write(&manifest_path, toml_contents)?;
        let sample = CargoTarget {
            name: "sample2".to_string(),
            display_name: "dummy".to_string(),
            manifest_path,
            kind: TargetKind::Binary,
            origin: Some(TargetOrigin::Named("sample2".into())),
            toml_specified: false,
            extended: false,
        };
        let found = find_main_file(&sample).expect("Should find custom main file");
        assert_eq!(found, custom_main);
        dir.close()?;
        Ok(())
    }

    // Test for an extended sample where "src/main.rs" exists.
    #[test]
    fn test_extended_sample_src_main() -> Result<(), Box<dyn std::error::Error>> {
        let dir = tempdir()?;
        // Simulate an extended sample folder (e.g. "examples/sample_ext")
        let sample_dir = dir.path().join("examples").join("sample_ext");
        fs::create_dir_all(sample_dir.join("src"))?;
        let main_rs = sample_dir.join("src/main.rs");
        fs::write(&main_rs, "fn main() {}")?;
        // Write a Cargo.toml in the sample folder.
        let manifest_path = sample_dir.join("Cargo.toml");
        let toml_contents = r#"
            [package]
            name = "sample_ext"
            version = "0.1.0"
            edition = "2021"
        "#;
        fs::write(&manifest_path, toml_contents)?;

        let sample = CargoTarget {
            name: "sample_ext".to_string(),
            display_name: "extended sample".to_string(),
            manifest_path: manifest_path.clone(),
            kind: TargetKind::Example,
            origin: Some(TargetOrigin::SubProject(manifest_path.to_path_buf())),
            toml_specified: false,
            extended: true,
        };

        // For extended samples, our function should find "src/main.rs" first.
        let found = find_main_file(&sample).expect("Should find src/main.rs in extended sample");
        assert_eq!(found, main_rs);
        dir.close()?;
        Ok(())
    }

    // Test for an extended sample where "src/main.rs" does not exist but "main.rs" exists.
    #[test]
    fn test_extended_sample_main_rs() -> Result<(), Box<dyn std::error::Error>> {
        let dir = tempdir()?;
        let sample_dir = dir.path().join("examples").join("sample_ext2");
        fs::create_dir_all(&sample_dir)?;
        let main_rs = sample_dir.join("main.rs");
        fs::write(&main_rs, "fn main() {}")?;
        let manifest_path = sample_dir.join("Cargo.toml");
        let toml_contents = r#"
            [package]
            name = "sample_ext2"
            version = "0.1.0"
            edition = "2021"
        "#;
        fs::write(&manifest_path, toml_contents)?;
        let sample = CargoTarget {
            name: "sample_ext2".to_string(),
            display_name: "extended sample 2".to_string(),
            manifest_path: manifest_path.clone(),
            kind: TargetKind::Example,
            origin: Some(TargetOrigin::SubProject(manifest_path.to_path_buf())),
            toml_specified: false,
            extended: true,
        };
        let found = find_main_file(&sample).expect("Should find main.rs in extended sample");
        assert_eq!(found, main_rs);
        dir.close()?;
        Ok(())
    }

    // Test for find_main_line: it should locate "fn main" and return the correct (line, column).
    #[test]
    fn test_find_main_line() -> Result<(), Box<dyn std::error::Error>> {
        let dir = tempdir()?;
        let file_path = dir.path().join("src/main.rs");
        fs::create_dir_all(file_path.parent().unwrap())?;
        // Create a file with some lines and a line with "fn main"
        let content = "\n\nfn helper() {}\nfn main() { println!(\"Hello\"); }\n";
        fs::write(&file_path, content)?;
        let pos = find_main_line(&file_path).expect("Should find fn main");
        // "fn main" should appear on line 4 (1-indexed)
        assert_eq!(pos.0, 4);
        dir.close()?;
        Ok(())
    }

    #[test]
    fn test_compute_vscode_args_non_extended() -> Result<(), Box<dyn std::error::Error>> {
        // Create a temporary directory and change the current working directory to it.
        let dir = tempdir()?;
        let temp_path = dir.path();
        env::set_current_dir(temp_path)?;

        // Create the examples directory and a dummy example file.
        let examples_dir = temp_path.join("examples");
        fs::create_dir_all(&examples_dir)?;
        let sample_file = examples_dir.join("sample_non_ext.rs");
        fs::write(&sample_file, "fn main() { println!(\"non-ext\"); }")?;

        // Create a dummy Cargo.toml in the temporary directory.
        let manifest_path = temp_path.join("Cargo.toml");
        fs::write(
            &manifest_path,
            "[package]\nname = \"dummy\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )?;

        // Construct the sample object using the temp folder's Cargo.toml path.
        let sample = CargoTarget {
            name: "sample_non_ext".to_string(),
            display_name: "non-extended".to_string(),
            manifest_path: manifest_path.clone(),
            kind: TargetKind::Example,
            toml_specified: false,
            origin: Some(TargetOrigin::SubProject(manifest_path.to_path_buf())),
            extended: false,
        };

        let (folder_str, goto_arg) = compute_vscode_args(&sample);
        // In this case, we expect folder_str to contain "examples" and goto_arg to refer to sample_non_ext.rs.
        assert!(folder_str.contains("examples"));
        assert!(goto_arg.unwrap().contains("sample_non_ext.rs"));

        // Cleanup is not required because the tempdir will be dropped,
        // which deletes all files inside the temporary directory.
        Ok(())
    }

    #[test]
    fn test_compute_vscode_args_extended_src_main() -> Result<(), Box<dyn std::error::Error>> {
        // Simulate an extended sample where Cargo.toml is in the sample folder and "src/main.rs" exists.
        let dir = tempdir()?;
        let sample_dir = dir.path().join("extended_sample");
        fs::create_dir_all(sample_dir.join("src"))?;
        let main_rs = sample_dir.join("src/main.rs");
        fs::write(&main_rs, "fn main() { println!(\"extended src main\"); }")?;
        let manifest_path = sample_dir.join("Cargo.toml");
        fs::write(
            &manifest_path,
            "[package]\nname = \"extended_sample\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )?;

        let sample = CargoTarget {
            name: "extended_sample".to_string(),
            display_name: "extended".to_string(),
            manifest_path: manifest_path.clone(),
            kind: TargetKind::Example,
            toml_specified: false,
            origin: Some(TargetOrigin::SubProject(manifest_path.to_path_buf())),
            extended: true,
        };

        let (folder_str, goto_arg) = compute_vscode_args(&sample);
        // The folder should be sample_dir/src since that's where main.rs is.
        assert!(folder_str.ends_with("src"));
        let goto = goto_arg.unwrap();
        // The goto argument should contain main.rs.
        assert!(goto.contains("main.rs"));
        dir.close()?;
        Ok(())
    }

    #[test]
    fn test_compute_vscode_args_extended_main_rs() -> Result<(), Box<dyn std::error::Error>> {
        // Simulate an extended sample where "src/main.rs" does not exist, but "main.rs" exists.
        let dir = tempdir()?;
        let sample_dir = dir.path().join("extended_sample2");
        fs::create_dir_all(&sample_dir)?;
        let main_rs = sample_dir.join("main.rs");
        fs::write(&main_rs, "fn main() { println!(\"extended main\"); }")?;
        let manifest_path = sample_dir.join("Cargo.toml");
        fs::write(
            &manifest_path,
            "[package]\nname = \"extended_sample2\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )?;

        let sample = CargoTarget {
            name: "extended_sample2".to_string(),
            display_name: "extended2".to_string(),
            manifest_path: manifest_path.clone(),
            kind: TargetKind::Example,
            toml_specified: false,
            origin: Some(TargetOrigin::SubProject(manifest_path.to_path_buf())),
            extended: true,
        };

        let (folder_str, goto_arg) = compute_vscode_args(&sample);
        // The folder should be the sample_dir (since main.rs is directly in it).
        assert!(folder_str.ends_with("extended_sample2"));
        let goto = goto_arg.unwrap();
        assert!(goto.contains("main.rs"));
        dir.close()?;
        Ok(())
    }
}