From 5ce1b9ef85a3a127bbf9fbaa69016f8cbe88addd Mon Sep 17 00:00:00 2001 From: Christopher Cyclonit Klinge Date: Sun, 7 Apr 2024 13:34:54 +0200 Subject: [PATCH] test(config): add tests for backwards compatibility --- .../fixtures/test-v1-config-arg/cliff.toml | 34 +++++++++++ .github/fixtures/test-v1-config-arg/commit.sh | 11 ++++ .../fixtures/test-v1-config-arg/expected.md | 31 ++++++++++ .../fixtures/test-v1-config-meta/cliff.toml | 38 +++++++++++++ .../fixtures/test-v1-config-meta/commit.sh | 11 ++++ .../fixtures/test-v1-config-meta/expected.md | 31 ++++++++++ .github/workflows/test-fixtures.yml | 3 + git-cliff-core/src/changelog.rs | 2 - git-cliff-core/src/config/embed.rs | 56 +------------------ git-cliff-core/src/config/migrate.rs | 8 ++- git-cliff-core/src/config/models_v2.rs | 6 +- git-cliff/src/config.rs | 54 +++++++++++++++++- website/docs/configuration/migration.md | 2 +- 13 files changed, 222 insertions(+), 65 deletions(-) create mode 100644 .github/fixtures/test-v1-config-arg/cliff.toml create mode 100755 .github/fixtures/test-v1-config-arg/commit.sh create mode 100644 .github/fixtures/test-v1-config-arg/expected.md create mode 100644 .github/fixtures/test-v1-config-meta/cliff.toml create mode 100755 .github/fixtures/test-v1-config-meta/commit.sh create mode 100644 .github/fixtures/test-v1-config-meta/expected.md diff --git a/.github/fixtures/test-v1-config-arg/cliff.toml b/.github/fixtures/test-v1-config-arg/cliff.toml new file mode 100644 index 0000000000..9a9d380abc --- /dev/null +++ b/.github/fixtures/test-v1-config-arg/cliff.toml @@ -0,0 +1,34 @@ +[changelog] +# changelog header +header = """ +# Changelog\n +All notable changes to this project will be documented in this file.\n +""" +# template for the changelog body +# https://keats.github.io/tera/docs/#introduction +body = """ +{% if version %}\ + ## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }} +{% else %}\ + ## [unreleased] +{% endif %}\ +{% for group, commits in commits | group_by(attribute="group") %} + ### {{ group | upper_first }} + {% for commit in commits %} + - {{ commit.message | upper_first }}\ + {% endfor %} +{% endfor %}\n +""" +# template for the changelog footer +footer = """ + +""" +# remove the leading and trailing whitespace from the templates +trim = true + +[git] +# regex for parsing and grouping commits +commit_parsers = [ + { message = "^feat", group = "Features", default_scope = "app" }, + { message = "^fix", group = "Bug Fixes", scope = "cli" }, +] diff --git a/.github/fixtures/test-v1-config-arg/commit.sh b/.github/fixtures/test-v1-config-arg/commit.sh new file mode 100755 index 0000000000..7c6fe32718 --- /dev/null +++ b/.github/fixtures/test-v1-config-arg/commit.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +set -e + +GIT_COMMITTER_DATE="2022-04-06 01:25:08" git commit --allow-empty -m "Initial commit" +GIT_COMMITTER_DATE="2022-04-06 01:25:09" git commit --allow-empty -m "feat: add feature 1" +GIT_COMMITTER_DATE="2022-04-06 01:25:10" git commit --allow-empty -m "fix: fix feature 1" +git tag v0.1.0 +GIT_COMMITTER_DATE="2022-04-06 01:25:11" git commit --allow-empty -m "feat(gui): add feature 2" +GIT_COMMITTER_DATE="2022-04-06 01:25:12" git commit --allow-empty -m "fix(gui): fix feature 2" +git tag v0.2.0 +GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "test: add tests" diff --git a/.github/fixtures/test-v1-config-arg/expected.md b/.github/fixtures/test-v1-config-arg/expected.md new file mode 100644 index 0000000000..72e96a6f09 --- /dev/null +++ b/.github/fixtures/test-v1-config-arg/expected.md @@ -0,0 +1,31 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +## [unreleased] + +### Test + +- Add tests + +## [0.2.0] - 2022-04-06 + +### Bug Fixes + +- Fix feature 2 + +### Features + +- Add feature 2 + +## [0.1.0] - 2022-04-06 + +### Bug Fixes + +- Fix feature 1 + +### Features + +- Add feature 1 + + diff --git a/.github/fixtures/test-v1-config-meta/cliff.toml b/.github/fixtures/test-v1-config-meta/cliff.toml new file mode 100644 index 0000000000..f92639e05b --- /dev/null +++ b/.github/fixtures/test-v1-config-meta/cliff.toml @@ -0,0 +1,38 @@ +[meta] +# The version of the config schema. +version = 1 + +[changelog] +# changelog header +header = """ +# Changelog\n +All notable changes to this project will be documented in this file.\n +""" +# template for the changelog body +# https://keats.github.io/tera/docs/#introduction +body = """ +{% if version %}\ + ## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }} +{% else %}\ + ## [unreleased] +{% endif %}\ +{% for group, commits in commits | group_by(attribute="group") %} + ### {{ group | upper_first }} + {% for commit in commits %} + - {{ commit.message | upper_first }}\ + {% endfor %} +{% endfor %}\n +""" +# template for the changelog footer +footer = """ + +""" +# remove the leading and trailing whitespace from the templates +trim = true + +[git] +# regex for parsing and grouping commits +commit_parsers = [ + { message = "^feat", group = "Features", default_scope = "app" }, + { message = "^fix", group = "Bug Fixes", scope = "cli" }, +] diff --git a/.github/fixtures/test-v1-config-meta/commit.sh b/.github/fixtures/test-v1-config-meta/commit.sh new file mode 100755 index 0000000000..7c6fe32718 --- /dev/null +++ b/.github/fixtures/test-v1-config-meta/commit.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +set -e + +GIT_COMMITTER_DATE="2022-04-06 01:25:08" git commit --allow-empty -m "Initial commit" +GIT_COMMITTER_DATE="2022-04-06 01:25:09" git commit --allow-empty -m "feat: add feature 1" +GIT_COMMITTER_DATE="2022-04-06 01:25:10" git commit --allow-empty -m "fix: fix feature 1" +git tag v0.1.0 +GIT_COMMITTER_DATE="2022-04-06 01:25:11" git commit --allow-empty -m "feat(gui): add feature 2" +GIT_COMMITTER_DATE="2022-04-06 01:25:12" git commit --allow-empty -m "fix(gui): fix feature 2" +git tag v0.2.0 +GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "test: add tests" diff --git a/.github/fixtures/test-v1-config-meta/expected.md b/.github/fixtures/test-v1-config-meta/expected.md new file mode 100644 index 0000000000..72e96a6f09 --- /dev/null +++ b/.github/fixtures/test-v1-config-meta/expected.md @@ -0,0 +1,31 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +## [unreleased] + +### Test + +- Add tests + +## [0.2.0] - 2022-04-06 + +### Bug Fixes + +- Fix feature 2 + +### Features + +- Add feature 2 + +## [0.1.0] - 2022-04-06 + +### Bug Fixes + +- Fix feature 1 + +### Features + +- Add feature 1 + + diff --git a/.github/workflows/test-fixtures.yml b/.github/workflows/test-fixtures.yml index d49b9fd40e..6a5413b81d 100644 --- a/.github/workflows/test-fixtures.yml +++ b/.github/workflows/test-fixtures.yml @@ -68,6 +68,9 @@ jobs: - fixtures-name: test-custom-tag-pattern command: --release-tags-pattern "alpha.*" - fixtures-name: test-configure-from-cargo-toml + - fixtures-name: test-v1-config-meta + - fixtures-name: test-v1-config-arg + command: --config-version 1 steps: - name: Checkout uses: actions/checkout@v4 diff --git a/git-cliff-core/src/changelog.rs b/git-cliff-core/src/changelog.rs index d93faa557d..b0590fa457 100644 --- a/git-cliff-core/src/changelog.rs +++ b/git-cliff-core/src/changelog.rs @@ -352,8 +352,6 @@ mod test { use super::*; use crate::config::models_v2::{ Bump, - ChangelogConfig, - CommitConfig, CommitParser, CommitSortOrder, ReleaseConfig, diff --git a/git-cliff-core/src/config/embed.rs b/git-cliff-core/src/config/embed.rs index 2c5ee88a7e..79be66bb6d 100644 --- a/git-cliff-core/src/config/embed.rs +++ b/git-cliff-core/src/config/embed.rs @@ -3,62 +3,8 @@ use crate::error::{ Error, Result, }; -use regex::{ - Regex, - RegexBuilder, -}; use rust_embed::RustEmbed; -use std::fs; -use std::{ - path::PathBuf, - str, -}; - -/// Manifest file information and regex for matching contents. -#[derive(Debug)] -struct ManifestInfo { - /// Path of the manifest. - path: PathBuf, - /// Regular expression for matching metadata in the manifest. - regex: Regex, -} - -lazy_static::lazy_static! { - /// Array containing manifest information for Rust and Python projects. - static ref MANIFEST_INFO: Vec = vec![ - ManifestInfo { - path: PathBuf::from("Cargo.toml"), - regex: RegexBuilder::new( - r"^\[(?:workspace|package)\.metadata\.git\-cliff\.", - ) - .multi_line(true) - .build() - .expect("failed to build regex"), - }, - ManifestInfo { - path: PathBuf::from("pyproject.toml"), - regex: RegexBuilder::new(r"^\[(?:tool)\.git\-cliff\.") - .multi_line(true) - .build() - .expect("failed to build regex"), - }, - ]; - -} - -/// Reads the config file contents from project manifest (e.g. Cargo.toml, -/// pyproject.toml) -pub fn read_from_manifest() -> Result> { - for info in (*MANIFEST_INFO).iter() { - if info.path.exists() { - let contents = fs::read_to_string(&info.path)?; - if info.regex.is_match(&contents) { - return Ok(Some(info.regex.replace_all(&contents, "[").to_string())); - } - } - } - Ok(None) -} +use std::str; /// Default configuration file embedder/extractor. /// diff --git a/git-cliff-core/src/config/migrate.rs b/git-cliff-core/src/config/migrate.rs index deb14ff56e..90fdc1ebfb 100644 --- a/git-cliff-core/src/config/migrate.rs +++ b/git-cliff-core/src/config/migrate.rs @@ -27,7 +27,10 @@ pub fn run(args: &MigrateArgs) -> Result<()> { if !args.in_path.exists() { return Err(Error::ArgumentError(format!( "File {0} does not exist.", - &args.in_path.to_str().unwrap() + &args + .in_path + .to_str() + .expect("could not unwrap argument 'in_path'") ))); } @@ -36,7 +39,8 @@ pub fn run(args: &MigrateArgs) -> Result<()> { // convert to the new config format let new_config = super::models_v2::Config::from(old_config); - let new_toml = toml::to_string(&new_config).unwrap(); + let new_toml = toml::to_string(&new_config) + .expect("could not serialize migrated config into toml"); // write the new config file let mut new_config_file = fs::OpenOptions::new() diff --git a/git-cliff-core/src/config/models_v2.rs b/git-cliff-core/src/config/models_v2.rs index 1788ee5afd..53e8da6a85 100644 --- a/git-cliff-core/src/config/models_v2.rs +++ b/git-cliff-core/src/config/models_v2.rs @@ -277,8 +277,10 @@ impl Config { tags_pattern: config_v1.git.tag_pattern, skip_tags_pattern: config_v1.git.ignore_tags, order_by: Some( - if config_v1.git.topo_order.is_some() && - config_v1.git.topo_order.unwrap() + if config_v1 + .git + .topo_order + .is_some_and(|topo_order| topo_order) { TagsOrderBy::Topology } else { diff --git a/git-cliff/src/config.rs b/git-cliff/src/config.rs index 372267dfa8..0979af5834 100644 --- a/git-cliff/src/config.rs +++ b/git-cliff/src/config.rs @@ -12,9 +12,59 @@ use git_cliff_core::error::{ Error, Result, }; +use regex::{ + Regex, + RegexBuilder, +}; use std::fs; use std::path::PathBuf; +/// Manifest file information and regex for matching contents. +#[derive(Debug)] +struct ManifestInfo { + /// Path of the manifest. + path: PathBuf, + /// Regular expression for matching metadata in the manifest. + regex: Regex, +} + +lazy_static::lazy_static! { + /// Array containing manifest information for Rust and Python projects. + static ref MANIFEST_INFO: Vec = vec![ + ManifestInfo { + path: PathBuf::from("Cargo.toml"), + regex: RegexBuilder::new( + r"^\[(?:workspace|package)\.metadata\.git\-cliff\.", + ) + .multi_line(true) + .build() + .expect("failed to build regex"), + }, + ManifestInfo { + path: PathBuf::from("pyproject.toml"), + regex: RegexBuilder::new(r"^\[(?:tool)\.git\-cliff\.") + .multi_line(true) + .build() + .expect("failed to build regex"), + }, + ]; + +} + +/// Reads the config file contents from project manifest (e.g. Cargo.toml, +/// pyproject.toml) +pub fn read_from_manifest() -> Result> { + for info in (*MANIFEST_INFO).iter() { + if info.path.exists() { + let contents = fs::read_to_string(&info.path)?; + if info.regex.is_match(&contents) { + return Ok(Some(info.regex.replace_all(&contents, "[").to_string())); + } + } + } + Ok(None) +} + /// Gets the effective config argument. fn get_effective_config_arg(path: PathBuf) -> PathBuf { if !path.exists() { @@ -71,9 +121,7 @@ pub fn load_config(args: &Opt) -> Result { fs::read_to_string(config_arg)? } // If the manifest contains a config, load it. - else if let Some(contents) = - git_cliff_core::config::embed::read_from_manifest()? - { + else if let Some(contents) = read_from_manifest()? { contents } // Otherwise fall back to using the embedded configuration from diff --git a/website/docs/configuration/migration.md b/website/docs/configuration/migration.md index 12b81fc295..0e3578e0e7 100644 --- a/website/docs/configuration/migration.md +++ b/website/docs/configuration/migration.md @@ -7,7 +7,7 @@ Example: - `git-cliff migrate-config --in cliff.toml --out cliff-new.toml` -## Backwards compatability +## Backwards compatibility While configuration version 1 is deprecated, the cli argument `--config-version` can be used to make git-cliff use old configuration files. Keep in mind that new features might not be supported when using the old configuration format. Example: