Skip to content

Commit 8740351

Browse files
committed
is_valid_repository()
1 parent 3c2d0ca commit 8740351

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ v0.6.0 (in development)
1616
- Added `GHRepo::as_str()` method
1717
- **Breaking**: The `GHRepo::is_valid_owner()` and `GHRepo::is_valid_name()`
1818
methods are now regular functions
19+
- Added `is_valid_repository()` function
1920

2021
v0.5.0 (2023-04-27)
2122
-------------------

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,18 @@ pub fn is_valid_owner(s: &str) -> bool {
678678
pub fn is_valid_name(s: &str) -> bool {
679679
matches!(split_name(s), Some((_, "")))
680680
}
681+
682+
/// Test whether a string is a valid repository specifier/full name of the form
683+
/// `{owner}/{name}`.
684+
///
685+
/// # Example
686+
///
687+
/// ```
688+
/// # use ghrepo::is_valid_repository;
689+
/// assert!(is_valid_repository("octocat/my-repo"));
690+
/// assert!(!is_valid_repository("octocat/my-repo.git"));
691+
/// assert!(!is_valid_repository("my-repo"));
692+
/// ```
693+
pub fn is_valid_repository(s: &str) -> bool {
694+
matches!(split_owner_name(s), Some((_, _, "")))
695+
}

tests/valid.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(clippy::items_after_test_module)]
2-
use ghrepo::{is_valid_name, is_valid_owner};
2+
use ghrepo::{is_valid_name, is_valid_owner, is_valid_repository};
33
use rstest::rstest;
44

55
#[rstest]
@@ -37,6 +37,7 @@ fn test_good_owner(#[case] owner: &str) {
3737
#[case("steven.universe")]
3838
#[case("steven-universe@beachcity.dv")]
3939
#[case("steven-univerß")]
40+
#[case("steven/universe")]
4041
#[case("")]
4142
#[case("none")]
4243
#[case("NONE")]
@@ -90,6 +91,22 @@ fn test_good_name(#[case] name: &str) {
9091
#[case("steven.git")]
9192
#[case("steven.GIT")]
9293
#[case("steven.Git")]
94+
#[case("steven/universe")]
9395
fn test_bad_name(#[case] name: &str) {
9496
assert!(!is_valid_name(name));
9597
}
98+
99+
#[rstest]
100+
#[case("steven/universe")]
101+
fn test_good_repository(#[case] spec: &str) {
102+
assert!(is_valid_repository(spec));
103+
}
104+
105+
#[rstest]
106+
#[case("steven/universe.git")]
107+
#[case("steven/universe/main")]
108+
#[case("https://github.com/steven/universe")]
109+
#[case("steven")]
110+
fn test_bad_repository(#[case] spec: &str) {
111+
assert!(!is_valid_repository(spec));
112+
}

0 commit comments

Comments
 (0)