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

Add e2e tests #45

Merged
merged 1 commit into from
Aug 7, 2024
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
229 changes: 229 additions & 0 deletions src/PackagesConfigConverter.UnitTests/E2ETests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
// Copyright (c) Jeff Kluge. All rights reserved.
//
// Licensed under the MIT license.

using log4net;
using log4net.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using Xunit;
using Xunit.Abstractions;

namespace PackagesConfigConverter.UnitTests
{
public class E2ETests : TestBase
{
private const string BaseTestCaseDir = @"TestData\E2ETests";
private const string BaseWorkingDir = @"_work";
private const string NuGetConfigFile = "NuGet.config";
private const string PackagesConfigFileName = "packages.config";
private const string BeforeProjectName = "before.csproj";
private const string AfterProjectName = "after.csproj";

private static readonly Regex ConverterInclude = new Regex($"{BeforeProjectName}$", RegexOptions.Compiled);

public E2ETests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

public static IEnumerable<object[]> TestCases()
{
foreach (string dir in Directory.EnumerateDirectories(BaseTestCaseDir))
{
string testCase = Path.GetFileName(dir);
yield return new object[] { testCase };
}
}

[Theory]
[MemberData(nameof(TestCases))]
public void E2ETest(string testCase)
{
// Copy test files to a working dir
string testCaseDir = Path.Combine(BaseTestCaseDir, testCase);
string workingDir = Path.Combine(BaseWorkingDir, testCase);

if (Directory.Exists(workingDir))
{
Directory.Delete(workingDir, recursive: true);
}

Directory.CreateDirectory(workingDir);
foreach (string file in Directory.EnumerateFiles(testCaseDir, "*", SearchOption.AllDirectories))
{
string relativePath = file.Substring(testCaseDir.Length + 1);
string destintion = Path.Combine(workingDir, relativePath);
File.Copy(file, destintion);
}

File.Copy(Path.Combine(BaseTestCaseDir, NuGetConfigFile), Path.Combine(workingDir, NuGetConfigFile));

string packagesConfigFile = Path.Combine(workingDir, PackagesConfigFileName);
Assert.True(File.Exists(packagesConfigFile));

// Restore the project
var process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "nuget.exe",
Arguments = "restore",
WorkingDirectory = workingDir,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
},
EnableRaisingEvents = true,
};

process.OutputDataReceived += (sender, eventArgs) =>
{
if (eventArgs.Data != null)
{
TestOutputHelper.WriteLine(eventArgs.Data);
}
};

process.ErrorDataReceived += (sender, eventArgs) =>
{
if (eventArgs.Data != null)
{
TestOutputHelper.WriteLine(eventArgs.Data);
}
};

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
Assert.Equal(0, process.ExitCode);

// Run the conversion
var converterSettings = new ProjectConverterSettings()
{
Include = ConverterInclude,
Log = new TestLog(TestOutputHelper),
RepositoryRoot = workingDir,
};
var converter = new ProjectConverter(converterSettings);
converter.ConvertRepository(CancellationToken.None);

// File was deleted
Assert.False(File.Exists(packagesConfigFile));

string expectedProjectContent = File.ReadAllText(Path.Combine(testCaseDir, AfterProjectName));
string actualProjectContent = File.ReadAllText(Path.Combine(workingDir, BeforeProjectName));
Assert.Equal(expectedProjectContent, actualProjectContent);
}

private sealed class TestLog : ILog
{
private const string DebugLevel = "Debug";
private const string ErrorLevel = "Error";
private const string FatalLevel = "Fatal";
private const string InfoLevel = "Info";
private const string WarnLevel = "Warn";

private readonly ITestOutputHelper _testOutputHelper;

public TestLog(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

public bool IsDebugEnabled => true;

public bool IsInfoEnabled => true;

public bool IsWarnEnabled => true;

public bool IsErrorEnabled => true;

public bool IsFatalEnabled => true;

public ILogger Logger => throw new NotImplementedException();

public void Debug(object message) => Log(DebugLevel, message);

public void Debug(object message, Exception exception) => Log(DebugLevel, message, exception);

public void DebugFormat(string format, params object[] args) => Log(DebugLevel, format, args);

public void DebugFormat(string format, object arg0) => Log(DebugLevel, string.Format(format, arg0));

public void DebugFormat(string format, object arg0, object arg1) => Log(DebugLevel, string.Format(format, arg0, arg1));

public void DebugFormat(string format, object arg0, object arg1, object arg2) => Log(DebugLevel, string.Format(format, arg0, arg2));

public void DebugFormat(IFormatProvider provider, string format, params object[] args) => Log(DebugLevel, string.Format(provider, format, args));

public void Error(object message) => Log(ErrorLevel, message);

public void Error(object message, Exception exception) => Log(ErrorLevel, message, exception);

public void ErrorFormat(string format, params object[] args) => Log(ErrorLevel, format, args);

public void ErrorFormat(string format, object arg0) => Log(ErrorLevel, string.Format(format, arg0));

public void ErrorFormat(string format, object arg0, object arg1) => Log(ErrorLevel, string.Format(format, arg0, arg1));

public void ErrorFormat(string format, object arg0, object arg1, object arg2) => Log(ErrorLevel, string.Format(format, arg0, arg2));

public void ErrorFormat(IFormatProvider provider, string format, params object[] args) => Log(ErrorLevel, string.Format(provider, format, args));

public void Fatal(object message) => Log(FatalLevel, message);

public void Fatal(object message, Exception exception) => Log(FatalLevel, message, exception);

public void FatalFormat(string format, params object[] args) => Log(FatalLevel, format, args);

public void FatalFormat(string format, object arg0) => Log(FatalLevel, string.Format(format, arg0));

public void FatalFormat(string format, object arg0, object arg1) => Log(FatalLevel, string.Format(format, arg0, arg1));

public void FatalFormat(string format, object arg0, object arg1, object arg2) => Log(FatalLevel, string.Format(format, arg0, arg2));

public void FatalFormat(IFormatProvider provider, string format, params object[] args) => Log(FatalLevel, string.Format(provider, format, args));

public void Info(object message) => Log(InfoLevel, message);

public void Info(object message, Exception exception) => Log(InfoLevel, message, exception);

public void InfoFormat(string format, params object[] args) => Log(InfoLevel, format, args);

public void InfoFormat(string format, object arg0) => Log(InfoLevel, string.Format(format, arg0));

public void InfoFormat(string format, object arg0, object arg1) => Log(InfoLevel, string.Format(format, arg0, arg1));

public void InfoFormat(string format, object arg0, object arg1, object arg2) => Log(InfoLevel, string.Format(format, arg0, arg2));

public void InfoFormat(IFormatProvider provider, string format, params object[] args) => Log(InfoLevel, string.Format(provider, format, args));

public void Warn(object message) => Log(WarnLevel, message);

public void Warn(object message, Exception exception) => Log(WarnLevel, message, exception);

public void WarnFormat(string format, params object[] args) => Log(WarnLevel, format, args);

public void WarnFormat(string format, object arg0) => Log(WarnLevel, string.Format(format, arg0));

public void WarnFormat(string format, object arg0, object arg1) => Log(WarnLevel, string.Format(format, arg0, arg1));

public void WarnFormat(string format, object arg0, object arg1, object arg2) => Log(WarnLevel, string.Format(format, arg0, arg2));

public void WarnFormat(IFormatProvider provider, string format, params object[] args) => Log(WarnLevel, string.Format(provider, format, args));

private void Log(string level, object message) => _testOutputHelper.WriteLine($"[{level}] {message}");

private void Log(string level, object message, Exception exception) => _testOutputHelper.WriteLine($"[{level}] {message}. Exception: {exception}");

private void Log(string level, string format, params object[] args) => _testOutputHelper.WriteLine($"[{level}] {format}", args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
<ItemGroup>
<ProjectReference Include="..\PackagesConfigConverter\PackagesConfigConverter.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="TestData\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{526743F1-40C3-4E9D-A23B-927EE3FC8583}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SomeProject</RootNamespace>
<AssemblyName>SomeProject</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" ExcludeAssets="Runtime, Compile" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" ExcludeAssets="Build" />
</ItemGroup>
<ItemGroup>
<Compile Include="SomeCompile.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<Content Include="SomeContent.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\..\..\packages\MSTest.TestAdapter.3.1.1\build\net462\MSTest.TestAdapter.props" Condition="Exists('..\..\..\..\packages\MSTest.TestAdapter.3.1.1\build\net462\MSTest.TestAdapter.props')" />
<Import Project="..\..\..\..\packages\Microsoft.NET.Test.Sdk.17.6.0\build\net462\Microsoft.NET.Test.Sdk.props" Condition="Exists('..\..\..\..\packages\Microsoft.NET.Test.Sdk.17.6.0\build\net462\Microsoft.NET.Test.Sdk.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{526743F1-40C3-4E9D-A23B-927EE3FC8583}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SomeProject</RootNamespace>
<AssemblyName>SomeProject</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.3.1.1\lib\net462\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.3.1.1\lib\net462\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="SomeCompile.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<Content Include="SomeContent.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\..\..\packages\Microsoft.NET.Test.Sdk.17.6.0\build\net462\Microsoft.NET.Test.Sdk.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.NET.Test.Sdk.17.6.0\build\net462\Microsoft.NET.Test.Sdk.props'))" />
<Error Condition="!Exists('..\..\..\..\packages\Microsoft.NET.Test.Sdk.17.6.0\build\net462\Microsoft.NET.Test.Sdk.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.NET.Test.Sdk.17.6.0\build\net462\Microsoft.NET.Test.Sdk.targets'))" />
<Error Condition="!Exists('..\..\..\..\packages\MSTest.TestAdapter.3.1.1\build\net462\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\MSTest.TestAdapter.3.1.1\build\net462\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\..\..\..\packages\MSTest.TestAdapter.3.1.1\build\net462\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\MSTest.TestAdapter.3.1.1\build\net462\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\..\..\..\packages\Microsoft.NET.Test.Sdk.17.6.0\build\net462\Microsoft.NET.Test.Sdk.targets" Condition="Exists('..\..\..\..\packages\Microsoft.NET.Test.Sdk.17.6.0\build\net462\Microsoft.NET.Test.Sdk.targets')" />
<Import Project="..\..\..\..\packages\MSTest.TestAdapter.3.1.1\build\net462\MSTest.TestAdapter.targets" Condition="Exists('..\..\..\..\packages\MSTest.TestAdapter.3.1.1\build\net462\MSTest.TestAdapter.targets')" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.NET.Test.Sdk" version="17.6.0" targetFramework="net462" />
<package id="MSTest.TestAdapter" version="3.1.1" targetFramework="net462" />
<package id="MSTest.TestFramework" version="3.1.1" targetFramework="net462" />
</packages>
Loading
Loading