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

Update MSTest.Test* packages from 3.6.4 to 3.7.0, follow best practices #848

Merged
merged 20 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyModel" Version="8.0.2" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.4" />
<PackageVersion Include="MSTest.TestFramework" Version="3.6.4" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.7.0" />
<PackageVersion Include="MSTest.TestFramework" Version="3.7.0" />
<PackageVersion Include="Microsoft.ComponentDetection.Common" Version="$(ComponentDetectionPackageVersion)" />
<PackageVersion Include="Microsoft.ComponentDetection.Contracts" Version="$(ComponentDetectionPackageVersion)" />
<PackageVersion Include="Microsoft.ComponentDetection.Detectors" Version="$(ComponentDetectionPackageVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ namespace Microsoft.Sbom.Adapters.Tests;
[TestClass]
public class ComponentDetectionToSBOMPackageAdapterTests
{
private static TestContext testContext;

[ClassInitialize]
public static void SetUp(TestContext testContext)
{
ComponentDetectionToSBOMPackageAdapterTests.testContext = testContext;
}
public TestContext TestContext { get; set; }

[TestMethod]
public void BasicAdapterTest_Succeeds()
{
#pragma warning disable JSON002 // Probable JSON string detected
var json = @"{
""componentsFound"": [
{
Expand All @@ -56,40 +51,45 @@ public void BasicAdapterTest_Succeeds()
""ContainerDetailsMap"": {},
""resultCode"": ""Success""
}";
#pragma warning restore JSON002 // Probable JSON string detected
var (errors, packages) = GenerateJsonFileForTestAndRun(json);

// Successful conversion
Assert.IsNotNull(errors.Report);
Assert.IsTrue(errors.Report?.Count == 1);
Assert.IsTrue(errors.Report.First().Type == AdapterReportItemType.Success);
Assert.AreEqual(1, errors.Report.Count);
DaveTryon marked this conversation as resolved.
Show resolved Hide resolved
Assert.AreEqual(AdapterReportItemType.Success, errors.Report.First().Type);

// Converted packaged is present and valid
Assert.IsTrue(packages?.Count == 1);
Assert.IsNotNull(packages);
Assert.AreEqual(1, packages.Count);
Assert.IsNotNull(packages[0]);
Assert.AreEqual(packages[0].PackageName, "@microsoft/yarn-graph-builder");
Assert.AreEqual(packages[0].PackageVersion, "1.0.0");
Assert.AreEqual("@microsoft/yarn-graph-builder", packages[0].PackageName);
Assert.AreEqual("1.0.0", packages[0].PackageVersion);

// This one contains no checksums, so verify that it is null
Assert.IsNotNull(packages[0].Checksum);
var checksums = packages[0].Checksum?.ToList();
Assert.IsTrue(checksums?.Count == 1);
Assert.IsNotNull(checksums);
Assert.AreEqual(1, checksums.Count);
Assert.IsNull(checksums[0].ChecksumValue);
}

[TestMethod]
public void NoComponents_Succeeds()
{
#pragma warning disable JSON002 // Probable JSON string detected
var json = @"{
""componentsFound"": [],
""detectorsInScan"": [],
""ContainerDetailsMap"": {},
""resultCode"": ""Success""
}";
#pragma warning restore JSON002 // Probable JSON string detected
var (errors, packages) = GenerateJsonFileForTestAndRun(json);

Assert.IsTrue(packages?.Count == 0);
Assert.IsTrue(errors.Report?.Count == 1); // Should still be successful even with no components
Assert.IsTrue(errors.Report.First().Type == AdapterReportItemType.Success);
Assert.IsNotNull(packages);
Assert.AreEqual(0, packages.Count);
Assert.AreEqual(1, errors.Report.Count); // Should still be successful even with no components
Assert.AreEqual(AdapterReportItemType.Success, errors.Report.First().Type);
}

[TestMethod]
Expand All @@ -98,10 +98,10 @@ public void MalformedInput_ReturnsError()
var json = "{";
var (errors, packages) = GenerateJsonFileForTestAndRun(json);

Assert.IsTrue(errors.Report?.Count == 1);
Assert.IsTrue(errors.Report.First().Type == AdapterReportItemType.Failure);
Assert.AreEqual(1, errors.Report.Count);
Assert.AreEqual(AdapterReportItemType.Failure, errors.Report.First().Type);
Assert.IsTrue(errors.Report.First().Details.Contains("Unable to parse bcde-output.json", StringComparison.Ordinal));
Assert.IsTrue(packages.Count == 0);
Assert.AreEqual(0, packages.Count);
}

[TestMethod]
Expand Down Expand Up @@ -157,7 +157,7 @@ public void CondaComponent_ToSbomPackage()
var sbomPackage = scannedComponent.ToSbomPackage(new AdapterReport());

Assert.AreEqual(condaComponent.Id, sbomPackage.Id);
Assert.AreEqual(condaComponent.PackageUrl?.ToString(), sbomPackage.PackageUrl);
AssertPackageUrlIsCorrect(condaComponent.PackageUrl, sbomPackage.PackageUrl);
Assert.AreEqual(condaComponent.Name, sbomPackage.PackageName);
Assert.AreEqual(condaComponent.Version, sbomPackage.PackageVersion);
Assert.AreEqual(condaComponent.Url, sbomPackage.PackageSource);
Expand All @@ -173,7 +173,7 @@ public void DockerImageComponent_ToSbomPackage()
var sbomPackage = scannedComponent.ToSbomPackage(new AdapterReport());

Assert.AreEqual(dockerImageComponent.Id, sbomPackage.Id);
Assert.AreEqual(dockerImageComponent.PackageUrl?.ToString(), sbomPackage.PackageUrl);
AssertPackageUrlIsCorrect(dockerImageComponent.PackageUrl, sbomPackage.PackageUrl);
Assert.AreEqual(dockerImageComponent.Name, sbomPackage.PackageName);
Assert.AreEqual(AlgorithmName.SHA256, sbomPackage.Checksum.First().Algorithm);
Assert.AreEqual(dockerImageComponent.Digest, sbomPackage.Checksum.First().ChecksumValue);
Expand All @@ -188,7 +188,8 @@ public void NpmComponent_ToSbomPackage()
var sbomPackage = scannedComponent.ToSbomPackage(new AdapterReport());

Assert.AreEqual(npmComponent.Id, sbomPackage.Id);
Assert.AreEqual(npmComponent.PackageUrl?.ToString(), sbomPackage.PackageUrl);
Assert.IsNotNull(npmComponent.PackageUrl);
Assert.AreEqual(npmComponent.PackageUrl.ToString(), sbomPackage.PackageUrl);
Assert.AreEqual(npmComponent.Name, sbomPackage.PackageName);
Assert.AreEqual(npmComponent.Version, sbomPackage.PackageVersion);
Assert.AreEqual($"Organization: {npmComponent.Author.Name} ({npmComponent.Author.Email})", sbomPackage.Supplier);
Expand All @@ -203,7 +204,8 @@ public void NpmComponent_ToSbomPackage_NoAuthor()
var sbomPackage = scannedComponent.ToSbomPackage(new AdapterReport());

Assert.AreEqual(npmComponent.Id, sbomPackage.Id);
Assert.AreEqual(npmComponent.PackageUrl?.ToString(), sbomPackage.PackageUrl);
Assert.IsNotNull(npmComponent.PackageUrl);
Assert.AreEqual(npmComponent.PackageUrl.ToString(), sbomPackage.PackageUrl);
Assert.AreEqual(npmComponent.Name, sbomPackage.PackageName);
Assert.AreEqual(npmComponent.Version, sbomPackage.PackageVersion);
Assert.IsNull(sbomPackage.Supplier);
Expand All @@ -218,7 +220,8 @@ public void NuGetComponent_ToSbomPackage()
var sbomPackage = scannedComponent.ToSbomPackage(new AdapterReport());

Assert.AreEqual(nuGetComponent.Id, sbomPackage.Id);
Assert.AreEqual(nuGetComponent.PackageUrl?.ToString(), sbomPackage.PackageUrl);
AssertPackageUrlIsCorrect(nuGetComponent.PackageUrl, sbomPackage.PackageUrl);
Assert.AreEqual(nuGetComponent.PackageUrl.ToString(), sbomPackage.PackageUrl);
Assert.AreEqual(nuGetComponent.Name, sbomPackage.PackageName);
Assert.AreEqual(nuGetComponent.Version, sbomPackage.PackageVersion);
Assert.AreEqual($"Organization: {nuGetComponent.Authors.First()}", sbomPackage.Supplier);
Expand All @@ -233,7 +236,8 @@ public void NuGetComponent_ToSbomPackage_NoAuthor()
var sbomPackage = scannedComponent.ToSbomPackage(new AdapterReport());

Assert.AreEqual(nuGetComponent.Id, sbomPackage.Id);
Assert.AreEqual(nuGetComponent.PackageUrl?.ToString(), sbomPackage.PackageUrl);
Assert.IsNotNull(nuGetComponent.PackageUrl);
Assert.AreEqual(nuGetComponent.PackageUrl.ToString(), sbomPackage.PackageUrl);
Assert.AreEqual(nuGetComponent.Name, sbomPackage.PackageName);
Assert.AreEqual(nuGetComponent.Version, sbomPackage.PackageVersion);
Assert.IsNull(sbomPackage.Supplier);
Expand All @@ -248,7 +252,8 @@ public void PipComponent_ToSbomPackage()
var sbomPackage = scannedComponent.ToSbomPackage(new AdapterReport());

Assert.AreEqual(pipComponent.Id, sbomPackage.Id);
Assert.AreEqual(pipComponent.PackageUrl?.ToString(), sbomPackage.PackageUrl);
Assert.IsNotNull(pipComponent.PackageUrl);
Assert.AreEqual(pipComponent.PackageUrl.ToString(), sbomPackage.PackageUrl);
Assert.AreEqual(pipComponent.Name, sbomPackage.PackageName);
Assert.AreEqual(pipComponent.Version, sbomPackage.PackageVersion);
}
Expand All @@ -263,12 +268,23 @@ public void GitComponent_ToSbomPackage()
var sbomPackage = scannedComponent.ToSbomPackage(new AdapterReport());

Assert.AreEqual(gitComponent.Id, sbomPackage.Id);
Assert.AreEqual(gitComponent.PackageUrl?.ToString(), sbomPackage.PackageUrl);
AssertPackageUrlIsCorrect(gitComponent.PackageUrl, sbomPackage.PackageUrl);
DaveTryon marked this conversation as resolved.
Show resolved Hide resolved
}

private void AssertPackageUrlIsCorrect(PackageUrl.PackageURL expectedPackageUrl, string actualPackageUrl)
{
if (expectedPackageUrl is null)
{
Assert.IsNull(actualPackageUrl);
return;
}

Assert.AreEqual(expectedPackageUrl.ToString(), actualPackageUrl);
}

private (AdapterReport report, List<SbomPackage> packages) GenerateJsonFileForTestAndRun(string json)
{
var baseDirectory = Path.Combine(testContext.TestRunDirectory, Guid.NewGuid().ToString());
var baseDirectory = Path.Combine(TestContext.TestRunDirectory, Guid.NewGuid().ToString());
var bcdeOutputPath = Path.Combine(baseDirectory, "bcde-output.json");

Directory.CreateDirectory(baseDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,20 @@ public void GetConfiguration_DefaultRuntime()
[TestMethod]
[DataRow(" ")]
[DataRow(null)]
[ExpectedException(typeof(ArgumentException))]
public void ThrowArgumentExceptionOnRootPathValues(string input)
{
ApiConfigurationBuilder.GetConfiguration(input, null, null, null, null);
Assert.ThrowsException<ArgumentException>(() => ApiConfigurationBuilder.GetConfiguration(input, null, null, null, null));
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ThrowArgumentNulExceptionOnNullMetadata()
{
ApiConfigurationBuilder.GetConfiguration("random", null, null, null, null);
Assert.ThrowsException<ArgumentNullException>(() => ApiConfigurationBuilder.GetConfiguration("random", null, null, null, null));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ThrowArgumentExceptionOnSpecificationZero()
{
ApiConfigurationBuilder.GetConfiguration("random", null, null, null, metadata, new List<SbomSpecification>(), runtime);
Assert.ThrowsException<ArgumentException>(() => ApiConfigurationBuilder.GetConfiguration("random", null, null, null, metadata, new List<SbomSpecification>(), runtime));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,13 @@ public void SetValueForManifestInfoForValidation_Succeeds()
}

[TestMethod]
[ExpectedException(typeof(ValidationArgException))]
public void NoValueForManifestInfoForValidation_Throws()
{
var config = GetConfigurationBaseObject();
config.ManifestToolAction = ManifestToolActions.Validate;
config.ManifestInfo.Value.Clear();

configSanitizer.SanitizeConfig(config);
Assert.ThrowsException<ValidationArgException>(() => configSanitizer.SanitizeConfig(config));
}

[TestMethod]
Expand Down Expand Up @@ -190,13 +189,12 @@ public void ForValidateGetsRealAlgorithmName_Succeeds_DoesNotThrow()
}

[TestMethod]
[ExpectedException(typeof(UnsupportedHashAlgorithmException))]
public void ForValidateBadAlgorithmNameGetsRealAlgorithmName_Throws()
{
var config = GetConfigurationBaseObject();
config.HashAlgorithm.Value = new AlgorithmName("a", null);
config.ManifestToolAction = ManifestToolActions.Validate;
configSanitizer.SanitizeConfig(config);
Assert.ThrowsException<UnsupportedHashAlgorithmException>(() => configSanitizer.SanitizeConfig(config));
}

[TestMethod]
Expand Down Expand Up @@ -319,7 +317,7 @@ public void ShouldNotOverridePackageSupplierIfProvided_Succeeds()
config.ManifestToolAction = ManifestToolActions.Validate;
configSanitizer.SanitizeConfig(config);

Assert.AreEqual(actualOrg, config.PackageSupplier.Value);
Assert.AreEqual(config.PackageSupplier.Value, actualOrg);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public async Task ConfigurationBuilderTest_ForGenerator_CombinesConfigs()

var configuration = await cb.GetConfiguration(args);

Assert.AreEqual(configuration.BuildDropPath.Source, SettingSource.CommandLine);
Assert.AreEqual(configuration.ConfigFilePath.Source, SettingSource.CommandLine);
Assert.AreEqual(configuration.ManifestInfo.Source, SettingSource.JsonConfig);
Assert.AreEqual(SettingSource.CommandLine, configuration.BuildDropPath.Source);
Assert.AreEqual(SettingSource.CommandLine, configuration.ConfigFilePath.Source);
Assert.AreEqual(SettingSource.JsonConfig, configuration.ManifestInfo.Source);

fileSystemUtilsMock.VerifyAll();
}
Expand All @@ -74,15 +74,14 @@ public async Task ConfigurationBuilderTest_ForGenerator_CombinesConfigs_CmdLineS

var configuration = await cb.GetConfiguration(args);

Assert.AreEqual(configuration.BuildDropPath.Source, SettingSource.CommandLine);
Assert.AreEqual(configuration.ConfigFilePath.Source, SettingSource.CommandLine);
Assert.AreEqual(configuration.ManifestInfo.Source, SettingSource.JsonConfig);
Assert.AreEqual(SettingSource.CommandLine, configuration.BuildDropPath.Source);
Assert.AreEqual(SettingSource.CommandLine, configuration.ConfigFilePath.Source);
Assert.AreEqual(SettingSource.JsonConfig, configuration.ManifestInfo.Source);

fileSystemUtilsMock.VerifyAll();
}

[TestMethod]
[ExpectedException(typeof(ValidationArgException))]
public async Task ConfigurationBuilderTest_Generation_BuildDropPathDoNotExist_Throws()
{
var configFileParser = new ConfigFileParser(fileSystemUtilsMock.Object);
Expand All @@ -97,11 +96,10 @@ public async Task ConfigurationBuilderTest_Generation_BuildDropPathDoNotExist_Th
PackageSupplier = "Contoso"
};

var configuration = await cb.GetConfiguration(args);
await Assert.ThrowsExceptionAsync<ValidationArgException>(() => cb.GetConfiguration(args));
}

[TestMethod]
[ExpectedException(typeof(AccessDeniedValidationArgException))]
public async Task ConfigurationBuilderTest_Generation_BuildDropPathNotWriteAccess_Throws()
{
var configFileParser = new ConfigFileParser(fileSystemUtilsMock.Object);
Expand All @@ -118,7 +116,7 @@ public async Task ConfigurationBuilderTest_Generation_BuildDropPathNotWriteAcces
PackageSupplier = "Contoso"
};

var configuration = await cb.GetConfiguration(args);
await Assert.ThrowsExceptionAsync<AccessDeniedValidationArgException>(() => cb.GetConfiguration(args));
}

[TestMethod]
Expand Down Expand Up @@ -175,7 +173,7 @@ public async Task ConfigurationBuilderTest_Generation_UserManifestDirPath_AddsMa
Assert.IsNotNull(config.ManifestDirPath);

var expectedPath = Path.Join("ManifestDirPath", Constants.ManifestFolder);
Assert.AreEqual(Path.GetFullPath(expectedPath), Path.GetFullPath(config.ManifestDirPath.Value));
Assert.AreEqual(Path.GetFullPath(config.ManifestDirPath.Value), Path.GetFullPath(expectedPath));

fileSystemUtilsMock.VerifyAll();
}
Expand Down Expand Up @@ -205,7 +203,7 @@ public async Task ConfigurationBuilderTest_Generation_NSBaseUri_Validated()
Assert.IsNotNull(config.ManifestDirPath);

var expectedPath = Path.Join("ManifestDirPath", Constants.ManifestFolder);
Assert.AreEqual(Path.GetFullPath(expectedPath), Path.GetFullPath(config.ManifestDirPath.Value));
Assert.AreEqual(Path.GetFullPath(config.ManifestDirPath.Value), Path.GetFullPath(expectedPath));

fileSystemUtilsMock.VerifyAll();
}
Expand Down Expand Up @@ -237,7 +235,7 @@ public async Task ConfigurationBuilderTest_Generation_BadNSBaseUriWithDefaultVal
Assert.IsNotNull(config.ManifestDirPath);

var expectedPath = Path.Join("ManifestDirPath", Constants.ManifestFolder);
Assert.AreEqual(Path.GetFullPath(expectedPath), Path.GetFullPath(config.ManifestDirPath.Value));
Assert.AreEqual(Path.GetFullPath(config.ManifestDirPath.Value), Path.GetFullPath(expectedPath));

fileSystemUtilsMock.VerifyAll();
mockAssemblyConfig.VerifyGet(a => a.DefaultSBOMNamespaceBaseUri);
Expand Down Expand Up @@ -267,7 +265,7 @@ public async Task ConfigurationBuilderTest_Generation_NullNSBaseUriChangesToDefa
Assert.IsNotNull(config);
Assert.IsNotNull(args.ManifestDirPath);
Assert.IsNotNull(config.NamespaceUriBase);
Assert.AreEqual(Path.Join("ManifestDirPath", Constants.ManifestFolder), config.ManifestDirPath.Value);
Assert.AreEqual(config.ManifestDirPath.Value, Path.Join("ManifestDirPath", Constants.ManifestFolder));

fileSystemUtilsMock.VerifyAll();
}
Expand All @@ -277,7 +275,6 @@ public async Task ConfigurationBuilderTest_Generation_NullNSBaseUriChangesToDefa
[DataRow("https://")]
[DataRow("ww.com")]
[DataRow("https//test.com")]
[ExpectedException(typeof(ValidationArgException), "The value of NamespaceUriBase must be a valid URI.")]
public async Task ConfigurationBuilderTest_Generation_BadNSBaseUri_Fails(string badNsUri)
{
var configFileParser = new ConfigFileParser(fileSystemUtilsMock.Object);
Expand All @@ -296,6 +293,6 @@ public async Task ConfigurationBuilderTest_Generation_BadNSBaseUri_Fails(string
PackageSupplier = "Contoso"
};

var config = await cb.GetConfiguration(args);
await Assert.ThrowsExceptionAsync<ValidationArgException>(() => cb.GetConfiguration(args), "The value of NamespaceUriBase must be a valid URI.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ public async Task ConfigurationBuilderTest_ForRedact_CombinesConfigs()

var configuration = await cb.GetConfiguration(args);

Assert.AreEqual(configuration.SbomDir.Source, SettingSource.CommandLine);
Assert.AreEqual(configuration.OutputPath.Source, SettingSource.CommandLine);
Assert.AreEqual(SettingSource.CommandLine, configuration.SbomDir.Source);
Assert.AreEqual(SettingSource.CommandLine, configuration.OutputPath.Source);

fileSystemUtilsMock.VerifyAll();
}

[TestMethod]
[ExpectedException(typeof(ValidationArgException))]
public async Task ConfigurationBuilderTest_Redact_OuputPathNotWriteAccess_Throws()
{
var configFileParser = new ConfigFileParser(fileSystemUtilsMock.Object);
Expand All @@ -62,6 +61,6 @@ public async Task ConfigurationBuilderTest_Redact_OuputPathNotWriteAccess_Throws
OutputPath = "OutputPath"
};

var configuration = await cb.GetConfiguration(args);
await Assert.ThrowsExceptionAsync<ValidationArgException>(() => cb.GetConfiguration(args));
}
}
Loading
Loading