2022-01-21 07:59:36 -06:00
|
|
|
|
// Licensed to the .NET Foundation under one or more agreements.
|
|
|
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-02-10 15:58:01 -06:00
|
|
|
|
using System.Diagnostics;
|
2022-01-21 07:59:36 -06:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Xunit;
|
2022-02-10 15:58:01 -06:00
|
|
|
|
using Xunit.Abstractions;
|
2022-01-21 07:59:36 -06:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.SourceBuild.SmokeTests
|
|
|
|
|
{
|
|
|
|
|
internal class BaselineHelper
|
|
|
|
|
{
|
2022-02-18 06:51:43 -08:00
|
|
|
|
public static void CompareEntries(string baselineFileName, IOrderedEnumerable<string> actualEntries)
|
2022-01-21 07:59:36 -06:00
|
|
|
|
{
|
|
|
|
|
IEnumerable<string> baseline = File.ReadAllLines(GetBaselineFilePath(baselineFileName));
|
|
|
|
|
string[] missingEntries = actualEntries.Except(baseline).ToArray();
|
|
|
|
|
string[] extraEntries = baseline.Except(actualEntries).ToArray();
|
|
|
|
|
|
|
|
|
|
string? message = null;
|
|
|
|
|
if (missingEntries.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
message = $"Missing entries in '{baselineFileName}' baseline: {Environment.NewLine}{string.Join(Environment.NewLine, missingEntries)}{Environment.NewLine}{Environment.NewLine}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (extraEntries.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
message += $"Extra entries in '{baselineFileName}' baseline: {Environment.NewLine}{string.Join(Environment.NewLine, extraEntries)}{Environment.NewLine}{Environment.NewLine}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.Null(message);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 12:25:05 -05:00
|
|
|
|
public static void CompareContents(string baselineFileName, string actualContents, ITestOutputHelper outputHelper, bool warnOnDiffs = false)
|
2022-02-10 15:58:01 -06:00
|
|
|
|
{
|
|
|
|
|
string baselineFilePath = GetBaselineFilePath(baselineFileName);
|
2022-02-18 06:51:43 -08:00
|
|
|
|
|
|
|
|
|
string actualFilePath = Path.Combine(Environment.CurrentDirectory, $"{baselineFileName}");
|
|
|
|
|
File.WriteAllText(actualFilePath, actualContents);
|
|
|
|
|
|
2022-03-23 12:25:05 -05:00
|
|
|
|
CompareFiles(baselineFilePath, actualFilePath, outputHelper, warnOnDiffs);
|
2022-02-18 06:51:43 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 12:25:05 -05:00
|
|
|
|
public static void CompareFiles(string baselineFilePath, string actualFilePath, ITestOutputHelper outputHelper, bool warnOnDiffs = false)
|
2022-02-18 06:51:43 -08:00
|
|
|
|
{
|
|
|
|
|
string baselineFileText = File.ReadAllText(baselineFilePath);
|
|
|
|
|
string actualFileText = File.ReadAllText(actualFilePath);
|
2022-02-10 15:58:01 -06:00
|
|
|
|
|
|
|
|
|
string? message = null;
|
|
|
|
|
|
2022-02-18 06:51:43 -08:00
|
|
|
|
if (baselineFileText != actualFileText)
|
|
|
|
|
{
|
2022-02-10 15:58:01 -06:00
|
|
|
|
// Retrieve a diff in order to provide a UX which calls out the diffs.
|
2022-02-18 06:51:43 -08:00
|
|
|
|
string diff = DiffFiles(baselineFilePath, actualFilePath, outputHelper);
|
2022-03-23 12:25:05 -05:00
|
|
|
|
string prefix = warnOnDiffs ? "##vso[task.logissue type=warning;]" : string.Empty;
|
|
|
|
|
message = $"{Environment.NewLine}{prefix}Baseline '{baselineFilePath}' does not match actual '{actualFilePath}`. {Environment.NewLine}"
|
2022-02-10 15:58:01 -06:00
|
|
|
|
+ $"{diff}{Environment.NewLine}";
|
2022-03-23 12:25:05 -05:00
|
|
|
|
|
|
|
|
|
if (warnOnDiffs)
|
|
|
|
|
{
|
|
|
|
|
outputHelper.WriteLine(message);
|
|
|
|
|
outputHelper.WriteLine("##vso[task.complete result=SucceededWithIssues;]");
|
|
|
|
|
}
|
2022-02-10 15:58:01 -06:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 12:25:05 -05:00
|
|
|
|
if (!warnOnDiffs)
|
|
|
|
|
{
|
|
|
|
|
Assert.Null(message);
|
|
|
|
|
}
|
2022-02-10 15:58:01 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string DiffFiles(string file1Path, string file2Path, ITestOutputHelper outputHelper)
|
|
|
|
|
{
|
|
|
|
|
(Process Process, string StdOut, string StdErr) diffResult =
|
|
|
|
|
ExecuteHelper.ExecuteProcess("git", $"diff --no-index {file1Path} {file2Path}", outputHelper);
|
|
|
|
|
Assert.Equal(1, diffResult.Process.ExitCode);
|
|
|
|
|
|
|
|
|
|
return diffResult.StdOut;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 06:51:43 -08:00
|
|
|
|
public static string GetAssetsDirectory() => Path.Combine(Directory.GetCurrentDirectory(), "assets");
|
|
|
|
|
|
|
|
|
|
private static string GetBaselineFilePath(string baselineFileName) => Path.Combine(GetAssetsDirectory(), "baselines", baselineFileName);
|
2022-01-21 07:59:36 -06:00
|
|
|
|
}
|
|
|
|
|
}
|