Skip to content

Commit

Permalink
[tests] Add test to keep track of trimmer warnings. (#20125)
Browse files Browse the repository at this point in the history
Add test to keep track of trimmer warnings and to make sure we don't accidentally
introduce new ones.

Ref: #10405
  • Loading branch information
rolfbjarne authored Feb 16, 2024
1 parent f392bbd commit da2525b
Show file tree
Hide file tree
Showing 3 changed files with 420 additions and 0 deletions.
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

8 comments on commit da2525b

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.