Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests and highlight issues with should_panic #683

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 10 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ members = ["component", "ci/build-channel", "ci/compare-versions"]

[dev-dependencies]
chrono = "0.4.33"
regex = "1.11"
strip-ansi-escapes = "0.2.0"

[lints.clippy]
Expand Down
129 changes: 129 additions & 0 deletions component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ impl Component {
&& name != FORC)
|| name == FORC_CLIENT
}

/// Tests if the supplied `Component`s come from same distribution
///
/// # Arguments
///
/// * `first` - The first `Component` to compare with
///
/// * `second` - The second `Component` to compare with
///
/// # Examples
///
/// ```rust
/// use component::Component;
///
/// let forc = Component::from_name("forc").unwrap();
/// let forc_fmt = Component::from_name("forc-fmt").unwrap();
///
/// assert!(Component::is_in_same_distribution(&forc, &forc_fmt));
/// ```
pub fn is_in_same_distribution(first: &Component, second: &Component) -> bool {
// Components come from the same distribution if:
// - their repository names are the same, and
// - their tarball prefixes are the same
first.repository_name == second.repository_name
&& first.tarball_prefix == second.tarball_prefix
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -253,4 +279,107 @@ mod tests {
fn test_collect_plugin_executables() {
assert!(Components::collect_plugin_executables().is_ok());
}

#[test]
fn test_from_name_forc() {
let component = Component::from_name(FORC).unwrap();
assert_eq!(component.name, FORC, "forc is a publishable component");
}

#[test]
fn test_from_name_publishables() {
for publishable in Components::collect_publishables().unwrap() {
let component = Component::from_name(&publishable.name).unwrap();
assert_eq!(
component.name, publishable.name,
"{} is a publishable component",
publishable.name
);
}
}

#[test]
fn test_from_name_plugins() {
for plugin in Components::collect_plugins().unwrap() {
let component = Component::from_name(&plugin.name).unwrap();
assert_eq!(
component.name, plugin.name,
"{} is a plugin in {}",
plugin.name, component.name
);
}
}

#[test]
#[should_panic] // TODO: #654 will fix this
fn test_from_name_executables() {
for executable in &Components::collect_plugin_executables().unwrap() {
let component = Component::from_name(executable).unwrap();
assert!(
component.executables.contains(executable),
"{} is an executable in {}",
executable,
component.name
);
}
}
alfiedotwtf marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn test_is_distributed_by_forc_forc() {
assert!(
Components::is_distributed_by_forc("forc"),
"forc is distributed by forc"
);
}

#[test]
fn test_is_distributed_by_forc_publishables() {
for publishable in Components::collect_publishables().unwrap() {
let component = Component::from_name(&publishable.name).unwrap();
is_distributed_by_forc(&component);
}
}

#[test]
#[should_panic] // TODO: #654 will fix this
fn test_is_distributed_by_forc_plugins() {
for plugin in Components::collect_plugins().unwrap() {
let component = Component::from_name(&plugin.name).unwrap();
is_distributed_by_forc(&component);
}
}

#[test]
#[should_panic] // TODO: #654 will fix this
fn test_is_distributed_by_forc_executables() {
for executable in Components::collect_plugin_executables().unwrap() {
let components = Components::collect().unwrap();
let component = components
.component
.values()
.find(|c| c.executables.contains(&executable))
.unwrap();

is_distributed_by_forc(component);
}
}

fn is_distributed_by_forc(component: &Component) {
let forc = Component::from_name(FORC).unwrap();
let is_distributed = Components::is_distributed_by_forc(&component.name);

if Component::is_in_same_distribution(&forc, component) {
assert!(
is_distributed,
"{:?} is distributed by forc",
component.name
)
} else {
assert!(
!is_distributed,
"{:?} is not distributed by forc",
component.name
)
}
}
}
67 changes: 67 additions & 0 deletions src/target_triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,70 @@ impl TargetTriple {
}
}
}

#[cfg(test)]
mod test_from_component {
use super::*;
use component::{Component, Components};
use regex::Regex;

#[test]
fn forc() {
let component = Component::from_name("forc").unwrap();
let target_triple = TargetTriple::from_component(&component.name).unwrap();
test_target_triple(&component, &target_triple);
}

#[test]
fn publishables() {
for publishable in Components::collect_publishables().unwrap() {
let component = Component::from_name(&publishable.name).unwrap();
let target_triple = TargetTriple::from_component(&component.name).unwrap();
test_target_triple(&component, &target_triple);
}
}

#[test]
#[should_panic] // TODO: #654 will fix this
fn plugins() {
for plugin in Components::collect_plugins().unwrap() {
let component = Component::from_name(&plugin.name).unwrap();
let target_triple = TargetTriple::from_component(&component.name).unwrap();
test_target_triple(&component, &target_triple);
}
}

#[test]
#[should_panic] // TODO: #654 will fix this
fn executables() {
for executable in Components::collect_plugin_executables().unwrap() {
let components = Components::collect().unwrap();
let component = components
.component
.values()
.find(|c| c.executables.contains(&executable))
.unwrap();

let target_triple = TargetTriple::from_component(&component.name).unwrap();
test_target_triple(component, &target_triple);
}
}

fn test_target_triple(component: &Component, target_triple: &TargetTriple) {
let forc = Component::from_name("forc").unwrap();

let expected_triple_regex = if Component::is_in_same_distribution(&forc, component) {
"^(darwin|linux)_(arm64|amd64)$"
} else {
"^(aarch64|x86_64)-(apple|unknown)-(darwin|linux-gnu)$"
};

let expected_triple = Regex::new(expected_triple_regex).unwrap();
assert!(
expected_triple.is_match(&target_triple.0),
"{} has triple '{}'",
component.name,
&target_triple.0
);
}
}
8 changes: 4 additions & 4 deletions src/toolchain_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ use tracing::{info, warn};
// additional info to OverrideCfg (representation of 'fuel-toolchain.toml').
// In this case, we want the path to the toml file. More info might be
// needed in future.
#[derive(Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ToolchainOverride {
pub cfg: OverrideCfg,
pub path: PathBuf,
}

// Representation of the entire 'fuel-toolchain.toml'.
#[derive(Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OverrideCfg {
pub toolchain: ToolchainCfg,
pub components: Option<HashMap<String, Version>>,
}

// Represents the [toolchain] table in 'fuel-toolchain.toml'.
#[derive(Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize)]
pub struct ToolchainCfg {
#[serde(deserialize_with = "deserialize_channel")]
pub channel: Channel,
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Channel {
pub name: String,
pub date: Option<Date>,
Expand Down
Loading
Loading