Skip to content

Commit e06a144

Browse files
author
Nitin Gurram
committed
Fix issue with Java tasks Code coverage functionality with TFVC
1 parent afefbcb commit e06a144

File tree

9 files changed

+116
-189
lines changed

9 files changed

+116
-189
lines changed

Tasks/ANT/ant.ps1

Lines changed: 59 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,6 @@ $buildRootPath = Split-Path $antBuildFile -Parent
111111
$reportDirectoryName = "ReportDirectory75C12DBC"
112112
$reportDirectory = Join-Path $buildRootPath $reportDirectoryName
113113

114-
try
115-
{
116-
if(Test-Path $reportDirectory)
117-
{
118-
# delete any previous code coverage data
119-
rm -r $reportDirectory -force | Out-Null
120-
}
121-
}
122-
catch
123-
{
124-
Write-Verbose "Failed to delete report directory"
125-
}
126-
127114
if($isCoverageEnabled)
128115
{
129116
if ($codeCoverageTool -eq "Cobertura")
@@ -145,61 +132,34 @@ $reportBuildFileName = "ReportBuildFile9B5907FC.xml"
145132
$reportBuildFile = Join-Path $buildRootPath $reportBuildFileName
146133
$instrumentedClassesDirectory = Join-Path $buildRootPath "InstrumentedClasses"
147134

148-
try
135+
if ($isCoverageEnabled)
149136
{
150-
if(Test-Path $reportBuildFile)
137+
Remove-Item -Recurse -Force $reportDirectory -ErrorAction SilentlyContinue
138+
Remove-Item -Recurse -Force $reportBuildFile -ErrorAction SilentlyContinue
139+
Remove-Item -Recurse -Force $instrumentedClassesDirectory -ErrorAction SilentlyContinue
140+
141+
# Create temp copy - requried in case of TFVC
142+
Copy-Item $antBuildFile "$antBuildFile.tmp" -Force -ErrorAction Continue
143+
Set-ItemProperty $antBuildFile -Name Attributes -Value Normal
144+
145+
try
151146
{
152-
# delete any previous code coverage report build file
153-
rm -r $reportBuildFile -force | Out-Null
147+
# Enable code coverage in build file
148+
if ($codeCoverageTool -eq "Cobertura")
149+
{
150+
$coberturaCCFile = Join-Path $buildRootPath "cobertura.ser"
151+
Remove-Item -Recurse -Force $coberturaCCFile -ErrorAction SilentlyContinue
152+
Remove-Item -Recurse -Force $instrumentedClassesDirectory -ErrorAction SilentlyContinue
153+
}
154+
155+
Enable-CodeCoverage -BuildTool 'Ant' -BuildFile $antBuildFile -CodeCoverageTool $codeCoverageTool -ClassFilter $classFilter -ClassFilesDirectories $classFilesDirectories -SourceDirectories $srcDirectories -SummaryFile $summaryFileName -ReportDirectory $reportDirectory -CCReportTask $CCReportTask -ReportBuildFile $reportBuildFile
156+
Write-Verbose "Code coverage is successfully enabled." -Verbose
154157
}
155-
}
156-
catch
157-
{
158-
Write-Verbose "Failed to delete report build file"
159-
}
160-
161-
try
162-
{
163-
if(Test-Path $instrumentedClassesDirectory)
158+
catch
164159
{
165-
# delete any previous instrumented classes directory
166-
rm -r $instrumentedClassesDirectory -force | Out-Null
160+
Write-Warning "Enabling code coverage failed. Check the build logs for errors" -Verbose
167161
}
168162
}
169-
catch
170-
{
171-
Write-Verbose "Failed to delete instrumented classes directory"
172-
}
173-
174-
if($isCoverageEnabled)
175-
{
176-
try
177-
{
178-
# Enable code coverage in build file
179-
if ($codeCoverageTool -eq "Cobertura")
180-
{
181-
$coberturaCCFile = Join-Path $buildRootPath "cobertura.ser"
182-
if(Test-Path $coberturaCCFile)
183-
{
184-
# delete any previous cobertura code coverage file
185-
rm -r $coberturaCCFile -force | Out-Null
186-
}
187-
188-
if(Test-Path $instrumentedClassesDirectory)
189-
{
190-
# delete any previous cobertura instrumented classes
191-
rm -r $instrumentedClassesDirectory -force | Out-Null
192-
}
193-
}
194-
195-
Enable-CodeCoverage -BuildTool 'Ant' -BuildFile $antBuildFile -CodeCoverageTool $codeCoverageTool -ClassFilter $classFilter -ClassFilesDirectories $classFilesDirectories -SourceDirectories $srcDirectories -SummaryFile $summaryFileName -ReportDirectory $reportDirectory -CCReportTask $CCReportTask -ReportBuildFile $reportBuildFile
196-
Write-Verbose "Code coverage is successfully enabled." -Verbose
197-
}
198-
catch
199-
{
200-
Write-Warning "Enabling code coverage failed. Check the build logs for errors" -Verbose
201-
}
202-
}
203163
else
204164
{
205165
Write-Verbose "Option to enable code coverage was not selected and is being skipped." -Verbose
@@ -222,19 +182,19 @@ if($publishJUnitResultsFromAntBuild)
222182
else
223183
{
224184
Write-Verbose "Calling Publish-TestResults"
225-
$runTitleMemberExists = CmdletHasMember "RunTitle"
226-
if($runTitleMemberExists)
227-
{
228-
Publish-TestResults -TestRunner "JUnit" -TestResultsFiles $matchingTestResultsFiles -Context $distributedTaskContext -RunTitle $testRunTitle -MergeResults $true
229-
}
230-
else
231-
{
232-
if(!([string]::IsNullOrWhiteSpace($testRunTitle)))
233-
{
234-
Write-Warning "Update the build agent to be able to use the custom run title feature."
235-
}
236-
Publish-TestResults -TestRunner "JUnit" -TestResultsFiles $matchingTestResultsFiles -Context $distributedTaskContext -MergeResults $true
237-
}
185+
$runTitleMemberExists = CmdletHasMember "RunTitle"
186+
if($runTitleMemberExists)
187+
{
188+
Publish-TestResults -TestRunner "JUnit" -TestResultsFiles $matchingTestResultsFiles -Context $distributedTaskContext -RunTitle $testRunTitle -MergeResults $true
189+
}
190+
else
191+
{
192+
if(!([string]::IsNullOrWhiteSpace($testRunTitle)))
193+
{
194+
Write-Warning "Update the build agent to be able to use the custom run title feature."
195+
}
196+
Publish-TestResults -TestRunner "JUnit" -TestResultsFiles $matchingTestResultsFiles -Context $distributedTaskContext -MergeResults $true
197+
}
238198
}
239199
}
240200
else
@@ -246,27 +206,26 @@ else
246206
if($isCoverageEnabled)
247207
{
248208

249-
# run report code coverage task which generates code coverage reports.
250-
$reportsGenerationFailed = $false
251-
Write-Verbose "Collecting code coverage reports" -Verbose
252-
try
253-
{
254-
if(Test-Path $reportBuildFile)
255-
{
256-
# This will handle compat between S91 and S92
257-
Invoke-Ant -AntBuildFile $reportBuildFile -Targets $CCReportTask
258-
}
259-
else
260-
{
261-
Invoke-Ant -AntBuildFile $antBuildFile -Targets $CCReportTask
262-
}
263-
}
264-
catch
265-
{
266-
$reportsGenerationFailed = $true
267-
}
209+
# run report code coverage task which generates code coverage reports.
210+
$reportsGenerationFailed = $false
211+
Write-Verbose "Collecting code coverage reports" -Verbose
212+
try
213+
{
214+
if(Test-Path $reportBuildFile)
215+
{
216+
# This will handle compat between S91 and S92
217+
Invoke-Ant -AntBuildFile $reportBuildFile -Targets $CCReportTask
218+
}
219+
else
220+
{
221+
Invoke-Ant -AntBuildFile $antBuildFile -Targets $CCReportTask
222+
}
223+
}
224+
catch
225+
{
226+
$reportsGenerationFailed = $true
227+
}
268228

269-
270229
if(-not $reportsGenerationFailed -and (Test-Path $summaryFile))
271230
{
272231
Write-Verbose "Summary file = $summaryFile" -Verbose
@@ -276,9 +235,13 @@ if($isCoverageEnabled)
276235
}
277236
else
278237
{
279-
Write-Host "##vso[task.logissue type=warning;code=006003;]"
238+
Write-Host "##vso[task.logissue type=warning;code=006003;]"
280239
Write-Warning "No code coverage results found to be published. This could occur if there were no tests executed or there was a build failure. Check the ant output for details." -Verbose
281240
}
241+
242+
# Reset temp copy and file permissions are reset by default
243+
Copy-Item "$antBuildFile.tmp" $antBuildFile -Force -ErrorAction Continue
244+
Remove-Item "$antBuildFile.tmp" -Force -ErrorAction Continue
282245
}
283246

284247
Write-Verbose "Leaving script Ant.ps1"

Tasks/ANT/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 1,
1515
"Minor": 0,
16-
"Patch": 49
16+
"Patch": 50
1717
},
1818
"demands": [
1919
"ant"

Tasks/ANT/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": {
1414
"Major": 1,
1515
"Minor": 0,
16-
"Patch": 49
16+
"Patch": 50
1717
},
1818
"demands": [
1919
"ant"

Tasks/Gradle/Gradle.ps1

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -115,64 +115,48 @@ $buildRootPath = $cwd
115115
$wrapperDirectory = Split-Path $wrapperScript -Parent
116116
$reportDirectoryName = "ReportDirectory84B7D86C"
117117
$reportDirectory = Join-Path $buildRootPath $reportDirectoryName
118+
$buildFile = Join-Path $buildRootPath "build.gradle"
118119

119-
try
120-
{
121-
if(Test-Path $reportDirectory)
122-
{
123-
# delete any code coverage data
124-
rm -r $reportDirectory -force | Out-Null
125-
}
126-
}
127-
catch
128-
{
129-
Write-Verbose "Failed to delete report directory"
130-
}
131-
132-
133-
# check if project is multi module gradle build or not
134-
$subprojects = Invoke-BatchScript -Path $wrapperScript -Arguments 'properties' -WorkingFolder $buildRootPath | Select-String '^subprojects: (.*)'|ForEach-Object {$_.Matches[0].Groups[1].Value}
135-
Write-Verbose "subprojects: $subprojects"
136-
$singlemodule = [string]::IsNullOrEmpty($subprojects) -or $subprojects -eq '[]'
137-
138-
if($codeCoverageTool -eq "JaCoCo")
120+
if ($isCoverageEnabled)
139121
{
140-
$summaryFileName = "summary.xml"
122+
Write-Verbose "Option to enable code coverage was selected and is being running." -Verbose
123+
124+
# check if project is multi module gradle build or not
125+
$subprojects = Invoke-BatchScript -Path $wrapperScript -Arguments 'properties' -WorkingFolder $buildRootPath | Select-String '^subprojects: (.*)'|ForEach-Object {$_.Matches[0].Groups[1].Value}
126+
Write-Verbose "subprojects: $subprojects"
127+
$singlemodule = [string]::IsNullOrEmpty($subprojects) -or $subprojects -eq '[]'
141128

142-
if($singlemodule)
129+
if($codeCoverageTool -eq "JaCoCo")
143130
{
144-
$reportingTaskName = "jacocoTestReport"
131+
$summaryFileName = "summary.xml"
132+
133+
if($singlemodule)
134+
{
135+
$reportingTaskName = "jacocoTestReport"
136+
}
137+
else
138+
{
139+
$reportingTaskName = "jacocoRootReport"
140+
}
145141
}
146-
else
142+
elseif($codeCoverageTool -eq "Cobertura")
147143
{
148-
$reportingTaskName = "jacocoRootReport"
144+
$summaryFileName = "coverage.xml"
145+
$reportingTaskName = "cobertura"
149146
}
150-
}
151-
elseif($codeCoverageTool -eq "Cobertura")
152-
{
153-
$summaryFileName = "coverage.xml"
154-
$reportingTaskName = "cobertura"
155-
}
156147

157-
$summaryFile = Join-Path $buildRootPath $reportDirectoryName
158-
$summaryFile = Join-Path $summaryFile $summaryFileName
159-
$buildFile = Join-Path $buildRootPath "build.gradle"
148+
$summaryFile = Join-Path $buildRootPath $reportDirectoryName
149+
$summaryFile = Join-Path $summaryFile $summaryFileName
160150

161-
# check if code coverage has been enabled
162-
if($isCoverageEnabled)
163-
{
164-
# Enable code coverage in build file
165-
Enable-CodeCoverage -BuildTool 'Gradle' -BuildFile $buildFile -CodeCoverageTool $codeCoverageTool -ClassFilter $classFilter -ClassFilesDirectories $classFilesDirectories -SummaryFile $summaryFileName -ReportDirectory $reportDirectoryName -IsMultiModule (!$singlemodule) -ErrorAction Stop
166-
Write-Verbose "Code coverage is successfully enabled." -Verbose
167-
}
168-
else
169-
{
170-
Write-Verbose "Option to enable code coverage was not selected and is being skipped." -Verbose
171-
}
151+
Remove-Item -Recurse -Force $reportDirectory -ErrorAction SilentlyContinue
152+
# Create temp copy - requried in case of TFVC
153+
Copy-Item $buildFile "$buildFile.tmp" -Force -ErrorAction Continue
154+
Set-ItemProperty $buildFile -Name Attributes -Value Normal
172155

156+
# Enable code coverage in build file
157+
Enable-CodeCoverage -BuildTool 'Gradle' -BuildFile $buildFile -CodeCoverageTool $codeCoverageTool -ClassFilter $classFilter -ClassFilesDirectories $classFilesDirectories -SummaryFile $summaryFileName -ReportDirectory $reportDirectoryName -IsMultiModule (!$singlemodule) -ErrorAction Stop
158+
Write-Verbose "Code coverage is successfully enabled." -Verbose
173159

174-
if($isCoverageEnabled)
175-
{
176160
$arguments = "$options $tasks $reportingTaskName"
177161
}
178162
else
@@ -219,7 +203,7 @@ else
219203

220204

221205
# check if code coverage has been enabled
222-
if($isCoverageEnabled)
206+
if ($isCoverageEnabled)
223207
{
224208
if(Test-Path $summaryFile)
225209
{
@@ -232,7 +216,11 @@ if($isCoverageEnabled)
232216
{
233217
Write-Host "##vso[task.logissue type=warning;code=005003;]"
234218
Write-Warning "No code coverage results found to be published. This could occur if there were no tests executed or there was a build failure. Check the gradle output for details." -Verbose
235-
}
219+
}
220+
221+
# Reset temp copy and file permissions are reset by default
222+
Copy-Item "$buildFile.tmp" $buildFile -Force -ErrorAction Continue
223+
Remove-Item "$buildFile.tmp" -Force -ErrorAction Continue
236224
}
237225

238226
Write-Verbose "Leaving script Gradle.ps1"

Tasks/Gradle/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"version": {
1313
"Major": 1,
1414
"Minor": 0,
15-
"Patch": 48
15+
"Patch": 49
1616
},
1717
"demands": [
1818
"java"

Tasks/Gradle/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"version": {
1313
"Major": 1,
1414
"Minor": 0,
15-
"Patch": 48
15+
"Patch": 49
1616
},
1717
"demands": [
1818
"java"

0 commit comments

Comments
 (0)