soldeer-core 0.6.0

Core functionality for Soldeer
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
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
//! Remappings management.
use crate::{
    config::{read_config_deps, Dependency, Paths, SoldeerConfig},
    errors::RemappingsError,
    utils::path_matches,
};
use derive_more::derive::From;
use log::debug;
use path_slash::PathExt as _;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::{
    fs::{self, File},
    io::Write as _,
    path::PathBuf,
};
use toml_edit::{value, Array, DocumentMut};

pub type Result<T> = std::result::Result<T, RemappingsError>;

/// Action to perform on the remappings.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum RemappingsAction {
    /// Add a dependency to the remappings.
    Add(Dependency),

    /// Remove a dependency from the remappings.
    Remove(Dependency),

    /// Update the remappings according to the config file.
    Update,
}

/// Location where to store the remappings, either in `remappings.txt` or the config file
/// (foundry/soldeer).
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum RemappingsLocation {
    /// Store the remappings in a dedicated `remappings.txt` file.
    #[default]
    Txt,

    /// Store the remappings in the `foundry.toml` config file.
    ///
    /// Note that remappings are never stored in the `soldeer.toml` file because foundry wouldn't
    /// be able to read them from there.
    Config,
}

/// Generate the remappings for storing into the `remappings.txt` file.
///
/// If the `remappings_regenerate` option is set to `true`, then any existing remappings are
/// discarded and the remappings are generated from the dependencies in the config file.
///
/// Otherwise, existing remappings are kept, and depending on the action, a remapping entry is added
/// or removed. For the [`RemappingsAction::Update`] action, the existing remappings are merged with
/// the dependencies in the config file.
pub fn remappings_txt(
    action: &RemappingsAction,
    paths: &Paths,
    soldeer_config: &SoldeerConfig,
) -> Result<()> {
    if soldeer_config.remappings_regenerate && paths.remappings.exists() {
        fs::remove_file(&paths.remappings)?;
        debug!(path:? = paths.remappings; "removed existing remappings file");
    }
    let contents = if paths.remappings.exists() {
        debug!(path:? = paths.remappings; "reading existing remappings from remappings.txt file");
        fs::read_to_string(&paths.remappings)?
    } else {
        String::new()
    };
    let existing_remappings: Vec<_> = contents.lines().filter_map(|r| r.split_once('=')).collect();

    let new_remappings = generate_remappings(action, paths, soldeer_config, &existing_remappings)?;

    let mut file = File::create(&paths.remappings)?;
    for remapping in new_remappings {
        writeln!(file, "{remapping}")?;
    }
    debug!(path:? = paths.remappings; "updated remappings.txt file");
    Ok(())
}

/// Generate the remappings for storing into the `foundry.toml` config file.
///
/// If the `remappings_regenerate` option is set to `true`, then any existing remappings are
/// discarded and the remappings are generated from the dependencies in the config file.
///
/// Otherwise, existing remappings are kept, and depending on the action, a remapping entry is added
/// or removed. For the [`RemappingsAction::Update`] action, the existing remappings are merged with
/// the dependencies in the config file.
///
/// The remappings are added to the default profile in all cases, and to any other profile that
/// already has a `remappings key`. If the profile doesn't have a remappings key, it is left
/// untouched.
pub fn remappings_foundry(
    action: &RemappingsAction,
    paths: &Paths,
    soldeer_config: &SoldeerConfig,
) -> Result<()> {
    let contents = fs::read_to_string(&paths.config)?;
    let mut doc: DocumentMut =
        contents.parse::<DocumentMut>().expect("config file should be valid toml");
    let Some(profiles) = doc["profile"].as_table_mut() else {
        // we don't add remappings if there are no profiles
        debug!("no config profile found, skipping remappings generation");
        return Ok(());
    };

    for (name, profile) in profiles.iter_mut() {
        // we normally only edit remappings of profiles which already have a remappings key
        match profile.get_mut("remappings").map(|v| v.as_array_mut()) {
            Some(Some(remappings)) => {
                debug!(name:% = name; "updating remappings for profile");
                let existing_remappings: Vec<_> = remappings
                    .iter()
                    .filter_map(|r| r.as_str())
                    .filter_map(|r| r.split_once('='))
                    .collect();
                let new_remappings =
                    generate_remappings(action, paths, soldeer_config, &existing_remappings)?;
                remappings.clear();
                for remapping in new_remappings {
                    remappings.push(remapping);
                }
                format_array(remappings);
            }
            _ => {
                if name == "default" {
                    debug!("updating remappings for default profile");
                    // except the default profile, where we always add the remappings
                    let new_remappings = generate_remappings(action, paths, soldeer_config, &[])?;
                    let mut array = new_remappings.into_iter().collect::<Array>();
                    format_array(&mut array);
                    profile["remappings"] = value(array);
                }
            }
        }
    }

    fs::write(&paths.config, doc.to_string())?;
    debug!(path:? = paths.config; "remappings updated in config file");

    Ok(())
}

/// Edit the remappings according to the action and the configuration.
///
/// Depending on the configuration, the remappings are either stored in a `remappings.txt` file or
/// in the `foundry.toml` config file.
///
/// Note that if the config is stored in a dedicated `soldeer.toml` file, then the
/// `remappings_location` setting is ignored and the remappings are always stored in a
/// `remappings.txt` file.
pub fn edit_remappings(
    action: &RemappingsAction,
    config: &SoldeerConfig,
    paths: &Paths,
) -> Result<()> {
    if config.remappings_generate {
        if paths.config.to_string_lossy().contains("foundry.toml") {
            match config.remappings_location {
                RemappingsLocation::Txt => {
                    debug!("updating remappings.txt according to config option");
                    remappings_txt(action, paths, config)?;
                }
                RemappingsLocation::Config => {
                    debug!("updating foundry.toml remappings according to config option");
                    remappings_foundry(action, paths, config)?;
                }
            }
        } else {
            debug!("updating remappings.txt because config file is soldeer.toml");
            remappings_txt(action, paths, config)?;
        }
    } else {
        debug!("skipping remappings update according to config option");
    }
    Ok(())
}

/// Format the default left part (alias) for a remappings entry.
///
/// The optional `remappings_prefix` setting is prepended to the dependency name, and the
/// version requirement string is appended (after a hyphen) if the `remappings_version` setting is
/// set to `true`. Finally, a trailing slash is added to the alias.
pub fn format_remap_name(soldeer_config: &SoldeerConfig, dependency: &Dependency) -> String {
    let version_suffix = if soldeer_config.remappings_version {
        &format!("-{}", dependency.version_req().replace('=', ""))
    } else {
        ""
    };
    format!("{}{}{}/", soldeer_config.remappings_prefix, dependency.name(), version_suffix)
}

/// Generate the remappings for a given action.
///
/// If the `remappings_regenerate` option is set to `true`, then any existing remappings are
/// discarded and the remappings are generated from the dependencies in the config file.
///
/// Otherwise, existing remappings are kept, and depending on the action, a remapping entry is added
/// or removed. For the [`RemappingsAction::Update`] action, the existing remappings are merged with
/// the dependencies in the config file.
///
/// Dependencies are sorted alphabetically for consistency.
fn generate_remappings(
    action: &RemappingsAction,
    paths: &Paths,
    soldeer_config: &SoldeerConfig,
    existing_remappings: &[(&str, &str)],
) -> Result<Vec<String>> {
    let mut new_remappings = Vec::new();
    if soldeer_config.remappings_regenerate {
        debug!("ignoring existing remappings and recreating from config");
        let (dependencies, _) = read_config_deps(&paths.config)?;
        new_remappings = remappings_from_deps(&dependencies, paths, soldeer_config)?
            .into_iter()
            .map(|i| i.remapping_string)
            .collect();
    } else {
        match &action {
            RemappingsAction::Remove(remove_dep) => {
                debug!(dep:% = remove_dep; "trying to remove dependency from remappings");
                // only keep items not matching the dependency to remove
                if let Ok(remove_og) = get_install_dir_relative(remove_dep, paths) {
                    for (existing_remapped, existing_og) in existing_remappings {
                        // TODO: make the detection smarter, and match on any path where the version
                        // is semver-compatible too.
                        if !existing_og.trim_end_matches('/').starts_with(&remove_og) {
                            new_remappings.push(format!("{existing_remapped}={existing_og}"));
                        } else {
                            debug!(dep:% = remove_dep; "found existing remapping corresponding to dependency to remove");
                        }
                    }
                } else {
                    debug!(dep:% = remove_dep; "could not find a directory matching the dependency to remove");
                    for (remapped, og) in existing_remappings {
                        new_remappings.push(format!("{remapped}={og}"));
                    }
                }
            }
            RemappingsAction::Add(add_dep) => {
                debug!(dep:% = add_dep; "adding remapping for dependency if necessary");
                // we only add the remapping if it's not already existing, otherwise we keep the old
                // remapping
                let add_dep_remapped = format_remap_name(soldeer_config, add_dep);
                let add_dep_og = get_install_dir_relative(add_dep, paths)?;
                let mut found = false; // whether a remapping existed for that dep already
                for (existing_remapped, existing_og) in existing_remappings {
                    new_remappings.push(format!("{existing_remapped}={existing_og}"));
                    if existing_og.trim_end_matches('/').starts_with(&add_dep_og) {
                        debug!(dep:% = add_dep; "remapping exists already, skipping");
                        found = true;
                    }
                }
                if !found {
                    debug!(dep:% = add_dep; "remapping not found, adding it");
                    new_remappings.push(format!("{add_dep_remapped}={add_dep_og}/"));
                }
            }
            RemappingsAction::Update => {
                // This is where we end up in the `update` command if we don't want to re-generate
                // all remappings. We need to merge existing remappings with the full list of deps.
                // We generate all remappings from the dependencies, then replace existing items.
                debug!("updating remappings, merging existing ones with the ones generated from config");
                let (dependencies, _) = read_config_deps(&paths.config)?;
                let new_remappings_info =
                    remappings_from_deps(&dependencies, paths, soldeer_config)?;
                if existing_remappings.is_empty() {
                    debug!("no existing remappings, using the ones from config");
                    new_remappings =
                        new_remappings_info.into_iter().map(|i| i.remapping_string).collect();
                } else {
                    let mut existing_remappings = Vec::from(existing_remappings);
                    for RemappingInfo { remapping_string: item, dependency: dep } in
                        new_remappings_info
                    {
                        debug!(dep:% = dep; "trying to find a matching existing remapping for config item");
                        let (_, item_og) =
                            item.split_once('=').expect("remappings should have two parts");
                        // try to find all existing items pointing to a matching dependency folder
                        let mut found = false;
                        existing_remappings.retain(|(existing_remapped, existing_og)| {
                            // only keep the first two components of the path (`dependencies`
                            // folder and the dependency folder)
                            let path: PathBuf =
                                PathBuf::from(existing_og).components().take(2).collect();
                            // if path matches, we should update the item's path with the new
                            // one and add it to the final list
                            if path_matches(&dep, &path) {
                                debug!(path = existing_og; "existing remapping matches the config item");
                                let path: PathBuf =
                                    PathBuf::from(existing_og).components().take(2).collect();
                                let existing_og_updated = existing_og.replace(
                                    path.to_slash_lossy().as_ref(),
                                    item_og.trim_end_matches('/'),
                                );
                                debug!(new_path = existing_og_updated; "updated remapping path");
                                new_remappings
                                    .push(format!("{existing_remapped}={existing_og_updated}"));
                                found = true;
                                // we remove this item from the existing remappings list as it's
                                // been processed
                                return false;
                            }
                            // keep this item to add it to the remappings again later
                            true
                        });
                        if !found {
                            debug!(dep:% = dep;"no existing remapping found for config item, adding it");
                            new_remappings.push(item);
                        }
                    }
                    // add extra existing remappings back
                    for (existing_remapped, existing_og) in existing_remappings {
                        debug!(path = existing_og; "adding extra remapping which was existing but didn't match a config item");
                        new_remappings.push(format!("{existing_remapped}={existing_og}"));
                    }
                }
            }
        }
    }

    // sort the remappings
    new_remappings.sort_unstable();
    Ok(new_remappings)
}

#[derive(Debug, Clone, From)]
struct RemappingInfo {
    remapping_string: String,
    dependency: Dependency,
}

/// Generate remappings from the dependencies list.
///
/// The remappings are generated in the form `alias/=path/`, where `alias` is the dependency name
/// with an optional prefix and version requirement suffix, and `path` is the relative path to the
/// dependency folder.
fn remappings_from_deps(
    dependencies: &[Dependency],
    paths: &Paths,
    soldeer_config: &SoldeerConfig,
) -> Result<Vec<RemappingInfo>> {
    dependencies
        .par_iter()
        .map(|dependency| {
            let dependency_name_formatted = format_remap_name(soldeer_config, dependency); // contains trailing slash
            let relative_path = get_install_dir_relative(dependency, paths)?;
            Ok((format!("{dependency_name_formatted}={relative_path}/"), dependency.clone()).into())
        })
        .collect::<Result<Vec<RemappingInfo>>>()
}

/// Find the install path (relative to project root) for a dependency that was already installed
///
/// # Errors
/// If the there is no folder in the dependencies folder corresponding to the dependency
fn get_install_dir_relative(dependency: &Dependency, paths: &Paths) -> Result<String> {
    let path = dunce::canonicalize(
        dependency
            .install_path_sync(&paths.dependencies)
            .ok_or(RemappingsError::DependencyNotFound(dependency.to_string()))?,
    )?;
    Ok(path
        .strip_prefix(&paths.root) // already canonicalized
        .map_err(|_| RemappingsError::DependencyNotFound(dependency.to_string()))?
        .to_slash_lossy()
        .to_string())
}

/// Format a TOML array as a multi-line array with indentation in case there is more than one
/// element.
///
/// # Examples
///
/// ```toml
/// [profile.default]
/// remappings = []
/// ```
///
/// ```toml
/// [profile.default]
/// remappings = ["lib1-1.0.0/=dependencies/lib1-1.0.0/"]
/// ```
///
/// ```toml
/// [profile.default]
/// remappings = [
///     "lib1-1.0.0/=dependencies/lib1-1.0.0/",
///     "lib2-2.0.0/=dependencies/lib2-2.0.0/",
/// ]
/// ```
fn format_array(array: &mut Array) {
    array.fmt();
    if (0..=1).contains(&array.len()) {
        array.set_trailing("");
        array.set_trailing_comma(false);
    } else {
        for item in array.iter_mut() {
            item.decor_mut().set_prefix("\n    ");
        }
        array.set_trailing("\n");
        array.set_trailing_comma(true);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{GitDependency, HttpDependency};
    use testdir::testdir;

    #[test]
    fn test_get_install_dir_relative() {
        let dir = testdir!();
        fs::write(dir.join("soldeer.toml"), "[dependencies]\n").unwrap();
        let dependencies_dir = dir.join("dependencies");
        fs::create_dir_all(&dependencies_dir).unwrap();
        let paths = Paths::from_root(&dir).unwrap();

        fs::create_dir_all(dependencies_dir.join("dep1-1.1.1")).unwrap();
        let dependency =
            HttpDependency::builder().name("dep1").version_req("^1.0.0").build().into();
        let res = get_install_dir_relative(&dependency, &paths);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(res.unwrap(), "dependencies/dep1-1.1.1");

        fs::create_dir_all(dependencies_dir.join("dep2-2.0.0")).unwrap();
        let dependency = GitDependency::builder()
            .name("dep2")
            .version_req("2.0.0")
            .git("git@github.com:test/test.git")
            .build()
            .into();
        let res = get_install_dir_relative(&dependency, &paths);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(res.unwrap(), "dependencies/dep2-2.0.0");

        let dependency = HttpDependency::builder().name("dep3").version_req("3.0.0").build().into();
        let res = get_install_dir_relative(&dependency, &paths);
        assert!(res.is_err(), "{res:?}");
    }

    #[test]
    fn test_format_remap_name() {
        let dependency =
            HttpDependency::builder().name("dep1").version_req("^1.0.0").build().into();
        let res = format_remap_name(
            &SoldeerConfig {
                remappings_version: false,
                remappings_prefix: String::new(),
                ..Default::default()
            },
            &dependency,
        );
        assert_eq!(res, "dep1/");
        let res = format_remap_name(
            &SoldeerConfig {
                remappings_version: true,
                remappings_prefix: String::new(),
                ..Default::default()
            },
            &dependency,
        );
        assert_eq!(res, "dep1-^1.0.0/");
        let res = format_remap_name(
            &SoldeerConfig {
                remappings_version: false,
                remappings_prefix: "@".to_string(),
                ..Default::default()
            },
            &dependency,
        );
        assert_eq!(res, "@dep1/");
        let res = format_remap_name(
            &SoldeerConfig {
                remappings_version: true,
                remappings_prefix: "@".to_string(),
                ..Default::default()
            },
            &dependency,
        );
        assert_eq!(res, "@dep1-^1.0.0/");

        let dependency =
            HttpDependency::builder().name("dep1").version_req("=1.0.0").build().into();
        let res = format_remap_name(
            &SoldeerConfig {
                remappings_version: true,
                remappings_prefix: String::new(),
                ..Default::default()
            },
            &dependency,
        );
        assert_eq!(res, "dep1-1.0.0/");
    }

    #[test]
    fn test_remappings_from_deps() {
        let dir = testdir!();
        let config = r#"[dependencies]
dep1 = "^1.0.0"
dep2 = "2.0.0"
dep3 = { version = "foobar", git = "git@github.com:test/test.git", branch = "foobar" }
"#;
        fs::write(dir.join("soldeer.toml"), config).unwrap();
        let dependencies_dir = dir.join("dependencies");
        fs::create_dir_all(&dependencies_dir).unwrap();
        let paths = Paths::from_root(&dir).unwrap();

        fs::create_dir_all(dependencies_dir.join("dep1-1.1.1")).unwrap();
        fs::create_dir_all(dependencies_dir.join("dep2-2.0.0")).unwrap();
        fs::create_dir_all(dependencies_dir.join("dep3-foobar")).unwrap();

        let (dependencies, _) = read_config_deps(&paths.config).unwrap();
        let res = remappings_from_deps(&dependencies, &paths, &SoldeerConfig::default());
        assert!(res.is_ok(), "{res:?}");
        let res = res.unwrap();
        assert_eq!(res.len(), 3);
        assert_eq!(res[0].remapping_string, "dep1-^1.0.0/=dependencies/dep1-1.1.1/");
        assert_eq!(res[1].remapping_string, "dep2-2.0.0/=dependencies/dep2-2.0.0/");
        assert_eq!(res[2].remapping_string, "dep3-foobar/=dependencies/dep3-foobar/");
    }

    #[test]
    fn test_generate_remappings_add() {
        let dir = testdir!();
        fs::write(dir.join("soldeer.toml"), "[dependencies]\n").unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        let config = SoldeerConfig::default();
        // empty existing remappings
        let existing_deps = vec![];
        let dep = HttpDependency::builder().name("lib1").version_req("1.0.0").build().into();
        let res = generate_remappings(&RemappingsAction::Add(dep), &paths, &config, &existing_deps);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(res.unwrap(), vec!["lib1-1.0.0/=dependencies/lib1-1.0.0/"]);

        // existing remappings not matching new one
        let existing_deps = vec![("lib1-1.0.0/", "dependencies/lib1-1.0.0/")];
        fs::create_dir_all(paths.dependencies.join("lib2-1.1.1")).unwrap();
        let dep = HttpDependency::builder().name("lib2").version_req("^1.0.0").build().into();
        let res = generate_remappings(&RemappingsAction::Add(dep), &paths, &config, &existing_deps);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(
            res.unwrap(),
            vec!["lib1-1.0.0/=dependencies/lib1-1.0.0/", "lib2-^1.0.0/=dependencies/lib2-1.1.1/"]
        );

        // existing remappings matching the new one
        let existing_deps = vec![("@lib1-1.0.0/foo", "dependencies/lib1-1.0.0/src")];
        let dep = HttpDependency::builder().name("lib1").version_req("1.0.0").build().into();
        let res = generate_remappings(&RemappingsAction::Add(dep), &paths, &config, &existing_deps);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(res.unwrap(), vec!["@lib1-1.0.0/foo=dependencies/lib1-1.0.0/src"]);
    }

    #[test]
    fn test_generate_remappings_remove() {
        let dir = testdir!();
        fs::write(dir.join("soldeer.toml"), "[dependencies]\n").unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib2-2.0.0")).unwrap();
        let config = SoldeerConfig::default();
        let existing_deps = vec![
            ("lib1-1.0.0/", "dependencies/lib1-1.0.0/"),
            ("lib2-2.0.0/", "dependencies/lib2-2.0.0/"),
        ];
        let dep = HttpDependency::builder().name("lib1").version_req("1.0.0").build().into();
        let res =
            generate_remappings(&RemappingsAction::Remove(dep), &paths, &config, &existing_deps);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(res.unwrap(), vec!["lib2-2.0.0/=dependencies/lib2-2.0.0/"]);

        // dep does not exist, no error
        let dep = HttpDependency::builder().name("lib3").version_req("1.0.0").build().into();
        let res =
            generate_remappings(&RemappingsAction::Remove(dep), &paths, &config, &existing_deps);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(
            res.unwrap(),
            vec!["lib1-1.0.0/=dependencies/lib1-1.0.0/", "lib2-2.0.0/=dependencies/lib2-2.0.0/"]
        );
    }

    #[test]
    fn test_generate_remappings_update() {
        let dir = testdir!();
        let contents = r#"[dependencies]
lib1 = "1.0.0"
lib2 = "2.0.0"
"#;
        fs::write(dir.join("soldeer.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib2-2.0.0")).unwrap();
        let config = SoldeerConfig::default();
        // all entries are customized
        let existing_deps = vec![
            ("lib1-1.0.0/", "dependencies/lib1-1.0.0/src/"),
            ("lib2/", "dependencies/lib2-2.0.0/"),
        ];
        let res = generate_remappings(&RemappingsAction::Update, &paths, &config, &existing_deps);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(
            res.unwrap(),
            vec!["lib1-1.0.0/=dependencies/lib1-1.0.0/src/", "lib2/=dependencies/lib2-2.0.0/"]
        );

        // one entry is missing
        let existing_deps = vec![("lib1-1.0.0/", "dependencies/lib1-1.0.0/")];
        let res = generate_remappings(&RemappingsAction::Update, &paths, &config, &existing_deps);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(
            res.unwrap(),
            vec!["lib1-1.0.0/=dependencies/lib1-1.0.0/", "lib2-2.0.0/=dependencies/lib2-2.0.0/"]
        );

        // extra entries are kep
        let existing_deps = vec![
            ("lib1-1.0.0/", "dependencies/lib1-1.0.0/"),
            ("lib2-2.0.0/", "dependencies/lib2-2.0.0/"),
            ("lib3/", "dependencies/lib3/"),
        ];
        let res = generate_remappings(&RemappingsAction::Update, &paths, &config, &existing_deps);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(
            res.unwrap(),
            vec![
                "lib1-1.0.0/=dependencies/lib1-1.0.0/",
                "lib2-2.0.0/=dependencies/lib2-2.0.0/",
                "lib3/=dependencies/lib3/"
            ]
        );
    }

    #[test]
    fn test_remappings_foundry_default_profile_empty() {
        let dir = testdir!();
        let contents = r#"[profile.default]

[dependencies]
lib1 = "1.0.0"
"#;
        fs::write(dir.join("foundry.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        let config = SoldeerConfig::default();
        let res = remappings_foundry(&RemappingsAction::Update, &paths, &config);
        assert!(res.is_ok(), "{res:?}");
        let contents = fs::read_to_string(&paths.config).unwrap();
        let doc: DocumentMut = contents.parse::<DocumentMut>().unwrap();
        assert_eq!(
            doc["profile"]["default"]["remappings"]
                .as_array()
                .unwrap()
                .into_iter()
                .map(|i| i.as_str().unwrap())
                .collect::<Vec<_>>(),
            vec!["lib1-1.0.0/=dependencies/lib1-1.0.0/"]
        );
    }

    #[test]
    fn test_remappings_foundry_second_profile_empty() {
        let dir = testdir!();
        let contents = r#"[profile.default]

[profile.local]

[dependencies]
lib1 = "1.0.0"
"#;
        fs::write(dir.join("foundry.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        let config = SoldeerConfig::default();
        // should only add remappings to the default profile
        let res = remappings_foundry(&RemappingsAction::Update, &paths, &config);
        assert!(res.is_ok(), "{res:?}");
        let contents = fs::read_to_string(&paths.config).unwrap();
        let doc: DocumentMut = contents.parse::<DocumentMut>().unwrap();
        assert_eq!(
            doc["profile"]["default"]["remappings"]
                .as_array()
                .unwrap()
                .into_iter()
                .map(|i| i.as_str().unwrap())
                .collect::<Vec<_>>(),
            vec!["lib1-1.0.0/=dependencies/lib1-1.0.0/"]
        );
        assert!(!doc["profile"]["local"].as_table().unwrap().contains_key("remappings"));
    }

    #[test]
    fn test_remappings_foundry_two_profiles() {
        let dir = testdir!();
        let contents = r#"[profile.default]
remappings = []

[profile.local]
remappings = []

[dependencies]
lib1 = "1.0.0"
"#;
        fs::write(dir.join("foundry.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        let config = SoldeerConfig::default();
        let res = remappings_foundry(&RemappingsAction::Update, &paths, &config);
        assert!(res.is_ok(), "{res:?}");
        let contents = fs::read_to_string(&paths.config).unwrap();
        let doc: DocumentMut = contents.parse::<DocumentMut>().unwrap();
        assert_eq!(
            doc["profile"]["default"]["remappings"]
                .as_array()
                .unwrap()
                .into_iter()
                .map(|i| i.as_str().unwrap())
                .collect::<Vec<_>>(),
            vec!["lib1-1.0.0/=dependencies/lib1-1.0.0/"]
        );
        assert_eq!(
            doc["profile"]["local"]["remappings"]
                .as_array()
                .unwrap()
                .into_iter()
                .map(|i| i.as_str().unwrap())
                .collect::<Vec<_>>(),
            vec!["lib1-1.0.0/=dependencies/lib1-1.0.0/"]
        );
    }

    #[test]
    fn test_remappings_foundry_keep_existing() {
        let dir = testdir!();
        let contents = r#"[profile.default]
remappings = ["lib1/=dependencies/lib1-1.0.0/src/"]

[dependencies]
lib1 = "1.0.0"
"#;
        fs::write(dir.join("foundry.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        let config = SoldeerConfig::default();
        let res = remappings_foundry(&RemappingsAction::Update, &paths, &config);
        assert!(res.is_ok(), "{res:?}");
        let contents = fs::read_to_string(&paths.config).unwrap();
        let doc: DocumentMut = contents.parse::<DocumentMut>().unwrap();
        assert_eq!(
            doc["profile"]["default"]["remappings"]
                .as_array()
                .unwrap()
                .into_iter()
                .map(|i| i.as_str().unwrap())
                .collect::<Vec<_>>(),
            vec!["lib1/=dependencies/lib1-1.0.0/src/"]
        );
    }

    #[test]
    fn test_remappings_txt_keep() {
        let dir = testdir!();
        let contents = r#"[dependencies]
lib1 = "1.0.0"
"#;
        fs::write(dir.join("soldeer.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        let remappings = "lib1/=dependencies/lib1-1.0.0/src/\n";
        fs::write(dir.join("remappings.txt"), remappings).unwrap();
        let config = SoldeerConfig::default();
        let res = remappings_txt(&RemappingsAction::Update, &paths, &config);
        assert!(res.is_ok(), "{res:?}");
        let contents = fs::read_to_string(&paths.remappings).unwrap();
        assert_eq!(contents, remappings);
    }

    #[test]
    fn test_remappings_txt_regenerate() {
        let dir = testdir!();
        let contents = r#"[dependencies]
lib1 = "1.0.0"
"#;
        fs::write(dir.join("soldeer.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        let remappings = "lib1/=dependencies/lib1-1.0.0/src/\n";
        fs::write(dir.join("remappings.txt"), remappings).unwrap();
        let config = SoldeerConfig { remappings_regenerate: true, ..Default::default() };
        let res = remappings_txt(&RemappingsAction::Update, &paths, &config);
        assert!(res.is_ok(), "{res:?}");
        let contents = fs::read_to_string(&paths.remappings).unwrap();
        assert_eq!(contents, "lib1-1.0.0/=dependencies/lib1-1.0.0/\n");
    }

    #[test]
    fn test_remappings_txt_missing() {
        let dir = testdir!();
        let contents = r#"[dependencies]
lib1 = "1.0.0"
lib2 = "2.0.0"
"#;
        fs::write(dir.join("soldeer.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib2-2.0.0")).unwrap();
        let remappings = "lib1/=dependencies/lib1-1.0.0/src/\n";
        fs::write(dir.join("remappings.txt"), remappings).unwrap();
        let config = SoldeerConfig::default();
        let res = remappings_txt(&RemappingsAction::Update, &paths, &config);
        assert!(res.is_ok(), "{res:?}");
        let contents = fs::read_to_string(&paths.remappings).unwrap();
        assert_eq!(
            contents,
            "lib1/=dependencies/lib1-1.0.0/src/\nlib2-2.0.0/=dependencies/lib2-2.0.0/\n"
        );
    }

    #[test]
    fn test_edit_remappings_soldeer_config() {
        let dir = testdir!();
        let contents = r#"[dependencies]
lib1 = "1.0.0"
"#;
        fs::write(dir.join("soldeer.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib1-1.0.0")).unwrap();
        // the config gets ignored in this case
        let config =
            SoldeerConfig { remappings_location: RemappingsLocation::Config, ..Default::default() };
        let res = edit_remappings(&RemappingsAction::Update, &config, &paths);
        assert!(res.is_ok(), "{res:?}");
        let contents = fs::read_to_string(&paths.remappings).unwrap();
        assert_eq!(contents, "lib1-1.0.0/=dependencies/lib1-1.0.0/\n");
    }

    #[test]
    fn test_generate_remappings_update_semver_custom() {
        let dir = testdir!();
        let contents = r#"[dependencies]
lib1 = "1"
lib2 = "2"
"#;
        fs::write(dir.join("soldeer.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        // libs have been updated to newer versions
        fs::create_dir_all(paths.dependencies.join("lib1-1.2.0")).unwrap();
        fs::create_dir_all(paths.dependencies.join("lib2-2.1.0")).unwrap();
        let config = SoldeerConfig::default();
        // all entries are customized, using an old version of the libs
        let existing_deps = vec![
            ("lib1-1/", "dependencies/lib1-1.1.1/src/"), // customize right part
            ("lib2/", "dependencies/lib2-2.0.1/src/"),   // customize both sides
        ];
        let res = generate_remappings(&RemappingsAction::Update, &paths, &config, &existing_deps);
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(
            res.unwrap(),
            vec!["lib1-1/=dependencies/lib1-1.2.0/src/", "lib2/=dependencies/lib2-2.1.0/src/"]
        );
    }

    #[test]
    fn test_generate_remappings_duplicates() {
        let dir = testdir!();
        let contents = r#"[profile.default]
remappings = [
    "@openzeppelin-contracts/=dependencies/@openzeppelin-contracts-5.0.2/",
    "@openzeppelin/contracts/=dependencies/@openzeppelin-contracts-5.0.2/",
    "foo/=bar/",
]
libs = ["dependencies"]

[dependencies]
"@openzeppelin-contracts" = "5.0.2"
"#;
        fs::write(dir.join("foundry.toml"), contents).unwrap();
        let paths = Paths::from_root(&dir).unwrap();
        fs::create_dir_all(paths.dependencies.join("@openzeppelin-contracts-5.0.2")).unwrap();
        let res = remappings_foundry(
            &RemappingsAction::Update,
            &paths,
            &SoldeerConfig {
                remappings_location: RemappingsLocation::Config,
                ..Default::default()
            },
        );
        assert!(res.is_ok(), "{res:?}");
        assert_eq!(fs::read_to_string(dir.join("foundry.toml")).unwrap(), contents);
    }
}