Skip to content

Commit

Permalink
Simplify the use of the AltCover MSBuild tasks via the associated pac…
Browse files Browse the repository at this point in the history
…kage-level `.targets` file
  • Loading branch information
SteveGilham committed Apr 9, 2024
1 parent 3aacf27 commit e58cc3a
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 222 deletions.
2 changes: 1 addition & 1 deletion AltCover.Api.Tests/AltCover.Api.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="..\nupkg\build\AltCover.targets" Link="AltCover.targets" />
<EmbeddedResource Include="..\nupkg\build\AltCover.proj" Link="AltCover.proj" />
<EmbeddedResource Include="..\AltCover.Tests\CompressBoth.xml" Link="CompressBoth.xml" />
<EmbeddedResource Include="..\AltCover.Tests\Compressible.xml" Link="Compressible.xml" />
<EmbeddedResource Include="..\AltCover.Tests\CompressInterior.xml" Link="CompressInterior.xml" />
Expand Down
2 changes: 1 addition & 1 deletion AltCover.Api.Tests/FSApiTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ module FSApiTests =
Assembly
.GetExecutingAssembly()
.GetManifestResourceNames()
|> Seq.find _.EndsWith("AltCover.targets", StringComparison.Ordinal)
|> Seq.find _.EndsWith("AltCover.proj", StringComparison.Ordinal)

use stream =
Assembly
Expand Down
2 changes: 1 addition & 1 deletion AltCover.Tests/AltCover.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<EmbeddedResource Include="AltCover.Usage.txt" />
<EmbeddedResource Include="AltCover.Runner.Usage.txt" />
<EmbeddedResource Include="coverage-04.xsd" />
<EmbeddedResource Include="..\nupkg\build\AltCover.targets" Link="AltCover.targets" />
<EmbeddedResource Include="..\nupkg\build\AltCover.proj" Link="AltCover.proj" />
<EmbeddedResource Include="HandRolledMonoCoverage.xml" />
<EmbeddedResource Include="Sample1WithOpenCover.xml" />
<EmbeddedResource Include="Sample1ClassExclusion.xml" />
Expand Down
2 changes: 1 addition & 1 deletion AltCover.Tests/Runner.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ module AltCoverRunnerTests =
Assembly
.GetExecutingAssembly()
.GetManifestResourceNames()
|> Seq.find _.EndsWith("AltCover.targets", StringComparison.Ordinal)
|> Seq.find _.EndsWith("AltCover.proj", StringComparison.Ordinal)

use stream =
Assembly
Expand Down
2 changes: 1 addition & 1 deletion AltCover.Tests/Tests3.fs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ module AltCoverTests3 =
Assembly
.GetExecutingAssembly()
.GetManifestResourceNames()
|> Seq.find _.EndsWith("AltCover.targets", StringComparison.Ordinal)
|> Seq.find _.EndsWith("AltCover.proj", StringComparison.Ordinal)

use stream =
Assembly
Expand Down
1 change: 1 addition & 0 deletions AltCover.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build Items", "Build Items"
Build\actions.fs = Build\actions.fs
Build\AddStrongName.fsx = Build\AddStrongName.fsx
Build\AltCover.nuspec = Build\AltCover.nuspec
nupkg\build\AltCover.proj = nupkg\build\AltCover.proj
nupkg\build\AltCover.props = nupkg\build\AltCover.props
nupkg\build\AltCover.targets = nupkg\build\AltCover.targets
Build\Apply-Xslt.ps1 = Build\Apply-Xslt.ps1
Expand Down
1 change: 1 addition & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ read the FAQ : https://github.com/SteveGilham/altcover/wiki/FAQ

# (Habu series release 28)
* [BREAKING; BUGFIX] Issue #206 : Update to net6+ for `dotnet test` integration and respect the `$(IsTestProject)` setting from the `Microsoft.NET.Test.Sdk` package.
* Simplify the use of the AltCover MSBuild tasks via the associated package-level `.targets` file by not even including the `VSTest` integration unless both `'$(AltCover)' == 'true' AND '$(IsTestProject)' == 'true'`.
* Mitigate instances of `System.IO.IOException: The process cannot access the file '[coverage report]' because it is being used by another process.`

# 8.8.10 (Habu series release 27)
Expand Down
220 changes: 220 additions & 0 deletions nupkg/build/AltCover.proj
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<AltCoverInPlace Condition="'$(AltCoverInPlace)' == ''">false</AltCoverInPlace>
<AltCoverVerbosity Condition="'$(AltCoverVerbosity)' == ''">Info</AltCoverVerbosity>
<AltCoverPreFlightCheck Condition="Exists('$(AltCoverSavedDirectory)_$(ProjectName)') AND '$(AltCoverInPlace)' != 'false'">Fail</AltCoverPreFlightCheck>
<AltCoverPreFlightCheck Condition="!Exists('$(AltCoverSavedDirectory)_$(ProjectName)') OR '$(AltCoverInPlace)' == 'false'">OK</AltCoverPreFlightCheck>
</PropertyGroup>

<Target Name="AltCoverPreFlight" Condition="'$(AltCover)' == 'true' AND '$(AltCoverInPlace)' == 'false'">
<Message Condition="Exists('$(AltCoverInstrumentedDirectory)_$(ProjectName)') AND '$(AltCoverForce)' == 'true'"
Importance="High"
Text="Directory '$(AltCoverInstrumentedDirectory)_$(ProjectName)' exists from a previous run. Forcibly deleting it" />
<RemoveDir Directories="$(AltCoverInstrumentedDirectory)_$(ProjectName)"
Condition="Exists('$(AltCoverInstrumentedDirectory)_$(ProjectName)') AND '$(AltCoverForce)' == 'true'" />
</Target>

<Target Name="AltCoverPreFlight" Condition="'$(AltCover)' == 'true' AND '$(AltCoverInPlace)' != 'false'">
<Error Condition="Exists('$(AltCoverSavedDirectory)_$(ProjectName)') AND '$(AltCoverForce)' != 'true'"
Text="Directory '$(AltCoverSavedDirectory)_$(ProjectName)' exists from a previous run. Either manually examine and delete it, or run with '/p:AltCoverForce=true' to force deletion" />
<Warning Condition="Exists('$(AltCoverSavedDirectory)_$(ProjectName)') AND '$(AltCoverForce)' == 'true'"
Text="Directory '$(AltCoverSavedDirectory)_$(ProjectName)' exists from a previous run. Forcibly deleting it" />
<RemoveDir Directories="$(AltCoverSavedDirectory)_$(ProjectName)"
Condition="Exists('$(AltCoverSavedDirectory)_$(ProjectName)') AND '$(AltCoverForce)' == 'true'" />
</Target>

<Target Name="AltCoverRunPreparation" Condition="'$(AltCover)' == 'true'">
<ItemGroup>
<AltCoverXmlOutput Include="$([MSBuild]::Unescape($(AltCoverReport)))" />
<AltCoverInputDirectory Include="$(TargetDir)" />
<AltCoverOutputDirectory Condition="'$(AltCoverInPlace)' == 'false'" Include="$(AltCoverInstrumentedDirectory)_$(ProjectName)" />
<AltCoverOutputDirectory Condition="'$(AltCoverInPlace)' != 'false'" Include="$(AltCoverSavedDirectory)_$(ProjectName)" />
</ItemGroup>

<PropertyGroup>
<AltCoverDollarChar>$</AltCoverDollarChar>
<AltCoverProjectName>$(AltCoverDollarChar)(ProjectName)</AltCoverProjectName>
<AltCoverSolutionDir>$(AltCoverDollarChar)(SolutionDir)</AltCoverSolutionDir>
<AltCoverNewGuid>$(AltCoverDollarChar)([System.Guid]::NewGuid())</AltCoverNewGuid>
<AltCoverReport Condition="'$(AltCoverReport)' != '' AND '$(TargetFrameworks)' != ''">%(AltCoverXmlOutput.RootDir)/%(AltCoverXmlOutput.Directory)/%(AltCoverXmlOutput.Filename).$(TargetFramework)%(AltCoverXmlOutput.Extension)</AltCoverReport>
<AltCoverReport Condition="'$(AltCoverReport)' == '' AND '$(TargetFrameworks)' == ''">$(ProjectDir)coverage$(AltCoverFileExtension)</AltCoverReport>
<AltCoverReport Condition="'$(AltCoverReport)' == '' AND '$(TargetFrameworks)' != ''">$(ProjectDir)coverage.$(TargetFramework)$(AltCoverFileExtension)</AltCoverReport>
<AltCoverShowStatic Condition="'$(AltCoverShowStatic' != '+' AND '$(AltCoverShowStatic' != '++'">-</AltCoverShowStatic>
<AltCoverReportFormat Condition="'$(AltCoverReportFormat)' == ''">OpenCover</AltCoverReportFormat>
</PropertyGroup>

<PropertyGroup>
<AltCoverReport1>$(AltCoverReport.Replace($(AltCoverProjectName),$(ProjectName)).Replace($(AltCoverNewGuid),$([System.Guid]::NewGuid().ToString())))</AltCoverReport1>
<AltCoverReport2 Condition="'$(SolutionDir)' == ''">$(AltCoverReport1.Replace($(AltCoverSolutionDir),'')</AltCoverReport2>
<AltCoverReport2 Condition="'$(SolutionDir)' != ''">$(AltCoverReport1.Replace($(AltCoverSolutionDir),$(SolutionDir)))</AltCoverReport2>
</PropertyGroup>

<AltCover.PowerShell Condition="'$(AltCoverImportModule)' == 'true'" />
<AltCover.GetVersion Condition="'$(AltCoverGetVersion)' == 'true'" />
<AltCover.Prepare
InputDirectories="@(AltCoverInputDirectory)"
OutputDirectories="@(AltCoverOutputDirectory)"
InPlace="$(AltCoverInPlace)"
SymbolDirectories="$(AltCoverSymbolDirectories.Split('|'))"
Dependencies="$(AltCoverDependencyList.Trim('|').Split('|'))"
Keys="$(AltCoverKeys.Split('|'))"
StrongNameKey="$(AltCoverStrongNameKey)"
Report="$(AltCoverReport2)"
FileFilter="$(AltCoverFileFilter.Replace('\',%00).Replace('||',%01).Split('|'))"
AssemblyFilter="$(AltCoverAssemblyFilter.Replace('\',%00).Replace('||',%01).Split('|'))"
AssemblyExcludeFilter="$(AltCoverAssemblyExcludeFilter.Replace('\',%00).Replace('||',%01).Split('|'))"
TypeFilter="$(AltCoverTypeFilter.Replace('\',%00).Replace('||',%01).Split('|'))"
MethodFilter="$(AltCoverMethodFilter.Replace('\',%00).Replace('||',%01).Split('|'))"
AttributeFilter="$(AltCoverAttributeFilter.Replace('\',%00).Replace('||',%01).Split('|'))"
PathFilter="$(AltCoverPathFilter.Replace('\',%00).Replace('||',%01).Split('|'))"
AttributeTopLevel="$(AltCoverAttributeTopLevel.Replace('\',%00).Replace('||',%01).Split('|'))"
TypeTopLevel="$(AltCoverTypeTopLevel.Replace('\',%00).Replace('||',%01).Split('|'))"
MethodTopLevel="$(AltCoverMethodTopLevel.Replace('\',%00).Replace('||',%01).Split('|'))"
ReportFormat="$(AltCoverReportFormat)"
CallContext="$(AltCoverCallContext.Split('|'))"
ZipFile="$(AltCoverZipFile)"
MethodPoint="$(AltCoverMethodPoint)"
SingleVisit="$(AltCoverSingle)"
LineCover="$(AltCoverLineCover)"
BranchCover="$(AltCoverBranchCover)"
SourceLink="$(AltCoverSourceLink)"
LocalSource="$(AltCoverLocalSource)"
VisibleBranches="$(AltCoverVisibleBranches)"
ShowStatic="$(AltCoverShowStatic)"
ShowGenerated="$(AltCoverShowGenerated)"
Verbosity="$(AltCoverVerbosity)"
Trivia="$(AltCoverTrivia)" />
</Target>

<!-- Override Microsoft.TestPlatform.targets to do coverage work -->
<Target Name="VSTest" DependsOnTargets="ShowInfoMessageIfProjectHasNoIsTestProjectProperty">
<CallTarget Targets="AltCoverVSTestPreFlight" ContinueOnError="ErrorAndStop" />
<CallTarget Targets="AltCoverVSTestCore" />
</Target>

<!-- As per the first part of Microsoft.TestPlatform.targets v2.1.300, but with pre-flight checks clause -->
<Target Name="AltCoverVSTestPreFlight">
<CallTarget Targets="AltCoverPreFlight" Condition="'$(IsTestProject)' == 'true'" />
<CallTarget Condition="'$(VSTestNoBuild)' != 'true' AND '$(IsTestProject)' == 'true'" Targets="BuildProject" />
<CallTarget Condition="'$(IsTestProject)' != 'true'" Targets="ShowCallOfVSTestTaskWithParameter" />
<CallTarget Condition="'$(IsTestProject)' == 'true'" Targets="AltCoverRunPreparation" ContinueOnError="ErrorAndStop" />
</Target>

<!-- As per the second part Microsoft.TestPlatform.targets v2.1.809, but with OnError clause -->
<Target Name="AltCoverVSTestCore">
<PropertyGroup>
<AltCoverTestTargetPath Condition="'$(AltCover)' != 'true' OR '$(AltCoverInPlace)' != 'false'">$(TargetPath)</AltCoverTestTargetPath>
<AltCoverTestTargetPath Condition="'$(AltCover)' == 'true' AND '$(AltCoverInPlace)' == 'false'">$([System.IO.Path]::Combine($(AltCoverInstrumentedDirectory)_$(ProjectName), $(TargetFileName)))</AltCoverTestTargetPath>
</PropertyGroup>

<ItemGroup>
<AltCoverBuildItem Include="@(ApplicationDefinition)" />
<AltCoverBuildItem Include="@(EditorConfigFiles)" />
<AltCoverBuildItem Include="@(XamlAppDef)" />
<AltCoverBuildItem Include="@(DesignData)" />
<AltCoverBuildItem Include="@(CodeAnalysisDictionary)" />
<AltCoverBuildItem Include="@(None)" />
<AltCoverBuildItem Include="@(Resource)" />
<AltCoverBuildItem Include="@(EntityDeploy)" />
<AltCoverBuildItem Include="@(Compile)" />
<AltCoverBuildItem Include="@(SplashScreen)" />
<AltCoverBuildItem Include="@(Content)" />
<AltCoverBuildItem Include="@(EmbeddedResource)" />
<AltCoverBuildItem Include="@(Page)" />
<AltCoverBuildItem Include="@(DesignDataWithDesignTimeCreatableTypes)" />
<AltCoverBuildItem Include="@(AdditionalFiles)" />
</ItemGroup>

<AltCover.ContingentCopy
Condition="'$(AltCoverTestTargetPath)' != '$(TargetPath)' AND ('%(AltCoverBuildItem.CopyToOutputDirectory)' == 'Always' OR '%(AltCoverBuildItem.CopyToOutputDirectory)' == 'PreserveNewest' )"
RelativeDir="%(AltCoverBuildItem.RelativeDir)"
ProjectDir="$(ProjectDir)"
CopyToOutputDirectory="%(AltCoverBuildItem.CopyToOutputDirectory)"
FileName="%(AltCoverBuildItem.FullPath)"
BuildOutputDirectory="$(TargetDir)"
InstrumentDirectory="$(AltCoverInstrumentedDirectory)_$(ProjectName)" />

<AltCover.RunSettings
TestSetting="$(VSTestSetting)"
Verbosity="$(AltCoverVerbosity)">
<Output TaskParameter="Extended" PropertyName="VSTestSetting" />
</AltCover.RunSettings>

<CallTarget Targets="ShowCallOfVSTestTaskWithParameter" />

<Microsoft.TestPlatform.Build.Tasks.VSTestTask
TestFileFullPath="$(AltCoverTestTargetPath)"
VSTestSetting="$([MSBuild]::ValueOrDefault($(VSTestSetting), '$(RunSettingsFilePath)'))"
VSTestTestAdapterPath="$(VSTestTestAdapterPath)"
VSTestFramework="$(TargetFrameworkMoniker)"
VSTestPlatform="$(PlatformTarget)"
VSTestTestCaseFilter="$(VSTestTestCaseFilter)"
VSTestLogger="$(VSTestLogger)"
VSTestListTests="$(VSTestListTests)"
VSTestDiag="$(VSTestDiag)"
VSTestCLIRunSettings="$(VSTestCLIRunSettings)"
VSTestConsolePath="$(VSTestConsolePath)"
VSTestResultsDirectory="$(VSTestResultsDirectory)"
VSTestVerbosity="$(VSTestVerbosity)"
VSTestCollect="$(VSTestCollect)"
VSTestBlame="$(VSTestBlame)"
VSTestBlameCrash="$(VSTestBlameCrash)"
VSTestBlameCrashDumpType="$(VSTestBlameCrashDumpType)"
VSTestBlameCrashCollectAlways="$(VSTestBlameCrashCollectAlways)"
VSTestBlameHang="$(VSTestBlameHang)"
VSTestBlameHangDumpType="$(VSTestBlameHangDumpType)"
VSTestBlameHangTimeout="$(VSTestBlameHangTimeout)"
VSTestTraceDataCollectorDirectoryPath="$(TraceDataCollectorDirectoryPath)"
VSTestArtifactsProcessingMode="$(VSTestArtifactsProcessingMode)"
VSTestSessionCorrelationId="$(VSTestSessionCorrelationId)"
VSTestNoLogo="$(VSTestNoLogo)"
Condition="'$(IsTestProject)' == 'true'" />

<OnError ExecuteTargets="AltCoverGenerateCoverageResult" Condition="'$(AltCoverFailFast)' != 'true'" />
<OnError ExecuteTargets="AltCoverTidy" Condition="'$(AltCoverFailFast)' == 'true'" />
</Target>

<Target Name="AltCoverDeleteTempRunSettingsFile">
<Message Text="Deleting $(VSTestSetting)" />
<Delete Files="$(VSTestSetting)">
<Output TaskParameter="DeletedFiles" ItemName="AltCoverDeletedList" />
</Delete>
<Message Text="Deleted files: '@(AltCoverDeletedList)'" />
</Target>

<Target Name="AltCoverTidy">
<ItemGroup>
<AltCoverRecorderGFiles Include="$(TargetDir)/AltCover.Recorder.g.*" />
<AltCoverSavedFiles Include="$(AltCoverSavedDirectory))_$(ProjectName)/*.*" />
</ItemGroup>
<CallTarget Targets="AltCoverDeleteTempRunSettingsFile"
Condition="$(VSTestSetting.EndsWith('.altcover.runsettings')) AND Exists($(VSTestSetting))" />
<Move SourceFiles="@(AltCoverSavedFiles)" DestinationFolder="$(TargetDir)" OverwriteReadOnlyFiles="true" Condition="'$(AltCoverInPlace)' != 'false'" />
<RemoveDir Directories="$(AltCoverSavedDirectory)_$(ProjectName)" Condition="Exists('$(AltCoverSavedDirectory))_$(ProjectName)') AND '$(AltCoverInPlace)' != 'false'" />
<AltCover.RetryDelete Files="@(AltCoverRecorderGFiles)" Condition="'$(AltCoverInPlace)' != 'false'" />
</Target>

<Target Name="AltCoverGenerateCoverageResult" AfterTargets="VSTest" Condition="'$(AltCover)' == 'true'">
<PropertyGroup>
<AltCoverColour Condition="'$(AltCoverShowSummary)' != ''">$(AltCoverShowSummary)</AltCoverColour>
<AltCoverColour Condition="'$(AltCoverShowSummary)' == ''">Foreground</AltCoverColour>
<AltCoverRecorderDirectory Condition="'$(AltCover)' != 'true' OR '$(AltCoverInPlace)' != 'false'">$(TargetDir)</AltCoverRecorderDirectory>
<AltCoverRecorderDirectory Condition="'$(AltCover)' == 'true' AND '$(AltCoverInPlace)' == 'false'">$(AltCoverInstrumentedDirectory)_$(ProjectName)</AltCoverRecorderDirectory>
</PropertyGroup>

<AltCover.Collect
RecorderDirectory="$(AltCoverRecorderDirectory)"
LcovReport="$(AltCoverLcovReport)"
Threshold="$(AltCoverThreshold)"
Cobertura="$(AltCoverCobertura)"
SummaryFormat="$(AltCoverSummaryFormat)"
Verbosity="$(AltCoverVerbosity)">
<Output TaskParameter="Summary" PropertyName="AltCoverSummary" />
</AltCover.Collect>
<AltCover.Echo Condition="'$(AltCoverShowSummary)' != '' AND '$(AltCoverSummary)' !=''"
Text="$(AltCoverSummary)" Colour="$(AltCoverColour)"
Verbosity="$(AltCoverVerbosity)" />
<CallTarget Targets="AltCoverTidy" />
</Target>
</Project>
Loading

0 comments on commit e58cc3a

Please sign in to comment.