spin_templates/
manager.rs

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
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
use std::{io::IsTerminal, path::Path};

use anyhow::Context;

use crate::{
    source::TemplateSource,
    store::{TemplateLayout, TemplateStore},
    template::Template,
};

/// Provides access to and operations on the set of installed
/// templates.
pub struct TemplateManager {
    store: TemplateStore,
}

/// Used during template installation to report progress and
/// current activity.
pub trait ProgressReporter {
    /// Report the specified message.
    fn report(&self, message: impl AsRef<str>);
}

/// Options controlling template installation.
#[derive(Debug)]
pub struct InstallOptions {
    exists_behaviour: ExistsBehaviour,
}

impl InstallOptions {
    /// Sets the option to update existing templates. If `update` is true,
    /// existing templates are updated. If false, existing templates are
    /// skipped.
    pub fn update(self, update: bool) -> Self {
        let exists_behaviour = if update {
            ExistsBehaviour::Update
        } else {
            ExistsBehaviour::Skip
        };

        Self { exists_behaviour }
    }
}

impl Default for InstallOptions {
    fn default() -> Self {
        Self {
            exists_behaviour: ExistsBehaviour::Skip,
        }
    }
}

#[derive(Debug)]
enum ExistsBehaviour {
    Skip,
    Update,
}

#[allow(clippy::large_enum_variant)] // it's not worth it
enum InstallationResult {
    Installed(Template),
    Skipped(String, SkippedReason),
}

/// The reason a template was skipped during installation.
pub enum SkippedReason {
    /// The template was skipped because it was already present.
    AlreadyExists,
    /// The template was skipped because its manifest was missing or invalid.
    InvalidManifest(String),
}

/// The results of installing a set of templates.
pub struct InstallationResults {
    /// The templates that were installed during the install operation.
    pub installed: Vec<Template>,
    /// The templates that were skipped during the install operation.
    pub skipped: Vec<(String, SkippedReason)>,
}

/// The result of listing templates.
#[derive(Debug)]
pub struct ListResults {
    /// The installed templates.
    pub templates: Vec<Template>,
    /// Any warnings identified during the list operation.
    pub warnings: Vec<(String, InstalledTemplateWarning)>,
    /// Any skipped templates (populated as a result of filtering by tags).
    pub skipped: Vec<Template>,
}

impl ListResults {
    /// Returns true if no templates were found or skipped, and the UI should prompt to
    /// install templates. This is specifically for interactive use and returns false
    /// if not connected to a terminal.
    pub fn needs_install(&self) -> bool {
        self.templates.is_empty() && self.skipped.is_empty() && std::io::stderr().is_terminal()
    }
}

/// A recoverable problem while listing templates.
#[derive(Debug)]
pub enum InstalledTemplateWarning {
    /// The manifest is invalid. The directory may not represent a template.
    InvalidManifest(String),
}

impl TemplateManager {
    /// Creates a `TemplateManager` for the default install location.
    pub fn try_default() -> anyhow::Result<Self> {
        let store = TemplateStore::try_default()?;
        Ok(Self::new(store))
    }

    pub(crate) fn new(store: TemplateStore) -> Self {
        Self { store }
    }

    /// Installs templates from the specified source.
    pub async fn install(
        &self,
        source: &TemplateSource,
        options: &InstallOptions,
        reporter: &impl ProgressReporter,
    ) -> anyhow::Result<InstallationResults> {
        if source.requires_copy() {
            reporter.report("Copying remote template source");
        }

        let local_source = source
            .get_local()
            .await
            .context("Failed to get template source")?;
        let template_dirs = local_source
            .template_directories()
            .await
            .context("Could not find templates in source")?;

        let mut installed = vec![];
        let mut skipped = vec![];

        for template_dir in template_dirs {
            let install_result = self
                .install_one(&template_dir, options, source, reporter)
                .await
                .with_context(|| {
                    format!("Failed to install template from {}", template_dir.display())
                })?;
            match install_result {
                InstallationResult::Installed(template) => installed.push(template),
                InstallationResult::Skipped(id, reason) => skipped.push((id, reason)),
            }
        }

        installed.sort_by_key(|t| t.id().to_owned());
        skipped.sort_by_key(|(id, _)| id.clone());

        Ok(InstallationResults { installed, skipped })
    }

    async fn install_one(
        &self,
        source_dir: &Path,
        options: &InstallOptions,
        source: &TemplateSource,
        reporter: &impl ProgressReporter,
    ) -> anyhow::Result<InstallationResult> {
        let layout = TemplateLayout::new(source_dir);
        let template = match Template::load_from(&layout) {
            Ok(t) => t,
            Err(e) => {
                let fake_id = source_dir
                    .file_name()
                    .map(|s| s.to_string_lossy().to_string())
                    .unwrap_or_else(|| format!("{}", source_dir.display()));
                let message = format!("{}", e);
                return Ok(InstallationResult::Skipped(
                    fake_id,
                    SkippedReason::InvalidManifest(message),
                ));
            }
        };
        let id = template.id();

        let message = format!("Installing template {}...", id);
        reporter.report(&message);

        let dest_dir = self.store.get_directory(id);

        let template = if dest_dir.exists() {
            match options.exists_behaviour {
                ExistsBehaviour::Skip => {
                    return Ok(InstallationResult::Skipped(
                        id.to_owned(),
                        SkippedReason::AlreadyExists,
                    ))
                }
                ExistsBehaviour::Update => {
                    copy_template_over_existing(id, source_dir, &dest_dir, source).await?
                }
            }
        } else {
            copy_template_into(id, source_dir, &dest_dir, source).await?
        };

        Ok(InstallationResult::Installed(template))
    }

    /// Uninstalls the specified template.
    pub async fn uninstall(&self, template_id: impl AsRef<str>) -> anyhow::Result<()> {
        let template_dir = self.store.get_directory(template_id);
        tokio::fs::remove_dir_all(&template_dir)
            .await
            .with_context(|| {
                format!(
                    "Failed to delete template directory {}",
                    template_dir.display()
                )
            })
    }

    /// Lists all installed templates.
    pub async fn list(&self) -> anyhow::Result<ListResults> {
        let mut templates = vec![];
        let mut warnings = vec![];

        for template_layout in self.store.list_layouts().await? {
            match Template::load_from(&template_layout) {
                Ok(template) => templates.push(template),
                Err(e) => warnings.push(build_list_warning(&template_layout, e)?),
            }
        }

        templates.sort_by_key(|t| t.id().to_owned());

        Ok(ListResults {
            templates,
            warnings,
            skipped: vec![],
        })
    }

    /// Lists all installed templates that match all the provided tags.
    pub async fn list_with_tags(&self, tags: &[String]) -> anyhow::Result<ListResults> {
        let ListResults {
            templates,
            warnings,
            ..
        } = self.list().await?;

        let (templates, skipped) = templates
            .into_iter()
            .partition(|tpl| tpl.matches_all_tags(tags));

        Ok(ListResults {
            templates,
            warnings,
            skipped,
        })
    }

    /// Gets the specified template. The result will be `Ok(Some(template))` if
    /// the template was found, and `Ok(None)` if the template was not
    /// found.
    pub fn get(&self, id: impl AsRef<str>) -> anyhow::Result<Option<Template>> {
        self.store
            .get_layout(id)
            .map(|l| Template::load_from(&l))
            .transpose()
    }
}

async fn copy_template_over_existing(
    id: &str,
    source_dir: &Path,
    dest_dir: &Path,
    source: &TemplateSource,
) -> anyhow::Result<Template> {
    // The nearby directory to which we initially copy the source
    let stage_dir = dest_dir.with_extension(".stage");
    // The nearby directory to which we move the existing
    let unstage_dir = dest_dir.with_extension(".unstage");

    // Clean up temp directories in case left over from previous failures.
    if stage_dir.exists() {
        tokio::fs::remove_dir_all(&stage_dir)
            .await
            .with_context(|| {
                format!(
                    "Failed while deleting {} in order to update {}",
                    stage_dir.display(),
                    id
                )
            })?
    };

    if unstage_dir.exists() {
        tokio::fs::remove_dir_all(&unstage_dir)
            .await
            .with_context(|| {
                format!(
                    "Failed while deleting {} in order to update {}",
                    unstage_dir.display(),
                    id
                )
            })?
    };

    // Copy template source into stage directory, and do best effort
    // cleanup if it goes wrong.
    let copy_to_stage_err = copy_template_into(id, source_dir, &stage_dir, source)
        .await
        .err();
    if let Some(e) = copy_to_stage_err {
        let _ = tokio::fs::remove_dir_all(&stage_dir).await;
        return Err(e);
    };

    // We have a valid template in stage.  Now, move existing to unstage...
    if let Err(e) = tokio::fs::rename(dest_dir, &unstage_dir)
        .await
        .with_context(|| {
            format!(
                "Failed to move existing template out of {} in order to update {}",
                dest_dir.display(),
                id
            )
        })
    {
        let _ = tokio::fs::remove_dir_all(&stage_dir).await;
        return Err(e);
    }

    // ...and move stage into position.
    if let Err(e) = tokio::fs::rename(&stage_dir, dest_dir)
        .await
        .with_context(|| {
            format!(
                "Failed to move new template into {} in order to update {}",
                dest_dir.display(),
                id
            )
        })
    {
        // Put it back quick and hope nobody notices.
        let _ = tokio::fs::rename(&unstage_dir, dest_dir).await;
        let _ = tokio::fs::remove_dir_all(&stage_dir).await;
        return Err(e);
    }

    // Remove whichever directories remain.  (As we are ignoring errors, we
    // can skip checking whether the directories exist.)
    let _ = tokio::fs::remove_dir_all(&stage_dir).await;
    let _ = tokio::fs::remove_dir_all(&unstage_dir).await;

    load_template_from(id, dest_dir)
}

async fn copy_template_into(
    id: &str,
    source_dir: &Path,
    dest_dir: &Path,
    source: &TemplateSource,
) -> anyhow::Result<Template> {
    tokio::fs::create_dir_all(&dest_dir)
        .await
        .with_context(|| {
            format!(
                "Failed to create directory {} for {}",
                dest_dir.display(),
                id
            )
        })?;

    fs_extra::dir::copy(source_dir, dest_dir, &copy_content()).with_context(|| {
        format!(
            "Failed to copy template content from {} to {} for {}",
            source_dir.display(),
            dest_dir.display(),
            id
        )
    })?;

    write_install_record(dest_dir, source);

    load_template_from(id, dest_dir)
}

fn write_install_record(dest_dir: &Path, source: &TemplateSource) {
    let layout = TemplateLayout::new(dest_dir);
    let install_record_path = layout.installation_record_file();

    // A failure here shouldn't fail the install
    let install_record = source.to_install_record();
    if let Ok(record_text) = toml::to_string_pretty(&install_record) {
        _ = std::fs::write(install_record_path, record_text);
    }
}

fn load_template_from(id: &str, dest_dir: &Path) -> anyhow::Result<Template> {
    let layout = TemplateLayout::new(dest_dir);
    Template::load_from(&layout).with_context(|| {
        format!(
            "Template {} was not copied correctly into {}",
            id,
            dest_dir.display()
        )
    })
}

fn copy_content() -> fs_extra::dir::CopyOptions {
    let mut options = fs_extra::dir::CopyOptions::new();
    options.content_only = true;
    options
}

fn build_list_warning(
    template_layout: &TemplateLayout,
    load_err: anyhow::Error,
) -> anyhow::Result<(String, InstalledTemplateWarning)> {
    match template_layout.metadata_dir().parent() {
        Some(source_dir) => {
            let fake_id = source_dir
                .file_name()
                .map(|s| s.to_string_lossy().to_string())
                .unwrap_or_else(|| format!("{}", source_dir.display()));
            let message = format!("{}", load_err);
            Ok((fake_id, InstalledTemplateWarning::InvalidManifest(message)))
        }
        None => Err(load_err).context("Failed to load template but unable to determine which one"),
    }
}

impl InstallationResults {
    /// Gets whether the `InstallationResults` contains no templates. This
    /// indicates that no templates were found in the installation source.
    pub fn is_empty(&self) -> bool {
        self.installed.is_empty() && self.skipped.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, fs, path::PathBuf};

    use tempfile::tempdir;

    use crate::{RunOptions, TemplateVariantInfo};

    use super::*;

    struct DiscardingReporter;

    impl ProgressReporter for DiscardingReporter {
        fn report(&self, _: impl AsRef<str>) {
            // Commit it then to the flames: for it can contain nothing but
            // sophistry and illusion.
        }
    }

    fn project_root() -> PathBuf {
        let crate_dir = env!("CARGO_MANIFEST_DIR");
        PathBuf::from(crate_dir).join("..").join("..")
    }

    fn test_data_root() -> PathBuf {
        let crate_dir = env!("CARGO_MANIFEST_DIR");
        PathBuf::from(crate_dir).join("tests")
    }

    // A template manager in a temp dir.  This exists to ensure the lifetime
    // of the TempDir
    struct TempManager {
        temp_dir: tempfile::TempDir,
        manager: TemplateManager,
    }

    impl TempManager {
        fn new() -> Self {
            let temp_dir = tempdir().unwrap();
            let store = TemplateStore::new(temp_dir.path());
            let manager = TemplateManager { store };
            Self { temp_dir, manager }
        }

        async fn new_with_this_repo_templates() -> Self {
            let mgr = Self::new();
            mgr.install_this_repo_templates().await;
            mgr
        }

        async fn install_this_repo_templates(&self) -> InstallationResults {
            let source = TemplateSource::File(project_root());
            self.manager
                .install(&source, &InstallOptions::default(), &DiscardingReporter)
                .await
                .unwrap()
        }

        async fn install_test_data_templates(&self) -> InstallationResults {
            let source = TemplateSource::File(test_data_root());
            self.manager
                .install(&source, &InstallOptions::default(), &DiscardingReporter)
                .await
                .unwrap()
        }

        fn store_dir(&self) -> &Path {
            self.temp_dir.path()
        }
    }

    impl std::ops::Deref for TempManager {
        type Target = TemplateManager;

        fn deref(&self) -> &Self::Target {
            &self.manager
        }
    }

    // Constructs an options object to control how a template is run, with defaults.
    // to save repeating the same thing over and over! By default, the options are:
    // * Create a mew application
    // * Dummy parameter values to satisfy the HTTP template
    // * All other flags are false
    // Use the `rest` callback to modify fields not given in the `name` and `output_path`
    // arguments. For example, to create a RunOptions with `no_vcs` turned on, do:
    // `run_options(name, path, |opts| { opts.no_vcs = true; })`
    fn run_options(
        name: impl AsRef<str>,
        output_path: impl AsRef<Path>,
        rest: impl FnOnce(&mut RunOptions),
    ) -> RunOptions {
        let mut options = RunOptions {
            variant: crate::template::TemplateVariantInfo::NewApplication,
            name: name.as_ref().to_owned(),
            output_path: output_path.as_ref().to_owned(),
            values: dummy_values(),
            accept_defaults: false,
            no_vcs: false,
            allow_overwrite: false,
        };
        rest(&mut options);
        options
    }

    fn make_values<SK: ToString, SV: ToString>(
        values: impl IntoIterator<Item = (SK, SV)>,
    ) -> HashMap<String, String> {
        values
            .into_iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect()
    }

    fn dummy_values() -> HashMap<String, String> {
        make_values([
            ("project-description", "dummy desc"),
            ("http-path", "/dummy/dummy/dummy/..."),
        ])
    }

    fn add_component_to(manifest_path: impl AsRef<Path>) -> crate::template::TemplateVariantInfo {
        crate::template::TemplateVariantInfo::AddComponent {
            manifest_path: manifest_path.as_ref().to_owned(),
        }
    }

    const TPLS_IN_THIS: usize = 11;

    #[tokio::test]
    async fn can_install_into_new_directory() {
        let manager = TempManager::new();
        assert_eq!(0, manager.list().await.unwrap().templates.len());

        let install_result = manager.install_this_repo_templates().await;
        assert_eq!(TPLS_IN_THIS, install_result.installed.len());
        assert_eq!(0, install_result.skipped.len());

        assert_eq!(TPLS_IN_THIS, manager.list().await.unwrap().templates.len());
        assert_eq!(0, manager.list().await.unwrap().warnings.len());
    }

    #[tokio::test]
    async fn skips_bad_templates() {
        let manager = TempManager::new();

        let temp_source = tempdir().unwrap();
        let temp_source_tpls_dir = temp_source.path().join("templates");
        fs_extra::dir::copy(
            project_root().join("templates"),
            &temp_source_tpls_dir,
            &copy_content(),
        )
        .unwrap();
        fs::create_dir(temp_source_tpls_dir.join("notta-template")).unwrap();
        let source = TemplateSource::File(temp_source.path().to_owned());

        assert_eq!(0, manager.list().await.unwrap().templates.len());
        assert_eq!(0, manager.list().await.unwrap().warnings.len());

        let install_result = manager
            .install(&source, &InstallOptions::default(), &DiscardingReporter)
            .await
            .unwrap();
        assert_eq!(TPLS_IN_THIS, install_result.installed.len());
        assert_eq!(1, install_result.skipped.len());

        assert!(matches!(
            install_result.skipped[0].1,
            SkippedReason::InvalidManifest(_)
        ));
    }

    #[tokio::test]
    async fn can_list_all_templates_with_empty_tags() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let list_results = manager.list_with_tags(&[]).await.unwrap();
        assert_eq!(0, list_results.skipped.len());
        assert_eq!(TPLS_IN_THIS, list_results.templates.len());
    }

    #[tokio::test]
    async fn skips_when_all_tags_do_not_match() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let tags_to_match = vec!["c".to_string(), "unused_tag".to_string()];

        let list_results = manager.list_with_tags(&tags_to_match).await.unwrap();
        assert_eq!(TPLS_IN_THIS, list_results.skipped.len());
        assert_eq!(0, list_results.templates.len());
    }

    #[tokio::test]
    async fn can_list_templates_with_multiple_tags() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let tags_to_match = vec!["http".to_string(), "c".to_string()];

        let list_results = manager.list_with_tags(&tags_to_match).await.unwrap();
        assert_eq!(TPLS_IN_THIS - 1, list_results.skipped.len());
        assert_eq!(1, list_results.templates.len());
    }

    #[tokio::test]
    async fn can_list_templates_with_case_insensitive_tags() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let list_results = manager.list_with_tags(&["C".to_string()]).await.unwrap();
        assert_eq!(TPLS_IN_THIS - 1, list_results.skipped.len());
        assert_eq!(1, list_results.templates.len());
    }

    #[tokio::test]
    async fn can_skip_templates_with_missing_tag() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let list_results = manager
            .list_with_tags(&["unused_tag".to_string()])
            .await
            .unwrap();
        assert_eq!(TPLS_IN_THIS, list_results.skipped.len());
        assert_eq!(0, list_results.templates.len());
    }

    #[tokio::test]
    async fn can_list_if_bad_dir_in_store() {
        let manager = TempManager::new();
        assert_eq!(0, manager.list().await.unwrap().templates.len());

        manager.install_this_repo_templates().await;
        assert_eq!(TPLS_IN_THIS, manager.list().await.unwrap().templates.len());
        assert_eq!(0, manager.list().await.unwrap().warnings.len());

        fs::create_dir(manager.store_dir().join("i-trip-you-up")).unwrap();

        let list_results = manager.list().await.unwrap();
        assert_eq!(TPLS_IN_THIS, list_results.templates.len());
        assert_eq!(1, list_results.warnings.len());
        assert_eq!("i-trip-you-up", list_results.warnings[0].0);
        assert!(matches!(
            list_results.warnings[0].1,
            InstalledTemplateWarning::InvalidManifest(_)
        ));
    }

    #[tokio::test]
    async fn can_uninstall() {
        let manager = TempManager::new_with_this_repo_templates().await;
        assert_eq!(TPLS_IN_THIS, manager.list().await.unwrap().templates.len());

        manager.uninstall("http-rust").await.unwrap();

        let installed = manager.list().await.unwrap();
        assert_eq!(TPLS_IN_THIS - 1, installed.templates.len());
        assert_eq!(0, installed.warnings.len());
        assert!(!installed.templates.iter().any(|t| t.id() == "http-rust"));
    }

    #[tokio::test]
    async fn can_install_if_some_already_exist() {
        let manager = TempManager::new_with_this_repo_templates().await;
        manager.uninstall("http-rust").await.unwrap();
        manager.uninstall("http-go").await.unwrap();
        assert_eq!(
            TPLS_IN_THIS - 2,
            manager.list().await.unwrap().templates.len()
        );

        let install_result = manager.install_this_repo_templates().await;
        assert_eq!(2, install_result.installed.len());
        assert_eq!(TPLS_IN_THIS - 2, install_result.skipped.len());

        let installed = manager.list().await.unwrap().templates;
        assert_eq!(TPLS_IN_THIS, installed.len());
        assert!(installed.iter().any(|t| t.id() == "http-rust"));
        assert!(installed.iter().any(|t| t.id() == "http-go"));
    }

    #[tokio::test]
    async fn can_update_existing() {
        let manager = TempManager::new_with_this_repo_templates().await;
        manager.uninstall("http-rust").await.unwrap();
        assert_eq!(
            TPLS_IN_THIS - 1,
            manager.list().await.unwrap().templates.len()
        );

        let source = TemplateSource::File(project_root());
        let install_result = manager
            .install(
                &source,
                &InstallOptions::default().update(true),
                &DiscardingReporter,
            )
            .await
            .unwrap();
        assert_eq!(TPLS_IN_THIS, install_result.installed.len());
        assert_eq!(0, install_result.skipped.len());

        let installed = manager.list().await.unwrap().templates;
        assert_eq!(TPLS_IN_THIS, installed.len());
        assert!(installed.iter().any(|t| t.id() == "http-go"));
    }

    #[tokio::test]
    async fn can_read_installed_template() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let template = manager.get("http-rust").unwrap().unwrap();
        assert_eq!(
            "HTTP request handler using Rust",
            template.description_or_empty()
        );

        let content_dir = template.content_dir().as_ref().unwrap();
        let cargo = tokio::fs::read_to_string(content_dir.join("Cargo.toml.tmpl"))
            .await
            .unwrap();
        assert!(cargo.contains("name = \"{{project-name | kebab_case}}\""));
    }

    #[tokio::test]
    async fn can_run_template() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let template = manager.get("http-rust").unwrap().unwrap();

        let dest_temp_dir = tempdir().unwrap();
        let output_dir = dest_temp_dir.path().join("myproj");

        let options = run_options("my project", &output_dir, |_| {});

        template.run(options).silent().await.unwrap();

        let cargo = tokio::fs::read_to_string(output_dir.join("Cargo.toml"))
            .await
            .unwrap();
        assert!(cargo.contains("name = \"my-project\""));
    }

    #[tokio::test]
    async fn can_run_template_with_accept_defaults() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let template = manager.get("http-rust").unwrap().unwrap();

        let dest_temp_dir = tempdir().unwrap();
        let output_dir = dest_temp_dir.path().join("myproj");
        let options = run_options("my project", &output_dir, |opts| {
            opts.values = HashMap::new();
            opts.accept_defaults = true;
        });

        template.run(options).silent().await.unwrap();

        let cargo = tokio::fs::read_to_string(output_dir.join("Cargo.toml"))
            .await
            .unwrap();
        assert!(cargo.contains("name = \"my-project\""));
        let spin_toml = tokio::fs::read_to_string(output_dir.join("spin.toml"))
            .await
            .unwrap();
        assert!(spin_toml.contains("route = \"/...\""));
    }

    #[tokio::test]
    async fn cannot_use_custom_filter_in_template() {
        let manager = TempManager::new();

        let install_results = manager.install_test_data_templates().await;

        assert_eq!(1, install_results.skipped.len());

        let (id, reason) = &install_results.skipped[0];
        assert_eq!("testing-custom-filter", id);
        let SkippedReason::InvalidManifest(message) = reason else {
            panic!("skip reason should be InvalidManifest"); // clippy dislikes assert!(false...)
        };
        assert_contains(message, "filters");
        assert_contains(message, "not supported");
    }

    #[tokio::test]
    async fn can_add_component_from_template() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let dest_temp_dir = tempdir().unwrap();
        let application_dir = dest_temp_dir.path().join("multi");

        // Set up the containing app
        {
            let template = manager.get("http-empty").unwrap().unwrap();

            let options = run_options("my multi project", &application_dir, |opts| {
                opts.values = make_values([("project-description", "my desc")])
            });

            template.run(options).silent().await.unwrap();
        }

        let spin_toml_path = application_dir.join("spin.toml");
        assert!(spin_toml_path.exists(), "expected spin.toml to be created");

        // Now add a component
        {
            let template = manager.get("http-rust").unwrap().unwrap();

            let options = run_options("hello", "hello", |opts| {
                opts.variant = add_component_to(&spin_toml_path);
            });

            template.run(options).silent().await.unwrap();
        }

        // And another
        {
            let template = manager.get("http-rust").unwrap().unwrap();

            let options = run_options("hello 2", "encore", |opts| {
                opts.variant = add_component_to(&spin_toml_path);
            });

            template.run(options).silent().await.unwrap();
        }

        let cargo1 = tokio::fs::read_to_string(application_dir.join("hello/Cargo.toml"))
            .await
            .unwrap();
        assert!(cargo1.contains("name = \"hello\""));

        let cargo2 = tokio::fs::read_to_string(application_dir.join("encore/Cargo.toml"))
            .await
            .unwrap();
        assert!(cargo2.contains("name = \"hello-2\""));

        let spin_toml = tokio::fs::read_to_string(&spin_toml_path).await.unwrap();
        assert!(spin_toml.contains("source = \"hello/target/wasm32-wasi/release/hello.wasm\""));
        assert!(spin_toml.contains("source = \"encore/target/wasm32-wasi/release/hello_2.wasm\""));
    }

    #[tokio::test]
    async fn can_add_variables_from_template() {
        let manager = TempManager::new();
        manager.install_test_data_templates().await;
        manager.install_this_repo_templates().await; // We will need some of the standard templates too

        let dest_temp_dir = tempdir().unwrap();
        let application_dir = dest_temp_dir.path().join("spinvars");

        // Set up the containing app
        {
            let template = manager.get("http-rust").unwrap().unwrap();

            let options = run_options("my various project", &application_dir, |_| {});

            template.run(options).silent().await.unwrap();
        }

        let spin_toml_path = application_dir.join("spin.toml");
        assert!(spin_toml_path.exists(), "expected spin.toml to be created");

        // Now add the variables
        {
            let template = manager.get("add-variables").unwrap().unwrap();

            let options = run_options("insertvars", "hello", |opts| {
                opts.variant = add_component_to(&spin_toml_path);
                opts.values = make_values([("service-url", "https://service.example.com")])
            });

            template.run(options).silent().await.unwrap();
        }

        let spin_toml = tokio::fs::read_to_string(&spin_toml_path).await.unwrap();

        assert!(spin_toml.contains("[variables]\nsecret"));
        assert!(spin_toml.contains("url = { default = \"https://service.example.com\" }"));

        assert!(spin_toml.contains("[component.insertvars]"));
        assert!(spin_toml.contains("[component.insertvars.variables]"));
        assert!(spin_toml.contains("kv_credentials = \"{{ secret }}\""));
    }

    #[tokio::test]
    async fn can_overwrite_existing_variables() {
        let manager = TempManager::new();
        manager.install_test_data_templates().await;
        manager.install_this_repo_templates().await; // We will need some of the standard templates too

        let dest_temp_dir = tempdir().unwrap();
        let application_dir = dest_temp_dir.path().join("spinvars");

        // Set up the containing app
        {
            let template = manager.get("http-rust").unwrap().unwrap();

            let options = run_options("my various project", &application_dir, |_| {});

            template.run(options).silent().await.unwrap();
        }

        let spin_toml_path = application_dir.join("spin.toml");
        assert!(spin_toml_path.exists(), "expected spin.toml to be created");

        // Now add the variables
        {
            let template = manager.get("add-variables").unwrap().unwrap();

            let options = run_options("insertvars", "hello", |opts| {
                opts.variant = add_component_to(&spin_toml_path);
                opts.values = make_values([("service-url", "https://service.example.com")]);
            });

            template.run(options).silent().await.unwrap();
        }

        // Now add them again but with different values
        {
            let template = manager.get("add-variables").unwrap().unwrap();

            let options = run_options("insertvarsagain", "hello", |opts| {
                opts.variant = add_component_to(&spin_toml_path);
                opts.values = make_values([("service-url", "https://other.example.com")]);
            });

            template.run(options).silent().await.unwrap();
        }

        let spin_toml = tokio::fs::read_to_string(&spin_toml_path).await.unwrap();
        assert!(spin_toml.contains("url = { default = \"https://other.example.com\" }"));
        assert!(!spin_toml.contains("service.example.com"));
    }

    #[tokio::test]
    async fn component_new_no_vcs() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let dest_temp_dir = tempdir().unwrap();
        let application_dir = dest_temp_dir.path().join("no-vcs-new");

        let template = manager.get("http-rust").unwrap().unwrap();

        let options = run_options("no vcs new", &application_dir, |opts| {
            opts.no_vcs = true;
        });

        template.run(options).silent().await.unwrap();
        let gitignore = application_dir.join(".gitignore");
        assert!(
            !gitignore.exists(),
            "expected .gitignore to not have been created"
        );
    }

    #[tokio::test]
    async fn component_add_no_vcs() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let dest_temp_dir = tempdir().unwrap();
        let application_dir = dest_temp_dir.path().join("no-vcs-add");

        // Set up the containing app
        {
            let template = manager.get("http-empty").unwrap().unwrap();

            let options = run_options("my multi project", &application_dir, |opts| {
                opts.values = make_values([("project-description", "my desc")]);
                opts.no_vcs = true;
            });

            template.run(options).silent().await.unwrap();
        }

        let gitignore = application_dir.join(".gitignore");
        let spin_toml_path = application_dir.join("spin.toml");
        assert!(
            !gitignore.exists(),
            "expected .gitignore to not have been created"
        );

        {
            let template = manager.get("http-rust").unwrap().unwrap();

            let options = run_options("added_component", "hello", |opts| {
                opts.variant = add_component_to(&spin_toml_path);
                opts.no_vcs = true;
            });
            template.run(options).silent().await.unwrap();
        }

        let gitignore_add = application_dir.join("hello").join(".gitignore");
        assert!(
            !gitignore_add.exists(),
            "expected .gitignore to not have been created"
        );
    }

    #[tokio::test]
    async fn can_add_component_with_different_trigger() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let dest_temp_dir = tempdir().unwrap();
        let application_dir = dest_temp_dir.path().join("multi");

        // Set up the containing app
        {
            let template = manager.get("redis-rust").unwrap().unwrap();

            let options = run_options("my multi project", &application_dir, |opts| {
                opts.values = make_values([
                    ("project-description", "my desc"),
                    ("redis-address", "redis://localhost:6379"),
                    ("redis-channel", "the-horrible-knuckles"),
                ])
            });

            template.run(options).silent().await.unwrap();
        }

        let spin_toml_path = application_dir.join("spin.toml");
        assert!(spin_toml_path.exists(), "expected spin.toml to be created");

        // Now add a component
        {
            let template = manager.get("http-rust").unwrap().unwrap();

            let options = run_options("hello", "hello", |opts| {
                opts.variant = add_component_to(&spin_toml_path);
            });

            template.run(options).silent().await.unwrap();
        }

        let spin_toml = tokio::fs::read_to_string(&spin_toml_path).await.unwrap();
        assert!(spin_toml.contains("[[trigger.redis]]"));
        assert!(spin_toml.contains("[[trigger.http]]"));
        assert!(spin_toml.contains("[component.my-multi-project]"));
        assert!(spin_toml.contains("[component.hello]"));
    }

    #[tokio::test]
    async fn cannot_add_component_that_does_not_match_manifest() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let dest_temp_dir = tempdir().unwrap();
        let application_dir = dest_temp_dir.path().join("multi");

        // Set up the containing app
        {
            let fake_v1_src = test_data_root().join("v1manifest.toml");
            let fake_v1_dest = application_dir.join("spin.toml");
            tokio::fs::create_dir_all(&application_dir).await.unwrap();
            tokio::fs::copy(fake_v1_src, fake_v1_dest).await.unwrap();
        }

        let spin_toml_path = application_dir.join("spin.toml");
        assert!(
            spin_toml_path.exists(),
            "expected v1 spin.toml to be created"
        );

        // Now add a component
        {
            let template = manager.get("http-rust").unwrap().unwrap();

            let options = run_options("hello", "hello", |opts| {
                opts.variant = add_component_to(&spin_toml_path);
            });

            template
                .run(options)
                .silent()
                .await
                .expect_err("Expected to fail to add component, but it succeeded");
        }
    }

    #[tokio::test]
    async fn cannot_generate_over_existing_files_by_default() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let template = manager.get("http-rust").unwrap().unwrap();

        let dest_temp_dir = tempdir().unwrap();
        let output_dir = dest_temp_dir.path().join("myproj");

        tokio::fs::create_dir_all(&output_dir).await.unwrap();
        let manifest_path = output_dir.join("spin.toml");
        tokio::fs::write(&manifest_path, "cookies").await.unwrap();

        let options = run_options("my project", &output_dir, |_| {});

        template
            .run(options)
            .silent()
            .await
            .expect_err("generate into existing dir should have failed");

        assert!(tokio::fs::read_to_string(&manifest_path)
            .await
            .unwrap()
            .contains("cookies"));
    }

    #[tokio::test]
    async fn can_generate_over_existing_files_if_so_configured() {
        let manager = TempManager::new_with_this_repo_templates().await;

        let template = manager.get("http-rust").unwrap().unwrap();

        let dest_temp_dir = tempdir().unwrap();
        let output_dir = dest_temp_dir.path().join("myproj");

        tokio::fs::create_dir_all(&output_dir).await.unwrap();
        let manifest_path = output_dir.join("spin.toml");
        tokio::fs::write(&manifest_path, "cookies").await.unwrap();

        let options = run_options("my project", &output_dir, |opts| {
            opts.allow_overwrite = true;
        });

        template
            .run(options)
            .silent()
            .await
            .expect("generate into existing dir should have succeeded");

        assert!(tokio::fs::read_to_string(&manifest_path)
            .await
            .unwrap()
            .contains("[[trigger.http]]"));
    }

    #[tokio::test]
    async fn cannot_new_a_component_only_template() {
        let manager = TempManager::new();
        manager.install_test_data_templates().await;
        manager.install_this_repo_templates().await;

        let dummy_dir = manager.store_dir().join("dummy");
        let manifest_path = dummy_dir.join("ignored_spin.toml");
        let add_component = TemplateVariantInfo::AddComponent { manifest_path };

        let redirect = manager.get("add-only-redirect").unwrap().unwrap();
        assert!(!redirect.supports_variant(&TemplateVariantInfo::NewApplication));
        assert!(redirect.supports_variant(&add_component));

        let http_rust = manager.get("http-rust").unwrap().unwrap();
        assert!(http_rust.supports_variant(&TemplateVariantInfo::NewApplication));
        assert!(http_rust.supports_variant(&add_component));

        let http_empty = manager.get("http-empty").unwrap().unwrap();
        assert!(http_empty.supports_variant(&TemplateVariantInfo::NewApplication));
        assert!(!http_empty.supports_variant(&add_component));
    }

    #[tokio::test]
    async fn fails_on_unknown_filter() {
        let manager = TempManager::new();
        manager.install_test_data_templates().await;

        let template = manager.get("bad-non-existent-filter").unwrap().unwrap();

        let dest_temp_dir = tempdir().unwrap();
        let output_dir = dest_temp_dir.path().join("myproj");
        let options = run_options("bad-filter-should-fail", &output_dir, move |opts| {
            opts.values = make_values([("p1", "biscuits")]);
        });

        let err = template
            .run(options)
            .silent()
            .await
            .expect_err("Expected template to fail but it passed");

        let err_str = err.to_string();

        assert_contains(&err_str, "internal error");
        assert_contains(&err_str, "unknown filter 'lol_snort'");
    }

    fn assert_contains(actual: &str, expected: &str) {
        assert!(
            actual.contains(expected),
            "expected string containing '{expected}' but got '{actual}'"
        );
    }
}