Skip to content

Commit

Permalink
test: test cargo replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
justinrubek committed Mar 18, 2023
1 parent 464ff42 commit 7312f0a
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ impl App {
println!("{:#?}", replacer);

// wait for user input
// let mut input = String::new();
// std::io::stdin().read_line(&mut input)?;
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
}

return Ok(());
Expand Down
5 changes: 5 additions & 0 deletions src/tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ impl FileJail {
)));
}

// Create the parent directory if it doesn't exist
if let Some(parent) = path.parent() {
std::fs::create_dir_all(self.directory().join(parent))?;
}

let file = File::create(self.directory().join(path))?;
let mut writer = BufWriter::new(file);
writer.write_all(contents.as_bytes())?;
Expand Down
128 changes: 126 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::{io::Read, path::Path};
use file::FileJail;

use crate::{
config::{Config, FileTableData},
replacers::{search::SearchReplacer, Replacer},
config::{Config, FileTableData, CargoReplaceMode},
replacers::{search::SearchReplacer, Replacer, cargo::CargoReplacer, VersionReplacement},
};

#[test]
Expand Down Expand Up @@ -151,3 +151,127 @@ dependencies = [
Ok(())
});
}

#[test]
/// Replaces a single crate workspace using the cargo replacer with auto-detection
/// This should update both the Cargo.toml and Cargo.lock files
fn cargo_individual() {
FileJail::expect_with(|jail| {
jail.create_file(
"Cargo.toml",
r#"
[package]
name = "package1"
edition = "2018"
version = "0.1.0"
[[bin]]
path = "src/main.rs"
name = "package1"
required-features = []
[dependencies]
package2 = "0.1.0"
package3 = "0.1.0"
"#,
)?;

let expected_toml = r#"[package]
name = "package1"
edition = "2018"
version = "0.2.0"
[dependencies]
package2 = "0.1.0"
package3 = "0.1.0"
[[bin]]
path = "src/main.rs"
name = "package1"
required-features = []
"#;


jail.create_file(
"src/main.rs",
r#"fn main() {
println!("Hello, world!");
}"#,
)?;

jail.create_file(
"Cargo.lock",
r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "package1"
version = "0.1.0"
dependencies = [
"package2",
"package3",
]
[[package]]
name = "package2"
version = "0.1.0"
[[package]]
name = "package3"
version = "0.1.0"
"#,

)?;

let expected_lock = r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "package1"
version = "0.2.0"
dependencies = [
"package2",
"package3",
]
[[package]]
name = "package2"
version = "0.1.0"
[[package]]
name = "package3"
version = "0.1.0"
"#;

let version_replacement = VersionReplacement {
old_version: "0.1.0".to_string(),
new_version: "0.2.0".to_string(),
};

let replacers = CargoReplacer::new(
version_replacement,
CargoReplaceMode::Autodetect,
)?.determine_replacements()?;

for replacer in replacers {
for replacer in replacer {
let file = replacer.temp_file.path();
let file_contents = std::fs::read_to_string(file)?;

if replacer.path.ends_with("Cargo.toml") {
assert_eq!(file_contents, expected_toml);
} else if replacer.path.ends_with("Cargo.lock") {
assert_eq!(file_contents, expected_lock);
} else {
panic!("Unexpected file path: {}", replacer.path.display());
}
}
}

Ok(())
});
}


0 comments on commit 7312f0a

Please sign in to comment.