/
e2e_test.dart
320 lines (276 loc) · 8.71 KB
/
e2e_test.dart
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dhttpd/dhttpd.dart';
import 'package:linkcheck/linkcheck.dart' show run;
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
// Get the directory of the script being run.
void main() {
group('linkcheck e2e', () {
late _MockStdout out;
final port = 4321;
setUp(() {
out = _MockStdout();
});
tearDown(() {
unawaited(out.close());
});
test('reports no errors or warnings for a site without issues', () async {
final server = await Dhttpd.start(path: getServingPath(0), port: port);
try {
final result = await run([':$port'], out);
expect(result, 0);
expect(out.output, contains('0 warnings'));
expect(out.output, contains('0 errors'));
} finally {
await server.destroy();
}
});
test('reports no errors or warnings for a site with correct <base>',
() async {
final server = await Dhttpd.start(path: getServingPath(4), port: port);
try {
final result = await run([':$port'], out);
expect(result, 0);
expect(out.output, contains('0 warnings'));
expect(out.output, contains('0 errors'));
} finally {
await server.destroy();
}
});
test('reports info when link is behind robots.txt rule', () async {
final server = await Dhttpd.start(path: getServingPath(5), port: port);
try {
final result = await run([':$port'], out);
expect(result, 0);
expect(out.output, contains('subdirectory/other.html'));
expect(out.output, contains('0 warnings'));
expect(out.output, contains('0 errors'));
} finally {
await server.destroy();
}
});
test(
'reports bad link in file that is disallowed in robots.txt '
'but allowed for linkcheck', () async {
final server = await Dhttpd.start(path: getServingPath(11), port: port);
try {
final result = await run([':$port'], out);
expect(result, 2);
expect(out.output, contains('non-existent.html'));
expect(out.output, contains('1 error'));
} finally {
await server.destroy();
}
});
group('reports exit code 2 for a site with errors', () {
test('in CSS', () async {
final server = await Dhttpd.start(path: getServingPath(1), port: port);
try {
final result = await run([':$port'], out);
expect(result, 2);
expect(out.output, contains('main.css'));
expect(out.output, contains('1 error'));
} finally {
await server.destroy();
}
});
test('in <link> import', () async {
final server = await Dhttpd.start(path: getServingPath(2), port: port);
try {
final result = await run([':$port'], out);
expect(result, 2);
expect(out.output, contains('nonexistent.css'));
expect(out.output, contains('1 error'));
} finally {
await server.destroy();
}
});
test('in <a> to non-existent internal page', () async {
final server = await Dhttpd.start(path: getServingPath(3), port: port);
try {
final result = await run([':$port'], out);
expect(result, 2);
expect(out.output, contains('other.html'));
expect(out.output, contains('nonexistent.html'));
expect(out.output, contains('1 error'));
} finally {
await server.destroy();
}
});
});
test('reports all missing @font-face sources', () async {
final server = await Dhttpd.start(path: getServingPath(6), port: port);
try {
final result = await run([':$port'], out);
expect(result, 2);
expect(out.output, contains('asset1.eot'));
expect(out.output, contains('asset2.ttf'));
expect(out.output, contains('asset3.woff'));
expect(out.output, contains('asset4.svg'));
} finally {
await server.destroy();
}
});
test('allows non-Base64-encoded SVG inline', () async {
final server = await Dhttpd.start(path: getServingPath(7), port: port);
try {
final result = await run([':$port'], out);
expect(result, 0);
expect(out.output, contains('0 warnings'));
expect(out.output, contains('0 errors'));
} finally {
await server.destroy();
}
});
test('skips URLs according to their resolved URL with fragment', () async {
final server = await Dhttpd.start(path: getServingPath(8), port: port);
try {
final result = await run(
[':$port', '--skip-file', 'test/case8/skip-file.txt'], out);
expect(result, 0);
expect(out.output, contains('0 warnings'));
expect(out.output, contains('0 errors'));
} finally {
await server.destroy();
}
});
test('skips external URLs according to their resolved URL with fragment',
() async {
final server = await Dhttpd.start(path: getServingPath(12), port: port);
try {
final result = await run(
[':$port', '-e', '--skip-file', 'test/case12/skip-file.txt'], out);
expect(result, 0);
expect(out.output, contains('0 warnings'));
expect(out.output, contains('0 errors'));
} finally {
await server.destroy();
}
});
test('works with unicode in title', () async {
final server = await Dhttpd.start(path: getServingPath(9), port: port);
try {
final result = await run([':$port'], out);
expect(result, 0);
} finally {
await server.destroy();
}
});
test('anchors are normalized', () async {
final server = await Dhttpd.start(path: getServingPath(10), port: port);
try {
final result = await run([':$port'], out);
expect(result, 0);
} finally {
await server.destroy();
}
});
test('fragment checking works with non-percent-encoded anchors', () async {
final server = await Dhttpd.start(path: getServingPath(13), port: port);
try {
final result = await run([':$port'], out);
expect(result, 0);
} finally {
await server.destroy();
}
});
test("destinations with wrong mime-types aren't checked", () async {
final server = await Dhttpd.start(path: getServingPath(14), port: port);
try {
final result = await run([':$port'], out);
expect(result, 0);
} finally {
await server.destroy();
}
});
}, tags: ['integration']);
}
String directory = path.absolute(path.dirname(scriptPath));
String scriptPath = scriptUri.toFilePath();
Uri scriptUri = Platform.script;
String getServingPath(int caseNumber) =>
path.join(directory, 'case$caseNumber');
class _MockStdout implements Stdout {
// StreamController<List<int>> _controller = new StreamController();
// StreamSink<List<int>> _sink;
StringBuffer buf = StringBuffer();
@override
final Encoding encoding = const Utf8Codec();
_MockStdout() {
// _sink = _controller.sink;
}
@override
Never get done => throw UnimplementedError();
@override
set encoding(Encoding encoding) {
throw UnimplementedError();
}
@override
bool get hasTerminal => false;
@override
IOSink get nonBlocking {
throw UnimplementedError();
}
String get output => buf.toString();
@override
bool get supportsAnsiEscapes => false;
@override
int get terminalColumns => 80;
@override
int get terminalLines => 40;
@override
void add(List<int> data) {
throw UnimplementedError();
// _sink.add(data);
}
@override
Never addError(Object error, [StackTrace? stackTrace]) {
throw error;
// _sink.addError(error, stackTrace);
}
@override
Never addStream(Stream<List<int>> stream) => throw UnimplementedError();
void clearOutput() {
buf.clear();
}
@override
Future<void> close() async {
// await _sink.close();
// await _controller.close();
}
@override
Never flush() => throw UnimplementedError();
@override
void write(Object? object) {
final string = '$object';
buf.write(string);
}
@override
void writeAll(Iterable<dynamic> objects, [String sep = '']) {
final iterator = objects.iterator;
if (!iterator.moveNext()) return;
if (sep.isEmpty) {
do {
write(iterator.current);
} while (iterator.moveNext());
} else {
write(iterator.current);
while (iterator.moveNext()) {
write(sep);
write(iterator.current);
}
}
}
@override
void writeCharCode(int charCode) {
write(String.fromCharCode(charCode));
}
@override
void writeln([Object? object]) {
object ??= '';
write(object);
write('\n');
}
}