Skip to content

Commit

Permalink
User/gustavoca/dont extract e2e tests (#684)
Browse files Browse the repository at this point in the history
* Inspect the content of the Nuget package instead of extracting to disk during e2e tests.

* Remove extra changes in Directory.Packages.Props
  • Loading branch information
gustavoaca1997 authored Aug 23, 2024
1 parent 82d5fe5 commit fe6c158
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
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

0 comments on commit fe6c158

Please sign in to comment.