Skip to content

Commit

Permalink
validate git urls (#976)
Browse files Browse the repository at this point in the history
commit-id:d8d2f542

formatting

better validation, test added
  • Loading branch information
tomek0123456789 authored Dec 5, 2023
1 parent 6731969 commit 3a6fb3d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
19 changes: 12 additions & 7 deletions scarb/src/manifest_editor/add.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::mem;

use anyhow::{anyhow, ensure, Result};
use anyhow::{anyhow, ensure, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use indoc::formatdoc;
use toml_edit::{value, Document, Entry, InlineTable, Item};
use url::Url;

Expand Down Expand Up @@ -130,12 +131,16 @@ impl Dep {
DefaultBranch
};

let git = match Url::parse(&git) {
Ok(url) => CanonicalUrl::new(&url)
.map(|git_url| git_url.as_str().to_string())
.unwrap_or(git),
Err(_) => git,
};
let git = CanonicalUrl::new(&Url::parse(&git).with_context(|| {
formatdoc!(
r#"
invalid URL provided: {git}
help: use an absolute URL to the Git repository
"#,
)
})?)
.map(|git_url| git_url.as_str().to_string())
.unwrap_or(git);

Box::new(GitSource {
version,
Expand Down
21 changes: 21 additions & 0 deletions scarb/tests/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,24 @@ fn add_dev_dep() {
"#})
.run();
}

#[test]
fn add_git_dep_with_invalid_url() {
ManifestEditHarness::offline()
.args(["add", "dep", "--git", "example.com"])
.input(indoc! {r#"
[package]
name = "hello"
version = "1.0.0"
"#})
.failure()
.stdout_matches(indoc! {r#"
error: invalid URL provided: example.com
help: use an absolute URL to the Git repository
Caused by:
relative URL without a base
"#})
.run()
}

0 comments on commit 3a6fb3d

Please sign in to comment.