-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathqltest.rs
More file actions
90 lines (85 loc) · 2.57 KB
/
qltest.rs
File metadata and controls
90 lines (85 loc) · 2.57 KB
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
use crate::config::Config;
use anyhow::Context;
use glob::glob;
use itertools::Itertools;
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::process::Command;
use tracing::info;
fn dump_lib() -> anyhow::Result<()> {
let path_iterator = glob("*.rs").context("globbing test sources")?;
let paths = path_iterator
.collect::<Result<Vec<_>, _>>()
.context("fetching test sources")?;
let lib = paths
.iter()
.map(|p| p.file_stem().expect("results of glob must have a name"))
.filter(|&p| !["main", "lib"].map(OsStr::new).contains(&p))
.map(|p| format!("mod {};", p.to_string_lossy()))
.join("\n");
fs::write("lib.rs", lib).context("writing lib.rs")
}
fn dump_cargo_manifest(dependencies: &[String]) -> anyhow::Result<()> {
let mut manifest = String::from(
r#"[workspace]
[package]
name = "test"
version="0.0.1"
edition="2021"
[lib]
path="lib.rs"
"#,
);
if fs::exists("main.rs").context("checking existence of main.rs")? {
manifest.push_str(
r#"[[bin]]
name = "main"
path = "main.rs"
"#,
);
}
if !dependencies.is_empty() {
manifest.push_str("[dependencies]\n");
for dep in dependencies {
manifest.push_str(dep);
manifest.push('\n');
}
}
fs::write("Cargo.toml", manifest).context("writing Cargo.toml")
}
fn set_sources(config: &mut Config) -> anyhow::Result<()> {
let path_iterator = glob("**/*.rs").context("globbing test sources")?;
config.inputs = path_iterator
.filter(|f| f.is_err() || !f.as_ref().unwrap().starts_with("target"))
.collect::<Result<Vec<_>, _>>()
.context("fetching test sources")?;
Ok(())
}
fn remove_file_if_exists(path: &Path) -> anyhow::Result<()> {
match fs::remove_file(path) {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
x => x,
}
.context(format!("removing file {}", path.display()))
}
pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
dump_lib()?;
set_sources(config)?;
remove_file_if_exists(Path::new("Cargo.lock"))?;
dump_cargo_manifest(&config.qltest_dependencies)?;
if config.qltest_cargo_check {
let status = Command::new("cargo")
.env("RUSTFLAGS", "-Awarnings")
.arg("check")
.arg("-q")
.status()
.context("spawning cargo check")?;
if status.success() {
info!("cargo check successful");
} else {
anyhow::bail!("requested cargo check failed");
}
}
Ok(())
}