Skip to content

Commit

Permalink
Add new tests and reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
nsyzrantsev committed Jan 5, 2025
1 parent b7d2389 commit 1fc7002
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
20 changes: 18 additions & 2 deletions zefiro-core/zefiro-cwl-parser/src/schema/command_line_tool.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
use crate::schema::requirements::CommandLineToolRequirement;
use crate::schema::requirements::{CommandLineToolRequirement, SUPPORTED_CWL_VERSIONS};
use crate::schema::types::{Any, CwlSchemaType, Documentation};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// This defines the schema of the CWL Command Line Tool Description document.
/// See: https://www.commonwl.org/v1.2/CommandLineTool.html
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CommandLineTool {
#[serde(default = "CommandLineTool::default_cwl_version")]
pub cwl_version: String,
#[serde(default = "CommandLineTool::default_class")]
pub class: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub doc: Option<Documentation>,
#[serde(default)]
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
#[serde(default)]
pub inputs: Vec<CommandInputParameter>,
#[serde(default)]
pub outputs: Vec<CommandOutputParameter>,
#[serde(default)]
pub requirements: Vec<CommandLineToolRequirement>,
}

impl CommandLineTool {
fn default_cwl_version() -> String {
SUPPORTED_CWL_VERSIONS[0].to_string()
}

fn default_class() -> String {
"CommandLineTool".to_string()
}
}

/// Represents an input parameter for a `CommandLineTool`.
/// See: https://www.commonwl.org/v1.2/CommandLineTool.html#CommandInputParameter
#[skip_serializing_none]
Expand Down
44 changes: 40 additions & 4 deletions zefiro-core/zefiro-cwl-parser/src/schema/document.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::schema::{command_line_tool::CommandLineTool, workflow::Workflow};
use crate::schema::{
command_line_tool::CommandLineTool, requirements::SUPPORTED_CWL_VERSIONS, workflow::Workflow,
};
use anyhow::{bail, ensure, Error, Result};
use serde::{Deserialize, Serialize};
use serde_yaml::{self, Value};
Expand All @@ -8,8 +10,6 @@ use std::{
str::FromStr,
};

const SUPPORTED_VERSIONS: &[&str] = &["v1.2"];

/// Represents a CWL Schema which can be either a CommandLineTool or a Workflow
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
Expand Down Expand Up @@ -40,7 +40,7 @@ impl CwlSchema {
.and_then(Value::as_str)
.ok_or_else(|| anyhow::anyhow!("Failed to determine CWL specification version."))?;
ensure!(
SUPPORTED_VERSIONS.contains(&version),
SUPPORTED_CWL_VERSIONS.contains(&version),
"Unsupported CWL version: {version}"
);

Expand Down Expand Up @@ -158,4 +158,40 @@ mod tests {
serde_yaml::to_value(&written_values).unwrap()
);
}

#[test]
fn test_clt_to_yaml_write_error() {
use std::io::{Error, ErrorKind, Write};

struct FailingWriter;
impl Write for FailingWriter {
fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
Err(Error::new(ErrorKind::Other, "Simulated write error"))
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}

let schema = CwlSchema::CommandLineTool(CommandLineTool::default());
assert!(schema.to_yaml(FailingWriter).is_err());
}

#[test]
fn test_wf_to_yaml_write_error() {
use std::io::{Error, ErrorKind, Write};

struct FailingWriter;
impl Write for FailingWriter {
fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
Err(Error::new(ErrorKind::Other, "Simulated write error"))
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}

let schema = CwlSchema::Workflow(Workflow::default());
assert!(schema.to_yaml(FailingWriter).is_err());
}
}
2 changes: 2 additions & 0 deletions zefiro-core/zefiro-cwl-parser/src/schema/requirements.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

pub const SUPPORTED_CWL_VERSIONS: [&str; 1] = ["v1.2"];

const CPU_NUM_DEFAULT: u32 = 1;
const RAM_SIZE_IN_MB_DEFAULT: u32 = 1024;
const TMPDIR_MIN_IN_MB_DEFAULT: u32 = 1024;
Expand Down
19 changes: 16 additions & 3 deletions zefiro-core/zefiro-cwl-parser/src/schema/workflow.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use crate::schema::command_line_tool::CommandLineTool;
use crate::schema::requirements::WorkflowRequirement;
use crate::schema::requirements::{WorkflowRequirement, SUPPORTED_CWL_VERSIONS};
use crate::schema::types::{Any, CwlSchemaType, Documentation, Scatter, Source};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// This defines the schema of the CWL Workflow Description document.
/// See: https://www.commonwl.org/v1.2/Workflow.html
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Workflow {
pub class: String,
#[serde(default = "Workflow::default_cwl_version")]
pub cwl_version: String,
#[serde(default = "Workflow::default_class")]
pub class: String,
pub doc: Option<Documentation>,
#[serde(default)]
pub id: String,
pub label: Option<String>,
pub inputs: Vec<WorkflowInputParameter>,
Expand All @@ -21,6 +24,16 @@ pub struct Workflow {
pub requirements: Vec<WorkflowRequirement>,
}

impl Workflow {
fn default_cwl_version() -> String {
SUPPORTED_CWL_VERSIONS[0].to_string()
}

fn default_class() -> String {
"Workflow".to_string()
}
}

/// Represents an input parameter for a `Workflow`.
/// See: https://www.commonwl.org/v1.2/Workflow.html#WorkflowInputParameter
#[skip_serializing_none]
Expand Down

0 comments on commit 1fc7002

Please sign in to comment.