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