Replace MSTest to VSTest to support Fakes

I’ve created a unit test using MS Fakes Framework. When I tried to run it from the build script, I’ve got an error:
Test method DateTimeHelperTests.ConvertServerTimeToTimeZoneTimeTest threw exception:
Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException: Failed to resolve profiler path from COR_PROFILER_PATH and COR_PROFILER environment variables.
The answer of
http://stackoverflow.com/questions/26743342/running-a-test-using-shims-on-a-visual-studio-2013-test-agent told that instead of “using mstest.exe  I should have been using vstest.console.exe.”
There are a couple articles that explain the differences between MSTest and VSTest.console.exe.
I’ve created below table to describe parameters that should be changed if you move from MSTest to VSTest.
MSTestExe
VSTest.Console.exe
Comments
C:\Program Files (x86)\Microsoft Visual Studio 14\Common7\IDE\MSTest.exe
C:\Program Files (x86)\Microsoft Visual Studio 14\Common7\IDE\CommonExtensions\Microsoft\ TestWindow\VSTest.Console.exe
Full Path for typical installation
mstest /testcontainer:tests.dll
Vstest.console.exe myTestFile.dll myOtherTestFile.dll
Pass test.dll
/resultsfile:testResults.trx
/Logger:trx
TestResults\UserName_MachineName 2016-04-07 18_12_02.trx
/category:!Broken
/TestCaseFilter:”TestCategory!=Broken”
  /testsettings:Local.TestSettings
/Settings:[file name] e.g.
 /Settings:Local.RunSettings
mstest /testcontainer: “C:\TestProject2\myTestFile.dll” /testsettings:Local.TestSettings /category:!Broken /resultsfile:testResults.trx
vstest.console.exe  “C:\TestProject2\myTestFile.dll /Settings:Local.RunSettings /TestCaseFilter:”TestCategory=!Broken” /Logger:trx
Full example
 Tests.runsettings
<!– MSTest adapter –>
<MSTest>
<MapInconclusiveToFailed>True </MapInconclusiveToFailed>
<CaptureTraceOutput>false</CaptureTraceOutput>
<DeleteDeploymentDirectoryAfterTestRunIsComplete> False </DeleteDeploymentDirectoryAfterTestRunIsComplete>
<DeploymentEnabled>False</DeploymentEnabled>
</MSTest>
</RunSettings>
RunSettings for vstest.console adapter
/test:[test name]
to run multiple tests, use the /test option multiple times.
TestClass matches if the name Contains pattern
/Tests:[test name]
To provide multiple values, separate them by commas. Example: /Tests:TestMethod1,testMethod2
Classes and Methods matches if the name contains pattern
Also VSTest is running all tests in a single thread, so there is no proper isolation between tests and settings in one tests may effect subsequent tests.
I had to fix a few tests before the whole test project passed under VSTest.

#fakes, #unit-tests, #vstest

AutoPropertiesToString helper method

I needed in ToString() method for a class to log auto-properties.  Below are two methods that I used

/// <summary>

///

/// </summary>

/// <param name=”obj”></param>

/// <param name=”declaredOnly”></param>

/// <returns></returns>

/// <remarks> Created to log  DeclaredOnly in the current class using GetType().GetProperties(), for full object dump other methods may be more appropriate

/// See  http://stackoverflow.com/questions/4023462/how-do-i-automatically-display-all-properties-of-a-class-and-their-values-in-a-s

/// and  http://stackoverflow.com/questions/852181/c-printing-all-properties-of-an-object?lq=1

///  </remarks>

public static string AutoPropertiesToString( object obj, bool printNulls=false, bool declaredOnly=true)

{

BindingFlags flags = BindingFlags .Public |  BindingFlags.Instance;

if (declaredOnly) flags |= BindingFlags.DeclaredOnly;

var propertyInfos = obj.GetType().GetProperties(flags);

var sb = new StringBuilder();

foreach ( var info in propertyInfos)

{

if (info.IsAutoProperty())

{

var val = info.GetValue(obj, null);

if (printNulls == false && val == null)

continue;

var value = val ?? “(null)”;

sb.AppendLine(info.Name + “: ” + value);

}

}
return sb.ToString();

}
/// <summary>

/// It’s not fool proof, quite brittle

/// </summary>

/// <param name=”info”></param>

/// <returns></returns>

/// <remarks> from http://stackoverflow.com/questions/2210309/how-to-find-out-if-a-property-is-an-auto-implemented-property-with-reflection </remarks>

public static bool IsAutoProperty( this PropertyInfo info)

{

bool mightBe = info.GetGetMethod().GetCustomAttributes(typeof (CompilerGeneratedAttribute ),true ).Any();

if (!mightBe)

{

return false;

}

if (info.DeclaringType == null)

return false;

bool maybe = info.DeclaringType

.GetFields( BindingFlags.NonPublic | BindingFlags.Instance)

.Where(f => f.Name.Contains(info.Name))

.Where(f => f.Name.Contains( “BackingField”))

.Any(f => f.GetCustomAttributes(typeof (CompilerGeneratedAttribute ),true ).Any());
return maybe;

}

#net, #auto-properties, #tostring

bad revision ‘rm’ error, when using git filter-branch –index-filter if called from batch file

I was trying to move a few directories from one repository to another and answer http://stackoverflow.com/a/17867910/52277 suggests to use

    git filter-branch –index-filter ‘git rm –cached -qr –ignore-unmatch — . && git reset -q $GIT_COMMIT — apps/AAA libs/XXX’ –prune-empty — –all
I’ve put the command into batch file and got the error
> fatal: bad revision ‘rm’
The same error happened for an example from git documentation https://git-scm.com/docs/git-filter-branch
    git filter-branch –index-filter ‘git rm –cached –ignore-unmatch — buildMaster.bat’ HEAD
However when I was running the command in git bash, it didn’t  show any error and worked successfully.

It was acceptable for me. But I am still curious why including inside .bat cause an error with unclear message

#batch, #branches, #error, #git

Windows Feedback application in Windows 10 is very immature.

Recently I wanted to submit feedback to mIcrosoft and had to use Windows Feedback application in Windows 10. I was surprised how immature it is. They have Connect website  https://connect.microsoft.com that working fine for many applications, including visual studio and SQL server. They also using Uservoice sites, that also have acceptable user interface( apart of stupid limit for number of votes). What was the reason to create new proprietary application, supported only by Windows 10.

Link in MS Feedback is generated as something like

 Windows-Feedback:?contextid=79&feedbackid=b11f8bdf-eaf4-4799-8e65-5161924ad22c&form=1&src=2

As you can expect, browsers do not understand these links. Even when I copied the link into feedback search field, it didn’t find it.

When I upvoted existing feedback, I’ve added a comment. However, when I open the same feedback again, my comments were not visible. They  should show the all discussion, not only initial suggestion.
I am not able to copy text of existing feedback. I opened it but text is not selectable. I tried to share via mail, but the feedback is added as image, not as text. 
It will be much better if MS will create Web client for feedback database to be viewed in normal browser.

#feedback, #windows

Powershell script that requires administrative privileges.

For Powershell it can be done easier
#from http://serverfault.com/a/12306/107224 
function Run-Elevated ($scriptblock )
{
  # TODO: make -NoExit a parameter
  # TODO: just open PS (no -Command parameter) if $scriptblock -eq ”
  $sh = new-object -com ‘Shell.Application’
  $sh. ShellExecute(‘powershell’, “-NoExit -Command $scriptblock , , ‘runas’)
}
Run-Elevated  “cd  $PSScriptRoot; ActualScript.ps1  ; Read-Host -Prompt `”Press_Enter_to_continue`”  ” 

#powershell-batch

Global Convertion from MStest Assert to FluentAssertions

I found that FluentAssertions http://www.fluentassertions.com/ provide more descriptive logs in case of assertion failure.
However most of our test methods use MSTest Assert class.
For example statement
 Assert.IsTrue(string.IsNullOrEmpty(discountItemRecord.DiscountCode));
should  be changed to
(string.IsNullOrEmpty(discountItemRecord.DiscountCode)).Should().BeTrue();
They can be easily converted with the help of Visual Studio Replace. Just  select “use Regular Expressions”
For example
Search for:   \s*Assert.IsTrue\((.*)\);             replace to:      ($1).Should().BeTrue();
Search for:   \s*Assert.IsFalse\((.*)\);            replace to:      ($1).Should().BeFalse();
Search for:   \s*Assert.IsNotNull\((.*)\);        replace to:   ($1).Should().NotBeNull();
Search for:  (\s*)Assert.AreEqual\((.*),(.*)\);replace to:    $1($3).Should().Be($2);
I’ve used only most common asserts,that we are using,but they can be extended for other asserts.
Example of regular expression can be found on regex101

#fluentassertions, #regex, #unit-tests

How I moved my blog from GeeksWithBlogs to WordPress.

When I decided to move my blog, I didn’t expect that it will require such effort. I found a post http://geekswithblogs.net/jakob/archive/2015/12/07/this-blog-is-moving.aspx that refer to tools stored on codePlex http://gwbtowp.codeplex.com/  (now it’s moved to https://github.com/MrHinsh/gwb-to-wordpress)
I downloaded the code  and had to fix a few errors, related to my data. Also I wanted to move my articles that in GeeksWithBlogs are stored separately from normal posts.The final code changes I’ve published at github fork https://github.com/MNF/gwb-to-wordpress.
I expected that import to WordPress will be straightforward.I found a few articles about  import plugins, e.g. http://nixmash.com/on-wordpress/importing-a-big-honkin-blogml-xml-file-into-wordpress/, but then realized that custom plugins not supported in Wordpress.com.
blogmigrator has less steps to do, and I tried it first. Also I have to do some changes to improve error handling(the changes are incorporated in https://github.com/Dillie-O/blogmigrator) and finally I’ve got my blog imported. But comments were not included. I checked the code and found that comments were not supported in interface that the tool is using.
BlogML.Helper supported comments, but due to bug only one comment per post. I fixed the bug as well as insufficient error logging and finally was able to import my blog to WordPress.
Thanks to the developers who shared their code to help me to finish this work.
By the way import to WordPress also has a few problems, that I had to report to support forum, but they were either resolved or had some workarounds.

#blog-migration