Monday, May 5, 2014

Pimp your Bash console on Windows

Since I have been doing so much Git work I wanted to post some things I do to pimp my Bash console out.

First thing is we need to get to our home directory.  Do this by typing this in your bash console:

cd

This should change your prompt to point to ~.  The next thing is we need to see if there is already a .bashrc file.  The easiest way I have found to do this is to type the following in the bash console:

explorer .

This should open a windows explorer window in your home directory.  Check to see if a .bashrc file exists.  If this file does not exist type the following in the bash console window:

touch .bashrc

Go back to windows explorer and you should now see the .bsahrc file.  Open the file in your favorite text editor.  Here is an example of what I have been putting in my .basrc file:

alias brc="source ~/.bashrc"
alias cls=clear
alias ..='cd ..'
alias cd..='cd ..'
alias dir='ls --color -X'
alias e='explorer .'

h()
{
    cd ~/skydrive/dev;
}

cl() {
    d=$1
    if [[ -z "$d" ]]; then
        d=$HOME
    fi
    if [[ -d "$d" ]]; then
        cd "$d"
        dir
    else
        echo "bash: cl: '$d': Directory not found"
    fi
}



brc an easy way to reload the bashrc file while in the bash console.  This way you don’t have to restart the console each time you make a change.
cls I am used to typing this in DOS
.. a shortcut to go back a directory
cd.. bash wants a space between the cd and the .. this alias lets me forget the space and it doesn’t complain about it.
dir I am used to typing this in DOS
e shortcut to open explorer in the current directory
h shortcut to my start directory where all my code is
cl change directory and list its contents

Saturday, March 29, 2014

Git Config Commands

I have been doing a bunch of work with Git lately.  I wanted to do a post about some commands I don’t want to forget.

Who are you?
git config --global user.name '<name goes here>'
git config --global user.email '<email address here>’

Turn off https verification
We are using Stash and its using a self signed certificate.  This command allows us to access the Stash repositories over HTTPS.

git config --global http.sslVerify false

Make notepadd++ your editor (no more vim)
I am not a VIM kinda guy so this was a huge help when using things like rebase.

git config --global core.editor '"C:/Program Files (x86)/Notepad++/notepad++.exe" -multiInst -nosession –noPlugin'

Set Visual Studio 2013 as your diff tool
git config --global difftool.prompt true
git config --global difftool.vs2013.cmd '"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\vsdiffmerge.exe" "$LOCAL" "$REMOTE" //t'
git config --global difftool.vs2013.keepbackup false
git config --global difftool.vs2013.trustexistcode true
git config --global diff.tool vs2013

Set Visual Studio 2013 as your merge tool
git config --global mergetool.prompt true
git config --global mergetool.vs2013.cmd '"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\vsdiffmerge.exe" "$REMOTE" "$LOCAL" "$BASE" "$MERGED" //m'
git config --global mergetool.vs2013.keepbackup false
git config --global mergetool.vs2013.trustexistcode true
git config --global merge.tool vs2013

Log Alias
This alias I find really useful. I got this from one of my co-workers.  Here is a link to where it came form (source).

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Saturday, February 22, 2014

Unit Testing Async Methods with Moq

The other day I was pairing with a colleague.  We where working on unit testing some classes that where using the new async and await features.  We ran into some issue trying to get mocking working for our classes dependencies. Here is some example code on how we got things to work.

So given the following code:

public interface IClass2
{
    Task<string> GetSomeString_MethodAsync();
}

public class Class2 : IClass2
{
    public async Task<string> GetSomeString_MethodAsync()
    {
        await Task.Run(() => Thread.Sleep(2000));
        return "hello world.";
    }
}

public class Class1
{
    private IClass2 class2;

    public Class1(IClass2 class2)
    {
        this.class2 = class2;
    }

    public async Task<string> SomeMethod()
    {
        return await this.class2.GetSomeString_MethodAsync();
    }
}

And we can test it like this:

private Mock<IClass2> class2Mock;
private Class1 class1;

[TestInitialize]
public void Setup()
{
    this.class2Mock = new Mock<IClass2>();
    this.class1 = new Class1(this.class2Mock.Object);
}
            
[TestMethod]
public async Task SomeMethodTest()
{
    var expectedString = "Hello World";
    var tcs = new TaskCompletionSource<string>();
    tcs.SetResult(expectedString);
    this.class2Mock.Setup(x => x.GetSomeString_MethodAsync()).Returns(tcs.Task);

    var result = await this.class1.SomeMethod();

    Assert.AreEqual(result, expectedString);
}

Note the usage of the TaskCompletionSource in the unit test along with the declaration of the test using “async Task”.