Skip to content

Commit

Permalink
Update rspec.ps1 to copy test case from RSPEC
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-marichal committed Oct 18, 2023
1 parent 98c1d02 commit 38860e1
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
42 changes: 42 additions & 0 deletions scripts/rspec/CopyTestCasesFromRspec.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# For each test case file in the RspecRulePath, copy the file to the OutputFolder with the name $FileName + $Scenario + $Extension
#
# The scenario is determined by the following rules:
# 1. The test case file name contains a dot, e.g. "MyTestCase.Scenario.razor"
# Then the scenario name is the second part of the file name, e.g. "Scenario"
# 2. The test case file name contain a dot, but the test case file is in a subfolder, e.g. "MyTestCase\MyTestCase.Scenario.razor"
# Then the scenario name is the second part of the file name, e.g. "Scenario", ignoring the folder name
# 3. The test case file name does not contain a dot, but the test case file is in a subfolder, e.g. "MyTestCase\Scenario.razor"
# Then the scenario name is the file name, e.g. "Scenario", ignoring the folder name
# 4. The test case file name does not contain a dot and the test case file is in the root folder, e.g. "MyTestCase.razor"
# Then the scenario name is empty, e.g. ""
#
# Example:
# 1. MyTestCase.Scenario.razor => RuleName.Scenario.razor
# 2. MyTestCase\MyTestCase.Scenario.razor => RuleName.Scenario.razor
# 3. MyTestCase\Scenario.razor => RuleName.Scenario.razor
# 4. MyTestCase.razor => RuleName.razor
#
function CopyTestCasesFromRspec($FileName, $RspecRulePath, $OutputFolder) {
$TestCaseFileExtension = @(
"*.cs",
"*.vb",
"*.razor",
"*.cshtml"
)

Get-ChildItem -Recurse -Path $RspecRulePath -Include $TestCaseFileExtension -File | ForEach-Object {

$scenario = "";

if ($_.BaseName.Contains("."))
{
$scenario = "." + $($_.BaseName -Split "\." | Select-Object -Last 1)
}
elseif ($_.Directory.FullName -ne $(Convert-Path $RspecRulePath))
{
$scenario = "." + $_.BaseName
}

Set-Content -NoNewline -Path "${OutputFolder}\\$FileName$scenario$($_.Extension)" -Value $(Get-Content $_ -Raw) -Encoding UTF8
}
}
10 changes: 10 additions & 0 deletions scripts/rspec/rspec.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ param (
Set-StrictMode -version 1.0
$ErrorActionPreference = "Stop"
$RuleTemplateFolder = "${PSScriptRoot}\\rspec-templates"
# Based on RuleApiCache.computeDefaultCachePath
# https://github.com/SonarSource/sonar-rule-api/blob/2323b7313c76e7adc2b7df037e96510b90660292/src/main/java/com/sonarsource/ruleapi/utilities/RuleApiCache.java#L20-L25
$RspecRepositoryPath = "${Env:USERPROFILE}\\.sonar\\rule-api\\rspec"
if (! [string]::IsNullOrEmpty($Env:SONAR_USER_HOME))
{
$RspecRepositoryPath = "${Env:SONAR_USER_HOME}\\rule-api\\rspec"
}

. ${PSScriptRoot}\CopyTestCasesFromRspec.ps1

$RuleApiError = "Could not find the Rule API Jar locally. Please download the latest rule-api from " + `
"'https://repox.jfrog.io/repox/sonarsource-private-releases/com/sonarsource/rule-api/rule-api/' " +`
Expand Down Expand Up @@ -224,6 +233,7 @@ if ($ClassName -And $RuleKey) {
elseif ($Language -eq "vbnet") {
GenerateRuleClassesVB
}
CopyTestCasesFromRspec $ClassName "${RspecRepositoryPath}\\rules\\${RuleKey}\\csharp" $TestCasesFolder
UpdateRuleTypeMapping
GenerateBaseClassIfSecondLanguage
}
60 changes: 60 additions & 0 deletions scripts/rspec/tests/CopyTestCasesFromRspec.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# How to run:
# $ Invoke-Pester -Output Detailed
#
# Requires Pester 5.0.x
# https://pester.dev/docs/quick-start
# $ Install-Module -Name Pester -Force -SkipPublisherCheck
# $ Import-Module Pester -Passthru
BeforeAll {
. $PSScriptRoot/../CopyTestCasesFromRspec.ps1

$InputFolder = "TestDrive:\input"
$OutputFolder = "TestDrive:\output"
}

$FileExtension = @(".cs", ".vb", ".razor", ".cshtml")

Describe 'CopyTestCasesFromRspec - <_> files' -ForEach $FileExtension {
BeforeEach {
if (Test-Path $InputFolder)
{
Remove-Item -Force -Recurse $InputFolder
}
if (Test-Path $OutputFolder)
{
Remove-Item -Force -Recurse $OutputFolder
}

New-Item -Path "TestDrive:\" -Name "input" -ItemType "directory"
New-Item -Path "TestDrive:\" -Name "output" -ItemType "directory"
}


It 'should copy <_> file' {
New-Item -Path $InputFolder -Name "SomeTestCase$_" -ItemType "file"

CopyTestCasesFromRspec "RuleName" $InputFolder $OutputFolder

Get-ChildItem -Path $OutputFolder -File -name | Should -Contain "RuleName$_"
"$OutputFolder\RuleName$_" | Should -Exist
}

It 'should take into account composed test case name' {
New-Item -Path $InputFolder -Name "SomeTestCase.Scenario1$_" -ItemType "file"

CopyTestCasesFromRspec "RuleName" $InputFolder $OutputFolder

Get-ChildItem -Path $OutputFolder -File -name | Should -Contain "RuleName.Scenario1$_"
"$OutputFolder\RuleName.Scenario1$_" | Should -Exist
}

It 'should include file if under a folder with filename used as a scenario' {
New-Item -Path $InputFolder -Name "AFolder" -ItemType "directory"
New-Item -Path "TestDrive:\input\AFolder" -Name "Scenario1$_" -ItemType "file"

CopyTestCasesFromRspec "RuleName" $InputFolder $OutputFolder

Get-ChildItem -Path $OutputFolder -File -name | Should -Contain "RuleName.Scenario1$_"
"$OutputFolder\RuleName.Scenario1$_" | Should -Exist
}
}

0 comments on commit 38860e1

Please sign in to comment.