Skip to content

Instantly share code, notes, and snippets.

@seesharper
Last active August 29, 2015 14:23
Show Gist options
  • Save seesharper/d292ddf21027bf6b3dc8 to your computer and use it in GitHub Desktop.
Save seesharper/d292ddf21027bf6b3dc8 to your computer and use it in GitHub Desktop.
Migrate MsTest files top Xunit
using System.Text.RegularExpressions;
string path = null;
if (Env.ScriptArgs.Count() == 0)
{
path = Directory.GetCurrentDirectory();
}
else
{
path = Env.ScriptArgs[0];
}
Console.WriteLine("Migrating MsTest files in {0}", path);
var files = GetFiles(path);
Console.WriteLine("Found {0} files", files.Length);
foreach (var file in files)
{
Console.WriteLine("Migrating {0}", file);
var source = ReadFile(file);
var result = Migrate(source);
WriteFile(file, result);
}
if (files.Length > 0)
{
Console.WriteLine("Migrated {0}", files.Length);
}
private static string ReadFile(string pathToFile)
{
using(var reader = new StreamReader(pathToFile))
{
return reader.ReadToEnd();
}
}
private static void WriteFile(string pathToFile, string content)
{
using(var writer = new StreamWriter(pathToFile))
{
writer.Write(content);
}
}
private static string[] GetFiles(string path)
{
return Directory.GetFiles(path, "*.cs");
}
private static string Migrate(string source)
{
source = source.Replace("[TestMethod]", "[Fact]")
.Replace("[TestClass]", "")
.Replace("Assert.AreEqual", "Assert.Equal")
.Replace("Assert.AreNotEqual", "Assert.NotEqual")
.Replace("Assert.IsTrue", "Assert.True")
.Replace("Assert.IsFalse", "Assert.False")
.Replace("Assert.IsNotNull", "Assert.NotNull")
.Replace("Assert.IsNull", "Assert.Null")
.Replace("Assert.AreNotSame", "Assert.NotSame")
.Replace("Assert.AreSame", "Assert.Same")
.Replace("using Microsoft.VisualStudio.TestTools.UnitTesting", "using Xunit");
source = HandleIsInstanceOfType(source);
source = HandleIsNotInstanceOfType(source);
return source;
}
private static string HandleIsInstanceOfType(string source)
{
Regex regex = new Regex(@"(Assert.IsInstanceOfType)\((.*),\s(typeof.*)\)");
return regex.Replace(source, "Assert.IsType($3, $2)");
}
private static string HandleIsNotInstanceOfType(string source)
{
Regex regex = new Regex(@"(Assert.IsNotInstanceOfType)\((.*),\s(typeof.*)\)");
return regex.Replace(source, "Assert.IsNotType($3, $2)");
}
scriptcs .\migrate.csx -- directoryContainingMsTestFiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment