Thursday, September 13, 2012

Where do the unit test code files go?

Recently I came into some very old post about handling unit test code name space and locations. It really looked a lot of effort for very simple tasks.

I don't put my unit test code into a separate project/assembly. The reason is simple, the unit test codes is not secondary citizen and shall be treated as seriously as "production" codes, if not more seriously. You may argue that shipping unit test codes to end user is meaningless. While, I kind of agree. So if you do want stripping the unit tests off your production assembly, a little trick will help, wrap all your unit test codes with a macro check:

#if DEBUG
using NUnit.Framework;
namespace your.name.space
{
}
#endif

and the final release build will not contain any unit test codes. Still your assembly will depend on NUnit assembly and by default the NUnit DLL will appear in your release folder. Removing it won't do any harm.

I came from Java camp and had the habit to put source code and test code side by side, that is, source code is under "project_root\src\" and test code under "project_root\Test\" folder. I know it's not a common practice in .NET world to have a "src" folder, but it does no harm and give me the feasibility to put more resource under the project root folder, besides the benefit of separate the unit test codes into different folders, so I'm going to stick with it. If you happen to have some convention to make folder structure appear in namespace structure, then another trick to overcome it  is, in project browser, select the "src" folder and press F4 to bring up the property windows, then set the "contribute to namespace" option to false.

Thursday, August 23, 2012

Recover Windows 7's Explorer from "No such interface supported" error

Yesterday I experienced a disaster. After I installed and removed Google Drive client, Windows 7 seemed gone crazy: if I click on a document, some times it was opened, sometimes it was not; if I click on an application, nothing happens; if I input the application name, e.g. cmd.exe, in the Run dialog, it always complains "No such interface supported".

I tried almost all the ways I could think / googled of:

  • using Windows restore to restore to the point before Google Drive was removed, it did not work. I could only find 1 restore point, which was wired.
  • try to use some registry cleaner as suggested by some internet posts, it did not work too. (though the way I start the application is a little trick that I'll talk about)
  • re-register all the DLLs on my hard drive, as some posts suggested, which also did not work
Here is the trick to execute an application, when Explorer and Run dialog does not work: it seemed in my case the shell extension to open some of the linked file extensions was still partially working, so I created a file with a non-registered extension and dbl click to open it, then select the application you want to execute. This way, I managed to still execute applications. But applications started from this way could not elevate to administrator mode. Luckily I already made cmd.exe by default run under administrator, so it was not a problem to me.

Back to the problem, I was lucky to have a backup local user account with administrator rights, and I decided to try it out and it was not poisoned as my domain account. So I guess that the problem was the registry database of my domain account. So after I have backup my files, I went to the "advanced system settings" dialog (type the quoted string in the search box on start menu), pressed the "settings" button in the "User profiles" group box, and deleted my domain's profile. All the files under my "C:\Users\MyUser" folder was gone. So do remember backup your files!

This worked for me, use at your own risk.

Wednesday, August 22, 2012

Push back to Gitblit over https, without a valid certificate

Now I have a Gitblit server running, waiting to be connected. The good thing about Gitblit is, you do not even need Git on the server, its package contains a Java implementation of Git. In the long run, you still need it, since the document says git-gc is not properly implemented.

If you already have a Git repository on your local PC, and you want to push it to Gitblit, it's quite simple:

1. Using Gitblit's web GUI to create a new repository, assign share mode. If you want to block anonymous access, make sure you select "authenticated view, clone & push".
2. Open the repository you just created, and you'll see a link started with https://, click the copy to clipboard icon to copy the link to clipboard
3. Start Git console and navigate to your local Git folder
4. Run below command: ( you shall be able to paste the link from clipboard )
  git remote add origin https://yourpcname:yourport/git/yourrepo
  git push origin master
and bang! you'll hit below error:
  "SSL certificate problem, verify that the CA cert is OK."
This is because Git will verify if the server's certificate is a valid certificate. If you are a poor man as I am, you probably don't have a valid certificate. Set an environment variable will make Git skip the verification step (I put this line in the ~/.bashrc file to avoid type it each time)
  $ export GIT_SSL_NO_VERIFY=true 
  $ git push origin master

This will push you master branch to Gitblit. Then you can go to web UI to verify it.

Another way to overcome this certificate issue is to create your own root certificate, import it as trusted root certificate, then create a certificate and sign it with the root certificate. 

Setup Gitblit on windows server 2003

Git is by its nature friendly to *nix systems. So setting up a Git repository on Windows has been really painful: quite several tools need to be integrated, sophisticated steps need to be performed. In short, it's time consuming, error prone, and hard. I tried it once, and do not want to try it again.

Recently I read about (forgot the article, sorry) this new OS project, Gitblit, which really make hosting Git repository on Windows a easy task. It's a Java application, which means it runs on not only *nix, but also Windows! They provide several packages for different usage scenarios, and also good document.

The setup process is very easy:

  • Download the "Gitblit Go" package, unzipped to the folder you plan to hold the software
  • Go to the folder, edit the installService.cmd file and change the "SET ARCH= " line. My server is x86 based, while the default setting is amd86
  • Edit the gitblit.properties, point git.repositoriesFolder to the folder where you hold your Git repositories. Note you should use forward slash even on Windows, e.g C:/GitRepo/. Change server.httpsBindInterface to empty to bind https to all ip addresses. By default it only binds to localhost, which will not allow other computers accessing it
  • Run makekeystore.cmd to create a self-certified key for https
  • Run installService.cmd to install it as a Windows service
  • Start the service and launch browser to navigate to https://yourpcname:8443, and login with admin/admin
  • Change the password of admin immediately.
And you are done!

You will want to examine the gitblit.properties for possible configurations. Most of the items are documented very well so for most items you can just read through the config file and config properly without searching through documents.

Friday, July 6, 2012

Some Bash tips from others

I'm mostly a Windows guy. All my computers are loaded with Windows. I know how to use *nix, though not very efficiently. Since I started using Git, I have to deal with Bash more and more, through MinGW. Thus it's better to pickup some tricks of Bash to work more efficiently.


  1. !$: will be replaced with the last parameter to the last command. For example, if you just executed a command like this:
       ls /a/very/long/path
    and then you want to know the detail of what's in the path, a simple way of doing it is:
      ls -l !$
    If !$ is modified with :h, then the last part of the path is removed.
  2. ^: leading ^ will start replacing last command line with something new. Coming from last example, if part of the path is wrong, then you can try this:
        ^wrong^correct
More info can be found in this link: http://samrowe.com/wordpress/advancing-in-the-bash-shell/

Monday, June 11, 2012

1st try on VS2012

After several hours download and installation, I finally can start trying the RC. From first sight, I really can not say I like the UI. Too much change and all black-white icon really make it hard to find the correct icon. Luckily I'm mainly a keyboard guy and does not use toolbars often. With R# EAP version installed, I don't really need to even drop down the stupid all capital case MENU. BTW, install the removeCAPS extension can help you easily turn it back to normal.

There are things I like about it. Even it's not released version, it's really stable, and the responsiveness is much better than VS2010. Also I can work with VS2010 solution file and project files without any problem. This is a must have feature for try out on projects you share with team.

Thursday, May 10, 2012

MSysGit mergetool broken after 1.7.7

I've been using Git on Windows (MSysGit) happily with BeyondCompare 3 for quite a while. Mostly I use BC3 to view changes. Occasionally I use it to merge conflicts. I don't merge very often, thanks to Git's powerful merging capability. It has worked fine. Recently I upgraded to version 1.7.10-preview20120409, and suddenly the git's merge command always fail with below error messages:

Merging:
my/path/filename.cs

Normal merge conflict for 'my/path/filename.cs':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (BC3):
git_path/libexec/git-core/mergetools/BC3: line 9: bcompare: command not found
my/path/filename.cs seems unchanged.
Was the merge successful? [y/n] n


Where is this line 9? It turns out that it seems MSysGit (or Git?) has put all merge tools to a folder named "git_path/libexec/git-core/mergetools/". In my installation, I can already find quite a bunch of tools:

araxis
bc3
defaults
deltawalker
diffuse
ecmerge
emerge
kdiff3
kompare
meld
opendiff
p4merge
tkdiff
tortoisemerge
vim
xxdiff


and they are actually bash scripts. For example, the bc3 file is like below:

diff_cmd () {
 "$merge_tool_path" "$LOCAL" "$REMOTE"
}

merge_cmd () {
 touch "$BACKUP"
 if $base_present
 then
  "$merge_tool_path" "$LOCAL" "$REMOTE" "$BASE" \
   -mergeoutput="$MERGED"
 else
  "$merge_tool_path" "$LOCAL" "$REMOTE" \
   -mergeoutput="$MERGED"
 fi
 check_unchanged
}

translate_merge_tool_path() {
 if type bcomp >/dev/null 2>/dev/null
 then
  echo bcomp
 else
  echo bcompare
 fi
}


and there is really is a line 9! Of course directly modify the file will do the trick. But I guess there are better ways. After testing around, trying to override the merge_tool_path environment variable, I found it's easier if I just put BC3's folder to path, and nothing else need to be changed!

Friday, May 4, 2012

Git: generate patches, after or before commit happens.

Occasionally, if not often, I need to generate patches to be applied to another git copy. This can happen either before I commit or after. It turns out, the ways to do it for the two scenarios are different.

If the change set is already committed, then below command line can generate the patch:



git format-patch -1

This will create a patch in the current folder with file name like 0001-comments-in-the-commit.patch.

-1 in the command line means generating patch for last one commit. If you want to patch in more commits, specify the number here. If you don't like the default file name and want to specify your own output file name, then you can direct output to stdout and the redirect stdout to a file, like below:


  git format-patch -1 --stdout > your_file_name.patch

So how do you do it when you don't want to commit the changes? A simple way is to generate the diff output and redirect it to a file. The problems is, like most people, I'm not satisfied with git's diff output and I use a graphical tool, BeyandCompare, to do the diff viewing. If I execute

git diff > pathfile.txt


the BC is invoked and there is nothing redirected to the patch file, unsurprisingly. So how to force the diff command to skip the GUI tool? As in most of the cases, a command line switch comes to help: --no-ext-diff. Below command line will happily output the result to console and skip BC:

 git diff --no-ext-diff Applying patches generated with git diff is simple:
patch -p1 < patch-file.txt

Tuesday, February 14, 2012

git diff: ignore the deleted files and newly added files

I have a project that has gone through some serious refactoring, during which a lot of garbage files were removed. Then during a code review, the HEAD was compared with the last release tag and bang!, BeyandCompare UI was popped up so many times that we had to abort it after a while.


git diff sha-for-pre-release


Then I though there must be a way to skip this and it turned out there is. When using git diff, there is an option, --diff-filter, among tons of others. And below are excerpted from the manual:


--diff-filter=[(A|C|D|M|R|T|U|X|B)…[*]]
Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type(i.e. regular file, symlink, submodule, …) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B). Any combination of the filter characters (including none) can be used. When * (All-or-none) is added to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected.


My favorite option is --diff-filter=MRT, which will skip added and delete files.

Saturday, February 4, 2012

Configurations of application using IoC, Spring.NET

I have being using Spring when I worked in Java world. Now that I'm in C# part of the world, I'd naturally go with Spring.NET. To be honest, before using Spring.NET, I did not even heard about StructureMap, Windsor, Autofac, NInject, let alone the Funq, Munq, Light.Core, etc. In general, I could live with XML configuration, and some configuration feature derived from XML really makes passing configuration data much easier, by putting below object definition in the container, you can isolate your often changed settings to a file without writing any glue code:

  <object id="SYSTEM.ConfigPropertyHolder" type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="LastLocationOverrides" value="true"/>
<property name="ConfigSections" value="config.app"/>
<property name="Locations">
<list>
<value>~/Config/config.app.xml</value>
</list>
</property>
</object>



now I can use a file named "config.app.xml", in folder Config, to hold my config settings:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="config.app" type="System.Configuration.NameValueSectionHandler, System" />
</configSections>

<config.app>
<add key="value1" value="some data" />
<add key="value2" value="other data" />
...
</config.app>
</configuration>



and in the container definition xml file, I can use the settings defined in the config file:

  <object id="obj_id" type="type.name">
<constructor-arg name="para1name" value="${value1}"/>
<property name="pro1name" value="${value2}"/>
</object>



And since most of Spring.NET's objects are defined with an id, there is even a further way to do the customization, using another class "Spring.Objects.Factory.Config.PropertyOverrideConfigurer, Spring.Core". Put such an object definition in container,


<object id="SYSTEM.ConfigPropertyOverride" type="Spring.Objects.Factory.Config.PropertyOverrideConfigurer, Spring.Core">
<property name="IgnoreResourceNotFound" value="true"/>
<property name="ConfigSections" value="config.system"/>
<property name="Locations">
<list>
<value>~/Config/config.sys.xml</value>
</list>
</property>
</object>



you gain the capability to directly change object properties in config.sys.app:

  <config.system>

<!--the key must be a <object_in_context>.<property>-->
<add key="obj_id.someProperty" value="12345"/>
<add key="another_obj.anotherProp" value="good!"/>
...
</config.system>



Ugly or not, it's really convenient and powerful. In general, Spring.NET is really a feature rich software package, not only a IoC container. And also the document is really good, comparing to most other OSS projects.

So what's the problem? The problem is I'm getting more and more tired with the angle brackets, and I would like to try something new, like NInject, Autofac.

Thursday, February 2, 2012

SubWCRev.exe and its dependencies

Our team is using Subversion as CVS, and thus it's natural to use SubWCRev.exe to retrieve source code version and embed into VersionInfo.cs file, so we can already rebuild the software when we get a bug report.

The problem is when the build process is running on build server, there is no TortoiseSVN installed on the server and I don't want to install it on all the build agents. So I took some time to find out the dlls that SubWCRev.exe depends on, in case I forget it:

# intl3_tsvn32.dll
# libapr_tsvn32.dll
# libaprutil_tsvn32.dll
# libsasl32.dll
# libsvn_tsvn32.dll

Saturday, January 21, 2012

tell Git to do ignore your changes to some files

Normally Git is so powerful with its branching capabilities, that you seldom need this feature. But sometimes it can be handy if you can simply tell Git to "pretend" that you have not changed some files, even they have really been changed.

If you are using Git as a SVN client, like I do, you will know that Git refuses sync with SVN with local changes. In a large project, it can be that there is a common development environment with a common deployed application server so the client developers can connect and debug without worrying about setup their own instances. So it's also common that when you checkout codes out of the server, in my case it's a SVN repository, the config file contains URL pointing to that common server. If you are the server side developer, then you have to start your own instance and use a client to connect to it. Then you have to change the config file to point the URL to your instance and remember never commit the changes ,otherwise your colleague will kill you :)

There are a bunch of ways to overcome this. You can first only check in your changes without the config files, then stash the config files and then commit to SVN. Or you can create your own local branch with your config files, checkout and rebase the branch each time you want to debug with your config, which is what I'm doing now.

A new way I realized today while reading this post was, I can tell Git simply ignore the changes you have made to some files, with below command:
git update-index --assume-unchanged somefile.txt
this way Git will assume the file is never changed and will perform happily whatever command you want that can normally only be performed to a clean copy. So what happens that the file is changed in upstream repository? By manual, "Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually."

Friday, January 20, 2012

Trick to make LiveMeeting addon for Outlook 2007 automatically connected.

Yes, my company uses Microsoft Office and related tools to handle normal communication. But some times it happens that the add-on is loaded but not really activated. You'll still see the LiveMeeting toolbar in outlook toolbar, but just one button "Enable LiveMeeting" there. This is annoying since you have to remember to push it each time you start outlook.

There is a simple solution to it: go to "start->all programes->microsoft office->microsoft office tools" and select the "microsoft office 2007 language settings", change the language to some other value, save it. Then open it again and change it back to your preferred language. Then start outlook again, you'll find the add-on is activated automatically with 3 buttons.

Tuesday, January 17, 2012

Simple.Data, is it really a good idea?

I've been keeping an eye on micro ORMs for a while. Yesterday I decided to give Simple.Data a try. From the introduction, it looks brilliant, the fluent API, the briefness, the simplicity,..., everything seems to be perfect... well, until I started coding.

Once I'm in VS, I immediately realized that Simple.Data might not be the one I wanted. Its magical syntax heavily depends on .NET's new dynamic feature (yes, it's .NET 4 and above only). That means you loose the static language's best capability: compile time checking. And also you loose the productivity from your tool, there is almost no intellisense provided by VS. You might say, what's the big deal, the syntax is cool. Yes, it's cool. But the problem is, if you make an accident typing or a normal typo (who doesn't?), then compiler won't be there for you and you'll only get the null at runtime. Maybe through unit test coverage may resolve this. But from where I stand, it seems to be just a better alternative of HQL dressed with a new cloth named fluent API.

But I did find some part that really make me happy. It seems Simple.Data recognizes fields separated by underscore. I've got a table with columns like CUSTOMER_ID, and Simple.Data can happily map it to a property named CustomerId. If I rename CUSTOMERID, then Simple.Data still works fine. This is clever!

There is a tool, Simple.Data.Pad, to address this issue. So far I haven't been able to make it work with sqlite yet.

In general, I'm not so convinced yet, but I'll continue on the project to work with it, let's see where I'm going to end with.

Sunday, January 15, 2012

.Net code contracts

I knew there is something new in .NET 4 called code contracts and I also saw some simple code snippets about it. Without digging in, I though it was just some fluent code sugar to help you replace assertions like
if (input_int > 0) throw new ArgumentException("some msg");
with
Contracts.Requires<argumentexception>(input_int > 0);
and come on, this fluent sucks, requires an exception? I would have written a better one!

But I was wrong!

I had some free time today and I opened a long bookmarked url and took a look at it. After reading through the first section, I knew I was wrong. There was post compile code weaving and checking. Without it, there is not much virtual inside, but with the post process, everything can happen. And it turns out to be a very much valuable technology than I thought. Kudos!

Some simple googling led me to this blog that provides some analysis I would also do, using Reflector on the generated code, to see the magic there. Also in their home page, there is a video at the bottom(?!) of the page, after some random comments!

Wednesday, January 11, 2012

Differences between event and delegate in C#

I did not realized some of them until I came across this article. In short,

  1. event is a modifier and delegate is a normal type declaration
  2. thus event can be used in interfaces and delegates can not
  3. event can only be accessed by the class defines it, not even the child classes (an in-direct access through protected method is possible), while delegate is more like a member
  4. normally event follows a convention on signature: foo(object source, EventArgs e)

Sunday, January 8, 2012

Start on Python

It's being quite a while since my last blog and I would like to have something really new to start the new year, Python.

Recently I started teaching my son Python. I don't know Python before. So I have to learn it while teaching him. I intended to learn Python for quite some time, but time has always been my biggest enemy, together with my memory: it's getting worse every year. If I learn something and don't use it immediately, then it's quite possible after 3 months, I'm new to it again. So I start blogging it, trying to make my memory last longer.

Python is an elegant language, it's very expressive and has very brief syntax - though this can be a double side sword - to a experienced programmer with long time Java/C#/C++/C experience, it takes some time to be used to it.

Some points about Python I'd like to note down:

  1. Python started at 1989, and first public appeared at 1991
  2. CPython is the C implemented version of Python, then standard Python
  3. Python generally has 2 kinds of primary types: numbers and strings, though array/list and hashtable/dictionary is first class member of the language.
  4. Python use space indention to indicate sub-scope. The indent does not have to be unified within program, but must be the same for the same scope
  5. like most script languages, variables do not need to be pre-claimed before being used
  6. Python can be compiled into byte codes, like Java class files, with extension .pyo or .pyc
  7. using '''/""", you can have multiple line strings
  8. raw string starts with a 'r': r"c:\a\path\file"
  9. unicode string starts with a 'u': u"this is unicode"
  10. raw_input() will return anything you input as a string, while input() will try to evaluate your input as Python code! For example:
    >>> a = 3
    >>> b = input()
    a+11
    >>> b
    14
  11. Python as +=, -=, *=, /=, similar to other languages, but no i++, ++i, i-- or --i. But ++i or --i won't give you any syntax error, since the +/- are treated two time sign prefix!