Skip to content

Commit

Permalink
feat: --ignore-fragments CLI parameter
Browse files Browse the repository at this point in the history
This adds `--ignore-fragments` parameter to the command line, when
specified no fragment checking will be performed.
  • Loading branch information
zregvart authored and Joshua Nelson committed Nov 20, 2020
1 parent 55892e2 commit b8b3c60
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## NEXT (UNRELEASED)

#### Added

* `--ignore-fragments` CLI parameter to disable URL fagment checking [PR#108]

[PR#108]: https://github.com/deadlinks/cargo-deadlinks/pull/108

<a name="0.6.0"></a>
## 0.6.0 (2020-11-19)

Expand Down
3 changes: 3 additions & 0 deletions src/bin/cargo-deadlinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Options:
-h --help Print this message.
--dir Specify a directory to check (default is target/doc/<package>).
--check-http Check 'http' and 'https' scheme links.
--ignore-fragments Don't check URL fragments.
--no-build Do not call `cargo doc` before running link checking. By default, deadlinks will call `cargo doc` if `--dir` is not passed.
--debug Use debug output. This option is deprecated; use `RUST_LOG=debug` instead.
-v --verbose Use verbose output. This option is deprecated; use `RUST_LOG==info` instead.
Expand All @@ -34,13 +35,15 @@ struct MainArgs {
flag_debug: bool,
flag_check_http: bool,
flag_no_build: bool,
flag_ignore_fragments: bool,
}

impl From<&MainArgs> for CheckContext {
fn from(args: &MainArgs) -> CheckContext {
CheckContext {
check_http: args.flag_check_http,
verbose: args.flag_debug,
check_fragments: !args.flag_ignore_fragments,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/bin/deadlinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Usage:
Options:
-h --help Print this message
--check-http Check 'http' and 'https' scheme links
--ignore-fragments Don't check URL fragments.
--debug Use debug output
-v --verbose Use verbose output
-V --version Print version info and exit.
Expand All @@ -27,13 +28,15 @@ struct MainArgs {
flag_verbose: bool,
flag_debug: bool,
flag_check_http: bool,
flag_ignore_fragments: bool,
}

impl From<MainArgs> for CheckContext {
fn from(args: MainArgs) -> CheckContext {
CheckContext {
check_http: args.flag_check_http,
verbose: args.flag_debug,
check_fragments: !args.flag_ignore_fragments,
}
}
}
Expand Down
50 changes: 48 additions & 2 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn is_fragment_available(
}

/// Check a URL with the "file" scheme for availability. Returns `false` if it is unavailable.
fn check_file_url(url: &Url, _ctx: &CheckContext) -> Result<(), CheckError> {
fn check_file_url(url: &Url, ctx: &CheckContext) -> Result<(), CheckError> {
let path = url.to_file_path().unwrap();

// determine the full path by looking if the path points to a directory,
Expand All @@ -206,6 +206,10 @@ fn check_file_url(url: &Url, _ctx: &CheckContext) -> Result<(), CheckError> {
return Err(CheckError::File(path));
};

if !ctx.check_fragments {
return Ok(());
}

// The URL might contain a fragment. In that case we need a full GET
// request to check if the fragment exists.
match url.fragment() {
Expand Down Expand Up @@ -278,7 +282,7 @@ fn check_http_url(url: &Url, ctx: &CheckContext) -> Result<(), CheckError> {

// The URL might contain a fragment. In that case we need a full GET
// request to check if the fragment exists.
if url.fragment().is_none() {
if url.fragment().is_none() || !ctx.check_fragments {
let resp = ureq::head(url.as_str()).call();

handle_response(url, resp).map(|_: ureq::Response| ())
Expand Down Expand Up @@ -331,6 +335,7 @@ mod test {
&CheckContext {
verbose: false,
check_http: false,
check_fragments: true,
},
)
}
Expand Down Expand Up @@ -397,6 +402,7 @@ mod test {
&CheckContext {
verbose: false,
check_http: false,
check_fragments: true,
},
)
.unwrap();
Expand All @@ -409,6 +415,7 @@ mod test {
&CheckContext {
verbose: false,
check_http: false,
check_fragments: true,
},
)
.unwrap();
Expand All @@ -421,6 +428,7 @@ mod test {
&CheckContext {
verbose: false,
check_http: false,
check_fragments: true,
},
) {
Err(CheckError::File(path)) => assert!(path.ends_with("tests/html/missing_index")),
Expand All @@ -443,6 +451,7 @@ mod test {
&CheckContext {
verbose: false,
check_http: true,
check_fragments: true,
},
)
.unwrap();
Expand Down Expand Up @@ -471,6 +480,7 @@ mod test {
&CheckContext {
verbose: false,
check_http: true,
check_fragments: true,
},
)
.unwrap();
Expand All @@ -497,6 +507,7 @@ mod test {
&CheckContext {
verbose: false,
check_http: true,
check_fragments: true,
},
) {
Err(CheckError::Fragment(Link::Http(url), fragment, None)) => {
Expand All @@ -514,4 +525,39 @@ mod test {

root.assert();
}

#[test]
fn test_disabling_fragment_checks_file() {
check_file_url(
&url_for("tests/html/anchors.html#nonexistent"),
&CheckContext {
verbose: false,
check_http: false,
check_fragments: false,
},
)
.unwrap();
}

#[test]
fn test_disabling_fragment_checks_http() {
let root = mock("HEAD", "/test_disabling_fragment_checks_http")
.with_status(200)
.create();

let mut url = mockito::server_url();
url.push_str("/test_disabling_fragment_checks_http#missing");

is_available(
&Url::parse(&url).unwrap(),
&CheckContext {
verbose: false,
check_http: true,
check_fragments: false,
},
)
.unwrap();

root.assert();
}
}
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ mod check;
mod parse;

// NOTE: this could be Copy, but we intentionally choose not to guarantee that.
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug)]
pub struct CheckContext {
pub check_http: bool,
pub verbose: bool,
pub check_fragments: bool,
}

impl Default for CheckContext {
fn default() -> Self {
CheckContext {
check_http: false,
verbose: false,
check_fragments: true,
}
}
}

#[derive(Debug)]
Expand Down

0 comments on commit b8b3c60

Please sign in to comment.