From fe6d2bdc9e45addf267af4791277a5ebae6adf9c 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 --- .github/actions/run-fixtures-test/action.yml | 146 +++++++++--------- .../test-configure-from-cargo-toml/Cargo.toml | 20 +-- .../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 + __TODO.toml | 78 ---------- git-cliff-core/src/changelog.rs | 2 - git-cliff-core/src/config/embed.rs | 5 +- git-cliff-core/src/config/migrate.rs | 8 +- git-cliff-core/src/config/models_v2.rs | 6 +- git-cliff/src/config.rs | 7 +- website/docs/configuration/migration.md | 2 +- 16 files changed, 260 insertions(+), 173 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 delete mode 100644 __TODO.toml diff --git a/.github/actions/run-fixtures-test/action.yml b/.github/actions/run-fixtures-test/action.yml index 7cf9dc693d..385d01f1ef 100644 --- a/.github/actions/run-fixtures-test/action.yml +++ b/.github/actions/run-fixtures-test/action.yml @@ -1,74 +1,72 @@ -name: Run a fixtures test -description: Run a fixtures test - -inputs: - fixtures-dir: - description: Path to the fixtures directory - required: true - command: - description: The git-cliff command to run - required: false - default: "" - date-format: - description: The date format to use - required: false - default: "%Y-%m-%d" - -runs: - using: composite - steps: - - name: Install toolchain - uses: dtolnay/rust-toolchain@nightly - - - name: Install git-cliff - run: cargo install --path git-cliff/ - shell: bash - - - name: Set git config - run: | - git config --global user.name "github-actions[bot]" - git config --global user.email "github-actions[bot]@users.noreply.github.com" - shell: bash - - - name: Create commits and tags - working-directory: ${{ inputs.fixtures-dir }} - run: | - git init - ./commit.sh - shell: bash - - - name: Generate a changelog - working-directory: ${{ inputs.fixtures-dir }} - run: git cliff --config cliff.toml ${{ inputs.command }} > output.md - shell: bash - - - name: Compare the output with the expected output - working-directory: ${{ inputs.fixtures-dir }} - env: - IN_DATE_FORMAT: ${{ inputs.date-format }} - run: | - cat output.md - current_date=$(date +"$IN_DATE_FORMAT") - sed -i "s/<>/$current_date/g" expected.md - diff --strip-trailing-cr output.md expected.md - shell: bash - - # test backwards compatibility using v1 confiuration - - - name: Generate a chanlog using v1 configuration - if: ${{ hashFiles(format('{0}/cliff.v1.toml', inputs.fixtures-dir)) }} - working-directory: ${{ inputs.fixtures-dir }} - run: git-cliff --config cliff.v1.toml --config-version 1 ${{ inputs.command }} > output.md - shell: bash - - - name: Compare the v1 configuration output with the expected output - if: ${{ hashFiles(format('{0}/cliff.v1.toml', inputs.fixtures-dir)) }} - working-directory: ${{ inputs.fixtures-dir }} - env: - IN_DATE_FORMAT: ${{ inputs.date-format }} - run: | - cat output.md - current_date=$(date +"$IN_DATE_FORMAT") - sed -i "s/<>/$current_date/g" expected.md - diff --strip-trailing-cr output.md expected.md - shell: bash +name: Run a fixtures test +description: Run a fixtures test + +inputs: + fixtures-dir: + description: Path to the fixtures directory + required: true + command: + description: The git-cliff command to run + required: false + default: "" + date-format: + description: The date format to use + required: false + default: "%Y-%m-%d" + +runs: + using: composite + steps: + - name: Install toolchain + uses: dtolnay/rust-toolchain@nightly + + - name: Install git-cliff + run: cargo install --path git-cliff/ + shell: bash + + - name: Set git config + run: | + git config --global user.name "github-actions[bot]" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + shell: bash + + - name: Create commits and tags + working-directory: ${{ inputs.fixtures-dir }} + run: | + git init + ./commit.sh + shell: bash + + - name: Generate a changelog + working-directory: ${{ inputs.fixtures-dir }} + run: git cliff -v --config cliff.toml ${{ inputs.command }} --output output.md + shell: bash + + - name: Compare the output with the expected output + working-directory: ${{ inputs.fixtures-dir }} + env: + IN_DATE_FORMAT: ${{ inputs.date-format }} + run: | + current_date=$(date +"$IN_DATE_FORMAT") + sed -i "s/<>/$current_date/g" expected.md + git diff --no-index expected.md output.md + shell: bash + + # test backwards compatibility using v1 confiuration + + - name: Generate a chanlog using v1 configuration + if: ${{ hashFiles(format('{0}/cliff.v1.toml', inputs.fixtures-dir)) }} + working-directory: ${{ inputs.fixtures-dir }} + run: git-cliff -v --config cliff.v1.toml --config-version 1 ${{ inputs.command }} --output output.md + shell: bash + + - name: Compare the v1 configuration output with the expected output + if: ${{ hashFiles(format('{0}/cliff.v1.toml', inputs.fixtures-dir)) }} + working-directory: ${{ inputs.fixtures-dir }} + env: + IN_DATE_FORMAT: ${{ inputs.date-format }} + run: | + current_date=$(date +"$IN_DATE_FORMAT") + sed -i "s/<>/$current_date/g" expected.md + git diff --no-index expected.md output.md + shell: bash diff --git a/.github/fixtures/test-configure-from-cargo-toml/Cargo.toml b/.github/fixtures/test-configure-from-cargo-toml/Cargo.toml index d44e73bd9d..c89b891d2a 100644 --- a/.github/fixtures/test-configure-from-cargo-toml/Cargo.toml +++ b/.github/fixtures/test-configure-from-cargo-toml/Cargo.toml @@ -4,14 +4,13 @@ version = "0.1.0" edition = "2021" [package.metadata.git-cliff.changelog] -# changelog header +# A static header for the changelog. 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 = """ +# A Tera template to be rendered for each release in the changelog (see https://keats.github.io/tera/docs/#introduction). +body_template = """ {% if version %}\ ## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }} {% else %}\ @@ -24,15 +23,16 @@ body = """ {% endfor %} {% endfor %}\n """ -# template for the changelog footer -footer = """ +# A Tera template to be rendered as the changelog's footer (see https://keats.github.io/tera/docs/#introduction). +footer_template = """ """ -# remove the leading and trailing whitespace from the templates -trim = true +# Whether to remove leading and trailing whitespaces from all lines of the changelog's body. +trim_body_whitespace = true -[package.metadata.git-cliff.git] -# regex for parsing and grouping commits +[package.metadata.git-cliff.commit] +# A list of parsers using regex for extracting data from the commit message. +# Sets the commits' `group` and `scope` and can decide to exclude commits from further processing. 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/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/__TODO.toml b/__TODO.toml deleted file mode 100644 index 54e6c602df..0000000000 --- a/__TODO.toml +++ /dev/null @@ -1,78 +0,0 @@ ->>> [changelog] ->>> # A static header for the changelog. ->>> header Option // changelog.header ->>> ->>> # A Tera template to be rendered for each release in the changelog (see https://keats.github.io/tera/docs/#introduction). ->>> body_template Option // changelog.body ->>> ->>> # A Tera template to be rendered as the changelog's footer (see https://keats.github.io/tera/docs/#introduction). ->>> footer_template Option // changelog.footer ->>> ->>> # Whether to remove leading and trailing whitespaces from all lines of the changelog's body. ->>> trim_body_whitespace Option // changelog.trim - ->>> # A list of postprocessors using regex to modify the changelog. ->>> postprocessors Option> ->>> ->>> # Whether to exclude changes that do not belong to any group from the changelog. ->>> exclude_ungrouped_changes Option // git.filter_commits - - ->>> [release] ->>> # Regex to select git tags that represent releases. ->>> # Example: "v[0-9].*" ->>> tags_pattern String // git.tag_pattern ->>> ->>> # Regex to select git tags that do not represent proper releases. Takes precedence over `release.tags_pattern`. ->>> # Changes belonging to these releases will be included in the next non-skipped release. ->>> # Example: "rc" ->>> skip_tags_pattern String // git.ignore_tags ->>> ->>> # Whether to order releases chronologically or topologically. ->>> # Must be either `time` or `topology`. ->>> order_by Enum: "time" / "topology" // git.topo_order - - ->>> [commit] ->>> # Whether to order commits newest to oldest or oldest to newest in their group. ->>> # Must be either `newest` or `oldest`. ->>> sort_order Enum: "newest" / "oldest" // git.sort_commits ->>> ->>> # Whether to limit the total number of commits to be included in the changelog. ->>> max_commit_count Option // git.limit_commits ->>> ->>> # Whether to split commits on newlines, treating each line as an individual commit. ->>> split_by_newline Option // git.split_commits ->>> ->>> # Regex to select git tags that should be excluded from the changelog. ->>> exclude_tags_pattern String // git.skip_tags ->>> ->>> # A list of preprocessors to modify commit messages using regex prior to further processing. ->>> message_preprocessors Option> // git.commit_preprocessors ->>> ->>> # A list of parsers using regex for extracting external references found in commit messages, and turning them into links. The gemerated links can be used in the body template as `commit.links`. ->>> # Example: "RFC(\\d+)" -> "https://datatracker.ietf.org/doc/html/rfc$1" ->>> link_parsers Option> // git.link_parsers ->>> ->>> # Whether to parse commits according to the conventional commits specification. ->>> # Sets the commits' `group` (= `type`), `scope`, `message` (= `description`), `body`, `breaking`, `breaking_description` and `footers`. ->>> parse_conventional_commits Option // git.conventional_commits - -# Whether to fail generating the changelog if the history contains commits that do not match the conventional commits specification. -require_conventional_commits Option - ->>> # Whether to exclude commits that do not match the conventional commits specification from the changelog. ->>> exclude_unconventional_commits Option // git.filter_unconventional ->>> ->>> # A list of parsers using regex for extracting data from the commit message. ->>> # Sets the commits' `group` and `scope` and can decide to exclude commits from further processing. ->>> commit_parsers Option> // git.commit_parsers ->>> ->>> # Whether to prevent breaking changes from being excluded by commit parsers. ->>> retain_breaking_changes Option // git.protect_breaking_commits - - -[remote.github] -owner String -repo String -token Option \ No newline at end of file diff --git a/git-cliff-core/src/changelog.rs b/git-cliff-core/src/changelog.rs index d0514f2244..4f3d40cb65 100644 --- a/git-cliff-core/src/changelog.rs +++ b/git-cliff-core/src/changelog.rs @@ -356,8 +356,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..90f3cf4960 100644 --- a/git-cliff-core/src/config/embed.rs +++ b/git-cliff-core/src/config/embed.rs @@ -53,11 +53,14 @@ pub fn read_from_manifest() -> Result> { if info.path.exists() { let contents = fs::read_to_string(&info.path)?; if info.regex.is_match(&contents) { + info!("Using configuration from manifest in {:?}.", info.path); return Ok(Some(info.regex.replace_all(&contents, "[").to_string())); } } } - Ok(None) + Err(Error::EmbeddedError(String::from( + "Could not read config from manifest.", + ))) } /// 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..c0b009e38a 100644 --- a/git-cliff/src/config.rs +++ b/git-cliff/src/config.rs @@ -65,7 +65,7 @@ pub fn load_config(args: &Opt) -> Result { // If `--config` denotes an existing file, load it from there. else if config_arg.is_file() { info!( - "Loading configuration from {}", + "Loading configuration from {}.", config_arg.to_string_lossy() ); fs::read_to_string(config_arg)? @@ -74,6 +74,7 @@ pub fn load_config(args: &Opt) -> Result { else if let Some(contents) = git_cliff_core::config::embed::read_from_manifest()? { + info!("Loading configuration from manifest."); contents } // Otherwise fall back to using the embedded configuration from @@ -90,7 +91,7 @@ pub fn load_config(args: &Opt) -> Result { // `--config-version` and the option `meta.version`. let raw_config = config_str.parse::()?; let config_version = determine_config_version(args, raw_config); - info!("Loading config version {config_version}."); + info!("Loading configuration version {config_version}."); // load the file using https://docs.rs/config let raw_config = config::Config::builder() @@ -109,7 +110,7 @@ pub fn load_config(args: &Opt) -> Result { return Ok(raw_config.try_deserialize::()?); } else { return Err(Error::ArgumentError(format!( - "Config version {} is not supported.", + "Configuration version {} is not supported.", config_version ))); } 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: