1 mod progress;
2 
3 use self::progress::Progress;
4 use anyhow::Result;
5 use flate2::read::GzDecoder;
6 use std::fs;
7 use std::path::Path;
8 use tar::Archive;
9 use walkdir::DirEntry;
10 
11 const REVISION: &str = "72da5a9d85a522b11e80d0fdd1fd95247d442604";
12 
13 #[rustfmt::skip]
14 static EXCLUDE: &[&str] = &[
15     // Compile-fail expr parameter in const generic position: f::<1 + 2>()
16     "src/test/ui/const-generics/closing-args-token.rs",
17     "src/test/ui/const-generics/const-expression-parameter.rs",
18 
19     // Deprecated anonymous parameter syntax in traits
20     "src/test/ui/issues/issue-13105.rs",
21     "src/test/ui/issues/issue-13775.rs",
22     "src/test/ui/issues/issue-34074.rs",
23     "src/test/ui/proc-macro/trait-fn-args-2015.rs",
24 
25     // Not actually test cases
26     "src/test/rustdoc-ui/test-compile-fail2.rs",
27     "src/test/rustdoc-ui/test-compile-fail3.rs",
28     "src/test/ui/include-single-expr-helper.rs",
29     "src/test/ui/include-single-expr-helper-1.rs",
30     "src/test/ui/issues/auxiliary/issue-21146-inc.rs",
31     "src/test/ui/json-bom-plus-crlf-multifile-aux.rs",
32     "src/test/ui/lint/expansion-time-include.rs",
33     "src/test/ui/macros/auxiliary/macro-comma-support.rs",
34     "src/test/ui/macros/auxiliary/macro-include-items-expr.rs",
35 ];
36 
base_dir_filter(entry: &DirEntry) -> bool37 pub fn base_dir_filter(entry: &DirEntry) -> bool {
38     let path = entry.path();
39     if path.is_dir() {
40         return true; // otherwise walkdir does not visit the files
41     }
42     if path.extension().map(|e| e != "rs").unwrap_or(true) {
43         return false;
44     }
45 
46     let mut path_string = path.to_string_lossy();
47     if cfg!(windows) {
48         path_string = path_string.replace('\\', "/").into();
49     }
50     let path = if let Some(path) = path_string.strip_prefix("tests/rust/") {
51         path
52     } else {
53         panic!("unexpected path in Rust dist: {}", path_string);
54     };
55 
56     if path.starts_with("src/test/compile-fail") || path.starts_with("src/test/rustfix") {
57         return false;
58     }
59 
60     if path.starts_with("src/test/ui") {
61         let stderr_path = entry.path().with_extension("stderr");
62         if stderr_path.exists() {
63             // Expected to fail in some way
64             return false;
65         }
66     }
67 
68     !EXCLUDE.contains(&path)
69 }
70 
71 #[allow(dead_code)]
edition(path: &Path) -> &'static str72 pub fn edition(path: &Path) -> &'static str {
73     if path.ends_with("dyn-2015-no-warnings-without-lints.rs") {
74         "2015"
75     } else {
76         "2018"
77     }
78 }
79 
clone_rust()80 pub fn clone_rust() {
81     let needs_clone = match fs::read_to_string("tests/rust/COMMIT") {
82         Err(_) => true,
83         Ok(contents) => contents.trim() != REVISION,
84     };
85     if needs_clone {
86         download_and_unpack().unwrap();
87     }
88     let mut missing = String::new();
89     let test_src = Path::new("tests/rust");
90     for exclude in EXCLUDE {
91         if !test_src.join(exclude).exists() {
92             missing += "\ntests/rust/";
93             missing += exclude;
94         }
95     }
96     if !missing.is_empty() {
97         panic!("excluded test file does not exist:{}\n", missing);
98     }
99 }
100 
download_and_unpack() -> Result<()>101 fn download_and_unpack() -> Result<()> {
102     let url = format!(
103         "https://github.com/rust-lang/rust/archive/{}.tar.gz",
104         REVISION
105     );
106     let response = reqwest::blocking::get(&url)?.error_for_status()?;
107     let progress = Progress::new(response);
108     let decoder = GzDecoder::new(progress);
109     let mut archive = Archive::new(decoder);
110     let prefix = format!("rust-{}", REVISION);
111 
112     let tests_rust = Path::new("tests/rust");
113     if tests_rust.exists() {
114         fs::remove_dir_all(tests_rust)?;
115     }
116 
117     for entry in archive.entries()? {
118         let mut entry = entry?;
119         let path = entry.path()?;
120         if path == Path::new("pax_global_header") {
121             continue;
122         }
123         let relative = path.strip_prefix(&prefix)?;
124         let out = tests_rust.join(relative);
125         entry.unpack(&out)?;
126     }
127 
128     fs::write("tests/rust/COMMIT", REVISION)?;
129     Ok(())
130 }
131