Skip to content

Commit

Permalink
Add tests and highlight issues with should_panic (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfiedotwtf committed Oct 24, 2024
1 parent fea5065 commit c4fa4da
Show file tree
Hide file tree
Showing 6 changed files with 788 additions and 14 deletions.
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
132 changes: 132 additions & 0 deletions component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ impl Component {
&& name != FORC)
|| name == FORC_CLIENT
}

pub fn is_in_same_distribution(forc: &Component, component: &Component) -> bool {
component.repository_name == forc.repository_name
&& component.tarball_prefix == forc.tarball_prefix
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -253,4 +258,131 @@ 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
);
}
}

// This currently panics because resolving the component from the executable
// is incorrect for some executables
#[test]
#[should_panic]
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
);
}
}

#[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 is_distributed = Components::is_distributed_by_forc(&publishable.name);

if publishable.name == FORC {
assert!(is_distributed, "forc is distributed by forc");
} else {
assert!(
!is_distributed,
"{} is not distributed by forc",
publishable.name
);
}
}
}

// This currently panics because is_distributed_by_forc() is incorrect for
// some plugins
#[test]
#[should_panic]
fn test_is_distributed_by_forc_plugins() {
let forc = Component::from_name(FORC).unwrap();

for plugin in Components::collect_plugins().unwrap() {
let component = Component::from_name(&plugin.name).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", plugin.name)
} else {
assert!(
!is_distributed,
"{} is not distributed by forc",
component.name
)
}
}
}

// This currently panics because is_distributed_by_forc() is incorrect for
// some executables
#[test]
#[should_panic]
fn test_is_distributed_by_forc_executables() {
let forc = Component::from_name(FORC).unwrap();

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 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
)
}
}
}
}
102 changes: 102 additions & 0 deletions src/target_triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,105 @@ impl TargetTriple {
}
}
}

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

#[test]
fn forc() {
let target_triple = TargetTriple::from_component("forc").unwrap();
let expected_triple = Regex::new("^(darwin|linux)_(arm64|amd64)$").unwrap();
assert!(
expected_triple.is_match(&target_triple.0),
"forc has triple '{}'",
&target_triple.0
);
}

#[test]
fn publishables() {
let forc = Component::from_name("forc").unwrap();

for publishable in Components::collect_publishables().unwrap() {
let component = Component::from_name(&publishable.name).unwrap();
let target_triple = TargetTriple::from_component(&component.name).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 '{}'",
publishable.name,
&target_triple.0
);
}
}

// This currently panics because calculating the triple is incorrect for
// some plugins
#[test]
#[should_panic]
fn plugins() {
let forc = Component::from_name("forc").unwrap();

for plugin in Components::collect_plugins().unwrap() {
let component = Component::from_name(&plugin.name).unwrap();
let target_triple = TargetTriple::from_component(&component.name).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 '{}'",
plugin.name,
&target_triple.0
);
}
}

// This currently panics because calculating the triple is incorrect for
// some executables
#[test]
#[should_panic]
fn executables() {
let forc = Component::from_name("forc").unwrap();

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();

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 '{}'",
executable,
&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

0 comments on commit c4fa4da

Please sign in to comment.