forked from joshtriplett/metadeps
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
814 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use std::{ | ||
collections::HashSet, | ||
fs, io, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use toml::{Table, Value}; | ||
|
||
use crate::{error::Error, parse::MetadataList, utils::merge_default}; | ||
|
||
#[cfg(feature = "binary")] | ||
mod binary; | ||
mod conditional; | ||
mod metadata; | ||
|
||
macro_rules! entry { | ||
($table:expr, $key:expr) => { | ||
$table | ||
.entry($key) | ||
.or_insert_with(|| Value::Table(Table::default())) | ||
.as_table_mut() | ||
.unwrap() | ||
}; | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
struct Package { | ||
name: &'static str, | ||
deps: Vec<&'static str>, | ||
config: Table, | ||
} | ||
|
||
impl Package { | ||
fn write_toml(self, test_name: &str) -> io::Result<PathBuf> { | ||
let mut table = self.config; | ||
|
||
let package = entry!(table, "package"); | ||
package.insert("name".into(), self.name.into()); | ||
|
||
if !self.deps.is_empty() { | ||
let dependencies = entry!(table, "dependencies"); | ||
for name in self.deps { | ||
let dep = entry!(dependencies, name); | ||
dep.insert("path".into(), format!("../{}", name).into()); | ||
} | ||
} | ||
|
||
let mut out = Path::new(env!("OUT_DIR")).join(format!("tests/{}/{}", test_name, self.name)); | ||
let _ = fs::remove_dir_all(&out); | ||
|
||
out.push("src"); | ||
fs::create_dir_all(&out)?; | ||
fs::write(out.join("lib.rs"), "")?; | ||
out.pop(); | ||
|
||
out.push("Cargo.toml"); | ||
fs::write(&out, table.to_string())?; | ||
|
||
Ok(out) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Test { | ||
metadata: MetadataList, | ||
manifest: PathBuf, | ||
} | ||
|
||
impl Test { | ||
fn new(name: impl AsRef<str>, packages: Vec<Package>) -> Self { | ||
assert!(!packages.is_empty()); | ||
|
||
println!("\n# Dependencies\n"); | ||
let mut manifest = None; | ||
for pkg in packages { | ||
let out = pkg | ||
.write_toml(name.as_ref()) | ||
.expect("Error writing Cargo.toml for test package"); | ||
println!("- {}", out.display()); | ||
manifest.get_or_insert(out); | ||
} | ||
|
||
let manifest = manifest.expect("There is no main test case"); | ||
let metadata = MetadataList::from(&manifest, "system-deps"); | ||
|
||
Self { metadata, manifest } | ||
} | ||
|
||
fn check(&self, key: &str) -> Result<Table, Error> { | ||
println!("{} {:#?}", key, self.metadata); | ||
let resolved = self | ||
.metadata | ||
.get::<Table>(key, merge_default)? | ||
.remove(key) | ||
.ok_or(Error::PackageNotFound(key.into()))?; | ||
|
||
println!("\n# Final\n"); | ||
println!("{}", toml::to_string_pretty(&resolved).unwrap()); | ||
|
||
match resolved { | ||
Value::Table(v) => Ok(v), | ||
_ => Err(Error::IncompatibleMerge), | ||
} | ||
} | ||
} | ||
|
||
fn assert_set<T: std::fmt::Debug + Eq + std::hash::Hash>( | ||
rhs: impl IntoIterator<Item = T>, | ||
lhs: impl IntoIterator<Item = T>, | ||
) { | ||
let r = rhs.into_iter().collect::<HashSet<_>>(); | ||
let l = lhs.into_iter().collect::<HashSet<_>>(); | ||
assert_eq!(r, l); | ||
} |
Oops, something went wrong.