use super::*;
#[test]
fn join_url_appends_path_to_base() {
assert_eq!(
join_url("https://github.com/owner/repo", "/issues/{number}", "42"),
"https://github.com/owner/repo/issues/42",
);
}
#[test]
fn join_url_collapses_double_slash_from_trailing_base() {
assert_eq!(
join_url("https://github.com/owner/repo/", "/pull/{number}", "7"),
"https://github.com/owner/repo/pull/7",
);
}
#[test]
fn join_url_adds_separator_when_template_lacks_leading_slash() {
assert_eq!(
join_url("https://example.com/o/r", "issues/{number}", "9"),
"https://example.com/o/r/issues/9",
);
}
#[test]
fn forge_paths_are_layout_specific() {
assert_eq!(Forge::GitHub.issue_path(), "/issues/{number}");
assert_eq!(Forge::GitHub.pr_path(), "/pull/{number}");
assert_eq!(Forge::GitLab.issue_path(), "/-/issues/{number}");
assert_eq!(Forge::GitLab.pr_path(), "/-/merge_requests/{number}");
assert_eq!(Forge::Gitea.issue_path(), "/issues/{number}");
assert_eq!(Forge::Gitea.pr_path(), "/pulls/{number}");
}
#[test]
fn forge_detects_known_hosts() {
assert_eq!(Forge::detect("https://github.com/o/r"), Some(Forge::GitHub));
assert_eq!(Forge::detect("https://gitlab.com/o/r"), Some(Forge::GitLab));
assert_eq!(Forge::detect("https://gitea.com/o/r"), Some(Forge::Gitea));
assert_eq!(
Forge::detect("https://codeberg.org/o/r"),
Some(Forge::Gitea),
);
}
#[test]
fn forge_detection_is_host_case_insensitive() {
assert_eq!(Forge::detect("https://GitHub.com/o/r"), Some(Forge::GitHub));
assert_eq!(
Forge::detect("https://GitLab.Example.com/o/r"),
Some(Forge::GitLab),
);
}
#[test]
fn forge_detects_self_hosted_subdomain_pattern() {
assert_eq!(
Forge::detect("https://gitlab.example.com/o/r"),
Some(Forge::GitLab),
);
assert_eq!(
Forge::detect("https://gitea.example.com/o/r"),
Some(Forge::Gitea),
);
assert_eq!(
Forge::detect("https://forgejo.example.com/o/r"),
Some(Forge::Gitea),
);
assert_eq!(
Forge::detect("https://github.example.com/o/r"),
Some(Forge::GitHub),
);
}
#[test]
fn forge_detection_has_no_fallback_for_unknown_hosts() {
assert_eq!(Forge::detect("https://code.example.com/o/r"), None);
assert_eq!(Forge::detect("https://git.example.com/o/r"), None);
assert_eq!(Forge::detect("https://bitbucket.org/o/r"), None);
assert_eq!(Forge::detect("https://bitbucket.example.com/o/r"), None);
}
#[test]
fn block_defines_reference_detects_existing_definition() {
let block = "Closes [#99].\n\n[#99]: https://example.com/issues/99";
assert!(block_defines_reference(block, "99"));
assert!(!block_defines_reference(block, "98"));
assert!(!block_defines_reference("Closes #99.", "99"));
}
#[test]
fn unknown_forge_value_is_rejected() {
assert!(toml::from_str::<Config>(r#"forge = "gitlab""#).is_ok());
assert!(toml::from_str::<Config>(r#"forge = "bitbucket""#).is_err());
}
#[test]
fn urls_are_derived_when_only_repository_is_set() {
let lint = |web_base: &str| BareIssueReference {
forge: None,
repo_web_base: Some(web_base.to_owned()),
suggest_issue_url: true,
suggest_pr_url: true,
doc_comment_form: DocForm::Inline,
include_plain_comments: false,
plain_comment_form: PlainForm::BareUrl,
};
let github = lint("https://github.com/owner/repo");
assert_eq!(github.effective_forge(), Some(Forge::GitHub));
assert_eq!(
github.issue_url("123").as_deref(),
Some("https://github.com/owner/repo/issues/123"),
);
assert_eq!(
github.pr_url("123").as_deref(),
Some("https://github.com/owner/repo/pull/123"),
);
let gitlab = lint("https://gitlab.com/owner/repo");
assert_eq!(gitlab.effective_forge(), Some(Forge::GitLab));
assert_eq!(
gitlab.issue_url("123").as_deref(),
Some("https://gitlab.com/owner/repo/-/issues/123"),
);
assert_eq!(
gitlab.pr_url("123").as_deref(),
Some("https://gitlab.com/owner/repo/-/merge_requests/123"),
);
}
#[test]
fn hash_reference_is_issue_only_on_gitlab() {
let lint = |web_base: &str| BareIssueReference {
forge: None,
repo_web_base: Some(web_base.to_owned()),
suggest_issue_url: true,
suggest_pr_url: true,
doc_comment_form: DocForm::Inline,
include_plain_comments: false,
plain_comment_form: PlainForm::BareUrl,
};
assert!(!lint("https://gitlab.com/o/r").hash_can_mean_pr());
assert!(!lint("https://gitlab.example.com/o/r").hash_can_mean_pr());
assert!(lint("https://github.com/o/r").hash_can_mean_pr());
assert!(lint("https://gitea.com/o/r").hash_can_mean_pr());
}
#[test]
fn resolve_repository_passes_valid_url() {
assert_eq!(
resolve_repository(Some("git@github.com:owner/repo.git")).as_deref(),
Some("https://github.com/owner/repo"),
);
}
#[test]
fn resolve_repository_is_none_when_unset() {
assert_eq!(resolve_repository(None), None);
}
#[test]
#[should_panic(expected = "is not a parseable git URL")]
fn resolve_repository_panics_on_unparseable() {
resolve_repository(Some("https://github.com/owner"));
}
#[test]
fn no_urls_when_repository_host_is_unrecognised() {
let lint = BareIssueReference {
forge: None,
repo_web_base: Some("https://git.example.com/owner/repo".to_owned()),
suggest_issue_url: true,
suggest_pr_url: true,
doc_comment_form: DocForm::Inline,
include_plain_comments: false,
plain_comment_form: PlainForm::BareUrl,
};
assert_eq!(lint.effective_forge(), None);
assert_eq!(lint.issue_url("123"), None);
assert_eq!(lint.pr_url("123"), None);
}
#[test]
fn renders_inline_doc_suggestion() {
assert_eq!(
render_doc_suggestion(DocForm::Inline, "#42", "https://example.com/issues/42"),
"[#42](https://example.com/issues/42)",
);
}
#[test]
fn renders_reference_doc_suggestion() {
assert_eq!(
render_doc_suggestion(DocForm::Reference, "#42", "https://example.com/issues/42"),
"[#42]",
);
}
#[test]
fn renders_bare_url_doc_suggestion() {
assert_eq!(
render_doc_suggestion(DocForm::BareUrl, "#42", "https://example.com/issues/42"),
"https://example.com/issues/42",
);
}
#[test]
fn renders_bracketed_url_doc_suggestion() {
assert_eq!(
render_doc_suggestion(
DocForm::BracketedUrl,
"#42",
"https://example.com/issues/42"
),
"<https://example.com/issues/42>",
);
}
#[test]
fn renders_plain_suggestion_bracketed() {
assert_eq!(
render_plain_suggestion(PlainForm::BracketedUrl, "https://example.com/issues/42"),
"<https://example.com/issues/42>",
);
}