Stucco icon indicating copy to clipboard operation
Stucco copied to clipboard

Issue with Git Tag Regular Expression Matching

Open jimbrig opened this issue 2 years ago • 0 comments

Description

Great package, I have been using the template for all of my modules! I noticed that during builds/testing my modules would always fail when implementing the Manifest.tests.ps1 tests with outputs like the following:

image

Even after releasing a couple tagged versions of my module and ensuring the manifest was correct, my tests were still skipping/failing this section, so I decided to dive a little deeper.

One thing to note is this isn't necessarily a bug, but more of a feature/enhancement to increase the flexibility of the tag detection via regex.

Details:

I tag my release commits with tags such as v0.0.1, v1.2.3, etc. (i.e. using a v before the semantic version).

The pattern currently being used is on [Line 73 of Manifest.tests.ps1](https://github.com/devblackops/Stucco/blob/c1b193a70407079c8664f2284401743094035d8d/Stucco/template/tests/Manifest.tests.ps1#L73 and is 'tag:\s*(\d+(?:\.\d+)*)', which honestly looks correct to me.

However, I realized that instead of using \s* (AKA a space followed by anything; which is where my v comes into play), \s?. should be used instead.

Why? Because using * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy), whereas using ?. says "optionally match anything once and move on" (for lack of a better way to explain regex).

Therefore, changing the pattern from 'tag:\s*(\d+(?:\.\d+)*)' to 'tag:\s?.(\d+(\.\d+)*)' fixed the test execution for my issue.

Steps to Reproduce

  1. Create a tagged git commit with the syntax v0.0.1.

  2. Run git log --decorate --oneline HEAD~1..HEAD and examine the output, it should include a tag: v0.0.1 string on the output:

image

  1. Run ./build.ps1 -Task Test or the Manifest.tests.ps1 script block directly and examine the output from the Git tagging section at the bottom. Notice how the $gitTagVersion = matches[1] statement fails?

Current Behavior

Failure to detect git tags in tests when they exist.

Expected behavior

All typical tagging patterns should be detected and passed in the test (i.e. 0.0.1 or v0.0.1, etc.)

Possible Solution

Change the RegEx matching expression to be more flexible in its detection of a tag from the git log.

Change 'tag:\s*(\d+(?:\.\d+)*)' to 'tag:\s?.(\d+(\.\d+)*)' to add flexibility for detection.

A more robust approach would be to:

a) Check for the word tag: in the git log result and fail if none detected (i.e. no tags) b) Match everything after tag: and before the next comma ,, to extract the tag text. c) Remove all letters and punctuation except for a period . from the tag text. c) Validate the extracted tag's version from (c) to the [Version] type and compare with manifest version, etc.

See above.

I have also already fixed it in my fork of this repo: see jimbrig/Stucco - Manifest.tests.ps1.

I can submit a PR as well.

For more RegEx details see the following examples:

  • https://regex101.com/r/EwkNsR/1 (fixed pattern) vs.
  • https://regex101.com/r/7khTAR/1 (old pattern failing)

Screenshots

Environment

  • Module version used:
  • Operating System and PowerShell version:

Additional context

Note that during my inspection of these test files I found some other potential areas of improvement also.

For example, on my machine, I have two git executables on my PATH and therefore the statement $git = (Get-Command git -CommandType Application -ErrorAction SilentlyContinue) would return git.exe git.exe instead of just git.exe causing the $thisCommit = & $git log --decorate --oneline HEAD~1..HEAD expression to fail:

image

I simply addressed this issue by changing $git = (Get-Command git -CommandType Application -ErrorAction SilentlyContinue) to $git = (Get-Command git -CommandType Application -ErrorAction SilentlyContinue)[0] to ensure it always is singular.

Thanks!

jimbrig avatar Jan 06 '23 00:01 jimbrig