|
| 1 | +// Copyright (c) Jeff Kluge. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the MIT license. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.IO; |
| 9 | +using System.Text.RegularExpressions; |
| 10 | +using System.Threading; |
| 11 | +using log4net; |
| 12 | +using log4net.Core; |
| 13 | +using Xunit; |
| 14 | +using Xunit.Abstractions; |
| 15 | + |
| 16 | +namespace PackagesConfigConverter.UnitTests |
| 17 | +{ |
| 18 | + public class E2ETests : TestBase |
| 19 | + { |
| 20 | + private const string BaseTestCaseDir = @"TestData\E2ETests"; |
| 21 | + private const string BaseWorkingDir = @"_work"; |
| 22 | + private const string NuGetConfigFile = "NuGet.config"; |
| 23 | + private const string PackagesConfigFileName = "packages.config"; |
| 24 | + private const string BeforeProjectName = "before.csproj"; |
| 25 | + private const string AfterProjectName = "after.csproj"; |
| 26 | + |
| 27 | + private static readonly Regex ConverterInclude = new($"{BeforeProjectName}$", RegexOptions.Compiled); |
| 28 | + |
| 29 | + public E2ETests(ITestOutputHelper testOutputHelper) |
| 30 | + : base(testOutputHelper) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + public static IEnumerable<object[]> TestCases() |
| 35 | + { |
| 36 | + foreach (string dir in Directory.EnumerateDirectories(BaseTestCaseDir)) |
| 37 | + { |
| 38 | + string testCase = Path.GetFileName(dir); |
| 39 | + yield return new object[] { testCase }; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + [Theory] |
| 44 | + [MemberData(nameof(TestCases))] |
| 45 | + public void E2ETest(string testCase) |
| 46 | + { |
| 47 | + // Copy test files to a working dir |
| 48 | + string testCaseDir = Path.Combine(BaseTestCaseDir, testCase); |
| 49 | + string workingDir = Path.Combine(BaseWorkingDir, testCase); |
| 50 | + |
| 51 | + if (Directory.Exists(workingDir)) |
| 52 | + { |
| 53 | + Directory.Delete(workingDir, recursive: true); |
| 54 | + } |
| 55 | + |
| 56 | + Directory.CreateDirectory(workingDir); |
| 57 | + foreach (string file in Directory.EnumerateFiles(testCaseDir, "*", SearchOption.AllDirectories)) |
| 58 | + { |
| 59 | + string relativePath = file.Substring(testCaseDir.Length + 1); |
| 60 | + string destintion = Path.Combine(workingDir, relativePath); |
| 61 | + File.Copy(file, destintion); |
| 62 | + } |
| 63 | + |
| 64 | + File.Copy(Path.Combine(BaseTestCaseDir, NuGetConfigFile), Path.Combine(workingDir, NuGetConfigFile)); |
| 65 | + |
| 66 | + string packagesConfigFile = Path.Combine(workingDir, PackagesConfigFileName); |
| 67 | + Assert.True(File.Exists(packagesConfigFile)); |
| 68 | + |
| 69 | + // Restore the project |
| 70 | + ProcessStartInfo startInfo = new() |
| 71 | + { |
| 72 | + FileName = "nuget.exe", |
| 73 | + Arguments = "restore", |
| 74 | + UseShellExecute = false, |
| 75 | + CreateNoWindow = true, |
| 76 | + RedirectStandardOutput = true, |
| 77 | + RedirectStandardError = true, |
| 78 | + }; |
| 79 | + Process process = new Process() |
| 80 | + { |
| 81 | + StartInfo = new ProcessStartInfo() |
| 82 | + { |
| 83 | + FileName = "nuget.exe", |
| 84 | + Arguments = "restore", |
| 85 | + WorkingDirectory = workingDir, |
| 86 | + UseShellExecute = false, |
| 87 | + CreateNoWindow = true, |
| 88 | + RedirectStandardOutput = true, |
| 89 | + RedirectStandardError = true, |
| 90 | + }, |
| 91 | + EnableRaisingEvents = true, |
| 92 | + }; |
| 93 | + |
| 94 | + process.OutputDataReceived += (sender, eventArgs) => |
| 95 | + { |
| 96 | + if (eventArgs.Data != null) |
| 97 | + { |
| 98 | + TestOutputHelper.WriteLine(eventArgs.Data); |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + process.ErrorDataReceived += (sender, eventArgs) => |
| 103 | + { |
| 104 | + if (eventArgs.Data != null) |
| 105 | + { |
| 106 | + TestOutputHelper.WriteLine(eventArgs.Data); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + process.Start(); |
| 111 | + process.BeginOutputReadLine(); |
| 112 | + process.BeginErrorReadLine(); |
| 113 | + process.WaitForExit(); |
| 114 | + Assert.Equal(0, process.ExitCode); |
| 115 | + |
| 116 | + // Run the conversion |
| 117 | + ProjectConverterSettings converterSettings = new() |
| 118 | + { |
| 119 | + Include = ConverterInclude, |
| 120 | + Log = new TestLog(TestOutputHelper), |
| 121 | + RepositoryRoot = workingDir, |
| 122 | + }; |
| 123 | + ProjectConverter converter = new(converterSettings); |
| 124 | + converter.ConvertRepository(CancellationToken.None); |
| 125 | + |
| 126 | + // File was deleted |
| 127 | + Assert.False(File.Exists(packagesConfigFile)); |
| 128 | + |
| 129 | + string expectedProjectContent = File.ReadAllText(Path.Combine(testCaseDir, AfterProjectName)); |
| 130 | + string actualProjectContent = File.ReadAllText(Path.Combine(workingDir, BeforeProjectName)); |
| 131 | + Assert.Equal(expectedProjectContent, actualProjectContent); |
| 132 | + } |
| 133 | + |
| 134 | + private sealed class TestLog : ILog |
| 135 | + { |
| 136 | + private const string DebugLevel = "Debug"; |
| 137 | + private const string ErrorLevel = "Error"; |
| 138 | + private const string FatalLevel = "Fatal"; |
| 139 | + private const string InfoLevel = "Info"; |
| 140 | + private const string WarnLevel = "Warn"; |
| 141 | + |
| 142 | + private readonly ITestOutputHelper _testOutputHelper; |
| 143 | + |
| 144 | + public TestLog(ITestOutputHelper testOutputHelper) |
| 145 | + { |
| 146 | + _testOutputHelper = testOutputHelper; |
| 147 | + } |
| 148 | + |
| 149 | + public bool IsDebugEnabled => true; |
| 150 | + |
| 151 | + public bool IsInfoEnabled => true; |
| 152 | + |
| 153 | + public bool IsWarnEnabled => true; |
| 154 | + |
| 155 | + public bool IsErrorEnabled => true; |
| 156 | + |
| 157 | + public bool IsFatalEnabled => true; |
| 158 | + |
| 159 | + public ILogger Logger => throw new NotImplementedException(); |
| 160 | + |
| 161 | + public void Debug(object message) => Log(DebugLevel, message); |
| 162 | + |
| 163 | + public void Debug(object message, Exception exception) => Log(DebugLevel, message, exception); |
| 164 | + |
| 165 | + public void DebugFormat(string format, params object[] args) => Log(DebugLevel, format, args); |
| 166 | + |
| 167 | + public void DebugFormat(string format, object arg0) => Log(DebugLevel, string.Format(format, arg0)); |
| 168 | + |
| 169 | + public void DebugFormat(string format, object arg0, object arg1) => Log(DebugLevel, string.Format(format, arg0, arg1)); |
| 170 | + |
| 171 | + public void DebugFormat(string format, object arg0, object arg1, object arg2) => Log(DebugLevel, string.Format(format, arg0, arg2)); |
| 172 | + |
| 173 | + public void DebugFormat(IFormatProvider provider, string format, params object[] args) => Log(DebugLevel, string.Format(provider, format, args)); |
| 174 | + |
| 175 | + public void Error(object message) => Log(ErrorLevel, message); |
| 176 | + |
| 177 | + public void Error(object message, Exception exception) => Log(ErrorLevel, message, exception); |
| 178 | + |
| 179 | + public void ErrorFormat(string format, params object[] args) => Log(ErrorLevel, format, args); |
| 180 | + |
| 181 | + public void ErrorFormat(string format, object arg0) => Log(ErrorLevel, string.Format(format, arg0)); |
| 182 | + |
| 183 | + public void ErrorFormat(string format, object arg0, object arg1) => Log(ErrorLevel, string.Format(format, arg0, arg1)); |
| 184 | + |
| 185 | + public void ErrorFormat(string format, object arg0, object arg1, object arg2) => Log(ErrorLevel, string.Format(format, arg0, arg2)); |
| 186 | + |
| 187 | + public void ErrorFormat(IFormatProvider provider, string format, params object[] args) => Log(ErrorLevel, string.Format(provider, format, args)); |
| 188 | + |
| 189 | + public void Fatal(object message) => Log(FatalLevel, message); |
| 190 | + |
| 191 | + public void Fatal(object message, Exception exception) => Log(FatalLevel, message, exception); |
| 192 | + |
| 193 | + public void FatalFormat(string format, params object[] args) => Log(FatalLevel, format, args); |
| 194 | + |
| 195 | + public void FatalFormat(string format, object arg0) => Log(FatalLevel, string.Format(format, arg0)); |
| 196 | + |
| 197 | + public void FatalFormat(string format, object arg0, object arg1) => Log(FatalLevel, string.Format(format, arg0, arg1)); |
| 198 | + |
| 199 | + public void FatalFormat(string format, object arg0, object arg1, object arg2) => Log(FatalLevel, string.Format(format, arg0, arg2)); |
| 200 | + |
| 201 | + public void FatalFormat(IFormatProvider provider, string format, params object[] args) => Log(FatalLevel, string.Format(provider, format, args)); |
| 202 | + |
| 203 | + public void Info(object message) => Log(InfoLevel, message); |
| 204 | + |
| 205 | + public void Info(object message, Exception exception) => Log(InfoLevel, message, exception); |
| 206 | + |
| 207 | + public void InfoFormat(string format, params object[] args) => Log(InfoLevel, format, args); |
| 208 | + |
| 209 | + public void InfoFormat(string format, object arg0) => Log(InfoLevel, string.Format(format, arg0)); |
| 210 | + |
| 211 | + public void InfoFormat(string format, object arg0, object arg1) => Log(InfoLevel, string.Format(format, arg0, arg1)); |
| 212 | + |
| 213 | + public void InfoFormat(string format, object arg0, object arg1, object arg2) => Log(InfoLevel, string.Format(format, arg0, arg2)); |
| 214 | + |
| 215 | + public void InfoFormat(IFormatProvider provider, string format, params object[] args) => Log(InfoLevel, string.Format(provider, format, args)); |
| 216 | + |
| 217 | + public void Warn(object message) => Log(WarnLevel, message); |
| 218 | + |
| 219 | + public void Warn(object message, Exception exception) => Log(WarnLevel, message, exception); |
| 220 | + |
| 221 | + public void WarnFormat(string format, params object[] args) => Log(WarnLevel, format, args); |
| 222 | + |
| 223 | + public void WarnFormat(string format, object arg0) => Log(WarnLevel, string.Format(format, arg0)); |
| 224 | + |
| 225 | + public void WarnFormat(string format, object arg0, object arg1) => Log(WarnLevel, string.Format(format, arg0, arg1)); |
| 226 | + |
| 227 | + public void WarnFormat(string format, object arg0, object arg1, object arg2) => Log(WarnLevel, string.Format(format, arg0, arg2)); |
| 228 | + |
| 229 | + public void WarnFormat(IFormatProvider provider, string format, params object[] args) => Log(WarnLevel, string.Format(provider, format, args)); |
| 230 | + |
| 231 | + private void Log(string level, object message) => _testOutputHelper.WriteLine($"[{level}] {message}"); |
| 232 | + |
| 233 | + private void Log(string level, object message, Exception exception) => _testOutputHelper.WriteLine($"[{level}] {message}. Exception: {exception}"); |
| 234 | + |
| 235 | + private void Log(string level, string format, params object[] args) => _testOutputHelper.WriteLine($"[{level}] {format}", args); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments