Skip to content

Finish Unit Test Angular Implementations and Configurations #21

Finish Unit Test Angular Implementations and Configurations

Finish Unit Test Angular Implementations and Configurations #21

name: Test and Analysis Code In Sonar Cloud
on:
workflow_call:
push:
branches:
- hotfix/*
- feature/*
- bugfix/*
pull_request:
types: [opened, synchronize, reopened]
branches:
- hotfix/*
- feature/*
- bugfix/*
jobs:
test_code_in_sonar_net:
name: Test Code in Sonar Cloud
runs-on: windows-latest
steps:
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'zulu'
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Cache SonarCloud packages
id: cache-sonar
uses: actions/cache@v3
with:
path: ~\sonar\cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Create if not Exist Cache Sonar Packages
if: steps.cache-sonar.outputs.cache-hit != 'true'
shell: powershell
run: |
New-Item -Path ~\sonar\cache -ItemType Directory
- name: Cache SonarCloud scanner
id: cache-sonar-scanner
uses: actions/cache@v3
with:
path: .\.sonar\scanner
key: ${{ runner.os }}-sonar-scanner
restore-keys: ${{ runner.os }}-sonar-scanner
- name: Install SonarCloud scanner
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
shell: powershell
run: |
New-Item -Path ./.sonar/scanner -ItemType Directory
dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
shell: powershell
run: |
./.sonar/scanner/dotnet-sonarscanner begin /k:"alexfariakof_Home_Broker_Chart" /o:"alexfariakof" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml" /d:sonar.exclusions="HomeBrokerXUnit/**, **/.documents/**, **/Program.cs, **/*.spec.ts, **/main.ts, **/environment.*" /d:sonar.cs.lcov.reportPaths="**/coveragereport/lcov.info" /d:sonar.typescript.opencover.reportsPaths="**/clover.xml" /d:sonar.typescript.lcov.reportPaths="**/coverage/lcov.info"
# Executa Build Restore com Teste e coleta o GUID gerado
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --collect:"XPlat Code Coverage;Format=opencover"
# Encontra o diretório mais recente na pasta TestResults
$latestDir = Get-ChildItem -Directory -Path .\HomeBrokerXUnit\TestResults | Sort-Object LastWriteTime -Descending | Select-Object -First 1
$sourceDirs = Join-Path -Path (Get-Location) -ChildPath "Business"; Join-Path -Path (Get-Location) -ChildPath "Domain"; Join-Path -Path (Get-Location) -ChildPath "Repository"; Join-Path -Path (Get-Location) -ChildPath "webapi"; Join-Path -Path (Get-Location) -ChildPath "angularapp";
# Verifica se encontrou um diretório e, em caso afirmativo, obtém o nome do diretório (GUID)
if ($latestDir -ne $null) {
$guid = $latestDir.Name
# Constrói os caminhos dinamicamente
$baseDirectory = Join-Path -Path (Get-Location) -ChildPath "HomeBrokerXUnit"
$coverageXmlPath = Join-Path -Path (Join-Path -Path $baseDirectory -ChildPath "TestResults") -ChildPath $guid
# Gera o relatório de cobertura usando o GUID capturado
dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator -reports:$baseDirectory\coverage.cobertura.xml -targetdir:$coverageXmlPath\coveragereport -reporttypes:"lcov;" -sourcedirs:$sourceDirs
}
cd angularapp
npm install npm@latest > $null 2>&1
npm install -g @angular/cli > $null 2>&1
npm install > $null 2>&1
npm run test:coverage
cd ..
./.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"