Skip to content

Commit

Permalink
Make suggestion test more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed Aug 17, 2023
1 parent 5680bef commit 971bf04
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions lychee-bin/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod cli {
use lychee_lib::{InputSource, ResponseBody};
use predicates::str::{contains, is_empty};
use pretty_assertions::assert_eq;
use regex::Regex;
use serde::Serialize;
use serde_json::Value;
use tempfile::NamedTempFile;
Expand Down Expand Up @@ -1229,18 +1230,34 @@ mod cli {

#[test]
fn test_suggests_url_alternatives() -> Result<()> {
let mut cmd = main_command();
let input = fixtures_path().join("INTERNET_ARCHIVE.md");

cmd.arg("--suggest")
.arg(input)
.assert()
.failure()
.code(2)
.stdout(contains("Suggestions"))
.stdout(contains("http://web.archive.org/web/"));
for _ in 0..3 {
// This can be flaky. Try up to 3 times
let mut cmd = main_command();
let input = fixtures_path().join("INTERNET_ARCHIVE.md");

cmd.arg("--no-progress").arg("--suggest").arg(input);

// Run he command and check if the output contains the expected
// suggestions
let assert = cmd.assert();
let output = assert.get_output();
let output = std::str::from_utf8(&output.stdout).unwrap();

// We're looking for a suggestion that
// - starts with http://web.archive.org/web/
// - ends with google.com/jobs.html
let re = Regex::new(r"http://web\.archive\.org/web/.*google\.com/jobs\.html").unwrap();
if re.is_match(output) {
// Test passed
return Ok(());
} else {
// Wait for a second before retrying
std::thread::sleep(std::time::Duration::from_secs(1));
}
}

Ok(())
// If we reached here, it means the test did not pass after multiple attempts
Err("Did not get the expected command output after multiple attempts.".into())
}

#[tokio::test]
Expand Down

0 comments on commit 971bf04

Please sign in to comment.