Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
review
  • Loading branch information
kalaninja committed May 4, 2023
commit dec59d88264e851f05276a2010f8e6077b7c7080
209 changes: 108 additions & 101 deletions scripts/ci/node-template-release/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
collections::HashMap,
fs::{self, File, OpenOptions},
io::{Read, Write},
path::{Path, PathBuf},
process::Command,
};
Expand Down Expand Up @@ -64,19 +63,18 @@ fn find_cargo_tomls(path: &PathBuf) -> Vec<PathBuf> {
result
}

/// Parse the given `Cargo.toml`
/// Parse the given `Cargo.toml`.
fn parse_cargo_toml(file: &Path) -> CargoToml {
let mut content = String::new();
File::open(file)
.expect("Cargo.toml exists")
.read_to_string(&mut content)
.expect("Reads file");
content.parse().expect("Cargo.toml is a valid toml file")
fs::read_to_string(file)
.unwrap_or_else(|e| panic!("Failed to read `{}`: {}", file.display(), e))
.parse()
.unwrap_or_else(|e| panic!("Failed to parse `{}`: {}", file.display(), e))
}

/// Write the given `Cargo.toml` to the given path.
fn write_cargo_toml(path: &Path, cargo_toml: CargoToml) {
let mut file = File::create(path).expect(&format!("Creates `{}`.", path.display()));
write!(file, "{}", cargo_toml).expect("Writes `Cargo.toml`");
fs::write(path, cargo_toml.to_string())
.unwrap_or_else(|e| panic!("Failed to write `{}`: {}", path.display(), e));
}

/// Gets the latest commit id of the repository given by `path`.
Expand Down Expand Up @@ -141,48 +139,6 @@ fn update_git_dependencies<F: Copy + Fn(&str) -> bool>(
.collect()
}

#[test]
fn test_update_git_dependencies() {
let toml = r#"
[dev-dependencies]
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }

[dependencies]
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
sp-io = { version = "7.0.0", path = "../../../../primitives/io" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../../../frame/system" }
"#;
let mut cargo_toml = toml.parse::<CargoToml>().expect("invalid doc");
let actual_deps = update_git_dependencies(&mut cargo_toml, |_| true);

assert_eq!(actual_deps.len(), 3);
assert_eq!(actual_deps.get("dependencies").unwrap().len(), 2);
assert_eq!(actual_deps.get("dev-dependencies").unwrap().len(), 0);
assert_eq!(
actual_deps.get("dependencies").unwrap().get("sp-io").unwrap(),
&Dependency { name: "sp-io".into(), version: Some("7.0.0".into()), default_features: None }
);
assert_eq!(
actual_deps.get("dependencies").unwrap().get("frame-system").unwrap(),
&Dependency {
name: "frame-system".into(),
version: Some("4.0.0-dev".into()),
default_features: Some(false),
}
);

let expected_toml = r#"
[dev-dependencies]
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }

[dependencies]
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
sp-io = { workspace = true }
frame-system = { workspace = true }
"#;
assert_eq!(cargo_toml.to_string(), expected_toml);
}

/// Processes all `Cargo.toml` files, aggregates dependencies and saves the changes.
fn process_cargo_tomls(cargo_tomls: &Vec<PathBuf>) -> Dependencies {
/// Merge dependencies from one collection in another.
Expand Down Expand Up @@ -246,55 +202,6 @@ fn update_root_cargo_toml(
cargo_toml.insert("profile", Item::Table(profile.into()));
}

#[test]
fn test_update_root_cargo_toml() {
let mut cargo_toml = CargoToml::new();
update_root_cargo_toml(
&mut cargo_toml,
&vec!["node".into(), "pallets/template".into(), "runtime".into()],
Dependencies::from([
(
"dependencies".into(),
HashMap::from([
(
"sp-io".into(),
Dependency {
name: "sp-io".into(),
version: Some("7.0.0".into()),
default_features: None,
},
),
(
"frame-system".into(),
Dependency {
name: "frame-system".into(),
version: Some("4.0.0-dev".into()),
default_features: Some(true),
},
),
]),
),
("dev-dependencies".into(), HashMap::new()),
("build-dependencies".into(), HashMap::new()),
]),
"commit_id",
);

let expected_toml = r#"[workspace]
members = ["node", "pallets/template", "runtime"]

[workspace.dependencies]
frame-system = { version = "4.0.0-dev", default-features = true, git = "https://github.com/paritytech/substrate.git", rev = "commit_id" }
sp-io = { version = "7.0.0", git = "https://github.com/paritytech/substrate.git", rev = "commit_id" }

[profile]

[profile.release]
panic = "unwind"
"#;
assert_eq!(cargo_toml.to_string(), expected_toml);
}

fn process_root_cargo_toml(
root_cargo_toml_path: &Path,
root_deps: Dependencies,
Expand Down Expand Up @@ -413,3 +320,103 @@ fn main() {
tar.append_dir_all("substrate-node-template", node_template_path)
.expect("Writes substrate-node-template archive");
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_update_git_dependencies() {
let toml = r#"
[dev-dependencies]
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }

[dependencies]
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
sp-io = { version = "7.0.0", path = "../../../../primitives/io" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../../../frame/system" }
"#;
let mut cargo_toml = toml.parse::<CargoToml>().expect("invalid doc");
let actual_deps = update_git_dependencies(&mut cargo_toml, |_| true);

assert_eq!(actual_deps.len(), 3);
assert_eq!(actual_deps.get("dependencies").unwrap().len(), 2);
assert_eq!(actual_deps.get("dev-dependencies").unwrap().len(), 0);
assert_eq!(
actual_deps.get("dependencies").unwrap().get("sp-io").unwrap(),
&Dependency {
name: "sp-io".into(),
version: Some("7.0.0".into()),
default_features: None
}
);
assert_eq!(
actual_deps.get("dependencies").unwrap().get("frame-system").unwrap(),
&Dependency {
name: "frame-system".into(),
version: Some("4.0.0-dev".into()),
default_features: Some(false),
}
);

let expected_toml = r#"
[dev-dependencies]
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }

[dependencies]
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
sp-io = { workspace = true }
frame-system = { workspace = true }
"#;
assert_eq!(cargo_toml.to_string(), expected_toml);
}

#[test]
fn test_update_root_cargo_toml() {
let mut cargo_toml = CargoToml::new();
update_root_cargo_toml(
&mut cargo_toml,
&vec!["node".into(), "pallets/template".into(), "runtime".into()],
Dependencies::from([
(
"dependencies".into(),
HashMap::from([
(
"sp-io".into(),
Dependency {
name: "sp-io".into(),
version: Some("7.0.0".into()),
default_features: None,
},
),
(
"frame-system".into(),
Dependency {
name: "frame-system".into(),
version: Some("4.0.0-dev".into()),
default_features: Some(true),
},
),
]),
),
("dev-dependencies".into(), HashMap::new()),
("build-dependencies".into(), HashMap::new()),
]),
"commit_id",
);

let expected_toml = r#"[workspace]
members = ["node", "pallets/template", "runtime"]

[workspace.dependencies]
frame-system = { version = "4.0.0-dev", default-features = true, git = "https://github.com/paritytech/substrate.git", rev = "commit_id" }
sp-io = { version = "7.0.0", git = "https://github.com/paritytech/substrate.git", rev = "commit_id" }

[profile]

[profile.release]
panic = "unwind"
"#;
assert_eq!(cargo_toml.to_string(), expected_toml);
}
}