Skip to content

Commit

Permalink
Add E2E tests for Microsoft.Sbom.Targets project (#658)
Browse files Browse the repository at this point in the history
* add base setup for tests

* updates to test

* cleanup

* Add more tests

* update package version

* cleanup

* mini fix for copying sample project

* add unloading step

* create separate project for E2E tests

* cleanup

* rearrange method

* cleanup

* check for platform

* try with locator

* disable analyzers for sample project

---------

Co-authored-by: vpatakottu <[email protected]>
  • Loading branch information
vpatakottu and vpatakottu authored Aug 15, 2024
1 parent 5667a00 commit d0929da
Show file tree
Hide file tree
Showing 10 changed files with 463 additions and 24 deletions.
4 changes: 3 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
<PackageVersion Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Microsoft.Build" Version="17.3.2" />
<PackageVersion Include="Microsoft.Build.Framework" Version="17.10.4" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.7.8" />
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.10.4" />
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.1.1" />
Expand Down Expand Up @@ -58,4 +60,4 @@
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="4.11.1" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>
</Project>
</Project>
6 changes: 6 additions & 0 deletions Microsoft.Sbom.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Sbom.Targets.Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Sbom.Tool.Tests", "test\Microsoft.Sbom.Tool.Tests\Microsoft.Sbom.Tool.Tests.csproj", "{FC5A9799-7C44-4BFA-BA22-55DCAF1A1B9F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Sbom.Targets.E2E.Tests", "test\Microsoft.Sbom.Targets.E2E.Tests\Microsoft.Sbom.Targets.E2E.Tests.csproj", "{3FDE7800-F61F-4C45-93AB-648A4C7979C7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -125,6 +127,10 @@ Global
{FC5A9799-7C44-4BFA-BA22-55DCAF1A1B9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC5A9799-7C44-4BFA-BA22-55DCAF1A1B9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC5A9799-7C44-4BFA-BA22-55DCAF1A1B9F}.Release|Any CPU.Build.0 = Release|Any CPU
{3FDE7800-F61F-4C45-93AB-648A4C7979C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3FDE7800-F61F-4C45-93AB-648A4C7979C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3FDE7800-F61F-4C45-93AB-648A4C7979C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3FDE7800-F61F-4C45-93AB-648A4C7979C7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.Sbom.Targets/Microsoft.Sbom.Targets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<AssemblyName>Microsoft.Sbom.Targets</AssemblyName>
<TargetFrameworks>net8.0;net472</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0;net472</TargetFrameworks>
<RuntimeIdentifiers>win-x64;osx-x64;linux-x64</RuntimeIdentifiers>
<IsPublishable>true</IsPublishable>
<IsPackable>true</IsPackable>
Expand Down Expand Up @@ -68,8 +68,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Condition="'$(TargetFramework)'=='net8.0'" Include="..\Microsoft.Sbom.Extensions.DependencyInjection\Microsoft.Sbom.Extensions.DependencyInjection.csproj" PrivateAssets="all" />
<ProjectReference Condition="'$(TargetFramework)'=='net8.0'" Include="..\Microsoft.Sbom.Tool\Microsoft.Sbom.Tool.csproj" PrivateAssets="all" IncludeAssets="output" />
<ProjectReference Condition="'$(TargetFramework)'=='net8.0' or '$(TargetFramework)'=='net6.0'" Include="..\Microsoft.Sbom.Extensions.DependencyInjection\Microsoft.Sbom.Extensions.DependencyInjection.csproj" PrivateAssets="all" />
<ProjectReference Condition="'$(TargetFramework)'=='net8.0' or '$(TargetFramework)'=='net6.0'" Include="..\Microsoft.Sbom.Tool\Microsoft.Sbom.Tool.csproj" PrivateAssets="all" IncludeAssets="output" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
Expand Down
32 changes: 14 additions & 18 deletions src/Microsoft.Sbom.Targets/Microsoft.Sbom.Targets.targets
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- TODO remove hardcoded str, reference this assembly using MSBuild props instead -->

<!--TODO we will eventaully want to replace or remove `AfterTargets="Build"`.
Including this line means that this code will run at the end of `dotnet build`s of any project that this .targets
file is imported to (see line 24 of Microsoft.Sbom.Targets.csproj as an example of importing this file). This is
good for testing purposes, but not the behavior we ultimately want.
We will need to discuss with the .NET SDK team if they want this target to be automatically included after any
target and if so, which one. They likely know of a nuget packaging target that it would make sense to hook this in with.-->

<PropertyGroup>
<GenerateSbom_TFM Condition=" '$(MSBuildRuntimeType)' == 'Full' ">net472</GenerateSbom_TFM>
<GenerateSbom_TFM Condition=" '$(MSBuildRuntimeType)' == 'Core' ">net8.0</GenerateSbom_TFM>

<SbomToolBinaryOutputPath>$([System.IO.Path]::Combine($(MSBuildThisFileDirectory),..,tasks,$(GenerateSbom_TFM),sbom-tool))</SbomToolBinaryOutputPath>
<AssemblyFilePath>$([System.IO.Path]::Combine($(MSBuildThisFileDirectory),..,tasks,$(GenerateSbom_TFM),Microsoft.Sbom.Targets.dll))</AssemblyFilePath>
<SbomToolBinaryOutputPath Condition="'$(SbomToolBinaryOutputPath)' == ''">$([System.IO.Path]::Combine($(MSBuildThisFileDirectory),..,tasks,$(GenerateSbom_TFM),sbom-tool))</SbomToolBinaryOutputPath>
<GenerateSbomTaskAssemblyFilePath Condition="'$(GenerateSbomTaskAssemblyFilePath)' == ''">$([System.IO.Path]::Combine($(MSBuildThisFileDirectory),..,tasks,$(GenerateSbom_TFM),Microsoft.Sbom.Targets.dll))</GenerateSbomTaskAssemblyFilePath>

<!--Set the SBOM CLI Tool path. This variable is only used in SbomCLIToolTask.cs-->
<SbomToolPath Condition=" '$(MSBuildRuntimeType)' == 'Full'">$(SbomToolBinaryOutputPath)</SbomToolPath>
Expand All @@ -22,7 +13,7 @@
</PropertyGroup>

<!--Based on the MSBuild runtime, GenerateSbom will either pull the GenerateSbomTask or SbomCLIToolTask logic-->
<UsingTask TaskName="Microsoft.Sbom.Targets.GenerateSbom" AssemblyFile="$(AssemblyFilePath)" />
<UsingTask TaskName="Microsoft.Sbom.Targets.GenerateSbom" AssemblyFile="$(GenerateSbomTaskAssemblyFilePath)" />

<PropertyGroup>
<GenerateSBOM Condition=" '$(GenerateSBOM)' == '' ">false</GenerateSBOM>
Expand All @@ -40,24 +31,29 @@
<SbomGenerationManifestInfo Condition=" '$(SbomGenerationManifestInfo)' == '' ">SPDX:2.2</SbomGenerationManifestInfo>
<SbomGenerationDeleteManifestDirIfPresent Condition=" '$(SbomGenerationDeleteManifestDirIfPresent)' == '' ">true</SbomGenerationDeleteManifestDirIfPresent>
<UnzipGuid>$([System.Guid]::NewGuid())</UnzipGuid>
<ShortUnzipGuidFolder>$([System.String]::Copy('$(UnzipGuid)').Substring(0, 8))</ShortUnzipGuidFolder>
</PropertyGroup>

<!-- After the Nuget Package is generated, we will unzip, scan, generate the SBOM and zip again. -->
<Target Name="GenerateSbomTarget" AfterTargets="Pack" Condition=" '$(GenerateSBOM)' == 'true'" >
<Error Condition="'$(BuildOutputTargetFolder)' == ''" Text="The GenerationSbomTarget requires the BuildOutputTargetFolder property to be non-null. Please set a folder name."/>

<!-- Unzip Nuget package, so it can be scanned by the SBOM Task. -->
<PropertyGroup>
<PackageOutputFullPath>$([System.IO.Path]::GetFullPath('$(PackageOutputPath)'))</PackageOutputFullPath>

<NugetPackage>
$([System.IO.Path]::Combine($(PackageOutputPath), $(PackageId).$(PackageVersion).nupkg))
$([System.IO.Path]::Combine($(PackageOutputFullPath), $(PackageId).$(PackageVersion).nupkg))
</NugetPackage>
<NugetPackageUnzip>
$([System.IO.Path]::Combine($(PackageOutputPath), $(PackageId).$(PackageVersion).$(UnzipGuid).temp))
$([System.IO.Path]::Combine($(PackageOutputFullPath), $(PackageId).$(PackageVersion).$(ShortUnzipGuidFolder).temp))
</NugetPackageUnzip>
</PropertyGroup>
<Unzip DestinationFolder="$(NugetPackageUnzip)" SourceFiles="$(NugetPackage)" OverwriteReadOnlyFiles="true" />

<!-- Call the SBOM Task to generate a SBOM. -->
<!--
Call the SBOM task to generate a SBOM. The SBOM will be generated at the BuildDropPath location, which is the root folder
of the NuGet package. NOTE: Currently, the ManifestDirPath is an optional parameter that will not be supported, as the desired location of the SBOM will be
at the BuildDropPath directory. However, this parameter is available for future revision and implementation.
-->
<GenerateSbom
BuildDropPath="$(NugetPackageUnzip)"
BuildComponentPath="$(SbomGenerationBuildComponentPath)"
Expand All @@ -72,7 +68,7 @@
Verbosity="$(SbomGenerationVerbosity)"
ManifestInfo="$(SbomGenerationManifestInfo)"
DeleteManifestDirIfPresent="$(SbomGenerationDeleteManifestDirIfPresent)"
ManifestDirPath="$(SbomGenerationManifestDirPath)"
ManifestDirPath=""
SbomToolPath="$(SbomToolPath)">
<Output TaskParameter="SbomPath" PropertyName="SbomPathResult" />
</GenerateSbom>
Expand Down
Loading

0 comments on commit d0929da

Please sign in to comment.