Skip to content

Commit

Permalink
Added tests and small fixed
Browse files Browse the repository at this point in the history
...discovered when writing the tests.
Ran rustfmt, clippy::all, cargo test, everything passed and I hope the tests I wrote are good.
Now with 100% less forgotten debug logs!
  • Loading branch information
Silux committed Feb 22, 2023
1 parent 27702d6 commit 44717ab
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
68 changes: 63 additions & 5 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn page(
}
div.footer {
@if conf.show_wget_footer {
(wget_footer(abs_uri, breadcrumbs[0].name.as_str(), current_user))
(wget_footer(abs_uri, conf.title.as_deref(), current_user.map(|x| &*x.name)))
}
@if !conf.hide_version_footer {
(version_footer())
Expand Down Expand Up @@ -268,24 +268,31 @@ fn version_footer() -> Markup {
}
}

fn wget_footer(abs_path: &Uri, root_dir_name: &str, current_user: Option<&CurrentUser>) -> Markup {
fn wget_footer(abs_path: &Uri, root_dir_name: Option<&str>, current_user: Option<&str>) -> Markup {
let escape_apostrophes = |x: &str| x.replace('\'', "'\"'\"'");

// Directory depth, 0 is root directory
let cut_dirs = match abs_path.path().matches('/').count() - 1 {
// Put all the files in a folder of this name
0 => format!(" -P {root_dir_name}"),
0 => format!(
" -P '{}'",
escape_apostrophes(
root_dir_name.unwrap_or_else(|| abs_path.authority().unwrap().as_str())
)
),
1 => String::new(),
// Avoids putting the files in excessive directories
x => format!(" --cut-dirs={}", x - 1),
};

// Ask for password if authentication is required
let user_params = match current_user {
Some(user) => format!(" --ask-password --user {}", user.name),
Some(user) => format!(" --ask-password --user '{}'", escape_apostrophes(user)),
None => String::new(),
};

let command =
format!("wget -rcnHp{cut_dirs} -R 'index.html*'{user_params} '{abs_path}?raw=true'");
format!("wget -rcnHp -R 'index.html*'{cut_dirs}{user_params} '{abs_path}?raw=true'");
let click_to_copy = format!("navigator.clipboard.writeText(\"{command}\")");

html! {
Expand Down Expand Up @@ -688,3 +695,54 @@ pub fn render_error(
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_wget_footer() {
let to_html = |x| {
format!(
r#"<div class="downloadDirectory"><p>Download folder:</p><a class="cmd" title="Click to copy!" style="cursor: pointer;" onclick="navigator.clipboard.writeText(&quot;wget -rcnHp -R 'index.html*' {0}/?raw=true'&quot;)">wget -rcnHp -R 'index.html*' {0}/?raw=true'</a></div>"#,
x
)
};
let uri = |x| Uri::try_from(x).unwrap();

let to_be_tested: String = wget_footer(
&uri("https://github.com/svenstaro/miniserve/"),
Some("Miniserve"),
None,
)
.into();
let solution = to_html("--cut-dirs=1 'https://github.com/svenstaro/miniserve");
assert_eq!(to_be_tested, solution);

let to_be_tested: String = wget_footer(&uri("https://github.com/"), None, None).into();
let solution = to_html("-P 'github.com' 'https://github.com");
assert_eq!(to_be_tested, solution);

let to_be_tested: String = wget_footer(
&uri("http://1und1.de/"),
Some("1&1 - Willkommen!!!"),
Some("Marcell D'Avis"),
)
.into();
let solution = to_html("-P '1&amp;1 - Willkommen!!!' --ask-password --user 'Marcell D'&quot;'&quot;'Avis' 'http://1und1.de");
assert_eq!(to_be_tested, solution);

let to_be_tested: String = wget_footer(
&uri("http://127.0.0.1:1234/geheime_dokumente.php/"),
Some("Streng Geheim!!!"),
Some("uøý`¶'7ÅÛé"),
)
.into();
let solution = to_html("--ask-password --user 'uøý`¶'&quot;'&quot;'7ÅÛé' 'http://127.0.0.1:1234/geheime_dokumente.php");
assert_eq!(to_be_tested, solution);

let to_be_tested: String = wget_footer(&uri("http://127.0.0.1:420/"), None, None).into();
let solution = to_html("-P '127.0.0.1:420' 'http://127.0.0.1:420");
assert_eq!(to_be_tested, solution);
}
}
22 changes: 19 additions & 3 deletions tests/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ use select::predicate::Class;
use select::predicate::Name;

/// The footer displays the correct wget command to download the folder recursively
#[rstest(depth, dir, case(0, ""), case(2, "very/deeply/nested/"))]
// This test can't test all aspects of the wget footer,
// a more detailed unit test is available
#[rstest(
depth,
dir,
case(0, ""),
case(1, "dira/"),
case(2, "very/deeply/"),
case(3, "very/deeply/nested/")
)]
fn ui_displays_wget_element(
depth: u8,
dir: &str,
Expand All @@ -32,11 +41,18 @@ fn ui_displays_wget_element(
.next()
.unwrap()
.text();
let cut_dirs = match depth {
// Put all the files in a folder of this name
0 => format!(" -P 'localhost:{}'", server.port()),
1 => String::new(),
// Avoids putting the files in excessive directories
x => format!(" --cut-dirs={}", x - 1),
};
assert_eq!(
wget_url,
format!(
"wget -r -c -nH -np --cut-dirs={} -R \"index.html*\" \"{}{}?raw=true\"",
depth,
"wget -rcnHp -R 'index.html*'{} '{}{}?raw=true'",
cut_dirs,
server.url(),
dir
)
Expand Down

0 comments on commit 44717ab

Please sign in to comment.