Skip to content

Commit

Permalink
Replace powershell to pwsh
Browse files Browse the repository at this point in the history
  • Loading branch information
BDisp committed Mar 4, 2025
1 parent c13f3f0 commit 4ec8904
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-duplicates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run Duplicate Test Check
run: powershell -File ./Scripts/FindDuplicateTestMethodsInSameFileName.ps1 -solutionPath "$PWD"
run: pwsh -File ./Scripts/FindDuplicateTestMethodsInSameFileName.ps1 -solutionPath "$PWD"
33 changes: 10 additions & 23 deletions Scripts/FindDuplicateTestMethodsInSameFileName.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,21 @@ param (
)

# Set the base path for relative paths (current directory when script is run)
$basePath = Get-Location | Select-Object -ExpandProperty Path

# Function to get relative path (compatible with PowerShell 5.1)
function Get-RelativePath {
param (
[string]$basePath,
[string]$fullPath
)
# Ensure paths are absolute and normalized
$basePath = [System.IO.Path]::GetFullPath($basePath)
$fullPath = [System.IO.Path]::GetFullPath($fullPath)
# Calculate relative path using .NET
if ($fullPath.StartsWith($basePath)) {
$relative = $fullPath.Substring($basePath.Length).TrimStart('\', '/')
return $relative
}
return $fullPath # Fallback to full path if not relative
}
$basePath = Get-Location

# Function to extract method names from a C# file
function Get-TestMethodNames {
param ($filePath)
$content = Get-Content -Path $filePath -Raw
$testMethods = @()

# Match test attributes and capture method names with flexible spacing/comments
$methodPattern = '(?s)(\[TestMethod\]|\[Test\]|\[Fact\]|\[Theory\])\s*[\s\S]*?public\s+(?:void|Task)\s+(\w+)\s*\('
$methods = [regex]::Matches($content, $methodPattern)

foreach ($match in $methods) {
$methodName = $match.Groups[2].Value
if ($methodName) {
$methodName = $match.Groups[2].Value # Group 2 is the method name
if ($methodName) { # Ensure we only add non-empty method names
$testMethods += $methodName
}
}
Expand All @@ -52,14 +36,15 @@ $fileGroups = $testFiles | Group-Object -Property Name
$duplicates = @{}

foreach ($group in $fileGroups) {
if ($group.Count -gt 1) {
if ($group.Count -gt 1) { # Only process files that exist in multiple locations
$fileName = $group.Name
$methodMap = @{}
$methodMap = @{} # Track methods for this specific filename

foreach ($file in $group.Group) {
$methods = Get-TestMethodNames -filePath $file.FullName
foreach ($method in $methods) {
if ($methodMap.ContainsKey($method)) {
# Duplicate found for this method in the same filename
if (-not $duplicates.ContainsKey($method)) {
$duplicates[$method] = @($methodMap[$method])
}
Expand All @@ -80,10 +65,12 @@ if ($duplicates.Count -eq 0) {
foreach ($dup in $duplicates.Keys) {
Write-Host "Method: $dup" -ForegroundColor Cyan
foreach ($fullPath in $duplicates[$dup]) {
$relativePath = Get-RelativePath -basePath $basePath -fullPath $fullPath
$relativePath = Resolve-Path -Path $fullPath -Relative -RelativeBasePath $basePath
Write-Host " - $relativePath" -ForegroundColor White
}
}
# Display total number of duplicate methods
Write-Host "Total number of duplicate methods: $($duplicates.Count)" -ForegroundColor Magenta
# Fail the pipeline by setting a non-zero exit code
exit 1
}

0 comments on commit 4ec8904

Please sign in to comment.