Last active
December 16, 2020 01:25
-
-
Save goldytech/4e7bb414e9d1a0d7e40a to your computer and use it in GitHub Desktop.
Convert MSTest to XUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.RegularExpressions; | |
using System; | |
using System.IO; | |
string path = string.Empty; | |
if (Env.ScriptArgs.Count() == 1) | |
{ | |
path = Env.ScriptArgs[0]; | |
} | |
else | |
{ | |
Console.WriteLine("File name argument not found"); | |
//return; | |
} | |
var source = ReadMsTestFile(path); | |
var result = Transform(source); | |
WriteFile(path,result); | |
Console.WriteLine("Conversion completed"); | |
private static string ReadMsTestFile(string file) | |
{ | |
using(var reader = new StreamReader(file)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
private static void WriteFile(string pathToFile, string content) | |
{ | |
using(var writer = new StreamWriter(pathToFile)) | |
{ | |
writer.Write(content); | |
} | |
} | |
private static string Transform(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)"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like in the 5 months since you wrote this, Microsoft has already changed 'Env.ScriptArgs' twice in breaking ways.
For the moment, the way to do this is:
if (Args.Count() == 1)
{
path = Args[0];
}
Then you can call this using mono on linux as follows:
find -name '*.cs' | xargs -n 1 bash -c 'csi convert.csx $0'
Also a note for users of this:
[TestCleanup]
becomes an implementation of Dispose, so you need to extend IDisposeable and implement that method.
[TestInitialize]
becomes a constructor
Also one more regex for you (note that if there are commas in the first string argument, this won't work right):
private static string HandleStringAssertContains(string source)
{
Regex regex = new Regex(@"(StringAssert.Contains)((.?), (.))");
return regex.Replace(source, "Assert.True($3.Contains($2))");
}