public async Task SarifFileWithContents_OpensEmbeddedNonFileUriBinaryFile() { string mockContent = null; var mockFileSystem = new Mock <IFileSystem>(); mockFileSystem .Setup(fs => fs.FileExists(It.IsAny <string>())) .Returns((string path) => false); mockFileSystem .Setup(fs => fs.FileWriteAllText(It.IsAny <string>(), It.IsAny <string>())) .Callback((string path, string content) => mockContent = content); mockFileSystem .Setup(fs => fs.FileSetAttributes(It.IsAny <string>(), FileAttributes.ReadOnly)) .Verifiable(); CodeAnalysisResultManager.Instance = new CodeAnalysisResultManager(mockFileSystem.Object); await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); string rebaselinedFile = CodeAnalysisResultManager.Instance.CreateFileFromContents(this.CurrentRunIndex, Key8); Models.ArtifactDetailsModel fileDetail = this.CurrentRunDataCache.FileDetails[Key8]; string fileText = mockContent; fileDetail.Sha256Hash.Should().Be(ExpectedHashValue2); fileText.Should().Be(ExpectedContents2); }
public async Task ErrorList_WithMultipleRuns_ManagerHasAllRows() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); var errorCount = CodeAnalysisResultManager.Instance.RunIndexToRunDataCache.Sum(c => c.Value.SarifErrors.Count); errorCount.Should().Be(3); }
public async Task SarifFileWithContents_SavesContents() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); var fileDetails = this.CurrentRunDataCache.FileDetails; fileDetails.Should().ContainKey(Key1); }
public async Task SarifFileWithContents_SavesContents() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); IDictionary <string, Models.ArtifactDetailsModel> fileDetails = this.CurrentRunDataCache.FileDetails; fileDetails.Should().ContainKey(Key1); }
public async Task SarifFileWithContents_DecodesTextContents() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); var fileDetail = this.CurrentRunDataCache.FileDetails[Key3]; var contents = fileDetail.GetContents(); contents.Should().Be(ExpectedContents1); }
public async Task SarifFileWithContents_GeneratesHash() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); var fileDetail = this.CurrentRunDataCache.FileDetails[Key1]; var contents = fileDetail.GetContents(); fileDetail.Sha256Hash.Should().Be(ExpectedHashValue1); contents.Should().Be(ExpectedContents1); }
public async Task SarifFileWithContents_HandlesEmptyBinaryContents() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); var fileDetail = this.CurrentRunDataCache.FileDetails[Key5]; var contents = fileDetail.GetContents(); fileDetail.Sha256Hash.Should().Be(EmptyStringHash); contents.Should().Be(string.Empty); }
public async Task SarifFileWithContents_DecodesBinaryContentsWithText() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); var fileDetail = this.CurrentRunDataCache.FileDetails[Key4]; var contents = fileDetail.GetContents(); fileDetail.Sha256Hash.Should().Be(ExpectedHashValue2); contents.Should().Be(ExpectedContents2); }
public async Task SarifFileWithContents_HandlesExistingHash() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); Models.ArtifactDetailsModel fileDetail = this.CurrentRunDataCache.FileDetails[Key7]; string contents = fileDetail.GetContents(); fileDetail.Sha256Hash.Should().Be(ExpectedHashValue1); contents.Should().Be(ExpectedContents2); }
public async Task SarifFileWithContents_OpensEmbeddedNonFileUriBinaryFile() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); var rebaselinedFile = CodeAnalysisResultManager.Instance.CreateFileFromContents(this.CurrentRunIndex, Key8); var fileDetail = this.CurrentRunDataCache.FileDetails[Key8]; var fileText = File.ReadAllText(rebaselinedFile); fileDetail.Sha256Hash.Should().Be(ExpectedHashValue2); fileText.Should().Be(ExpectedContents2); }
public async Task ErrorList_WithMultipleRuns_ListObjectHasAllRows() { await TestUtilities.InitializeTestEnvironmentAsync(this.testLog); var hasFirstError = SarifTableDataSource.Instance.HasErrors("/item1.cpp"); var hasSecondError = SarifTableDataSource.Instance.HasErrors("/item2.cpp"); var hasThirdError = SarifTableDataSource.Instance.HasErrors("/item3.cpp"); var hasBothErrors = hasFirstError && hasSecondError && hasThirdError; hasBothErrors.Should().BeTrue(); }
public async Task ErrorList_IsSarifLogOpened() { TestUtilities.InitializeTestEnvironment(); string logFileName1 = @"C:\git\repo\sarif-sdk\test_data\testresult1.sarif"; string logFileName2 = @"C:\git\repo\sarif-sdk\test_data\testtool2.sarif"; bool sarifLogOpened = ErrorListService.IsSarifLogOpened(logFileName1); sarifLogOpened.Should().BeFalse(); sarifLogOpened = ErrorListService.IsSarifLogOpened(logFileName2); sarifLogOpened.Should().BeFalse(); // load sarif log 1 await TestUtilities.InitializeTestEnvironmentAsync(this.testLog, logFileName1); bool hasFirstError = SarifTableDataSource.Instance.HasErrors("/item1.cpp"); bool hasSecondError = SarifTableDataSource.Instance.HasErrors("/item2.cpp"); bool hasThirdError = SarifTableDataSource.Instance.HasErrors("/item3.cpp"); bool hasBothErrors = hasFirstError && hasSecondError && hasThirdError; hasBothErrors.Should().BeTrue(); sarifLogOpened = ErrorListService.IsSarifLogOpened(logFileName1); sarifLogOpened.Should().BeTrue(); sarifLogOpened = ErrorListService.IsSarifLogOpened(logFileName2); sarifLogOpened.Should().BeFalse(); // load sarif log 2 await TestUtilities.InitializeTestEnvironmentAsync(this.testLog, logFileName2, cleanErrors : false); sarifLogOpened = ErrorListService.IsSarifLogOpened(logFileName1); sarifLogOpened.Should().BeTrue(); sarifLogOpened = ErrorListService.IsSarifLogOpened(logFileName2); sarifLogOpened.Should().BeTrue(); // close sairf log 1 await ErrorListService.CloseSarifLogItemsAsync(new string[] { logFileName1 }); sarifLogOpened = ErrorListService.IsSarifLogOpened(logFileName1); sarifLogOpened.Should().BeFalse(); sarifLogOpened = ErrorListService.IsSarifLogOpened(logFileName2); sarifLogOpened.Should().BeTrue(); }
public async Task <SarifLog> VerifySuppressionTests(dynamic testCase) { DateTime now = DateTime.UtcNow; string[] resultToBeSuppressed = testCase.ResultToBeSuppressed; string sarifLogFilePath = @"E:\Users\AnUser\Sarif\Logs\ResultsToSuppress.sarif"; var transformedContents = new StringBuilder(); var mockFileSystem = new Mock <IFileSystem>(); mockFileSystem .Setup(x => x.FileCreate(sarifLogFilePath)) .Returns(() => new StringBuilderFileStreamMock(transformedContents)); CodeAnalysisResultManager.Instance = new CodeAnalysisResultManager(mockFileSystem.Object); SarifLog sarifLog = CreateTestSarifLog(); await TestUtilities.InitializeTestEnvironmentAsync(sarifLog, sarifLogFilePath); IEnumerable <SarifErrorListItem> itemToBeSuppressed = CodeAnalysisResultManager.Instance .RunIndexToRunDataCache[CodeAnalysisResultManager.Instance.CurrentRunIndex] .SarifErrors.Where(i => resultToBeSuppressed.Contains(i.Rule.Id)); SuppressionModel model = testCase.Model; if (model != null && itemToBeSuppressed != null) { model.SelectedErrorListItems = itemToBeSuppressed; } sarifLog.Runs.First().HasSuppressedResults().Should().Be(false); CodeAnalysisResultManager.Instance.AddSuppressionToSarifLog(model); SarifLog suppressedLog = JsonConvert.DeserializeObject <SarifLog>(transformedContents.ToString()); if (!testCase.Expected.SuppressionAdded) { suppressedLog.Should().BeNull(); return(suppressedLog); } Run run = suppressedLog.Runs.First(); run.HasSuppressedResults().Should().Be(testCase.Expected.RunHasSuppressions); foreach (Result result in run.Results) { if (resultToBeSuppressed.Contains(result.RuleId)) { // suppressed result result.Suppressions.Should().NotBeNullOrEmpty(); result.Suppressions.Count.Should().Be(1); result.TryIsSuppressed(out bool isSuppressed).Should().Be(true); isSuppressed.Should().Be(model.Status == SuppressionStatus.Accepted); Suppression suppression = result.Suppressions.First(); suppression.Status.Should().Be(model.Status); suppression.Kind.Should().Be(model.Kind); } else { if (testCase.Expected.RunHasSuppressions) { // not suppressed result should have empty suppressions result.Suppressions.Should().NotBeNull(); result.Suppressions.Should().BeEmpty(); } } } return(suppressedLog); }