Skip to content

Commit

Permalink
Create configuration option for workflows (#87)
Browse files Browse the repository at this point in the history
A new field has been added to the configuration to define a list of
workflows with their respecitive jobs. This will be used in a new
command to automatically update previously added workflows.
  • Loading branch information
jdno authored Dec 12, 2023
1 parent fdd8a72 commit 48a445d
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/cli/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl<'a> Init<'a> {
.repository(repository)
.build(),
))
.workflows(Vec::new())
.build();

config.save(self.project)?;
Expand Down
43 changes: 37 additions & 6 deletions src/cli/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use typed_builder::TypedBuilder;
use crate::Project;

pub use self::library::LibraryConfiguration;
pub use self::workflow::WorkflowConfiguration;

mod library;
mod workflow;

const CONFIG_FILE_NAME: &str = "flowcrafter.yml";

Expand All @@ -18,6 +20,8 @@ const CONFIG_FILE_NAME: &str = "flowcrafter.yml";
pub struct Configuration {
#[serde(with = "serde_yaml::with::singleton_map")]
library: LibraryConfiguration,
#[serde(default)]
workflows: Vec<WorkflowConfiguration>,
}

impl Configuration {
Expand Down Expand Up @@ -77,6 +81,12 @@ mod tests {
github:
owner: jdno
repository: flowcrafter
workflows:
- name: rust
jobs:
- lint
- style
- test
"#
);

Expand All @@ -88,6 +98,10 @@ mod tests {
.repository("flowcrafter")
.build(),
))
.workflows(vec![WorkflowConfiguration::builder()
.name("rust")
.jobs(vec!["lint".into(), "style".into(), "test".into()])
.build()])
.build()
}

Expand Down Expand Up @@ -187,6 +201,17 @@ mod tests {
#[cfg(feature = "serde")]
#[test]
fn trait_deserialize() {
let configuration: Configuration = serde_yaml::from_str(SERIALIZED_CONFIGURATION).unwrap();

assert!(matches!(
configuration.library,
LibraryConfiguration::GitHub(_)
));
}

#[cfg(feature = "serde")]
#[test]
fn trait_deserialize_without_workflows() {
let yaml = indoc!(
r#"
---
Expand Down Expand Up @@ -222,12 +247,18 @@ mod tests {

let expected = indoc!(
r#"
library:
github:
instance: https://api.github.com/
owner: jdno
repository: flowcrafter
"#
library:
github:
instance: https://api.github.com/
owner: jdno
repository: flowcrafter
workflows:
- name: rust
jobs:
- lint
- style
- test
"#
);

assert_eq!(expected, yaml);
Expand Down
83 changes: 83 additions & 0 deletions src/cli/configuration/workflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use typed_builder::TypedBuilder;

#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, TypedBuilder)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WorkflowConfiguration {
#[builder(setter(into))]
name: String,
#[builder(setter(into))]
jobs: Vec<String>,
}

impl WorkflowConfiguration {
pub fn name(&self) -> &str {
&self.name
}

pub fn jobs(&self) -> &[String] {
&self.jobs
}
}

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

use super::*;

#[cfg(feature = "serde")]
#[test]
fn trait_deserialize_with_empty_jobs() {
let yaml = indoc!(
r#"
---
name: test
jobs: []
"#
);

let config: WorkflowConfiguration =
serde_yaml::from_str(yaml).expect("failed to deserialize YAML");

assert_eq!(config.name(), "test");
assert!(config.jobs().is_empty());
}

#[cfg(feature = "serde")]
#[test]
fn trait_deserialize_with_jobs() {
let yaml = indoc!(
r#"
---
name: test
jobs:
- lint
- style
"#
);

let config: WorkflowConfiguration =
serde_yaml::from_str(yaml).expect("failed to deserialize YAML");

assert_eq!(config.name(), "test");
assert_eq!(config.jobs(), &["lint", "style"]);
}

#[test]
fn trait_send() {
fn assert_send<T: Send>() {}
assert_send::<WorkflowConfiguration>();
}

#[test]
fn trait_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<WorkflowConfiguration>();
}

#[test]
fn trait_unpin() {
fn assert_unpin<T: Unpin>() {}
assert_unpin::<WorkflowConfiguration>();
}
}

0 comments on commit 48a445d

Please sign in to comment.