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

[tests] Add test to keep track of trimmer warnings. #20125

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
17 changes: 17 additions & 0 deletions tests/common/BinLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ public class BuildLogEvent {
public string? ProjectFile;
public string? Message;

public BuildLogEvent Clone ()
{
var rv = new BuildLogEvent {
Type = Type,
ColumnNumber = ColumnNumber,
EndColumnNumber = EndColumnNumber,
LineNumber = LineNumber,
EndLineNumber = EndLineNumber,
Code = Code,
SubCategory = SubCategory,
File = File,
ProjectFile = ProjectFile,
Message = Message
};
return rv;
}

public override string ToString ()
{
var rv = new StringBuilder ();
Expand Down
67 changes: 67 additions & 0 deletions tests/dotnet/UnitTests/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,72 @@ public static void AssertNoWarnings (this ExecutionResult result, Func<BuildLogE

Assert.Fail ($"No warnings expected, but got:\n\t{string.Join ("\n\t", warnings.Select (v => v.ToString ()))}");
}

public static void AssertWarnings (this IEnumerable<BuildLogEvent> actualWarnings, IEnumerable<ExpectedBuildMessage> expectedWarnings)
{
// Source paths may be full (and local) paths. So make full paths relative to the root of the repository.
actualWarnings = actualWarnings.Select (w => {
if (w.File?.StartsWith (Configuration.SourceRoot) == true) {
var rv = w.Clone ();
rv.File = w.File [(Configuration.SourceRoot.Length + 1)..];
return rv;
}
return w;
});

var newWarnings = actualWarnings.Where (v => !expectedWarnings.Any (x => x.IsMatch (v))).ToArray ();
var missingWarnings = expectedWarnings.Where (v => !actualWarnings.Any (x => v.IsMatch (x))).ToArray ();

if (newWarnings.Length == 0 && missingWarnings.Length == 0)
return;

var sb = new StringBuilder ();
sb.AppendLine ($"\t\t\t\texpectedWarnings = new ExpectedBuildMessage [] {{");
foreach (var w in actualWarnings.OrderBy (v => v.File).ThenBy (v => v.Message)) {
sb.AppendLine ($"\t\t\t\t\tnew ExpectedBuildMessage (\"{w.File}\" /* line {w.LineNumber} */, \"{w.Message}\"),");
}
sb.AppendLine ($"\t\t\t\t}};");
if (newWarnings.Length > 0) {
Console.WriteLine ($"Got {newWarnings.Length} new warnings:");
Console.WriteLine ();
foreach (var evt in newWarnings)
Console.WriteLine ($" {evt.File}:{evt.LineNumber} {evt.Message}");
}
if (missingWarnings.Length > 0) {
Console.WriteLine ($"Did not get {missingWarnings.Length} missing warnings:");
Console.WriteLine ();
foreach (var evt in missingWarnings)
Console.WriteLine ($" {evt.File}: {evt.Message}");
}
Console.WriteLine ($"If this is expected, here's the updated list of expected warnings:");
Console.WriteLine (sb);
Assert.That (newWarnings, Is.Empty, "New warnings");
Assert.That (missingWarnings, Is.Empty, "Empty warnings");
}
}

public class ExpectedBuildMessage {
public string File;
public string Message;

public ExpectedBuildMessage (string file, string message)
{
File = file;
Message = message;
}

public bool IsMatch (BuildLogEvent evt)
{
if (evt.Message != Message)
return false;
if (evt.File != File)
return false;
return true;
}

public override string? ToString ()
{
return $"{File}: {Message}";
}
}
}
Loading