Git: apply a lost stash

Today’s scenario: stash save something in order to get the latest source code, stash pop to re-apply the last changes, do something silly and lose all in-flight changes.
Fear not, there’s a way to recover the “lost” stash, assuming you still have the output of the stash pop.
In fact, after the pop operation there should be a line that goes like this:

Dropped refs/stash@{0} (f2acd7f56e93236bbb813a9dab3bba18e124da04)

Armed with the SHA code, just cherry-pick it:

git cherry-pick f2acd7f56e93236bbb813a9dab3bba18e124da04 -m 1

.
Watch out: this will commit your change as it is. You may want to git reset --soft HEAD^1 and git commit --amend accordingly!

Advertisements

Running Coded-UI automated tests from the command line

Short and concise post, more like a memory aid for myself, about running Coded-UI tests without the need of the Visual Studio GUI. Disclaimer: I’m talking about VS 2012 (though this may apply to VS 2010 too).

According to MSDN,

MSTest is used to run load test in Visual Studio 2012. By default, unit tests and coded UI tests use VSTest.Console.exe in Visual Studio 2012. However, MSTest is used for compatibility with test projects created using Visual Studio 2010, or if a .testsettings file is manually added to a Visual Studio 2012 solution containing a unit test project, or coded UI test project.

Indeed you can run Coded-UI tests from the VS 2012 command prompt, by simply issuing VSTest.Console.exe NameOfYourTestSuite.dll. However, you could do the same by issuing MSTest.exe NameOfYourTestSuite.dll without the need of a .testsettings file.

It goes without saying, beware that if you try running your tests using MSTest from within a VS 2010 prompt, you’ll most likely end up having the command whinging like this: Unable to load the test container 'NameOfYourTestSuite.dll' or one of its dependencies. Error details: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITesting, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

That said, if you want to be able to run similar tests on a machine that does not have VS 2012 installed, you just need to download the Agents for Visual Studio 2012.

String manipulation from Python to PowerShell

Having spent most of my time so far in Python and Bash, it feels quite natural for me to try and find equivalent commands to the ones I used to use. Here’s a very brief list.

Even though PowerShell’s strong point is working with objects, I need from time to time to split or join strings. In Python both functions are methods of the string object (which, in the case of join, made me a little perplexed in the beginning). InPowerShell, they are both operators. To put it in practice, Python’s "a_b_c".split("_") and "+".join(["a","b","c"]) become "a_b_c" -split "_" and "a","b","c" -join "+", respectively, in PowerShell. Must say, in this case, I like PowerShell syntax better as far as clarity is concerned.

However, in Powershell, both join and split can also be used as unary operators – rather confusingly. That’s to say, if you don’t need to specify any argument (such as the delimeter as we did above) you could use the operators as follows: -split "a b c" and -join ("a","b","c"). Mind the parentheses! See for yourself what happens if you omit them, and check help about_join to get an explanation.

String formatting is another task I do on a regular basis. "{0} is {1}".format("learning","fun") is one of Python’s ways to do that (although it can become much more complex) and, quite conveniently, this is the PowerShell way: "{0} is {1}" -f "learning","fun". Not really that different, uh?

Another relatively common task is type casting. Say you want to cast a number as string. In Python you’d do str(42), in PowerShell you’ll type 42 -as [string] (or the convenient shorthand [string]42).

Enough for today, next time objects finally!

This is part of a series of posts on PowerShell. Some of these tips come from the book “PowerShell in Depth“.