|
| 1 | +use std::borrow::Cow; |
| 2 | + |
| 3 | +use console::Style; |
| 4 | +use rari_doc::resolve::{url_meta_from, UrlMeta}; |
| 5 | +use rari_types::locale::Locale; |
| 6 | + |
| 7 | +use crate::error::ToolError; |
| 8 | +use crate::redirects::add_redirects; |
| 9 | + |
| 10 | +pub fn add_redirect(from_url: &str, to_url: &str) -> Result<(), ToolError> { |
| 11 | + do_add_redirect(from_url, to_url)?; |
| 12 | + |
| 13 | + let green = Style::new().green(); |
| 14 | + let bold = Style::new().bold(); |
| 15 | + |
| 16 | + println!( |
| 17 | + "{} {} {} {}", |
| 18 | + green.apply_to("Saved"), |
| 19 | + bold.apply_to(from_url), |
| 20 | + green.apply_to("→"), |
| 21 | + bold.apply_to(to_url), |
| 22 | + ); |
| 23 | + |
| 24 | + Ok(()) |
| 25 | +} |
| 26 | + |
| 27 | +fn do_add_redirect(from_url: &str, to_url: &str) -> Result<(), ToolError> { |
| 28 | + validate_args(from_url, to_url)?; |
| 29 | + let UrlMeta { |
| 30 | + locale: from_locale, |
| 31 | + .. |
| 32 | + } = url_meta_from(from_url)?; |
| 33 | + if !to_url.starts_with("http") { |
| 34 | + if from_url == to_url { |
| 35 | + return Err(ToolError::InvalidRedirectToURL(format!( |
| 36 | + "redirect url is the same as the from url: {to_url}" |
| 37 | + ))); |
| 38 | + } |
| 39 | + |
| 40 | + let UrlMeta { |
| 41 | + locale: to_locale, .. |
| 42 | + } = url_meta_from(to_url)?; |
| 43 | + |
| 44 | + // Enforce that the locales match or the target is in the default Locale |
| 45 | + if from_locale != to_locale && to_locale != Locale::EnUs { |
| 46 | + return Err(ToolError::InvalidRedirectToURL(format!( |
| 47 | + "redirect url locales do not match: {from_locale} != {to_locale}" |
| 48 | + ))); |
| 49 | + } |
| 50 | + } |
| 51 | + add_redirects(from_locale, &[(from_url.to_owned(), to_url.to_owned())])?; |
| 52 | + Ok(()) |
| 53 | +} |
| 54 | + |
| 55 | +fn validate_args(from_url: &str, to_url: &str) -> Result<(), ToolError> { |
| 56 | + if from_url.is_empty() { |
| 57 | + return Err(ToolError::InvalidUrl(Cow::Borrowed( |
| 58 | + "from_url cannot be empty", |
| 59 | + ))); |
| 60 | + } |
| 61 | + if to_url.is_empty() { |
| 62 | + return Err(ToolError::InvalidUrl(Cow::Borrowed( |
| 63 | + "to_url cannot be empty", |
| 64 | + ))); |
| 65 | + } |
| 66 | + if from_url.contains("#") { |
| 67 | + return Err(ToolError::InvalidUrl(Cow::Borrowed( |
| 68 | + "from_url cannot contain '#'", |
| 69 | + ))); |
| 70 | + } |
| 71 | + Ok(()) |
| 72 | +} |
| 73 | + |
| 74 | +// These tests use file system fixtures to simulate content and translated content. |
| 75 | +// The file system is a shared resource, so we force tests to be run serially, |
| 76 | +// to avoid concurrent fixture management issues. |
| 77 | +// Using `file_serial` as a synchonization lock, we run all tests using |
| 78 | +// the same `key` (here: file_fixtures) to be serialized across modules. |
| 79 | +#[cfg(test)] |
| 80 | +use serial_test::file_serial; |
| 81 | +#[cfg(test)] |
| 82 | +#[file_serial(file_fixtures)] |
| 83 | +mod test { |
| 84 | + use rari_types::locale::Locale; |
| 85 | + |
| 86 | + use super::*; |
| 87 | + use crate::tests::fixtures::docs::DocFixtures; |
| 88 | + use crate::tests::fixtures::redirects::RedirectFixtures; |
| 89 | + use crate::utils::test_utils::get_redirects_map; |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn test_add_redirect() { |
| 93 | + let slugs = vec![ |
| 94 | + "Web/API/ExampleOne".to_string(), |
| 95 | + "Web/API/SomethingElse".to_string(), |
| 96 | + ]; |
| 97 | + let _docs = DocFixtures::new(&slugs, Locale::EnUs); |
| 98 | + let _redirects = RedirectFixtures::new( |
| 99 | + &vec![( |
| 100 | + "docs/Web/API/Something".to_string(), |
| 101 | + "docs/Web/API/SomethingElse".to_string(), |
| 102 | + )], |
| 103 | + Locale::EnUs, |
| 104 | + ); |
| 105 | + |
| 106 | + let result = do_add_redirect( |
| 107 | + "/en-US/docs/Web/API/ExampleGone", |
| 108 | + "/en-US/docs/Web/API/ExampleOne", |
| 109 | + ); |
| 110 | + assert!(result.is_ok()); |
| 111 | + |
| 112 | + let redirects = get_redirects_map(Locale::EnUs); |
| 113 | + assert_eq!(redirects.len(), 2); |
| 114 | + assert!(redirects.contains_key("/en-US/docs/Web/API/ExampleGone")); |
| 115 | + assert_eq!( |
| 116 | + redirects.get("/en-US/docs/Web/API/ExampleGone").unwrap(), |
| 117 | + "/en-US/docs/Web/API/ExampleOne" |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn test_add_redirect_missing_target() { |
| 123 | + let slugs = vec!["Web/API/ExampleOne".to_string()]; |
| 124 | + let _docs = DocFixtures::new(&slugs, Locale::EnUs); |
| 125 | + let _redirects = RedirectFixtures::new(&vec![], Locale::EnUs); |
| 126 | + |
| 127 | + let result = do_add_redirect( |
| 128 | + "/en-US/docs/Web/API/ExampleGone", |
| 129 | + "/en-US/docs/Web/API/ExampleMissing", |
| 130 | + ); |
| 131 | + assert!(result.is_err()); |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn test_add_redirect_differing_locales() { |
| 136 | + let slugs = vec!["Web/API/ExampleOne".to_string()]; |
| 137 | + let _docs = DocFixtures::new(&slugs, Locale::EnUs); |
| 138 | + let _docs_pt = DocFixtures::new(&slugs, Locale::PtBr); |
| 139 | + let _redirects = RedirectFixtures::new(&vec![], Locale::EnUs); |
| 140 | + let _redirects_pt = RedirectFixtures::new(&vec![], Locale::PtBr); |
| 141 | + |
| 142 | + // Locales do not match |
| 143 | + let result = do_add_redirect( |
| 144 | + "/en-US/docs/Web/API/ExampleGone", |
| 145 | + "/pt-BR/docs/Web/API/ExampleOne", |
| 146 | + ); |
| 147 | + assert!(result.is_err()); |
| 148 | + assert!(matches!(result, Err(ToolError::InvalidRedirectToURL(_)))); |
| 149 | + |
| 150 | + // Target is en-US, even if locales differ |
| 151 | + let result = do_add_redirect( |
| 152 | + "/pt-BR/docs/Web/API/ExampleGone", |
| 153 | + "/en-US/docs/Web/API/ExampleOne", |
| 154 | + ); |
| 155 | + assert!(result.is_ok()); |
| 156 | + |
| 157 | + let redirects = get_redirects_map(Locale::PtBr); |
| 158 | + assert!(redirects.contains_key("/pt-BR/docs/Web/API/ExampleGone")); |
| 159 | + assert_eq!( |
| 160 | + redirects.get("/pt-BR/docs/Web/API/ExampleGone").unwrap(), |
| 161 | + "/en-US/docs/Web/API/ExampleOne" |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn test_add_redirect_external() { |
| 167 | + let slugs = vec!["Web/API/ExampleOne".to_string()]; |
| 168 | + let _docs = DocFixtures::new(&slugs, Locale::EnUs); |
| 169 | + let _redirects = RedirectFixtures::new(&vec![], Locale::EnUs); |
| 170 | + |
| 171 | + let result = do_add_redirect("/en-US/docs/Web/API/ExampleGone", "https://example.com/"); |
| 172 | + assert!(result.is_ok()); |
| 173 | + |
| 174 | + let redirects = get_redirects_map(Locale::EnUs); |
| 175 | + assert!(redirects.contains_key("/en-US/docs/Web/API/ExampleGone")); |
| 176 | + assert_eq!( |
| 177 | + redirects.get("/en-US/docs/Web/API/ExampleGone").unwrap(), |
| 178 | + "https://example.com/" |
| 179 | + ); |
| 180 | + } |
| 181 | +} |
0 commit comments