Skip to content

Commit e6c3c40

Browse files
committed
fixup tests
1 parent f56e086 commit e6c3c40

File tree

2 files changed

+87
-82
lines changed

2 files changed

+87
-82
lines changed

tests/cli.rs

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ fn test_dead_link() {
1515
predicate::str::is_match(
1616
r#"^Reading files
1717
Checking 1 links from 1 files \(1 documents\)
18-
\...index\.html
19-
error: bad link .bar.html
18+
\..index\.html
19+
error: bad link /bar.html
2020
2121
Found 1 bad links
2222
"#,
@@ -40,8 +40,8 @@ fn test_dead_anchor() {
4040
predicate::str::is_match(
4141
r#"^Reading files
4242
Checking 1 links from 2 files \(2 documents\)
43-
\...index\.html
44-
error: bad link .bar.html#goo
43+
\..index\.html
44+
error: bad link /bar.html#goo
4545
4646
Found 0 bad links
4747
Found 1 bad anchors
@@ -65,81 +65,3 @@ fn test_bad_dir() {
6565
"Error: IO error for operation on non_existing_dir:",
6666
));
6767
}
68-
69-
#[test]
70-
fn test_redirects() {
71-
let site = assert_fs::TempDir::new().unwrap();
72-
73-
site.child("_redirects")
74-
.write_str(
75-
"# This is a comment\n\
76-
\n\
77-
/old-page /new-page.html 301\n\
78-
/external https://example.com/page\n\
79-
/broken /missing-page.html\n\
80-
/another /target.html",
81-
)
82-
.unwrap();
83-
84-
site.child("new-page.html").touch().unwrap();
85-
site.child("target.html").touch().unwrap();
86-
87-
site.child("index.html")
88-
.write_str("<a href='/old-page'>link</a>")
89-
.unwrap();
90-
91-
let mut cmd = Command::cargo_bin("hyperlink").unwrap();
92-
cmd.current_dir(site.path()).arg(".");
93-
94-
cmd.assert().failure().code(1).stdout(
95-
predicate::str::is_match(
96-
r#"^Reading files
97-
Checking 4 links from 4 files \(4 documents\)
98-
\....._redirects
99-
error: bad link .missing-page\.html
100-
101-
Found 1 bad links
102-
"#,
103-
)
104-
.unwrap(),
105-
);
106-
107-
site.close().unwrap();
108-
}
109-
110-
#[test]
111-
fn test_redirects_only_at_root() {
112-
let site = assert_fs::TempDir::new().unwrap();
113-
114-
site.child("_redirects")
115-
.write_str("/old-page /new-page.html")
116-
.unwrap();
117-
118-
site.child("subdir/_redirects")
119-
.write_str("/sub-old /sub-new.html")
120-
.unwrap();
121-
122-
site.child("new-page.html").touch().unwrap();
123-
124-
site.child("index.html")
125-
.write_str("<a href='/old-page'>link to old</a><a href='/sub-old'>link to sub</a>")
126-
.unwrap();
127-
128-
let mut cmd = Command::cargo_bin("hyperlink").unwrap();
129-
cmd.current_dir(site.path()).arg(".");
130-
131-
cmd.assert().failure().code(1).stdout(
132-
predicate::str::is_match(
133-
r#"^Reading files
134-
Checking 3 links from 4 files \(3 documents\)
135-
\./index\.html
136-
error: bad link /sub-old
137-
138-
Found 1 bad links
139-
"#,
140-
)
141-
.unwrap(),
142-
);
143-
144-
site.close().unwrap();
145-
}

tests/cli_snapshots.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use assert_fs::prelude::*;
12
use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
23
use std::process::Command;
34

@@ -98,3 +99,85 @@ fn test_version() {
9899
----- stderr -----
99100
"###);
100101
}
102+
103+
#[test]
104+
fn test_redirects() {
105+
let site = assert_fs::TempDir::new().unwrap();
106+
107+
site.child("_redirects")
108+
.write_str(
109+
"# This is a comment\n\
110+
\n\
111+
/old-page /new-page.html 301\n\
112+
/external https://example.com/page\n\
113+
/broken /missing-page.html\n\
114+
/another /target.html",
115+
)
116+
.unwrap();
117+
118+
site.child("new-page.html").touch().unwrap();
119+
site.child("target.html").touch().unwrap();
120+
121+
site.child("index.html")
122+
.write_str("<a href='/old-page'>link</a>")
123+
.unwrap();
124+
125+
let mut settings = insta::Settings::clone_current();
126+
settings.add_filter(r"[/\\]", "/");
127+
let _guard = settings.bind_to_scope();
128+
129+
assert_cmd_snapshot!(cli().arg(".").current_dir(site.path()), @r###"
130+
success: false
131+
exit_code: 1
132+
----- stdout -----
133+
Reading files
134+
Checking 4 links from 4 files (4 documents)
135+
./_redirects
136+
error: bad link /missing-page.html
137+
138+
Found 1 bad links
139+
140+
----- stderr -----
141+
"###);
142+
143+
site.close().unwrap();
144+
}
145+
146+
#[test]
147+
fn test_redirects_only_at_root() {
148+
let site = assert_fs::TempDir::new().unwrap();
149+
150+
site.child("_redirects")
151+
.write_str("/old-page /new-page.html")
152+
.unwrap();
153+
154+
site.child("subdir/_redirects")
155+
.write_str("/sub-old /sub-new.html")
156+
.unwrap();
157+
158+
site.child("new-page.html").touch().unwrap();
159+
160+
site.child("index.html")
161+
.write_str("<a href='/old-page'>link to old</a><a href='/sub-old'>link to sub</a>")
162+
.unwrap();
163+
164+
let mut settings = insta::Settings::clone_current();
165+
settings.add_filter(r"[/\\]", "/");
166+
let _guard = settings.bind_to_scope();
167+
168+
assert_cmd_snapshot!(cli().arg(".").current_dir(site.path()), @r###"
169+
success: false
170+
exit_code: 1
171+
----- stdout -----
172+
Reading files
173+
Checking 3 links from 4 files (3 documents)
174+
./index.html
175+
error: bad link /sub-old
176+
177+
Found 1 bad links
178+
179+
----- stderr -----
180+
"###);
181+
182+
site.close().unwrap();
183+
}

0 commit comments

Comments
 (0)