Skip to content

Commit 04534b3

Browse files
committed
add check for non existing tests and examplesC
1 parent f739aa5 commit 04534b3

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/runner/toml_frobber.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@ use crates::Crate;
22
use prelude::*;
33
use std::path::Path;
44
use toml::value::Table;
5-
use toml::{self, Value};
5+
use toml::{self, value::Array, Value};
66

77
pub(super) struct TomlFrobber<'a> {
88
krate: &'a Crate,
99
table: Table,
10+
dir: &'a Path,
1011
}
1112

1213
impl<'a> TomlFrobber<'a> {
13-
pub(super) fn new(krate: &'a Crate, cargo_toml: &Path) -> Fallible<Self> {
14+
pub(super) fn new(krate: &'a Crate, cargo_toml: &'a Path) -> Fallible<Self> {
1415
let toml_content = ::std::fs::read_to_string(cargo_toml)
1516
.with_context(|_| format!("missing Cargo.toml from {}", krate))?;
17+
1618
let table: Table = toml::from_str(&toml_content)
1719
.with_context(|_| format!("unable to parse {}", cargo_toml.display(),))?;
1820

19-
Ok(TomlFrobber { krate, table })
21+
let dir = cargo_toml.parent().unwrap();
22+
23+
Ok(TomlFrobber { krate, table, dir })
2024
}
2125

2226
#[cfg(test)]
@@ -27,13 +31,48 @@ impl<'a> TomlFrobber<'a> {
2731
pub(super) fn frob(&mut self) {
2832
info!("started frobbing {}", self.krate);
2933

34+
self.remove_missing_items("example");
35+
self.remove_missing_items("test");
3036
self.remove_workspaces();
3137
self.remove_unwanted_cargo_features();
3238
self.remove_dependencies();
3339

3440
info!("finished frobbing {}", self.krate);
3541
}
3642

43+
fn test_existance(dir: &Path, value: &Array, folder: &str) -> Array {
44+
value
45+
.iter()
46+
.filter_map(|t| t.as_table())
47+
.map(|table| {
48+
let name = table.get("name").unwrap().to_string();
49+
let path = table.get("path").map_or_else(
50+
|| dir.join(folder).join(name + ".rs"),
51+
|path| dir.join(path.as_str().unwrap()),
52+
);
53+
(table, path)
54+
})
55+
.filter(|(_table, path)| path.exists())
56+
.filter_map(|(table, _path)| Value::try_from(table).ok())
57+
.collect()
58+
}
59+
60+
fn remove_missing_items(&mut self, category: &str) {
61+
let folder = &(String::from(category) + "s");
62+
63+
println!("tables {:?}: {:?}", category, self.table.get(category));
64+
65+
let _krate = self.krate.to_string();
66+
let dir = self.dir;
67+
68+
if let Some(array) = self.table.get_mut(category) {
69+
*(array) =
70+
toml::Value::Array(Self::test_existance(dir, array.as_array().unwrap(), folder));
71+
}
72+
73+
println!("tables example: {:?}", self.table.get("example"));
74+
}
75+
3776
fn remove_workspaces(&mut self) {
3877
let krate = self.krate.to_string();
3978

0 commit comments

Comments
 (0)