Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User/gustavoca/dont extract e2e tests #684

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<PackageVersion Include="Serilog.Sinks.Map" Version="1.0.2" />
<PackageVersion Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
Expand Down
50 changes: 19 additions & 31 deletions test/Microsoft.Sbom.Targets.E2E.Tests/GenerateSbomE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.Sbom.Targets.E2E.Tests;
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Locator;
Expand All @@ -31,7 +32,6 @@ public class GenerateSbomE2ETests
private static string sbomSpecificationName = "SPDX";
private static string sbomSpecificationVersion = "2.2";
private static string sbomSpecificationDirectoryName = $"{sbomSpecificationName}_{sbomSpecificationVersion}".ToLowerInvariant();
private string manifestPath;
private string expectedPackageName;
private string expectedVersion;
private string expectedSupplier;
Expand Down Expand Up @@ -128,21 +128,24 @@ private void RestoreBuildPack(Project sampleProject)
Assert.IsTrue(pack, "Failed to pack the project");
}

private void ExtractPackage()
private void InspectPackageIsWellFormed(bool isManifestPathGenerated = true)
{
const string backSlash = "\\";
const string forwardSlash = "/";
// Unzip the contents of the NuGet package
var nupkgPath = Path.Combine(projectDirectory, "bin", configuration);
var nupkgFile = Path.Combine(nupkgPath, $"{expectedPackageName}.{expectedVersion}.nupkg");
var zipFile = Path.Combine(nupkgPath, $"{expectedPackageName}.{expectedVersion}.zip");
var extractPath = Path.Combine(projectDirectory, "bin", configuration, $"{Guid.NewGuid()}.temp");
var manifestRelativePath = Path.Combine("_manifest", sbomSpecificationDirectoryName, "manifest.spdx.json")
.Replace(backSlash, forwardSlash);

// Rename the .nupkg file to .zip
File.Copy(nupkgFile, zipFile, true);

// Extract the .zip file
ZipFile.ExtractToDirectory(zipFile, extractPath);

manifestPath = Path.Combine(extractPath, "_manifest", sbomSpecificationDirectoryName, "manifest.spdx.json");
// Check the content of the NuGet package
using (var archive = ZipFile.Open(nupkgFile, ZipArchiveMode.Read))
{
Assert.IsTrue(archive.Entries.Count() > 0);
// Nuget's zip code expects forward slashes as path separators.
Assert.IsTrue(archive.Entries.All(entry => !entry.FullName.Contains(backSlash)));
Assert.AreEqual(isManifestPathGenerated, archive.Entries.Any(entry => entry.FullName.Equals(manifestRelativePath)));
}
}

[TestMethod]
Expand All @@ -161,10 +164,7 @@ public void SbomGenerationSucceedsForDefaultProperties()
RestoreBuildPack(sampleProject);

// Extract the NuGet package
ExtractPackage();

// Validate the SBOM exists in the package.
Assert.IsTrue(File.Exists(manifestPath));
InspectPackageIsWellFormed();
}

[TestMethod]
Expand All @@ -187,10 +187,7 @@ public void SbomGenerationSucceedsForValidNamespaceBaseUriUniquePart()
RestoreBuildPack(sampleProject);

// Extract the NuGet package
ExtractPackage();

// Validate the SBOM exists in the package.
Assert.IsTrue(File.Exists(manifestPath));
InspectPackageIsWellFormed();
}

[TestMethod]
Expand Down Expand Up @@ -222,10 +219,7 @@ public void SbomGenerationSucceedsForValidRequiredParams()
RestoreBuildPack(sampleProject);

// Extract the NuGet package
ExtractPackage();

// Validate the SBOM exists in the package.
Assert.IsTrue(File.Exists(manifestPath));
InspectPackageIsWellFormed();
}

[TestMethod]
Expand Down Expand Up @@ -312,10 +306,7 @@ public void SbomGenerationSkipsForUnsetGenerateSBOMFlag()
RestoreBuildPack(sampleProject);

// Extract the NuGet package
ExtractPackage();

// Ensure the manifest file was not created
Assert.IsTrue(!File.Exists(manifestPath));
InspectPackageIsWellFormed(isManifestPathGenerated: false);
}

[TestMethod]
Expand All @@ -338,9 +329,6 @@ public void SbomGenerationSucceedsForMultiTargetedProject()
RestoreBuildPack(sampleProject);

// Extract the NuGet package
ExtractPackage();

// Validate the SBOM exists in the package.
Assert.IsTrue(File.Exists(manifestPath));
InspectPackageIsWellFormed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="Runtime" />
<PackageReference Include="Microsoft.Build.Locator" />
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="Runtime" />
<PackageReference Include="System.IO.Compression" />
</ItemGroup>

<ItemGroup>
Expand Down