Skip to content

Commit

Permalink
Write APICompat suppressions when baseline suppressions aren't empty (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorHofer authored Nov 20, 2024
1 parent d691b06 commit 425cbaa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void GenerateSuppressionFile(ISuppressionEngine suppressionEngine,
return;
}

if (suppressionEngine.WriteSuppressionsToFile(suppressionOutputFile, preserveUnnecessarySuppressions).Count > 0)
if (suppressionEngine.WriteSuppressionsToFile(suppressionOutputFile, preserveUnnecessarySuppressions).SuppressionFileUpdated)
{
log.LogMessage(MessageImportance.High,
string.Format(CommonResources.WroteSuppressions, suppressionOutputFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public interface ISuppressionEngine
/// </summary>
/// <param name="suppressionOutputFile">The path to the file to be written.</param>
/// <param name="preserveUnnecessarySuppressions">If <see langword="true"/>, preserves unnecessary suppressions.</param>
/// <returns>Returns the set of suppressions written.</returns>
IReadOnlyCollection<Suppression> WriteSuppressionsToFile(string suppressionOutputFile, bool preserveUnnecessarySuppressions = false);
/// <returns>Returns a boolean indicating whether the suppression file got updated and the set of suppressions written.</returns>
(bool SuppressionFileUpdated, IReadOnlyCollection<Suppression> UpdatedSuppressions) WriteSuppressionsToFile(string suppressionOutputFile, bool preserveUnnecessarySuppressions = false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public bool IsErrorSuppressed(Suppression error)
public void AddSuppression(Suppression suppression) => _suppressions.Add(suppression);

/// <inheritdoc/>
public IReadOnlyCollection<Suppression> WriteSuppressionsToFile(string suppressionOutputFile, bool preserveUnnecessarySuppressions = false)
public (bool SuppressionFileUpdated, IReadOnlyCollection<Suppression> UpdatedSuppressions)
WriteSuppressionsToFile(string suppressionOutputFile, bool preserveUnnecessarySuppressions = false)
{
// If unnecessary suppressions should be preserved in the suppression file, union the
// baseline suppressions with the set of actual suppressions. Duplicates are ignored.
Expand All @@ -114,9 +115,11 @@ public IReadOnlyCollection<Suppression> WriteSuppressionsToFile(string suppressi
suppressionsToSerialize.UnionWith(_baselineSuppressions);
}

if (suppressionsToSerialize.Count == 0)
// If there aren't any suppressions and baseline suppressions, skip writing the
// suppression file.
if (suppressionsToSerialize.Count == 0 && _baselineSuppressions.Count == 0)
{
return Array.Empty<Suppression>();
return (false, []);
}

Suppression[] orderedSuppressions = suppressionsToSerialize
Expand All @@ -138,7 +141,7 @@ public IReadOnlyCollection<Suppression> WriteSuppressionsToFile(string suppressi
CreateXmlSerializer().Serialize(xmlWriter, orderedSuppressions);
AfterWritingSuppressionsCallback(stream);

return orderedSuppressions;
return (true, orderedSuppressions);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ public void SuppressionEngine_WriteSuppressionsToFile_SuppressionsRoundTrip()
suppressionEngine.LoadSuppressions("NonExistentFile.xml");

string filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName(), "DummyFile.xml");
IReadOnlyCollection<Suppression> writtenSuppressions = suppressionEngine.WriteSuppressionsToFile(filePath, preserveUnnecessarySuppressions: true);
var (suppressionFileUpdated, writtenSuppressions) = suppressionEngine.WriteSuppressionsToFile(filePath, preserveUnnecessarySuppressions: true);

Assert.True(suppressionFileUpdated);
Assert.NotEmpty(writtenSuppressions);

// Trimming away the comment as the serializer doesn't preserve them.
Expand Down Expand Up @@ -97,7 +98,11 @@ public void SuppressionEngine_WriteSuppressionsToFile_ReturnsEmptyWithAllUnneces
suppressionEngine.LoadSuppressions("NonExistentFile.xml");

string filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName(), "DummyFile.xml");
Assert.Empty(suppressionEngine.WriteSuppressionsToFile(filePath));

var (suppressionFileUpdated, updatedSuppressions) = suppressionEngine.WriteSuppressionsToFile(filePath);

Assert.True(suppressionFileUpdated);
Assert.Empty(updatedSuppressions);
}

[Fact]
Expand Down Expand Up @@ -126,18 +131,21 @@ public void SuppressionEngine_WriteSuppressionsToFile_UnnecessarySuppressionsOmi
suppressionEngine.AddSuppression(usedSuppression);

string filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName(), "DummyFile.xml");
IReadOnlyCollection<Suppression> writtenSuppressions = suppressionEngine.WriteSuppressionsToFile(filePath);
var (suppressionFileUpdated, updatedSuppressions) = suppressionEngine.WriteSuppressionsToFile(filePath);

Assert.Equal(new Suppression[] { usedSuppression }, writtenSuppressions);
Assert.True(suppressionFileUpdated);
Assert.Equal([usedSuppression], updatedSuppressions);
}

[Fact]
public void SuppressionEngine_WriteSuppressionsToFile_ReturnsEmptyWithEmptyFilePath()
{
EmptyTestSuppressionEngine suppressionEngine = new();

Assert.Empty(suppressionEngine.Suppressions);
Assert.Empty(suppressionEngine.WriteSuppressionsToFile(string.Empty, preserveUnnecessarySuppressions: true));

var (suppressionFileUpdated, updatedSuppressions) = suppressionEngine.WriteSuppressionsToFile(string.Empty, preserveUnnecessarySuppressions: true);
Assert.False(suppressionFileUpdated);
Assert.Empty(updatedSuppressions);
}

[Fact]
Expand Down Expand Up @@ -189,7 +197,11 @@ public void SuppressionEngine_BaseliningNewErrorsDoesNotOverrideSuppressions()
suppressionEngine.AddSuppression(newSuppression);

string filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName(), "DummyFile.xml");
Assert.NotEmpty(suppressionEngine.WriteSuppressionsToFile(filePath, preserveUnnecessarySuppressions: true));

var (suppressionFileUpdated, updatedSuppressions) = suppressionEngine.WriteSuppressionsToFile(filePath, preserveUnnecessarySuppressions: true);

Assert.True(suppressionFileUpdated);
Assert.NotEmpty(updatedSuppressions);

XmlSerializer xmlSerializer = new(typeof(Suppression[]), new XmlRootAttribute("Suppressions"));
Suppression[] deserializedSuppressions = xmlSerializer.Deserialize(stream) as Suppression[];
Expand Down

0 comments on commit 425cbaa

Please sign in to comment.