Skip to content

Commit

Permalink
feat(tools): add validate-redirects and support locale arg
Browse files Browse the repository at this point in the history
* add validate-redirects command
* support locale arguments for fix-redirects
  • Loading branch information
fiji-flo committed Jan 28, 2025
1 parent 5e2cccf commit 4488aed
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 97 deletions.
23 changes: 19 additions & 4 deletions crates/rari-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use rari_tools::add_redirect::add_redirect;
use rari_tools::history::gather_history;
use rari_tools::inventory::gather_inventory;
use rari_tools::r#move::r#move;
use rari_tools::redirects::fix_redirects;
use rari_tools::redirects::{fix_redirects, validate_redirects};
use rari_tools::remove::remove;
use rari_tools::sidebars::{fmt_sidebars, sync_sidebars};
use rari_tools::sync_translated_content::sync_translated_content;
Expand Down Expand Up @@ -104,7 +104,9 @@ enum ContentSubcommand {
///
/// This shortens multiple redirect chains to single ones.
/// This is also run as part of sync_translated_content.
FixRedirects,
FixRedirects(FixRedirectArgs),
/// Validate redirects.
ValidateRedirects(ValidateRedirectArgs),
/// Create content inventory as JSON
Inventory,
}
Expand Down Expand Up @@ -136,6 +138,16 @@ struct AddRedirectArgs {
to_url: String,
}

#[derive(Args)]
struct ValidateRedirectArgs {
locales: Option<Vec<Locale>>,
}

#[derive(Args)]
struct FixRedirectArgs {
locales: Option<Vec<Locale>>,
}

#[derive(Args)]
struct SyncTranslatedContentArgs {
locales: Option<Vec<Locale>>,
Expand Down Expand Up @@ -467,8 +479,11 @@ fn main() -> Result<(), Error> {
ContentSubcommand::SyncSidebars => {
sync_sidebars()?;
}
ContentSubcommand::FixRedirects => {
fix_redirects()?;
ContentSubcommand::FixRedirects(args) => {
fix_redirects(args.locales.as_deref())?;
}
ContentSubcommand::ValidateRedirects(args) => {
validate_redirects(args.locales.as_deref())?;
}
ContentSubcommand::Inventory => {
gather_inventory()?;
Expand Down
10 changes: 5 additions & 5 deletions crates/rari-tools/src/add_redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ mod test {
];
let _docs = DocFixtures::new(&slugs, Locale::EnUs);
let _redirects = RedirectFixtures::new(
&vec![(
&[(
"docs/Web/API/Something".to_string(),
"docs/Web/API/SomethingElse".to_string(),
)],
Expand All @@ -122,7 +122,7 @@ mod test {
fn test_add_redirect_missing_target() {
let slugs = vec!["Web/API/ExampleOne".to_string()];
let _docs = DocFixtures::new(&slugs, Locale::EnUs);
let _redirects = RedirectFixtures::new(&vec![], Locale::EnUs);
let _redirects = RedirectFixtures::new(&[], Locale::EnUs);

let result = do_add_redirect(
"/en-US/docs/Web/API/ExampleGone",
Expand All @@ -136,8 +136,8 @@ mod test {
let slugs = vec!["Web/API/ExampleOne".to_string()];
let _docs = DocFixtures::new(&slugs, Locale::EnUs);
let _docs_pt = DocFixtures::new(&slugs, Locale::PtBr);
let _redirects = RedirectFixtures::new(&vec![], Locale::EnUs);
let _redirects_pt = RedirectFixtures::new(&vec![], Locale::PtBr);
let _redirects = RedirectFixtures::new(&[], Locale::EnUs);
let _redirects_pt = RedirectFixtures::new(&[], Locale::PtBr);

// Locales do not match
let result = do_add_redirect(
Expand Down Expand Up @@ -166,7 +166,7 @@ mod test {
fn test_add_redirect_external() {
let slugs = vec!["Web/API/ExampleOne".to_string()];
let _docs = DocFixtures::new(&slugs, Locale::EnUs);
let _redirects = RedirectFixtures::new(&vec![], Locale::EnUs);
let _redirects = RedirectFixtures::new(&[], Locale::EnUs);

let result = do_add_redirect("/en-US/docs/Web/API/ExampleGone", "https://example.com/");
assert!(result.is_ok());
Expand Down
4 changes: 4 additions & 0 deletions crates/rari-tools/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub enum ToolError {
InvalidRedirectFromURL(String),
#[error("Invalid 'to' URL for redirect: {0}")]
InvalidRedirectToURL(String),
#[error("Invalid redirects: not in alphabetical order: {0} -> {1} before {2} -> {3}")]
InvalidRedirectOrder(String, String, String, String),
#[error("Invalid redirect for {0} -> {1} or {2} -> {3}")]
InvalidRedirect(String, String, String, String),
#[error(transparent)]
RedirectError(#[from] RedirectError),
#[error("Invalid yaml {0}")]
Expand Down
Loading

0 comments on commit 4488aed

Please sign in to comment.