Skip to content

Commit

Permalink
Ensure we fail on unknown config properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
bittrance committed Dec 27, 2023
1 parent 317fe6e commit 232fbee
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use serde::{Deserialize, Deserializer};
use crate::{errors::GitOpsError, opts::CliOptions};

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConfigFile {
pub tasks: Vec<GitTaskConfig>,
}

#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GitTaskConfig {
pub name: String,
pub github: Option<GithubConfig>,
Expand Down Expand Up @@ -56,6 +58,7 @@ impl TryFrom<&CliOptions> for GitTaskConfig {
}

#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GithubConfig {
pub app_id: String,
pub private_key_file: PathBuf,
Expand Down Expand Up @@ -86,6 +89,7 @@ impl TryFrom<&CliOptions> for Option<GithubConfig> {
}

#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GitConfig {
#[serde(deserialize_with = "url_from_string")]
pub url: Url,
Expand All @@ -112,6 +116,7 @@ impl TryFrom<&CliOptions> for GitConfig {
}

#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ActionConfig {
pub name: String,
pub entrypoint: String,
Expand Down Expand Up @@ -169,7 +174,7 @@ pub fn read_config(reader: impl Read) -> Result<ConfigFile, GitOpsError> {
mod tests {
use std::time::Duration;

use crate::config::GitTaskConfig;
use crate::{config::GitTaskConfig, errors::GitOpsError};

use super::read_config;

Expand All @@ -186,6 +191,55 @@ mod tests {
read_config(config.as_bytes()).unwrap();
}

#[test]
fn fail_on_unknown_git_config() {
let config = r#"tasks:
- name: testo
git:
url: https://github.com/bittrance/kitops
non: sense
actions:
- name: list files
entrypoint: /bin/ls
"#;
assert!(matches!(
read_config(config.as_bytes()),
Err(GitOpsError::MalformedConfig(_))
));
}

#[test]
fn fail_on_unknown_action_config() {
let config = r#"tasks:
- name: testo
git:
url: https://github.com/bittrance/kitops
actions:
- name: list files
non: sense
entrypoint: /bin/ls
"#;
assert!(matches!(
read_config(config.as_bytes()),
Err(GitOpsError::MalformedConfig(_))
));
}

#[test]
fn action_environment_config() {
let config = r#"tasks:
- name: testo
git:
url: https://github.com/bittrance/kitops
actions:
- name: list files
entrypoint: /bin/ls
environment:
FOO: bar
"#;
read_config(config.as_bytes()).unwrap();
}

#[test]
fn parse_gittaskconfig() {
let raw_config = r#"name: testo
Expand Down

0 comments on commit 232fbee

Please sign in to comment.