-
Notifications
You must be signed in to change notification settings - Fork 198
/
mod.rs
1054 lines (933 loc) · 35.2 KB
/
mod.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Web interface of docs.rs
pub(crate) mod page;
use crate::utils::get_correct_docsrs_style_file;
use crate::utils::report_error;
use anyhow::anyhow;
use log::info;
use serde_json::Value;
/// ctry! (cratesfyitry) is extremely similar to try! and itry!
/// except it returns an error page response instead of plain Err.
#[macro_export]
macro_rules! ctry {
($req:expr, $result:expr $(,)?) => {
match $result {
Ok(success) => success,
Err(error) => {
let request: &::iron::Request = $req;
// This is very ugly, but it makes it impossible to get a type inference error
// from this macro
let web_error = $crate::web::ErrorPage {
title: "Internal Server Error",
message: ::std::option::Option::Some(::std::borrow::Cow::Owned(error.to_string())),
status: ::iron::status::InternalServerError,
};
let error = anyhow::anyhow!(error)
.context(format!("called `ctry!()` on an `Err` value while attempting to fetch the route {:?}", request.url));
$crate::utils::report_error(&error);
return $crate::web::page::WebPage::into_response(web_error, request);
}
}
};
}
/// cexpect will check an option and if it's not Some
/// it will return an error page response
macro_rules! cexpect {
($req:expr, $option:expr $(,)?) => {
match $option {
Some(success) => success,
None => {
let request: &::iron::Request = $req;
// This is very ugly, but it makes it impossible to get a type inference error
// from this macro
let web_error = $crate::web::ErrorPage {
title: "Internal Server Error",
message: None,
status: ::iron::status::InternalServerError,
};
let error = anyhow::anyhow!("called `cexpect!()` on a `None` value while attempting to fetch the route {:?}", request.url);
$crate::utils::report_error(&error);
return $crate::web::page::WebPage::into_response(web_error, request);
}
}
};
}
/// Gets an extension from Request
macro_rules! extension {
($req:expr, $ext:ty) => {{
// Bind $req so we can have good type errors and avoid re-evaluation
let request: &::iron::Request = $req;
cexpect!(request, request.extensions.get::<$ext>())
}};
}
mod build_details;
mod builds;
pub(crate) mod crate_details;
mod csp;
mod error;
mod extensions;
mod features;
mod file;
pub(crate) mod metrics;
mod releases;
mod routes;
mod rustdoc;
mod sitemap;
mod source;
mod statics;
use crate::{impl_webpage, Context};
use anyhow::Error;
use chrono::{DateTime, Utc};
use csp::CspMiddleware;
use error::Nope;
use extensions::InjectExtensions;
use iron::{
self,
headers::{Expires, HttpDate},
modifiers::Redirect,
status,
status::Status,
Chain, Handler, Iron, IronError, IronResult, Listening, Request, Response, Url,
};
use page::TemplateData;
use postgres::Client;
use router::{NoRoute, TrailingSlash};
use semver::{Version, VersionReq};
use serde::Serialize;
use std::{borrow::Cow, net::SocketAddr, sync::Arc};
/// Duration of static files for staticfile and DatabaseFileHandler (in seconds)
const STATIC_FILE_CACHE_DURATION: u64 = 60 * 60 * 24 * 30 * 12; // 12 months
const DEFAULT_BIND: &str = "0.0.0.0:3000";
struct MainHandler {
shared_resource_handler: Box<dyn Handler>,
router_handler: Box<dyn Handler>,
inject_extensions: InjectExtensions,
}
impl MainHandler {
fn chain<H: Handler>(inject_extensions: InjectExtensions, base: H) -> Chain {
let mut chain = Chain::new(base);
chain.link_before(inject_extensions);
chain.link_before(CspMiddleware);
chain.link_after(CspMiddleware);
chain
}
fn new(template_data: Arc<TemplateData>, context: &dyn Context) -> Result<MainHandler, Error> {
let inject_extensions = InjectExtensions::new(context, template_data)?;
let routes = routes::build_routes();
let shared_resources =
Self::chain(inject_extensions.clone(), rustdoc::SharedResourceHandler);
let router_chain = Self::chain(inject_extensions.clone(), routes.iron_router());
Ok(MainHandler {
shared_resource_handler: Box::new(shared_resources),
router_handler: Box::new(router_chain),
inject_extensions,
})
}
}
impl Handler for MainHandler {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
fn if_404(
e: IronError,
handle: impl FnOnce() -> IronResult<Response>,
) -> IronResult<Response> {
if e.response.status == Some(status::NotFound) {
// the routes are ordered from least specific to most; give precedence to the
// new error message.
handle()
} else {
Err(e)
}
}
// This is kind of a mess.
//
// Almost all files should be served through the `router_handler`; eventually
// `shared_resource_handler` should go through the router too.
//
// Unfortunately, combining `shared_resource_handler` with the `router_handler` breaks
// things, because right now `shared_resource_handler` allows requesting files from *any*
// subdirectory and the router requires us to give a specific path. Changing them to a
// specific path means that buggy docs from 2018 will have missing CSS (#1181) so until
// that's fixed, we need to keep the current (buggy) behavior.
//
// It's important that `shared_resource_handler` comes first so that global rustdoc files take
// precedence over local ones (see #1327).
self.shared_resource_handler
.handle(req)
.or_else(|e| if_404(e, || self.router_handler.handle(req)))
.or_else(|e| {
// in some cases the iron router will return a redirect as an `IronError`.
// Here we convert these into an `Ok(Response)`.
if e.error.downcast_ref::<TrailingSlash>().is_some()
|| e.response.status == Some(status::MovedPermanently)
{
Ok(e.response)
} else {
Err(e)
}
})
.or_else(|e| {
let err = if let Some(err) = e.error.downcast_ref::<error::Nope>() {
*err
} else if e.error.downcast_ref::<NoRoute>().is_some()
|| e.response.status == Some(status::NotFound)
{
error::Nope::ResourceNotFound
} else if e.response.status == Some(status::InternalServerError) {
report_error(&anyhow!("internal server error: {}", e.error));
error::Nope::InternalServerError
} else {
report_error(&anyhow!(
"No error page for status {:?}; {}",
e.response.status,
e.error
));
// TODO: add in support for other errors that are actually used
error::Nope::InternalServerError
};
Self::chain(self.inject_extensions.clone(), err).handle(req)
})
}
}
#[derive(Debug)]
struct MatchVersion {
/// Represents the crate name that was found when attempting to load a crate release.
///
/// `match_version` will attempt to match a provided crate name against similar crate names with
/// dashes (`-`) replaced with underscores (`_`) and vice versa.
pub corrected_name: Option<String>,
pub version: MatchSemver,
pub rustdoc_status: bool,
pub target_name: String,
}
impl MatchVersion {
/// If the matched version was an exact match to the requested crate name, returns the
/// `MatchSemver` for the query. If the lookup required a dash/underscore conversion, returns
/// `CrateNotFound`.
fn assume_exact(self) -> Result<MatchSemver, Nope> {
if self.corrected_name.is_none() {
Ok(self.version)
} else {
Err(Nope::CrateNotFound)
}
}
}
/// Represents the possible results of attempting to load a version requirement.
/// The id (i32) of the release is stored to simplify successive queries.
#[derive(Debug, Clone, PartialEq, Eq)]
enum MatchSemver {
/// `match_version` was given an exact version, which matched a saved crate version.
Exact((String, i32)),
/// `match_version` was given a semver version requirement, which matched the given saved crate
/// version.
Semver((String, i32)),
// `match_version` was given the string "latest", which matches the given saved crate version.
Latest((String, i32)),
}
impl MatchSemver {
/// Discard information about whether the loaded version was an exact match, and return the
/// matched version string and id.
pub fn into_parts(self) -> (String, i32) {
match self {
MatchSemver::Exact((v, i))
| MatchSemver::Semver((v, i))
| MatchSemver::Latest((v, i)) => (v, i),
}
}
}
/// Checks the database for crate releases that match the given name and version.
///
/// `version` may be an exact version number or loose semver version requirement. The return value
/// will indicate whether the given version exactly matched a version number from the database.
///
/// This function will also check for crates where dashes in the name (`-`) have been replaced with
/// underscores (`_`) and vice-versa. The return value will indicate whether the crate name has
/// been matched exactly, or if there has been a "correction" in the name that matched instead.
fn match_version(
conn: &mut Client,
name: &str,
input_version: Option<&str>,
) -> Result<MatchVersion, Nope> {
let (crate_id, corrected_name) = {
let rows = conn
.query(
"SELECT id, name
FROM crates
WHERE normalize_crate_name(name) = normalize_crate_name($1)",
&[&name],
)
.unwrap();
let row = rows.get(0).ok_or(Nope::CrateNotFound)?;
let id: i32 = row.get(0);
let db_name = row.get(1);
if db_name != name {
(id, Some(db_name))
} else {
(id, None)
}
};
// first load and parse all versions of this crate,
// skipping and reporting versions that are not semver valid.
// `releases_for_crate` is already sorted, newest version first.
let releases = crate_details::releases_for_crate(conn, crate_id)
.expect("error fetching releases for crate");
if releases.is_empty() {
return Err(Nope::CrateNotFound);
}
// version is an Option<&str> from router::Router::get, need to decode first.
// Any encoding errors we treat as _any version_.
use iron::url::percent_encoding::percent_decode;
let req_version = input_version
.and_then(|v| percent_decode(v.as_bytes()).decode_utf8().ok())
.unwrap_or_else(|| "*".into());
// first check for exact match, we can't expect users to use semver in query
if let Ok(parsed_req_version) = Version::parse(&req_version) {
if let Some(release) = releases
.iter()
.find(|release| release.version == parsed_req_version)
{
return Ok(MatchVersion {
corrected_name,
version: MatchSemver::Exact((release.version.to_string(), release.id)),
rustdoc_status: release.rustdoc_status,
target_name: release.target_name.clone(),
});
}
}
// Now try to match with semver, treat `newest` and `latest` as `*`
let req_semver = if req_version == "newest" || req_version == "latest" {
VersionReq::STAR
} else {
VersionReq::parse(&req_version).map_err(|err| {
log::info!(
"could not parse version requirement \"{}\": {:?}",
req_version,
err
);
Nope::VersionNotFound
})?
};
// starting here, we only look at non-yanked releases
let releases: Vec<_> = releases.iter().filter(|r| !r.yanked).collect();
// try to match the version in all un-yanked releases.
if let Some(release) = releases
.iter()
.find(|release| req_semver.matches(&release.version))
{
return Ok(MatchVersion {
corrected_name,
version: if input_version == Some("latest") {
MatchSemver::Latest((release.version.to_string(), release.id))
} else {
MatchSemver::Semver((release.version.to_string(), release.id))
},
rustdoc_status: release.rustdoc_status,
target_name: release.target_name.clone(),
});
}
// semver `*` does not match pre-releases.
// When someone wants the latest release and we have only pre-releases
// just return the latest prerelease.
if req_semver == VersionReq::STAR {
return releases
.first()
.map(|release| MatchVersion {
corrected_name: corrected_name.clone(),
version: MatchSemver::Semver((release.version.to_string(), release.id)),
rustdoc_status: release.rustdoc_status,
target_name: release.target_name.clone(),
})
.ok_or(Nope::VersionNotFound);
}
// Since we return with a CrateNotFound earlier if the db reply is empty,
// we know that versions were returned but none satisfied the version requirement.
// This can only happen when all versions are yanked.
Err(Nope::VersionNotFound)
}
/// Wrapper around the Markdown parser and renderer to render markdown
fn render_markdown(text: &str) -> String {
use comrak::{markdown_to_html, ComrakExtensionOptions, ComrakOptions};
let options = ComrakOptions {
extension: ComrakExtensionOptions {
superscript: true,
table: true,
autolink: true,
tasklist: true,
strikethrough: true,
..ComrakExtensionOptions::default()
},
..ComrakOptions::default()
};
markdown_to_html(text, &options)
}
#[must_use = "`Server` blocks indefinitely when dropped"]
pub struct Server {
inner: Listening,
}
impl Server {
pub fn start(addr: Option<&str>, context: &dyn Context) -> Result<Self, Error> {
// Initialize templates
let template_data = Arc::new(TemplateData::new(&mut *context.pool()?.get()?)?);
let server = Self::start_inner(addr.unwrap_or(DEFAULT_BIND), template_data, context)?;
info!("Running docs.rs web server on http://{}", server.addr());
Ok(server)
}
fn start_inner(
addr: &str,
template_data: Arc<TemplateData>,
context: &dyn Context,
) -> Result<Self, Error> {
let mut iron = Iron::new(MainHandler::new(template_data, context)?);
if cfg!(test) {
iron.threads = 1;
}
let inner = iron
.http(addr)
.unwrap_or_else(|_| panic!("Failed to bind to socket on {}", addr));
Ok(Server { inner })
}
pub(crate) fn addr(&self) -> SocketAddr {
self.inner.socket
}
/// Iron is bugged, and it never closes the server even when the listener is dropped. To
/// avoid never-ending tests this method forgets about the server, leaking it and allowing the
/// program to end.
///
/// The OS will then close all the dangling servers once the process exits.
///
/// https://docs.rs/iron/0.5/iron/struct.Listening.html#method.close
#[cfg(test)]
pub(crate) fn leak(self) {
std::mem::forget(self.inner);
}
}
/// Converts Timespec to nice readable relative time string
fn duration_to_str(init: DateTime<Utc>) -> String {
let now = Utc::now();
let delta = now.signed_duration_since(init);
let delta = (
delta.num_days(),
delta.num_hours(),
delta.num_minutes(),
delta.num_seconds(),
);
match delta {
(days, ..) if days > 5 => format!("{}", init.format("%b %d, %Y")),
(days @ 2..=5, ..) => format!("{} days ago", days),
(1, ..) => "one day ago".to_string(),
(_, hours, ..) if hours > 1 => format!("{} hours ago", hours),
(_, 1, ..) => "an hour ago".to_string(),
(_, _, minutes, _) if minutes > 1 => format!("{} minutes ago", minutes),
(_, _, 1, _) => "one minute ago".to_string(),
(_, _, _, seconds) if seconds > 0 => format!("{} seconds ago", seconds),
_ => "just now".to_string(),
}
}
/// Creates a `Response` which redirects to the given path on the scheme/host/port from the given
/// `Request`.
fn redirect(url: Url) -> Response {
let mut resp = Response::with((status::Found, Redirect(url)));
resp.headers.set(Expires(HttpDate(time::now())));
resp
}
fn redirect_base(req: &Request) -> String {
// Try to get the scheme from CloudFront first, and then from iron
let scheme = req
.headers
.get_raw("cloudfront-forwarded-proto")
.and_then(|values| values.get(0))
.and_then(|value| std::str::from_utf8(value).ok())
.filter(|proto| *proto == "http" || *proto == "https")
.unwrap_or_else(|| req.url.scheme());
// Only include the port if it's needed
let port = req.url.port();
if port == 80 {
format!("{}://{}", scheme, req.url.host())
} else {
format!("{}://{}:{}", scheme, req.url.host(), port)
}
}
/// MetaData used in header
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub(crate) struct MetaData {
pub(crate) name: String,
// If we're on a page with /latest/ in the URL, the string "latest".
// Otherwise, the version as a string.
pub(crate) version_or_latest: String,
// The exact version of the crate being shown. Never contains "latest".
pub(crate) version: String,
pub(crate) description: Option<String>,
pub(crate) target_name: Option<String>,
pub(crate) rustdoc_status: bool,
pub(crate) default_target: String,
pub(crate) doc_targets: Vec<String>,
pub(crate) yanked: bool,
/// CSS file to use depending on the rustdoc version used to generate this version of this
/// crate.
pub(crate) rustdoc_css_file: String,
}
impl MetaData {
fn from_crate(
conn: &mut Client,
name: &str,
version: &str,
version_or_latest: &str,
) -> Option<MetaData> {
let rows = conn
.query(
"SELECT crates.name,
releases.version,
releases.description,
releases.target_name,
releases.rustdoc_status,
releases.default_target,
releases.doc_targets,
releases.yanked,
releases.doc_rustc_version
FROM releases
INNER JOIN crates ON crates.id = releases.crate_id
WHERE crates.name = $1 AND releases.version = $2",
&[&name, &version],
)
.unwrap();
let row = rows.get(0)?;
Some(MetaData {
name: row.get(0),
version: row.get(1),
version_or_latest: version_or_latest.to_string(),
description: row.get(2),
target_name: row.get(3),
rustdoc_status: row.get(4),
default_target: row.get(5),
doc_targets: MetaData::parse_doc_targets(row.get(6)),
yanked: row.get(7),
rustdoc_css_file: get_correct_docsrs_style_file(row.get(8)).unwrap(),
})
}
fn parse_doc_targets(targets: Value) -> Vec<String> {
targets
.as_array()
.map(|array| {
array
.iter()
.filter_map(|item| item.as_str().map(|s| s.to_owned()))
.collect()
})
.unwrap_or_else(Vec::new)
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub(crate) struct ErrorPage {
/// The title of the page
pub title: &'static str,
/// The error message, displayed as a description
pub message: Option<Cow<'static, str>>,
#[serde(skip)]
pub status: Status,
}
impl_webpage! {
ErrorPage = "error.html",
status = |err| err.status,
}
#[cfg(test)]
mod test {
use super::*;
use crate::{docbuilder::DocCoverage, test::*, web::match_version};
use kuchiki::traits::TendrilSink;
use serde_json::json;
fn release(version: &str, env: &TestEnvironment) -> i32 {
env.fake_release()
.name("foo")
.version(version)
.create()
.unwrap()
}
fn version(v: Option<&str>, db: &TestDatabase) -> Option<String> {
let version = match_version(&mut db.conn(), "foo", v)
.ok()?
.assume_exact()
.ok()?
.into_parts()
.0;
Some(version)
}
#[allow(clippy::unnecessary_wraps)]
fn semver(version: &'static str) -> Option<String> {
Some(version.into())
}
#[allow(clippy::unnecessary_wraps)]
fn exact(version: &'static str) -> Option<String> {
Some(version.into())
}
fn clipboard_is_present_for_path(path: &str, web: &TestFrontend) -> bool {
let data = web.get(path).send().unwrap().text().unwrap();
let node = kuchiki::parse_html().one(data);
node.select("#clipboard").unwrap().count() == 1
}
#[test]
fn test_index_returns_success() {
wrapper(|env| {
let web = env.frontend();
assert!(web.get("/").send()?.status().is_success());
Ok(())
});
}
#[test]
fn test_doc_coverage_for_crate_pages() {
wrapper(|env| {
env.fake_release()
.name("foo")
.version("0.0.1")
.source_file("test.rs", &[])
.doc_coverage(DocCoverage {
total_items: 10,
documented_items: 6,
total_items_needing_examples: 2,
items_with_examples: 1,
})
.create()?;
let web = env.frontend();
let foo_crate = kuchiki::parse_html().one(web.get("/crate/foo/0.0.1").send()?.text()?);
for value in &["60.0%", "6", "10", "2", "1"] {
assert!(foo_crate
.select(".pure-menu-item b")
.unwrap()
.any(|e| e.text_contents().contains(value)));
}
let foo_doc = kuchiki::parse_html().one(web.get("/foo/0.0.1/foo").send()?.text()?);
assert!(foo_doc
.select(".pure-menu-link b")
.unwrap()
.any(|e| e.text_contents().contains("60.0%")));
Ok(())
});
}
#[test]
fn test_show_clipboard_for_crate_pages() {
wrapper(|env| {
env.fake_release()
.name("fake_crate")
.version("0.0.1")
.source_file("test.rs", &[])
.create()
.unwrap();
let web = env.frontend();
assert!(clipboard_is_present_for_path(
"/crate/fake_crate/0.0.1",
web
));
assert!(clipboard_is_present_for_path(
"/crate/fake_crate/0.0.1/source/",
web
));
assert!(clipboard_is_present_for_path(
"/fake_crate/0.0.1/fake_crate",
web
));
Ok(())
});
}
#[test]
fn test_hide_clipboard_for_non_crate_pages() {
wrapper(|env| {
env.fake_release()
.name("fake_crate")
.version("0.0.1")
.create()
.unwrap();
let web = env.frontend();
assert!(!clipboard_is_present_for_path("/about", web));
assert!(!clipboard_is_present_for_path("/releases", web));
assert!(!clipboard_is_present_for_path("/", web));
assert!(!clipboard_is_present_for_path("/not/a/real/path", web));
Ok(())
});
}
#[test]
fn standard_library_redirects() {
wrapper(|env| {
let web = env.frontend();
for krate in &["std", "alloc", "core", "proc_macro", "test"] {
let target = format!("https://doc.rust-lang.org/stable/{}/", krate);
// with or without slash
assert_redirect(&format!("/{}", krate), &target, web)?;
assert_redirect(&format!("/{}/", krate), &target, web)?;
}
let target = "https://doc.rust-lang.org/stable/proc_macro/";
// with or without slash
assert_redirect("/proc-macro", target, web)?;
assert_redirect("/proc-macro/", target, web)?;
let target = "https://doc.rust-lang.org/nightly/nightly-rustc/";
// with or without slash
assert_redirect("/rustc", target, web)?;
assert_redirect("/rustc/", target, web)?;
let target = "https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc/";
// with or without slash
assert_redirect("/rustdoc", target, web)?;
assert_redirect("/rustdoc/", target, web)?;
// queries are supported
assert_redirect(
"/std?search=foobar",
"https://doc.rust-lang.org/stable/std/?search=foobar",
web,
)?;
Ok(())
})
}
#[test]
fn double_slash_does_redirect_and_remove_slash() {
wrapper(|env| {
env.fake_release()
.name("bat")
.version("0.2.0")
.create()
.unwrap();
let web = env.frontend();
assert_redirect_unchecked("/bat//", "/bat/", web)?;
Ok(())
})
}
#[test]
fn binary_docs_redirect_to_crate() {
wrapper(|env| {
env.fake_release()
.name("bat")
.version("0.2.0")
.binary(true)
.create()
.unwrap();
let web = env.frontend();
assert_redirect("/bat/0.2.0", "/crate/bat/0.2.0", web)?;
assert_redirect("/bat/0.2.0/i686-unknown-linux-gnu", "/crate/bat/0.2.0", web)?;
/* TODO: this should work (https://github.com/rust-lang/docs.rs/issues/603)
assert_redirect("/bat/0.2.0/i686-unknown-linux-gnu/bat", "/crate/bat/0.2.0", web)?;
assert_redirect("/bat/0.2.0/i686-unknown-linux-gnu/bat/", "/crate/bat/0.2.0/", web)?;
*/
Ok(())
})
}
#[test]
fn can_view_source() {
wrapper(|env| {
env.fake_release()
.name("regex")
.version("0.3.0")
.source_file("src/main.rs", br#"println!("definitely valid rust")"#)
.create()
.unwrap();
let web = env.frontend();
assert_success("/crate/regex/0.3.0/source/src/main.rs", web)?;
assert_success("/crate/regex/0.3.0/source", web)?;
assert_success("/crate/regex/0.3.0/source/src", web)?;
assert_success("/regex/0.3.0/src/regex/main.rs.html", web)?;
Ok(())
})
}
#[test]
// https://github.com/rust-lang/docs.rs/issues/223
fn prereleases_are_not_considered_for_semver() {
wrapper(|env| {
let db = env.db();
let version = |v| version(v, db);
let release = |v| release(v, env);
release("0.3.1-pre");
for search in &["*", "newest", "latest"] {
assert_eq!(version(Some(search)), semver("0.3.1-pre"));
}
release("0.3.1-alpha");
assert_eq!(version(Some("0.3.1-alpha")), exact("0.3.1-alpha"));
release("0.3.0");
let three = semver("0.3.0");
assert_eq!(version(None), three);
// same thing but with "*"
assert_eq!(version(Some("*")), three);
// make sure exact matches still work
assert_eq!(version(Some("0.3.0")), exact("0.3.0"));
Ok(())
});
}
#[test]
fn platform_dropdown_not_shown_with_no_targets() {
wrapper(|env| {
release("0.1.0", env);
let web = env.frontend();
let text = web.get("/foo/0.1.0/foo").send()?.text()?;
let platform = kuchiki::parse_html()
.one(text)
.select(r#"ul > li > a[aria-label="Platform"]"#)
.unwrap()
.count();
assert_eq!(platform, 0);
// sanity check the test is doing something
env.fake_release()
.name("foo")
.version("0.2.0")
.add_platform("x86_64-unknown-linux-musl")
.create()?;
let text = web.get("/foo/0.2.0/foo").send()?.text()?;
let platform = kuchiki::parse_html()
.one(text)
.select(r#"ul > li > a[aria-label="Platform"]"#)
.unwrap()
.count();
assert_eq!(platform, 1);
Ok(())
});
}
#[test]
// https://github.com/rust-lang/docs.rs/issues/221
fn yanked_crates_are_not_considered() {
wrapper(|env| {
let db = env.db();
let release_id = release("0.3.0", env);
let query = "UPDATE releases SET yanked = true WHERE id = $1 AND version = '0.3.0'";
db.conn().query(query, &[&release_id]).unwrap();
assert_eq!(version(None, db), None);
assert_eq!(version(Some("0.3"), db), None);
release("0.1.0+4.1", env);
assert_eq!(version(Some("0.1.0+4.1"), db), exact("0.1.0+4.1"));
assert_eq!(version(None, db), semver("0.1.0+4.1"));
Ok(())
});
}
#[test]
// https://github.com/rust-lang/docs.rs/issues/1682
fn prereleases_are_considered_when_others_dont_match() {
wrapper(|env| {
let db = env.db();
// normal release
release("1.0.0", env);
// prereleases
release("2.0.0-alpha.1", env);
release("2.0.0-alpha.2", env);
// STAR gives me the prod release
assert_eq!(version(Some("*"), db), exact("1.0.0"));
// prerelease query gives me the latest prerelease
assert_eq!(version(Some(">=2.0.0-alpha"), db), exact("2.0.0-alpha.2"));
Ok(())
})
}
#[test]
// vaguely related to https://github.com/rust-lang/docs.rs/issues/395
fn metadata_has_no_effect() {
wrapper(|env| {
let db = env.db();
release("0.1.0+4.1", env);
release("0.1.1", env);
assert_eq!(version(None, db), semver("0.1.1"));
release("0.5.1+zstd.1.4.4", env);
assert_eq!(version(None, db), semver("0.5.1+zstd.1.4.4"));
assert_eq!(version(Some("0.5"), db), semver("0.5.1+zstd.1.4.4"));
assert_eq!(
version(Some("0.5.1+zstd.1.4.4"), db),
exact("0.5.1+zstd.1.4.4")
);
Ok(())
});
}
#[test]
fn serialize_metadata() {
let mut metadata = MetaData {
name: "serde".to_string(),
version: "1.0.0".to_string(),
version_or_latest: "1.0.0".to_string(),
description: Some("serde does stuff".to_string()),
target_name: None,
rustdoc_status: true,
default_target: "x86_64-unknown-linux-gnu".to_string(),
doc_targets: vec![
"x86_64-unknown-linux-gnu".to_string(),
"arm64-unknown-linux-gnu".to_string(),
],
yanked: false,
rustdoc_css_file: "rustdoc.css".to_string(),
};
let correct_json = json!({
"name": "serde",
"version": "1.0.0",
"version_or_latest": "1.0.0",
"description": "serde does stuff",
"target_name": null,
"rustdoc_status": true,
"default_target": "x86_64-unknown-linux-gnu",
"doc_targets": [
"x86_64-unknown-linux-gnu",
"arm64-unknown-linux-gnu",
],
"yanked": false,
"rustdoc_css_file": "rustdoc.css",
});
assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
metadata.target_name = Some("serde_lib_name".to_string());
let correct_json = json!({
"name": "serde",
"version": "1.0.0",
"version_or_latest": "1.0.0",
"description": "serde does stuff",
"target_name": "serde_lib_name",
"rustdoc_status": true,
"default_target": "x86_64-unknown-linux-gnu",
"doc_targets": [
"x86_64-unknown-linux-gnu",
"arm64-unknown-linux-gnu",
],
"yanked": false,
"rustdoc_css_file": "rustdoc.css",
});
assert_eq!(correct_json, serde_json::to_value(&metadata).unwrap());
metadata.description = None;
let correct_json = json!({
"name": "serde",
"version": "1.0.0",
"version_or_latest": "1.0.0",
"description": null,
"target_name": "serde_lib_name",