chore(source_tree): add .sol extension when missing (#1077)

Just ensure that any paths with no extension default to a .sol

Tests: cargo test --lib ethers-etherscan -- source_tree
This commit is contained in:
Curtis Spencer 2022-03-24 10:04:46 -07:00 committed by GitHub
parent fa04247808
commit 3c0d8c36dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 11 deletions

View File

@ -21,7 +21,10 @@ impl SourceTree {
pub fn write_to(&self, dir: &Path) -> Result<()> { pub fn write_to(&self, dir: &Path) -> Result<()> {
create_dir_all(&dir)?; create_dir_all(&dir)?;
for entry in &self.entries { for entry in &self.entries {
let sanitized_path = sanitize_path(&entry.path); let mut sanitized_path = sanitize_path(&entry.path);
if sanitized_path.extension().is_none() {
sanitized_path.set_extension("sol");
}
let joined = dir.join(sanitized_path); let joined = dir.join(sanitized_path);
if let Some(parent) = joined.parent() { if let Some(parent) = joined.parent() {
create_dir_all(parent)?; create_dir_all(parent)?;
@ -45,25 +48,22 @@ mod tests {
use super::*; use super::*;
use std::fs::read_dir; use std::fs::read_dir;
/// Ensure that the source tree is written correctly and .sol extension is added to a path with
/// no extension.
#[test] #[test]
fn test_source_tree_write() { fn test_source_tree_write() {
let tempdir = tempfile::tempdir().unwrap(); let tempdir = tempfile::tempdir().unwrap();
let st = SourceTree { let st = SourceTree {
entries: vec![ entries: vec![
SourceTreeEntry { path: PathBuf::from("a/a.sol"), contents: String::from("Test") }, SourceTreeEntry { path: PathBuf::from("a/a.sol"), contents: String::from("Test") },
SourceTreeEntry { SourceTreeEntry { path: PathBuf::from("b/b"), contents: String::from("Test 2") },
path: PathBuf::from("b/b.sol"),
contents: String::from("Test 2"),
},
], ],
}; };
st.write_to(tempdir.path()).unwrap(); st.write_to(tempdir.path()).unwrap();
let written_paths = read_dir(tempdir.path()).unwrap(); let a_sol_path = PathBuf::new().join(&tempdir).join("a").join("a.sol");
let paths: Vec<PathBuf> = let b_sol_path = PathBuf::new().join(&tempdir).join("b").join("b.sol");
written_paths.into_iter().filter_map(|x| x.ok()).map(|x| x.path()).collect(); assert!(a_sol_path.exists());
assert_eq!(paths.len(), 2); assert!(b_sol_path.exists());
assert!(paths.contains(&tempdir.path().join("a")));
assert!(paths.contains(&tempdir.path().join("b")));
} }
/// Ensure that the .. are ignored when writing the source tree to disk because of /// Ensure that the .. are ignored when writing the source tree to disk because of