Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 0 additions & 76 deletions CheckMate.ps1

This file was deleted.

8 changes: 8 additions & 0 deletions CheckMate.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@{
RootModule = 'CheckMate.psm1'
ModuleVersion = '0.2'
GUID = '014c80b0-082f-4b4c-833a-55852f1a2303'
Author = 'renao'
Description = 'CheckMate - Runs checks, mate.'
FunctionsToExport = @('Invoke-CheckMate')
}
98 changes: 98 additions & 0 deletions CheckMate.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
function Invoke-CheckMate {
<#
.SYNOPSIS
This is CheckMate. He will run everything inside a checks folder.

.DESCRIPTION
CheckMate is intended to run automated checks for a software projects repository. Its also meant to be easily extended by just adding PowerShell scripts to its checks/-folder.

.PARAMETER RepoRoot
The root folder to run the checks from.

.PARAMETER ReportPath
(optional) Specifies the output path for the report - will be generated inside the repoPath otherwise.

.PARAMETER ChecksBasePath
(optional) Specifies the output path for the report - will be generated inside the repoPath otherwise.

.EXAMPLE
Run only the checks from the `checks/Sanity` directory on the `./MySolution/MyProject` path and generate a `ChecksResult.md` file with the results:

Invoke-CheckMate -RepoRoot "./MySolution/ProjectDirectory" -ReportPath "CheckResults.md" -ChecksBasePath "checks/Sanity"

#>

param(
# Root directory to run the checks for
[string]$RepoRoot = ".",

# Base directory for the checks to run (relative to this script)
[string]$ChecksBasePath = "checks",

# name of the report file to be created
[string]$ReportPath = "$(get-date -f yyyy-MM-dd-HH-mm-ss)_autoreview-report.md"
)

$workingDirectory = Resolve-RepoRoot -RepoRoot $RepoRoot

if ($null -eq $workingDirectory) {
throw "RepoRoot is not valid or does not exist: $RepoRoot"
}

$checkFolder = Join-Path $PSScriptRoot $ChecksBasePath
$results = @{}

# Alle Checks rekursiv einsammeln
Get-ChildItem -Path $checkFolder -Filter *.ps1 -Recurse | ForEach-Object {
$scriptPath = $_.FullName

try
{
$simplifiedCheckPath = [System.IO.Path]::GetRelativePath($checkFolder, $scriptPath)
$result = & $scriptPath -RepoRoot $workingDirectory
$checkSuccess = $LASTEXITCODE
$results[$simplifiedCheckPath] = @($checkSuccess, $result)
}
catch
{
Write-Error "Exception while running `n$_"
$results[$scriptPath] = @(1, "Threw Exception: $_")

return 1
}
}

Import-Module "$PSScriptRoot/common/MarkdownReport.psd1"
$markdownReport = New-MarkdownReport -results $results

Write-Output "`n$markdownReport"


$reportFile = if ([System.IO.Path]::IsPathRooted($ReportPath)) {
$ReportPath
} else {
Join-Path $workingDirectory $ReportPath
}

Set-Content -Value $markdownReport -Encoding UTF8 $reportFile
Write-Output "`n See report file: $reportFile"

return 0
}

function Resolve-RepoRoot {
param(
[string] $repoRoot
)

$resolvedPath = Resolve-Path $repoRoot -ErrorAction SilentlyContinue

if (
(-not $resolvedPath) -or
(-not $(Test-Path -Path $resolvedPath -PathType Container)))
{
return $null
}

return $resolvedPath.Path
}
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,26 @@ So CheckMate only needs to be run in a **PowerShell Core 7+**.

## Usage

You can run `CheckMate.ps1` without adding any additional parameter:
1. Import CheckMate

Go and import the CheckMade Module in your current PowerShell session, e.g.:

```powershell
Import-Module CheckMate.psd1
```

2. Invoke CheckMate to run its tests

```powershell
./CheckMate.ps1
Invoke-CheckMate
```

CheckMate will fall back to its current directory a test object, use the `checks` directory for the tests to run and generate a report file by itself.

Anyhow, you can alter these default configuration by using the corresponding parameters:

```powershell
./CheckMate.ps1 -repoPath "." -ChecksBasePath "./checks/Sanity" -ReportPath "CheckResults.md"
./Invoke-CheckMate -repoPath "." -ChecksBasePath "./checks/Sanity" -ReportPath "CheckResults.md"
```

## License
Expand Down
24 changes: 10 additions & 14 deletions tests/CheckMate.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
Describe "CheckMate.ps1 - Pfadauflösung" {
It "RepoRoot sollte als absoluter Pfad aufgelöst werden" {
$testRepoRoot = "."
$resolvedPath = (Resolve-Path $testRepoRoot).Path
$resolvedPath | Should -Not -BeNullOrEmpty
$resolvedPath | Should -Match '^([A-Za-z]:\\|\\\\\\\\|/)' # Windows/Unix-Pfad
}
BeforeAll {
$modulePath = Join-Path $PSScriptRoot '..\CheckMate.psd1'
Import-Module $modulePath -Force
}

It "ReportPath sollte relativ zu RepoRoot aufgelöst werden, falls nicht absolut" {
$testRepoRoot = (Get-Location).Path
$testReportPath = "report.md"
$expectedReportPath = Join-Path $testRepoRoot $testReportPath
$actualReportPath = if ([System.IO.Path]::IsPathRooted($testReportPath)) { $testReportPath } else { Join-Path $testRepoRoot $testReportPath }
$actualReportPath | Should -Be $expectedReportPath
Describe "Invoke-CheckMate" {
Context "Validating parameters" {
It "Exits FAILED when repository path does not exist" {
{ Invoke-CheckMate -RepoRoot "C:\nonexistant" } | Should -Throw
}
}
}
}