cargo-generate 0.23.10

cargo, make me a project
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
//! Classified template source: where the template comes from, validated
//! at construction time.

use std::path::PathBuf;

/// Options threaded into git clones for remote sources. Local sources
/// ignore these. Used by `TemplateSource::into_template_location`.
#[derive(Debug, Clone, Default)]
pub struct CloneOptions {
    pub branch: Option<String>,
    pub tag: Option<String>,
    pub revision: Option<String>,
    pub ssh_identity: Option<PathBuf>,
    pub gitconfig: Option<PathBuf>,
    pub force_git_init: bool,
    pub skip_submodules: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GitHost {
    GitHub,
    GitLab,
    Bitbucket,
    SourceHut,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TemplateSource {
    HostShorthand { host: GitHost, owner_repo: String },
    GithubOwnerRepo { owner: String, repo: String },
    RemoteUrl(String),
    LocalRelative(PathBuf),
    LocalAbsolute(PathBuf),
    Favorite(Box<Self>),
}

impl GitHost {
    pub fn to_url(self, owner_repo: &str) -> String {
        match self {
            Self::GitHub => format!("https://github.com/{owner_repo}.git"),
            Self::GitLab => format!("https://gitlab.com/{owner_repo}.git"),
            Self::Bitbucket => format!("https://bitbucket.org/{owner_repo}.git"),
            Self::SourceHut => format!("https://git.sr.ht/~{owner_repo}"),
        }
    }
}

fn strip_host_prefix(s: &str) -> Option<(GitHost, &str)> {
    let prefix = s.get(..3)?;
    let rest = s.get(3..)?;
    let host = match prefix {
        "gh:" => GitHost::GitHub,
        "gl:" => GitHost::GitLab,
        "bb:" => GitHost::Bitbucket,
        "sr:" => GitHost::SourceHut,
        _ => return None,
    };
    Some((host, rest))
}

fn looks_like_url(s: &str) -> bool {
    if s.starts_with("https://")
        || s.starts_with("http://")
        || s.starts_with("ssh://")
        || s.starts_with("git://")
        || s.starts_with("file://")
    {
        return true;
    }
    // scp-style `git@host:path`: a `user@host:rest` form where the colon
    // is BEFORE the first slash (URLs and paths put `/` before `:`).
    if let Some(at) = s.find('@') {
        if let Some(colon_offset) = s[at..].find(':') {
            let colon = at + colon_offset;
            let slash = s.find('/').unwrap_or(s.len());
            if colon < slash {
                return true;
            }
        }
    }
    false
}

fn parse_owner_repo(s: &str) -> Option<(String, String)> {
    // Require exactly one `/`, both sides nonempty, owner side doesn't
    // start with `.` (which would be a relative path form).
    let (owner, repo) = s.split_once('/')?;
    if owner.is_empty() || repo.is_empty() {
        return None;
    }
    if repo.contains('/') {
        return None;
    }
    if owner.starts_with('.') {
        return None;
    }
    let owner_ok = owner
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.'));
    let repo_ok = repo
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.'));
    (owner_ok && repo_ok).then(|| (owner.to_owned(), repo.to_owned()))
}

impl TemplateSource {
    /// Classify a raw template input into a `TemplateSource`. See the
    /// design doc for the precedence rules; ordering here is intentional.
    pub fn classify(
        input: &str,
        app_config: &crate::app_config::AppConfig,
        cwd: &std::path::Path,
    ) -> Self {
        Self::classify_with_depth(input, app_config, cwd, 0)
    }

    fn classify_with_depth(
        input: &str,
        app_config: &crate::app_config::AppConfig,
        cwd: &std::path::Path,
        depth: u8,
    ) -> Self {
        const FAVORITE_RECURSION_LIMIT: u8 = 8;

        // 1. Configured favorite name (only when within recursion budget).
        if depth <= FAVORITE_RECURSION_LIMIT {
            if let Some(fav) = app_config.get_favorite_cfg(input) {
                if let Some(git) = fav.git.as_deref() {
                    let inner = Self::classify_with_depth(git, app_config, cwd, depth + 1);
                    return Self::Favorite(Box::new(inner));
                }
                if let Some(p) = fav.path.as_ref() {
                    let inner = if p.is_absolute() {
                        Self::LocalAbsolute(p.clone())
                    } else {
                        Self::LocalRelative(cwd.join(p))
                    };
                    return Self::Favorite(Box::new(inner));
                }
                // Malformed favorite (neither git nor path) — fall through to
                // non-favorite classification rules below.
            }
        }

        // 2. Host prefix
        if let Some((host, rest)) = strip_host_prefix(input) {
            return Self::HostShorthand {
                host,
                owner_repo: rest.to_owned(),
            };
        }
        // 3. Full URL
        if looks_like_url(input) {
            return Self::RemoteUrl(input.to_owned());
        }
        // 4. Absolute path
        let p = std::path::Path::new(input);
        if p.is_absolute() {
            return Self::LocalAbsolute(p.to_path_buf());
        }
        // 5. Relative path that exists as a directory
        let resolved = cwd.join(p);
        if resolved.is_dir() {
            return Self::LocalRelative(resolved);
        }
        // 6. Bare owner/repo → github
        if let Some((owner, repo)) = parse_owner_repo(input) {
            return Self::GithubOwnerRepo { owner, repo };
        }
        // 7. Catch-all — let git produce the clearer error.
        Self::RemoteUrl(input.to_owned())
    }

    /// User-facing label preserving the form the user typed. Used in
    /// warnings, log lines, error messages.
    #[allow(dead_code)]
    pub fn display_label(&self) -> std::borrow::Cow<'_, str> {
        use std::borrow::Cow;
        match self {
            Self::HostShorthand { host, owner_repo } => {
                let prefix = match host {
                    GitHost::GitHub => "gh",
                    GitHost::GitLab => "gl",
                    GitHost::Bitbucket => "bb",
                    GitHost::SourceHut => "sr",
                };
                Cow::Owned(format!("{prefix}:{owner_repo}"))
            }
            Self::GithubOwnerRepo { owner, repo } => Cow::Owned(format!("{owner}/{repo}")),
            Self::RemoteUrl(url) => Cow::Borrowed(url.as_str()),
            Self::LocalRelative(p) | Self::LocalAbsolute(p) => Cow::Owned(p.display().to_string()),
            Self::Favorite(inner) => Cow::Owned(format!("favorite → {}", inner.display_label())),
        }
    }

    /// Adapter producing the legacy `TemplateLocation`. `clone_opts` are
    /// threaded through to `GitUserInput::new` for remote variants;
    /// ignored for local variants. Once consumers migrate, this method
    /// goes away.
    pub fn into_template_location(
        self,
        clone_opts: &CloneOptions,
    ) -> crate::user_parsed_input::TemplateLocation {
        use crate::user_parsed_input::{GitUserInput, TemplateLocation};
        match self {
            Self::HostShorthand { host, owner_repo } => TemplateLocation::Git(
                GitUserInput::with_url_and_clone_opts(host.to_url(&owner_repo), clone_opts),
            ),
            Self::GithubOwnerRepo { owner, repo } => {
                TemplateLocation::Git(GitUserInput::with_url_and_clone_opts(
                    GitHost::GitHub.to_url(&format!("{owner}/{repo}")),
                    clone_opts,
                ))
            }
            Self::RemoteUrl(url) => {
                TemplateLocation::Git(GitUserInput::with_url_and_clone_opts(url, clone_opts))
            }
            Self::LocalAbsolute(p) | Self::LocalRelative(p) => TemplateLocation::Path(p),
            Self::Favorite(inner) => inner.into_template_location(clone_opts),
        }
    }

    /// Like `into_template_location` but forces local paths through git clone.
    /// Used by the `--git` flag, which means "use git even for local paths"
    /// so that branch/tag/ssh-identity options are honoured.
    pub fn into_git_template_location(
        self,
        clone_opts: &CloneOptions,
    ) -> crate::user_parsed_input::TemplateLocation {
        use crate::user_parsed_input::{GitUserInput, TemplateLocation};
        match self {
            Self::LocalAbsolute(p) | Self::LocalRelative(p) => TemplateLocation::Git(
                GitUserInput::with_url_and_clone_opts(p.display().to_string(), clone_opts),
            ),
            Self::Favorite(inner) => inner.into_git_template_location(clone_opts),
            other => other.into_template_location(clone_opts),
        }
    }

    #[cfg(test)]
    fn into_template_location_for_test(self) -> crate::user_parsed_input::TemplateLocation {
        self.into_template_location(&CloneOptions::default())
    }

    /// Whether this source should be acquired by cloning vs copying.
    /// Favorites delegate to their inner source.
    #[allow(dead_code)]
    pub fn is_remote(&self) -> bool {
        match self {
            Self::HostShorthand { .. } | Self::GithubOwnerRepo { .. } | Self::RemoteUrl(_) => true,
            Self::LocalRelative(_) | Self::LocalAbsolute(_) => false,
            Self::Favorite(inner) => inner.is_remote(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app_config::AppConfig;
    use std::path::Path;

    fn empty_config() -> AppConfig {
        AppConfig::default()
    }

    #[test]
    fn classify_host_shorthand() {
        let s = TemplateSource::classify("gh:owner/repo", &empty_config(), Path::new("/tmp"));
        assert_eq!(
            s,
            TemplateSource::HostShorthand {
                host: GitHost::GitHub,
                owner_repo: "owner/repo".to_owned()
            }
        );
    }

    #[test]
    fn classify_https_url() {
        let s = TemplateSource::classify(
            "https://github.com/owner/repo.git",
            &empty_config(),
            Path::new("/tmp"),
        );
        assert_eq!(
            s,
            TemplateSource::RemoteUrl("https://github.com/owner/repo.git".to_owned())
        );
    }

    #[test]
    fn classify_scp_url() {
        let s = TemplateSource::classify(
            "git@github.com:owner/repo.git",
            &empty_config(),
            Path::new("/tmp"),
        );
        assert_eq!(
            s,
            TemplateSource::RemoteUrl("git@github.com:owner/repo.git".to_owned())
        );
    }

    #[test]
    fn classify_absolute_path() {
        // Windows requires a drive letter for `Path::is_absolute()`;
        // a leading `/` alone is "rooted but not absolute."
        let input = if cfg!(windows) {
            r"C:\Users\me\template"
        } else {
            "/Users/me/template"
        };
        let s = TemplateSource::classify(input, &empty_config(), Path::new("/tmp"));
        assert_eq!(s, TemplateSource::LocalAbsolute(PathBuf::from(input)));
    }

    #[test]
    fn classify_existing_relative_dir() {
        let cwd = tempfile::TempDir::new().unwrap();
        std::fs::create_dir_all(cwd.path().join("template")).unwrap();
        let s = TemplateSource::classify("./template", &empty_config(), cwd.path());
        assert_eq!(
            s,
            TemplateSource::LocalRelative(cwd.path().join("template"))
        );
    }

    #[test]
    fn classify_owner_repo_when_no_local_dir() {
        let cwd = tempfile::TempDir::new().unwrap();
        let s = TemplateSource::classify("owner/repo", &empty_config(), cwd.path());
        assert_eq!(
            s,
            TemplateSource::GithubOwnerRepo {
                owner: "owner".to_owned(),
                repo: "repo".to_owned()
            }
        );
    }

    #[test]
    fn classify_relative_dir_beats_owner_repo() {
        let cwd = tempfile::TempDir::new().unwrap();
        std::fs::create_dir_all(cwd.path().join("owner/repo")).unwrap();
        let s = TemplateSource::classify("owner/repo", &empty_config(), cwd.path());
        assert_eq!(
            s,
            TemplateSource::LocalRelative(cwd.path().join("owner/repo"))
        );
    }

    #[test]
    fn classify_fallback_to_remote_url() {
        let cwd = tempfile::TempDir::new().unwrap();
        let s = TemplateSource::classify("garbage~~~", &empty_config(), cwd.path());
        assert_eq!(s, TemplateSource::RemoteUrl("garbage~~~".to_owned()));
    }

    #[test]
    fn git_host_to_url_github() {
        assert_eq!(
            GitHost::GitHub.to_url("owner/repo"),
            "https://github.com/owner/repo.git"
        );
    }

    #[test]
    fn git_host_to_url_gitlab() {
        assert_eq!(
            GitHost::GitLab.to_url("owner/repo"),
            "https://gitlab.com/owner/repo.git"
        );
    }

    #[test]
    fn git_host_to_url_bitbucket() {
        assert_eq!(
            GitHost::Bitbucket.to_url("owner/repo"),
            "https://bitbucket.org/owner/repo.git"
        );
    }

    #[test]
    fn git_host_to_url_sourcehut_inserts_tilde() {
        assert_eq!(
            GitHost::SourceHut.to_url("user/repo"),
            "https://git.sr.ht/~user/repo"
        );
    }

    #[test]
    fn strip_host_prefix_recognizes_gh() {
        assert_eq!(strip_host_prefix("gh:o/r"), Some((GitHost::GitHub, "o/r")));
    }
    #[test]
    fn strip_host_prefix_recognizes_gl() {
        assert_eq!(strip_host_prefix("gl:o/r"), Some((GitHost::GitLab, "o/r")));
    }
    #[test]
    fn strip_host_prefix_recognizes_bb() {
        assert_eq!(
            strip_host_prefix("bb:o/r"),
            Some((GitHost::Bitbucket, "o/r"))
        );
    }
    #[test]
    fn strip_host_prefix_recognizes_sr() {
        assert_eq!(
            strip_host_prefix("sr:u/r"),
            Some((GitHost::SourceHut, "u/r"))
        );
    }
    #[test]
    fn strip_host_prefix_rejects_unknown_prefix() {
        assert_eq!(strip_host_prefix("xx:o/r"), None);
    }
    #[test]
    fn strip_host_prefix_rejects_short_input() {
        assert_eq!(strip_host_prefix("gh"), None);
    }
    #[test]
    fn strip_host_prefix_rejects_bare_owner_repo() {
        assert_eq!(strip_host_prefix("owner/repo"), None);
    }

    #[test]
    fn looks_like_url_https() {
        assert!(looks_like_url("https://github.com/owner/repo.git"));
    }
    #[test]
    fn looks_like_url_http() {
        assert!(looks_like_url("http://example.com/repo.git"));
    }
    #[test]
    fn looks_like_url_ssh_scheme() {
        assert!(looks_like_url("ssh://git@github.com/owner/repo.git"));
    }
    #[test]
    fn looks_like_url_git_scheme() {
        assert!(looks_like_url("git://github.com/owner/repo.git"));
    }
    #[test]
    fn looks_like_url_scp_style() {
        assert!(looks_like_url("git@github.com:owner/repo.git"));
    }
    #[test]
    fn looks_like_url_rejects_owner_repo() {
        assert!(!looks_like_url("owner/repo"));
    }
    #[test]
    fn looks_like_url_rejects_relative_path() {
        assert!(!looks_like_url("./template"));
    }
    #[test]
    fn looks_like_url_rejects_absolute_path() {
        assert!(!looks_like_url("/Users/me/template"));
    }
    #[test]
    fn looks_like_url_rejects_host_prefix() {
        assert!(!looks_like_url("gh:owner/repo"));
    }

    #[test]
    fn parse_owner_repo_simple() {
        assert_eq!(
            parse_owner_repo("owner/repo"),
            Some(("owner".to_owned(), "repo".to_owned()))
        );
    }
    #[test]
    fn parse_owner_repo_with_dashes_and_dots() {
        assert_eq!(
            parse_owner_repo("my-org/my.repo"),
            Some(("my-org".to_owned(), "my.repo".to_owned()))
        );
    }
    #[test]
    fn parse_owner_repo_rejects_relative_dot_prefix() {
        assert_eq!(parse_owner_repo("./path"), None);
    }
    #[test]
    fn parse_owner_repo_rejects_double_dot() {
        assert_eq!(parse_owner_repo("../path"), None);
    }
    #[test]
    fn parse_owner_repo_rejects_three_segments() {
        assert_eq!(parse_owner_repo("a/b/c"), None);
    }
    #[test]
    fn parse_owner_repo_rejects_leading_slash() {
        assert_eq!(parse_owner_repo("/abs/path"), None);
    }
    #[test]
    fn parse_owner_repo_rejects_trailing_slash() {
        assert_eq!(parse_owner_repo("owner/"), None);
    }
    #[test]
    fn parse_owner_repo_rejects_no_slash() {
        assert_eq!(parse_owner_repo("ownerrepo"), None);
    }

    #[test]
    fn classify_malformed_favorite_falls_through_to_non_favorite() {
        use crate::app_config::FavoriteConfig;
        use std::collections::HashMap;
        let mut favorites = HashMap::new();
        // Malformed favorite: name is set but neither git nor path is.
        favorites.insert("just-a-name".to_owned(), FavoriteConfig::default());
        let cfg = AppConfig {
            favorites: Some(favorites),
            ..AppConfig::default()
        };
        let cwd = tempfile::TempDir::new().unwrap();
        let s = TemplateSource::classify("just-a-name", &cfg, cwd.path());
        // No `/`, no scheme, no local dir → catch-all RemoteUrl with the
        // verbatim input, NOT wrapped in Favorite.
        assert_eq!(s, TemplateSource::RemoteUrl("just-a-name".to_owned()));
    }

    use crate::app_config::FavoriteConfig;
    use std::collections::HashMap;

    fn config_with_favorite(name: &str, fav: FavoriteConfig) -> AppConfig {
        let mut map = HashMap::new();
        map.insert(name.to_owned(), fav);
        AppConfig {
            favorites: Some(map),
            ..AppConfig::default()
        }
    }

    #[test]
    fn classify_favorite_with_git_string_recurses() {
        let cfg = config_with_favorite(
            "myfave",
            FavoriteConfig {
                git: Some("gh:owner/repo".to_owned()),
                ..FavoriteConfig::default()
            },
        );
        let s = TemplateSource::classify("myfave", &cfg, Path::new("/tmp"));
        assert_eq!(
            s,
            TemplateSource::Favorite(Box::new(TemplateSource::HostShorthand {
                host: GitHost::GitHub,
                owner_repo: "owner/repo".to_owned(),
            }))
        );
    }

    #[test]
    fn classify_favorite_with_absolute_path() {
        use std::path::PathBuf;
        let abs = if cfg!(windows) {
            r"C:\abs\template"
        } else {
            "/abs/template"
        };
        let cfg = config_with_favorite(
            "myfave",
            FavoriteConfig {
                path: Some(PathBuf::from(abs)),
                ..FavoriteConfig::default()
            },
        );
        let s = TemplateSource::classify("myfave", &cfg, Path::new("/tmp"));
        assert_eq!(
            s,
            TemplateSource::Favorite(Box::new(TemplateSource::LocalAbsolute(PathBuf::from(abs))))
        );
    }

    #[test]
    fn classify_favorite_cycle_bounded_falls_back() {
        // Two favorites pointing at each other; cycle bound prevents
        // infinite recursion. After the depth limit we stop following
        // favorites and the last name is treated as a non-favorite.
        let mut map = HashMap::new();
        map.insert(
            "a".to_owned(),
            FavoriteConfig {
                git: Some("b".to_owned()),
                ..FavoriteConfig::default()
            },
        );
        map.insert(
            "b".to_owned(),
            FavoriteConfig {
                git: Some("a".to_owned()),
                ..FavoriteConfig::default()
            },
        );
        let cfg = AppConfig {
            favorites: Some(map),
            ..AppConfig::default()
        };
        let s = TemplateSource::classify("a", &cfg, Path::new("/tmp"));
        // The exact deep structure doesn't matter; what matters is that
        // classify terminates and produces something well-formed.
        assert!(matches!(s, TemplateSource::Favorite(_)));
    }

    #[test]
    fn display_label_host_shorthand() {
        let s = TemplateSource::HostShorthand {
            host: GitHost::GitHub,
            owner_repo: "o/r".to_owned(),
        };
        assert_eq!(s.display_label(), "gh:o/r");
    }
    #[test]
    fn display_label_owner_repo() {
        let s = TemplateSource::GithubOwnerRepo {
            owner: "o".to_owned(),
            repo: "r".to_owned(),
        };
        assert_eq!(s.display_label(), "o/r");
    }
    #[test]
    fn display_label_remote_url() {
        let s = TemplateSource::RemoteUrl("https://x/y".to_owned());
        assert_eq!(s.display_label(), "https://x/y");
    }
    #[test]
    fn display_label_local_relative() {
        let s = TemplateSource::LocalRelative(PathBuf::from("./t"));
        assert_eq!(s.display_label(), "./t");
    }
    #[test]
    fn display_label_local_absolute() {
        let s = TemplateSource::LocalAbsolute(PathBuf::from("/abs/t"));
        assert_eq!(s.display_label(), "/abs/t");
    }
    #[test]
    fn display_label_favorite_wraps_inner() {
        let s = TemplateSource::Favorite(Box::new(TemplateSource::GithubOwnerRepo {
            owner: "o".to_owned(),
            repo: "r".to_owned(),
        }));
        assert_eq!(s.display_label(), "favorite → o/r");
    }

    use crate::user_parsed_input::TemplateLocation;

    #[test]
    fn into_template_location_maps_host_shorthand_to_git_url() {
        let s = TemplateSource::HostShorthand {
            host: GitHost::GitHub,
            owner_repo: "o/r".to_owned(),
        };
        match s.into_template_location_for_test() {
            TemplateLocation::Git(g) => assert_eq!(g.url(), "https://github.com/o/r.git"),
            TemplateLocation::Path(_) => panic!("expected Git"),
        }
    }
    #[test]
    fn into_template_location_maps_owner_repo_to_git_url() {
        let s = TemplateSource::GithubOwnerRepo {
            owner: "o".to_owned(),
            repo: "r".to_owned(),
        };
        match s.into_template_location_for_test() {
            TemplateLocation::Git(g) => assert_eq!(g.url(), "https://github.com/o/r.git"),
            TemplateLocation::Path(_) => panic!("expected Git"),
        }
    }
    #[test]
    fn into_template_location_maps_remote_url_verbatim() {
        let s = TemplateSource::RemoteUrl("ssh://git@x/y.git".to_owned());
        match s.into_template_location_for_test() {
            TemplateLocation::Git(g) => assert_eq!(g.url(), "ssh://git@x/y.git"),
            TemplateLocation::Path(_) => panic!("expected Git"),
        }
    }
    #[test]
    fn into_template_location_maps_local_to_path() {
        let s = TemplateSource::LocalAbsolute(PathBuf::from("/abs"));
        match s.into_template_location_for_test() {
            TemplateLocation::Path(p) => assert_eq!(p, Path::new("/abs")),
            TemplateLocation::Git(_) => panic!("expected Path"),
        }
    }

    #[test]
    fn is_remote_for_each_variant() {
        assert!(TemplateSource::HostShorthand {
            host: GitHost::GitHub,
            owner_repo: "o/r".to_owned()
        }
        .is_remote());
        assert!(TemplateSource::GithubOwnerRepo {
            owner: "o".to_owned(),
            repo: "r".to_owned()
        }
        .is_remote());
        assert!(TemplateSource::RemoteUrl("x".to_owned()).is_remote());
        assert!(!TemplateSource::LocalRelative(PathBuf::from("./t")).is_remote());
        assert!(!TemplateSource::LocalAbsolute(PathBuf::from("/t")).is_remote());
        // Favorite delegates to inner
        assert!(
            TemplateSource::Favorite(Box::new(TemplateSource::RemoteUrl("x".to_owned())))
                .is_remote()
        );
    }
}