Skip to content
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
20 changes: 18 additions & 2 deletions src/assembly-differ/AssemblyComparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ public class AllComparisonResults
public AllComparisonResults(List<AssemblyComparison> results, SuggestedVersionChange preventChange) =>
(Comparisons, PreventVersionChange) = (results, preventChange);

public SuggestedVersionChange SuggestedVersionChange =>
Comparisons.Count == 0 ? SuggestedVersionChange.None : Comparisons.Select(c => c.SuggestedVersionChange).OrderByDescending(c => c).First();
/// <summary>Assemblies only the old version has, which count as removed API.</summary>
public IReadOnlyList<FileInfo> Removed { get; init; } = [];

/// <summary>Assemblies only the new version has, which count as added API.</summary>
public IReadOnlyList<FileInfo> Added { get; init; } = [];

public SuggestedVersionChange SuggestedVersionChange
{
get
{
var change = Comparisons.Select(c => c.SuggestedVersionChange).DefaultIfEmpty(SuggestedVersionChange.None).Max();
if (Removed.Count > 0)
return SuggestedVersionChange.Major;
if (Added.Count > 0 && change < SuggestedVersionChange.Minor)
return SuggestedVersionChange.Minor;
return change;
}
}
}
30 changes: 27 additions & 3 deletions src/assembly-differ/DiffCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public int Diff(
var firstAssemblies = firstProvider.GetAssemblies(targets).ToList();
var secondAssemblies = secondProvider.GetAssemblies(targets).ToList();
var pairs = CreateAssemblyPairs(firstAssemblies, secondAssemblies).ToList();
var onlyInFirst = Unpaired(firstAssemblies, pairs.Select(p => p.First));
var onlyInSecond = Unpaired(secondAssemblies, pairs.Select(p => p.Second));

if (!pairs.Any())
{
Expand All @@ -104,7 +106,17 @@ public int Diff(
return 1;
}

var result = new AllComparisonResults(pairs, preventChangeValue);
foreach (var assembly in onlyInFirst)
Console.WriteLine($"[diff] Only in the old version: {assembly.Name}");
foreach (var assembly in onlyInSecond)
Console.WriteLine($"[diff] Only in the new version: {assembly.Name}");

// without targets, an assembly only one side has is as likely a dependency as API, so it only counts when named
var result = new AllComparisonResults(pairs, preventChangeValue)
{
Removed = targets.Count > 0 ? onlyInFirst : [],
Added = targets.Count > 0 ? onlyInSecond : [],
};
foreach (var assemblyPair in pairs)
{
assemblyPair.Diff = ApiDiff.Compare(assemblyPair.First.FullName, assemblyPair.Second.FullName);
Expand Down Expand Up @@ -141,6 +153,18 @@ public int Diff(
return 0;
}

private static IEnumerable<AssemblyComparison> CreateAssemblyPairs(IEnumerable<FileInfo> first, IEnumerable<FileInfo> second) =>
first.Join(second, f => f.Name.ToUpperInvariant(), f => f.Name.ToUpperInvariant(), (f1, f2) => new AssemblyComparison(f1, f2));
/// <summary>
/// Pairs the assemblies by file name. When each side has exactly one assembly they are compared whatever their names,
/// such as a dll and a renamed copy of it.
/// </summary>
private static IEnumerable<AssemblyComparison> CreateAssemblyPairs(IReadOnlyList<FileInfo> first, IReadOnlyList<FileInfo> second) =>
first.Count == 1 && second.Count == 1
? [new AssemblyComparison(first[0], second[0])]
: first.Join(second, f => f.Name.ToUpperInvariant(), f => f.Name.ToUpperInvariant(), (f1, f2) => new AssemblyComparison(f1, f2));

private static List<FileInfo> Unpaired(IEnumerable<FileInfo> assemblies, IEnumerable<FileInfo> paired)
{
var pairedPaths = new HashSet<string>(paired.Select(p => p.FullName));
return assemblies.Where(a => !pairedPaths.Contains(a.FullName)).ToList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exit code: 0
--- stdout
[diff] Difference found: AssemblyProvider:Lib - Copy.dll and AssemblyProvider:Lib.dll
<Assembly Name="Lib, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" DiffType="Modified">
<Module Name="Lib.dll" DiffType="Modified">
<Type Name="Lib.Widget" DiffType="Modified">
<Method Name="Draw()" DiffType="Deleted" />
</Type>
</Module>
</Assembly>[diff] Suggested version change: Major
--- stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exit code: 0
--- stdout
[diff] Only in the new version: Extra.dll
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: Minor
--- stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exit code: 0
--- stdout
[diff] Only in the new version: Extra.dll
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: None
--- stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exit code: 0
--- stdout
[diff] Only in the old version: Extra.dll
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: Major
--- stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exit code: 0
--- stdout
[diff] Only in the old version: Extra.dll
[diff] No diff between {fixture}/old/Lib.dll and {fixture}/new/Lib.dll
[diff] Suggested version change: None
--- stderr
37 changes: 37 additions & 0 deletions tests/assembly-differ.Tests/PairingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AwesomeAssertions;
using Nullean.Snapshots;

namespace Differ.Tests;

/// <summary>How the assemblies on the two sides are paired, and what happens to one only a single side has.</summary>
public class PairingTests
{
[Test]
public async Task A_single_assembly_on_each_side_is_compared_whatever_its_name()
{
using var fixture = Scenarios.Create("method-removed");
var copy = Path.Combine(fixture.Root, "Lib - Copy.dll");
File.Copy(fixture.OldAssembly, copy);

var result = await Cli.RunAsync("diff", $"assembly|{copy}", $"assembly|{fixture.NewAssembly}");

Report.Of(result, fixture).Should().MatchSnapshot();
}

public static IEnumerable<(string, string)> Unpaired() =>
[("added-untargeted", "new"), ("added-targeted", "new -t Lib -t Extra"), ("removed-untargeted", "old"), ("removed-targeted", "old -t Lib -t Extra"),];

[Test]
[MethodDataSource(nameof(Unpaired))]
public async Task An_assembly_only_one_side_has_is_reported(string name, string setup)
{
using var fixture = Scenarios.Create("unchanged");
var parts = setup.Split(' ');
var side = parts[0] == "new" ? fixture.NewDirectory : fixture.OldDirectory;
Fixture.Compile("namespace Extra; public class Thing { }", Path.Combine(side, "Extra.dll"));

var result = await Cli.RunAsync(["diff", $"directory|{fixture.OldDirectory}", $"directory|{fixture.NewDirectory}", .. parts.Skip(1)]);

Report.Of(result, fixture).Should().MatchSnapshot(name: name);
}
}
Loading