Skip to content

Commit

Permalink
add tests for init/apply_dist/ci
Browse files Browse the repository at this point in the history
  • Loading branch information
duckinator committed Feb 5, 2025
1 parent 338473d commit 1b6e08b
Showing 1 changed file with 181 additions and 14 deletions.
195 changes: 181 additions & 14 deletions cargo-dist/src/init/apply_dist/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ use super::helpers::*;
use crate::config::v1::ci::github::GithubCiLayer;
use crate::config::v1::ci::{CiLayer, CommonCiLayer};
use crate::config::v1::layer::{BoolOr, BoolOrOptExt};

Check failure on line 4 in cargo-dist/src/init/apply_dist/ci.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `BoolOrOptExt`

error: unused import: `BoolOrOptExt` --> cargo-dist/src/init/apply_dist/ci.rs:4:40 | 4 | use crate::config::v1::layer::{BoolOr, BoolOrOptExt}; | ^^^^^^^^^^^^

Check failure on line 4 in cargo-dist/src/init/apply_dist/ci.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `BoolOrOptExt`

error: unused import: `BoolOrOptExt` --> cargo-dist/src/init/apply_dist/ci.rs:4:40 | 4 | use crate::config::v1::layer::{BoolOr, BoolOrOptExt}; | ^^^^^^^^^^^^
use axoasset::toml_edit;
use axoasset::toml_edit::{self, DocumentMut, Item, Table};

Check failure on line 5 in cargo-dist/src/init/apply_dist/ci.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `DocumentMut`

error: unused import: `DocumentMut` --> cargo-dist/src/init/apply_dist/ci.rs:5:33 | 5 | use axoasset::toml_edit::{self, DocumentMut, Item, Table}; | ^^^^^^^^^^^

Check failure on line 5 in cargo-dist/src/init/apply_dist/ci.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `DocumentMut`

error: unused import: `DocumentMut` --> cargo-dist/src/init/apply_dist/ci.rs:5:33 | 5 | use axoasset::toml_edit::{self, DocumentMut, Item, Table}; | ^^^^^^^^^^^

pub fn apply(table: &mut toml_edit::Table, ci: &Option<CiLayer>) {

Check failure on line 7 in cargo-dist/src/init/apply_dist/ci.rs

View workflow job for this annotation

GitHub Actions / clippy

function `apply` is never used

error: function `apply` is never used --> cargo-dist/src/init/apply_dist/ci.rs:7:8 | 7 | pub fn apply(table: &mut toml_edit::Table, ci: &Option<CiLayer>) { | ^^^^^
let Some(ci) = ci else {
// Nothing to do.
return;
};
let Some(ci_table) = table.get_mut("ci") else {
// Nothing to do.
return;
};
let toml_edit::Item::Table(ci_table) = ci_table else {
panic!("Expected [dist.ci] to be a table");
};
let ci_table = table
.entry("ci")
.or_insert(Item::Table(Table::new()))
.as_table_mut()
.expect("[dist.ci] should be a table");

apply_ci_common(ci_table, &ci.common);

Expand All @@ -42,12 +40,11 @@ pub fn apply(table: &mut toml_edit::Table, ci: &Option<CiLayer>) {
}

fn apply_ci_github(ci_table: &mut toml_edit::Table, github: &GithubCiLayer) {

Check failure on line 42 in cargo-dist/src/init/apply_dist/ci.rs

View workflow job for this annotation

GitHub Actions / clippy

function `apply_ci_github` is never used

error: function `apply_ci_github` is never used --> cargo-dist/src/init/apply_dist/ci.rs:42:4 | 42 | fn apply_ci_github(ci_table: &mut toml_edit::Table, github: &GithubCiLayer) { | ^^^^^^^^^^^^^^^
let Some(gh_table) = ci_table.get_mut("github") else {
return;
};
let toml_edit::Item::Table(gh_table) = gh_table else {
panic!("Expected [dist.ci.github] to be a table");
};
let gh_table = ci_table
.entry("github")
.or_insert(Item::Table(Table::new()))
.as_table_mut()
.expect("[dist.ci.github] should be a table");

apply_ci_common(gh_table, &github.common);

Expand Down Expand Up @@ -180,3 +177,173 @@ fn apply_ci_common(table: &mut toml_edit::Table, common: &CommonCiLayer) {
common.post_announce_jobs.as_ref(),
);
}

#[cfg(test)]
mod test {
use super::*;
use crate::config::JobStyle;

Check failure on line 184 in cargo-dist/src/init/apply_dist/ci.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `crate::config::JobStyle`

error: unused import: `crate::config::JobStyle` --> cargo-dist/src/init/apply_dist/ci.rs:184:9 | 184 | use crate::config::JobStyle; | ^^^^^^^^^^^^^^^^^^^^^^^
use miette::IntoDiagnostic;

fn source() -> toml_edit::DocumentMut {
let src = axoasset::SourceFile::new("fake-dist-workspace.toml", String::new());
let doc = src.deserialize_toml_edit().into_diagnostic().unwrap();
doc

Check failure on line 190 in cargo-dist/src/init/apply_dist/ci.rs

View workflow job for this annotation

GitHub Actions / clippy

returning the result of a `let` binding from a block

error: returning the result of a `let` binding from a block --> cargo-dist/src/init/apply_dist/ci.rs:190:9 | 189 | let doc = src.deserialize_toml_edit().into_diagnostic().unwrap(); | ----------------------------------------------------------------- unnecessary `let` binding 190 | doc | ^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return = note: `-D clippy::let-and-return` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::let_and_return)]` help: return the expression directly | 189 ~ 190 ~ src.deserialize_toml_edit().into_diagnostic().unwrap() |
}

// Given a DocumentMut, make sure it has a [dist] table, and return
// a reference to that dist table.
fn dist_table(doc: &mut toml_edit::DocumentMut) -> &mut toml_edit::Table {
let dist = doc
.entry("dist")
.or_insert(Item::Table(Table::new()))
.as_table_mut()
.unwrap();
// Don't show the empty top-level [dist].
dist.set_implicit(true);
// Return the table we just created.
dist
}

#[test]
fn apply_ci_empty() {
let expected = "";

let ci = Some(CiLayer {
common: CommonCiLayer {
merge_tasks: None,
fail_fast: None,
cache_builds: None,
build_local_artifacts: None,
dispatch_releases: None,
release_branch: None,
pr_run_mode: None,
tag_namespace: None,
plan_jobs: None,
build_local_jobs: None,
build_global_jobs: None,
host_jobs: None,
publish_jobs: None,
post_announce_jobs: None,
},
github: None,
});

let mut doc = source();
let table = dist_table(&mut doc);

apply(table, &ci);

let toml_text = table.to_string();
assert_eq!(toml_text, expected);
}

#[test]
fn apply_ci_everything() {
let expected = r#"
# CI configuration for dist
[dist.ci]
# Whether to run otherwise-parallelizable tasks on the same machine
merge-tasks = true
# Whether failing tasks should make us give up on all other tasks
fail-fast = true
# Whether builds should try to be cached in CI
cache-builds = true
# Whether CI should include auto-generated code to build local artifacts
build-local-artifacts = true
# Whether CI should trigger releases with dispatches instead of tag pushes
dispatch-releases = true
# Trigger releases on pushes to this branch instead of tag pushes
release-branch = "main"
# Which actions to run on pull requests
pr-run-mode = "skip"
# A prefix git tags must include for dist to care about them
tag-namespace = "some-namespace"
# Additional plan jobs to run in CI
plan-jobs = ["./plan-job"]
# Additional local artifacts jobs to run in CI
build-local-jobs = ["./build-local-job-1", "./build-local-job-2"]
# Additional global artifacts jobs to run in CI
build-global-jobs = ["./build-global-job"]
# Additional hosts jobs to run in CI
host-jobs = ["./host-job"]
# Additional publish jobs to run in CI
publish-jobs = ["./publish-job"]
# Additional jobs to run in CI, after the announce job finishes
post-announce-jobs = ["./post-announce-job"]
# Whether dist should generate workflows for GitHub CI
github = true
"#;

let ci = Some(CiLayer {
common: CommonCiLayer {
merge_tasks: Some(true),
fail_fast: Some(true),
cache_builds: Some(true),
build_local_artifacts: Some(true),
dispatch_releases: Some(true),
release_branch: Some("main".to_string()),
pr_run_mode: Some(dist_schema::PrRunMode::Skip),
tag_namespace: Some("some-namespace".to_string()),
plan_jobs: Some(vec![
"./plan-job".parse().unwrap(),
]),
build_local_jobs: Some(vec![
"./build-local-job-1".parse().unwrap(),
"./build-local-job-2".parse().unwrap(),
]),
build_global_jobs: Some(vec![
"./build-global-job".parse().unwrap(),
]),
host_jobs: Some(vec![
"./host-job".parse().unwrap(),
]),
publish_jobs: Some(vec![
"./publish-job".parse().unwrap(),
]),
post_announce_jobs: Some(vec![
"./post-announce-job".parse().unwrap(),
]),
},
github: Some(BoolOr::Bool(true)),
});

let mut doc = source();
let table = dist_table(&mut doc);

apply(table, &ci);

let toml_text = doc.to_string();
assert_eq!(expected, toml_text);
}

#[test]
fn apply_ci_gh_complex() {
let expected = r#"
# CI configuration for dist
[dist.ci]
# Configure generated workflows for GitHub CI
[dist.ci.github]
build-setup = "build-setup"
"#;

let ci = Some(CiLayer {
common: CommonCiLayer::default(),
github: Some(BoolOr::Val(GithubCiLayer {
common: CommonCiLayer::default(),
build_setup: Some("build-setup".to_string()),
permissions: None,
runners: None,
})),
});

let mut doc = source();
let table = dist_table(&mut doc);

apply(table, &ci);

let toml_text = doc.to_string();
assert_eq!(expected, toml_text);

}
}

0 comments on commit 1b6e08b

Please sign in to comment.