Skip to content

Commit

Permalink
fix: could not parse repository (#58)
Browse files Browse the repository at this point in the history
* fix

* fmt

* improve
  • Loading branch information
shixinhuang99 committed Mar 3, 2024
1 parent dc0dd69 commit 7936e08
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mockito = "1.0.2"
path-slash = "0.2.1"
similar-asserts = "1.5.0"
tempfile = "3.5.0"
test-case = "=3.3.1"

[profile.release]
strip = true
Expand Down
39 changes: 19 additions & 20 deletions src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ fn repo_re() -> &'static Regex {
static REPO_RE: OnceLock<Regex> = OnceLock::new();

REPO_RE.get_or_init(|| {
Regex::new(
r"^(?:https://github\.com/)?([^/\s]+)/([^/\s.git]+)(?:\.git)?$",
)
.unwrap()
let re = r"^(?:https://github\.com/)?([^/\s]+)/([^/\s]+)$";
Regex::new(re).unwrap()
})
}

Expand All @@ -27,7 +25,11 @@ impl Repository {
.ok_or(anyhow::anyhow!("Could not parse the input: `{}`", input))?;

let owner = caps[1].to_string();
let name = caps[2].to_string();
let mut name = caps[2].to_string();

if name.ends_with(".git") {
name.truncate(name.len() - 4);
}

Ok(Self {
owner,
Expand All @@ -48,31 +50,28 @@ impl Repository {
mod tests {

use anyhow::Result;
use test_case::test_case;

use super::Repository;

#[test]
fn test_repo_parse() -> Result<()> {
let repo = Repository::parse("foo/bar")?;

assert_eq!(repo.owner, "foo");
assert_eq!(repo.name, "bar");

Ok(())
}
#[test_case("foo/bar"; "basic")]
#[test_case("https://github.com/foo/bar.git"; "complete url")]
#[test_case("foo/bar.git"; "url but no header")]
#[test_case("https://github.com/foo/bar"; "url but no extension")]
fn test_repo_parse_basic(input: &str) -> Result<()> {
let repo = Repository::parse(input)?;

#[test]
fn test_repo_parse_git_url() -> Result<()> {
let repo = Repository::parse("https://github.com/foo/bar.git")?;
assert_eq!(repo.owner, "foo");
assert_eq!(repo.name, "bar");

Ok(())
}

#[test]
fn test_repo_parse_err() {
let repo = Repository::parse("foo");
#[test_case(""; "empty")]
#[test_case("foo"; "incomplete")]
#[test_case("foo/bar/baz"; "paths exceeded")]
fn test_repo_parse_err(input: &str) {
let repo = Repository::parse(input);
assert!(repo.is_err());
}
}

0 comments on commit 7936e08

Please sign in to comment.