Skip to content

Commit

Permalink
feat+: add new test command (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArwynFr committed Jun 15, 2023
1 parent 6eccc60 commit 415c1bc
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 19 deletions.
20 changes: 19 additions & 1 deletion .github/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@ The following documents provide additional information on rules and standards ap

== How to use the module

This module contains a single command :
This module contains the following commands :

=== Test-ConventionalCommitHeader

Verifies whether the commit message header conforms to the conventional commit format.

```Powershell
Test-ConventionalCommitHeader
[[-Message] <string>]
[-StrictTypes]
[-AdditionalModifiers]
[<CommonParameters>]
```

`-Message`:: *Required* and *Pipelinable*. The versioning message to parse. See https://www.conventionalcommits.org/en/v1.0.0/[conventionalcommits.org] for details on the message structure.

`-StrictTypes`:: *Switch*. If true, the command will enforce the commit type to be in the following list: `build`, `chore`, `ci`, `docs`, `feat`, `fix`, `perf`, `refactor`, `revert`, `style`, `test`.

`-AdditionalModifiers`:: *Switch*. If true, the command will allow usage of `+` and `-` in addition to `!`. See below.

=== ConvertTo-ConventionalCommitHeader

Expand Down
1 change: 1 addition & 0 deletions ConventionalCommits/ConventionalCommits.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
RootModule = 'ConventionalCommits.psm1'
FunctionsToExport = @(
'ConvertTo-ConventionalCommitHeader'
'Test-ConventionalCommitHeader'
)
}
96 changes: 79 additions & 17 deletions ConventionalCommits/ConventionalCommits.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
function ConvertTo-ConventionalCommitHeader {
function Get-InternalStructure {
[CmdletBinding()]
[OutputType([hashtable])]
param (
[Parameter(ValueFromPipeline)]
[string]
$Message,

[Parameter()]
[switch]
$AdditionalModifiers
)

Process {
$pattern = $AdditionalModifiers ? '^(\w+)(\((\w+)\))?([!+-])?: (.*)' : '^(\w+)(\((\w+)\))?([!])?: (.*)'
$result = $Message -match $pattern
if (-not $result) { return $null }

return @{
Type = $matches[1]
Scope = $matches[3]
Modifier = $matches[4]
Description = $matches[5]
}
}
}

function Test-InternalStructure {
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(ValueFromPipeline)]
[hashtable]
$Header,

[Parameter()]
[switch]
$StrictTypes
)

Process {
$allowed_types = @('build'; 'chore'; 'ci'; 'docs'; 'feat'; 'fix'; 'perf'; 'refactor'; 'revert'; 'style'; 'test')
if ($null -eq $Header) { return $false }
if (-not $StrictTypes) { return $true }
if (-not $allowed_types.Contains($Header.Type)) { return $false }
return $true
}
}

function Test-ConventionalCommitHeader {
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(ValueFromPipeline)]
[string]
Expand All @@ -14,24 +64,36 @@ function ConvertTo-ConventionalCommitHeader {
$AdditionalModifiers
)

$allowed_types = @('build'; 'chore'; 'ci'; 'docs'; 'feat'; 'fix'; 'perf'; 'refactor'; 'revert'; 'style'; 'test')
$pattern = $AdditionalModifiers ? '^(\w+)(\((\w+)\))?([!+-])?: (.*)' : '^(\w+)(\((\w+)\))?([!])?: (.*)'
$result = $Message -match $pattern

if (-not $result) {
throw 'Commit message header does not follow the conventions'
Process {
return $Message | Get-InternalStructure -AdditionalModifiers:$AdditionalModifiers | Test-InternalStructure -StrictTypes:$StrictTypes
}
}

$conventions = @{
Type = $matches[1]
Scope = $matches[3]
Modifier = $matches[4]
Description = $matches[5]
}
function ConvertTo-ConventionalCommitHeader {
[CmdletBinding()]
[OutputType([hashtable])]
param (
[Parameter(ValueFromPipeline)]
[string]
$Message,

if ($StrictTypes -and (-not $allowed_types.Contains($conventions.Type))) {
throw 'Commit message header type is not valid'
}
[Parameter()]
[switch]
$StrictTypes,

[Parameter()]
[switch]
$AdditionalModifiers
)

return $conventions
Process {
$conventions = $Message | Get-InternalStructure -AdditionalModifiers:$AdditionalModifiers
$valid = $conventions | Test-InternalStructure -StrictTypes:$StrictTypes

if (-not $valid ) {
throw 'Commit message does not follow the conventions'
}

return $conventions
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ InModuleScope ConventionalCommits {
It 'Should fail a loose message' {
{ 'another(scope)!: description' | ConvertTo-ConventionalCommitHeader -StrictTypes } | Should -Throw
}
# 'build'; 'chore'; 'ci'; 'docs'; 'feat'; 'fix'; 'perf'; 'refactor'; 'revert'; 'style'; 'test'
It 'Should parse a strict message' {
$actual = 'build(scope)!: description' | ConvertTo-ConventionalCommitHeader -StrictTypes
$actual.Type | Should -Be 'build'
Expand Down
61 changes: 61 additions & 0 deletions tests/Test.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
InModuleScope ConventionalCommits {
Describe 'Test-ConventionalCommitHeader' {
Context 'Default behavior' {
It 'Should parse a strict message' {
'fix(scope)!: description' | Test-ConventionalCommitHeader | Should -BeTrue
}
It 'Should parse a loose message' {
'custom(scope)!: description' | Test-ConventionalCommitHeader | Should -BeTrue
}
It 'Should fail an additional modifier' {
'fix(scope)+: description' | Test-ConventionalCommitHeader | Should -BeFalse
}
}
Context 'Strict mode' {
It 'Should fail a loose message' {
'another(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeFalse
}
It 'Should parse a strict message' {
'build(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'chore(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'ci(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'docs(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'feat(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'fix(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'perf(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'refactor(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'revert(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'style(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
It 'Should parse a strict message' {
'test(scope)!: description' | Test-ConventionalCommitHeader -StrictTypes | Should -BeTrue
}
}
Context 'Additional modifiers' {
It 'Should parse a minor modifier' {
'fix(scope)+: description' | Test-ConventionalCommitHeader -AdditionalModifiers | Should -BeTrue
}
It 'Should parse a patch modifier' {
'fix(scope)-: description' | Test-ConventionalCommitHeader -AdditionalModifiers | Should -BeTrue
}
}
}
}

0 comments on commit 415c1bc

Please sign in to comment.