Skip to content

Commit d036752

Browse files
committed
test(cli): More tests
Signed-off-by: Dmitry Dygalo <[email protected]>
1 parent 49fda68 commit d036752

File tree

2 files changed

+141
-8
lines changed

2 files changed

+141
-8
lines changed

css-inline/src/main.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
126126
) -> Result<(), ParseError> {
127127
match flag {
128128
"inline-style-tags" => parsed.inline_style_tags = parse_value(value, flag)?,
129+
"load-remote-stylesheets" => parsed.load_remote_stylesheets = parse_value(value, flag)?,
129130
"base-url" => parsed.base_url = Some(value.to_string()),
130131
"extra-css" => parsed.extra_css = Some(value.to_string()),
131132
"extra-css-file" => parsed.extra_css_files.push(value.to_string()),
@@ -149,7 +150,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
149150
"version" | "v" => parsed.version = true,
150151
"keep-style-tags" => parsed.keep_style_tags = true,
151152
"keep-link-tags" => parsed.keep_link_tags = true,
152-
"load-remote-stylesheets" => parsed.load_remote_stylesheets = true,
153153
_ => {
154154
return Err(ParseError {
155155
message: format!("Unknown flag: {flag}"),
@@ -259,26 +259,38 @@ OPTIONS:
259259
if let Some(flag) = arg.strip_prefix("--") {
260260
// Handle --key=value format
261261
if let Some((flag, value)) = flag.split_once('=') {
262-
handle_flag_with_value(&mut args, flag, value)?;
262+
if let Err(error) = handle_flag_with_value(&mut args, flag, value) {
263+
eprintln!("{error}");
264+
std::process::exit(1);
265+
}
263266
} else {
264267
// Handle --key format (boolean or expecting value)
265268
if requires_value(flag) {
266269
// Expects a value
267270
if let Some(value) = raw_args.next() {
268-
handle_flag_with_value(&mut args, flag, &value)?;
271+
if let Err(error) = handle_flag_with_value(&mut args, flag, &value) {
272+
eprintln!("{error}");
273+
std::process::exit(1);
274+
}
269275
} else {
270276
eprintln!("Error parsing arguments: Flag --{flag} requires a value");
271277
std::process::exit(1);
272278
}
273279
} else {
274280
// Boolean flag
275-
handle_boolean_flag(&mut args, flag)?;
281+
if let Err(error) = handle_boolean_flag(&mut args, flag) {
282+
eprintln!("{error}");
283+
std::process::exit(1);
284+
}
276285
}
277286
}
278287
} else if let Some(flag) = arg.strip_prefix('-') {
279288
if flag.len() == 1 {
280289
// Single character short flag
281-
handle_boolean_flag(&mut args, flag)?;
290+
if let Err(error) = handle_boolean_flag(&mut args, flag) {
291+
eprintln!("{error}");
292+
std::process::exit(1);
293+
}
282294
} else {
283295
eprintln!("Error parsing arguments: Invalid flag: -{flag}");
284296
std::process::exit(1);

css-inline/tests/test_cli.rs

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ pub mod tests {
1414
fn success() {
1515
css_inline()
1616
.arg("tests/example.html")
17-
.arg("--output-filename-prefix=inlined.keep-style-tags.")
17+
.arg("--output-filename-prefix=inlined.success.")
1818
.assert()
1919
.success()
2020
.stdout("tests/example.html: SUCCESS\n");
21-
let content = fs::read_to_string("tests/inlined.keep-style-tags.example.html").unwrap();
21+
let content = fs::read_to_string("tests/inlined.success.example.html").unwrap();
2222
assert_eq!(
2323
content,
2424
"<html><head>\n \
@@ -38,10 +38,11 @@ pub mod tests {
3838
css_inline()
3939
.arg("tests/example.html")
4040
.arg("--keep-style-tags")
41+
.arg("--output-filename-prefix=inlined.keep-style-tags.")
4142
.assert()
4243
.success()
4344
.stdout("tests/example.html: SUCCESS\n");
44-
let content = fs::read_to_string("tests/inlined.example.html").unwrap();
45+
let content = fs::read_to_string("tests/inlined.keep-style-tags.example.html").unwrap();
4546
assert_eq!(
4647
content,
4748
"<html><head>\n \n \
@@ -55,6 +56,49 @@ pub mod tests {
5556
)
5657
}
5758

59+
#[test]
60+
fn dont_inline_styles() {
61+
css_inline()
62+
.arg("tests/example.html")
63+
.arg("--inline-style-tags=false")
64+
.arg("--output-filename-prefix=inlined.dont-inline-styles.")
65+
.assert()
66+
.success()
67+
.stdout("tests/example.html: SUCCESS\n");
68+
let content = fs::read_to_string("tests/inlined.dont-inline-styles.example.html").unwrap();
69+
assert_eq!(
70+
content,
71+
"<html><head>\n \n \n \n\
72+
</head>\n\
73+
<body>\n\
74+
<a class=\"test-class\" href=\"https://example.com\">Test</a>\n\
75+
<h1>Test</h1>\n\n\n\
76+
</body></html>"
77+
)
78+
}
79+
80+
#[test]
81+
fn no_remote_stylesheets() {
82+
css_inline()
83+
.arg("tests/example.html")
84+
.arg("--load-remote-stylesheets=false")
85+
.arg("--output-filename-prefix=inlined.no-remote-stylesheets.")
86+
.assert()
87+
.success()
88+
.stdout("tests/example.html: SUCCESS\n");
89+
let content =
90+
fs::read_to_string("tests/inlined.no-remote-stylesheets.example.html").unwrap();
91+
assert_eq!(
92+
content,
93+
"<html><head>\n \n \n \n\
94+
</head>\n\
95+
<body>\n\
96+
<a class=\"test-class\" href=\"https://example.com\" style=\"color: #ffffff;\">Test</a>\n\
97+
<h1 style=\"text-decoration: none;\">Test</h1>\n\n\n\
98+
</body></html>"
99+
)
100+
}
101+
58102
#[test]
59103
#[cfg(feature = "stylesheet-cache")]
60104
fn cache_valid() {
@@ -175,11 +219,88 @@ pub mod tests {
175219
}
176220

177221
#[test_case("--help", "css-inline inlines CSS into HTML")]
222+
#[test_case("-h", "css-inline inlines CSS into HTML")]
178223
#[test_case("--version", "css-inline")]
224+
#[test_case("-v", "css-inline")]
179225
fn args(arg: &str, expected: &str) {
180226
let stdout = css_inline().arg(arg).assert().success().to_string();
181227
assert!(stdout.contains(expected), "{}", stdout);
182228
}
229+
230+
#[test]
231+
fn flag_requires_value_but_none_provided() {
232+
css_inline()
233+
.arg("--base-url")
234+
.assert()
235+
.failure()
236+
.stderr("Error parsing arguments: Flag --base-url requires a value\n");
237+
}
238+
239+
#[test]
240+
fn invalid_multi_character_short_flag() {
241+
css_inline()
242+
.arg("-abc")
243+
.assert()
244+
.failure()
245+
.stderr("Error parsing arguments: Invalid flag: -abc\n");
246+
}
247+
248+
#[test]
249+
fn keep_link_tags_flag() {
250+
css_inline()
251+
.arg("tests/example.html")
252+
.arg("--keep-link-tags")
253+
.arg("--output-filename-prefix=inlined.keep-link-tags.")
254+
.assert()
255+
.success()
256+
.stdout("tests/example.html: SUCCESS\n");
257+
}
258+
259+
#[test]
260+
fn unknown_short_flag() {
261+
css_inline()
262+
.arg("-b")
263+
.assert()
264+
.failure()
265+
.stderr("Unknown flag: b\n");
266+
}
267+
268+
#[test]
269+
fn unknown_boolean_flag() {
270+
css_inline()
271+
.arg("--unknown-flag")
272+
.assert()
273+
.failure()
274+
.stderr("Unknown flag: unknown-flag\n");
275+
}
276+
277+
#[test]
278+
fn unknown_flag_with_value() {
279+
css_inline()
280+
.arg("--unknown-flag=value")
281+
.assert()
282+
.failure()
283+
.stderr("Unknown flag: --unknown-flag\n");
284+
}
285+
286+
#[test]
287+
fn invalid_boolean_value_for_flag() {
288+
css_inline()
289+
.arg("--inline-style-tags=invalid")
290+
.assert()
291+
.failure()
292+
.stderr("Failed to parse value 'invalid' for flag 'inline-style-tags': provided string was not `true` or `false`\n");
293+
}
294+
295+
#[test]
296+
#[cfg(feature = "stylesheet-cache")]
297+
fn invalid_numeric_value_for_cache_size() {
298+
css_inline()
299+
.arg("--cache-size=invalid")
300+
.assert()
301+
.failure()
302+
.stderr("Failed to parse value 'invalid' for flag 'cache-size': invalid digit found in string\n");
303+
}
183304
}
184305

185306
#[cfg(not(feature = "cli"))]

0 commit comments

Comments
 (0)