Sunday, November 6, 2011

git started #1. Using git with a svn server

So, you already know that svn sucks and you want to switch to git because you know it's awesome, but you are stuck in a team environment of svn-zombies.

What can you do? You can try to convert all the non-deads at once... or you can use git-svn and get them into git one by one.

git-svn let's you interact with a svn server using git as your client. Some git GUIs support git svn. On Mac OSX you can use GitX(L) or SourceTree. Here is how to do it using the commandline.

To get a copy of your svn repo (assuming you have the de facto standard structure of trunk, branches and tags dirs) do:

git svn init -s http://path/to/your/svn/project

From here, use your local repo as usually, i.e. do some hacking, add your files and commit them. Rinse, repeat.

git add somefile 
git commit
...

When you are ready to commit your changes, get the changes from the svn repo:

git svn rebase

and commit your changes:

git svn dcommit

That's it!

Sunday, October 23, 2011

Change

“You never change things by fighting the existing reality. To change something, build a new model that makes the existing model obsolete.” — R. Buckminster Fuller

Friday, April 15, 2011

Becoming an Agile Manager: Transforming the Three Dysfunctions of Management

A terrific talk by Diana Larsen on how to overcome 3 dysfunctional management traps: magical thinking, illusion of control and individual blame using Agile.

Saturday, January 29, 2011

Setting up a ruby testing environment on Linux

Now that we have setup our ruby environment on Linux, we are going to create a setup for testing purposes by adding some useful gems.

For that, we will use a feature from rvm called gemsets. A gemset is an independent setup for your environment. That means you can have many environments with a different set of gems (hence the name) installed independently on each one (even with different ruby versions).

Usually, when working on a project, you would create a gemset specific for that project, but in this case, we are going to create a gemset for practicing tdd, doing katas, coderetreats, etc. If you plan to use rvm in a serious project (which you should), please read first about rvm best practices.

A brief introduction to gemsets
For each version of ruby, rvm creates two gemsets, the default, empty gemset and the global gemset. You can list your gemsets with:
rvm gemset list
Gems that are installed to the @global gemset are shared to all other gemset for that ruby. We won't install anything here. Instead we will create our own gemset and put everything there.

Create a tdd gemset:
rvm gemset create tdd
Switch to that gemset:
rvm use gemset 1.9.2@tdd
If you want to make this your default gemset (the one that will be loaded by default) you can do that by doing:
rvm use gemset 1.9.2@tdd --default
Verify you are on the new gemset:
rvm gemset name
You should get:
tdd
Gems for testing that rock
You can install gems for your current gemset using:
gem install
To do unit testing in ruby, rspec is a great choice.
gem install rspec
To start using it, create an rspec file, like example_spec.rb inside a spec folder on the root of your project:
describe "something" do
  it "does something that passes" do
    5.should eq(5)
  end
You can then execute it by running:
rspec spec/
For a nicer, colored output, pass in the --colour flag to rspec
rspec spec/ --colour 
Or create a .rspec config file on your project root (or in your home dir if you want to always use it), to tell rspec to use the colored output by default
echo "--colour" >> .rspec

Excecuting rspec by hand all the time gets boring quickly, and it turns out computers are quite good at doing tasks repeteadly, so you can instead use autotest, a cool gem from the ZenTest suite that will automagically execute your tests when you save your files.
gem install autotest
Execute it from your project root.
autotest --style rspec2 --quiet
The --style flag tells autotest to look for rspec kind of tests. The --quiet flag removes a bit of noise you probably don't care about.

If you have created the .rpec file on your project root, you can omit the style flag and autotest will still find the tests.
autotest --quiet
If you are really lazy like me, you can instruct autotest to be quiet by default by creating a .autotest file in your home:
echo "Autotest.options[:quiet] = true" >> ~/.autotest
So whatever option you want to use, let's try it. Modify your rspec file and save it. Awesome, isn't it? But wait, there is more!

We'll add a nice gem called test_notifier. This is the topping of the cake. It will notify your OS with the result of your tests.

In order to it on GNOME, we will need to install libnotify:
sudo apt-get install libnotify-bin
gem install test_notifier
Configure autotest to use test_notifier:
echo 'require "test_notifier/runner/autotest"' >> .autotest
Or add the following to your rspec/spec_helper file if you want to use it directly with rspec:
require "test_notifier/runner/spec"
Now you should get a nice notification when you save your file.

Tuesday, January 25, 2011

Setting up your ruby environment on Linux

Here are the instructions on how to set up a ruby environment for Linux, in case you want to start playing with it.

NOTE: Don't use the packages from your distribution. They are outdated and will cause more harm than good. Use one of the methods below.

The quick and easy way
If you are planning to use Rails anyways, you can use the script from railsready.
It will install rails, passenger, and a shitload dependencies it need for everything to work smoothly.
You can install it by running:
sudo wget --no-check-certificate https://github.com/joshfng/railsready/raw/master/railsready.sh && bash railsready.sh
The script will ask if you want to build Ruby from source or install RVM. Choose RVM.

It will download around 50MB of libraries from the repositories and then download and compile rvm, ruby and download again some other gems, so it will take some time. Don't worry, you don't have to do anything, just wait.

The almost-as-quick, almost-as-easy way
If you don't intend to use rails and don't feel like installing all that stuff, use the following method. It's basically doing the same thing as the previous script, except you don't need so many dependencies, and you won't install rails or passenger.

Installing RVM

RVM (Ruby Version Manager) is a tool which allows us to easily install, manage and work with multiple ruby environments (interpreters and gems) on the same machine. This means we can create different environments with distincts versions, of ruby, rails, or whatever gem we want, without any headaches.

In order to install rvm, you'll need curl, git and some compiler tools. We can get them with the following command:
sudo apt-get install git-core curl build-essential
To install rvm execute:
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
This command will install rvm in your home directory.
You need to include a line at the end of the .bashrc file to tell it to load rvm.
Follow carefuly the instructions on your terminal, specially the part about converting the return to an if statement
"[ -z "$PS1" ] && return"
NOTE: The railsready script doesn't do this, and just appends the line loading rvm at the end of the file. RVM maintainers recommend otherwise, so you better do it their way.

If you find any problems, check rvm site troubleshooting information (section 'Trhoubleshooting your install')

Finally reload your envirnonment executing:
source ~/.bashrc
Installing ruby
Now you have rvm installed and running, you can proceed to install ruby. You'll need some packages first, the following command will tell you which:
rvm notes
In my case, these were:
# For Ruby (MRI & ree)  you should install the following OS dependencies:
sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf
Now, we can install ruby 1.9.2:
rvm install 1.9.2
This will take a while, since it is dowloading ruby sources and compiling them.

You can check ruby is all fine and dandy by running
ruby -v
You should get something similar to:
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-linux]
On the next post I will explain how to setup your environment for testing using rvm.

Saturday, January 1, 2011

12 months, 12 katas: January. String calculator

As recommended by @plagelao on twitter, the kata for January will be String Calculator.
Here are the specs:

  1. Create a simple String calculator with a method int Add(string numbers)
    1. The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example “” or “1” or “1,2”
    2. Start with the simplest test case of an empty string and move to 1 and two numbers
    3. Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
    4. Remember to refactor after each passing test
  2. Allow the Add method to handle an unknown amount of numbers
  3. Allow the Add method to handle new lines between numbers (instead of commas).
    1. the following input is ok:  “1\n2,3”  (will equal 6)
    2. the following input is NOT ok:  “1,\n” 
    3. Make sure you only test for correct inputs. there is no need to test for invalid inputs for these katas
  4. Allow the Add method to handle a different delimiter:
    1. to change a delimiter, the beginning of the string will contain a separate line that looks like this:   “//[delimiter]\n[numbers…]” for example “//;\n1;2” should return three where the default delimiter is ‘;’ .
    2. the first line is optional. all existing scenarios should still be supported
  5. Calling Add with a negative number will throw an exception “negatives not allowed” - and the negative that was passed.if there are multiple negatives, show all of them in the exception message
    stop here if you are a beginner. Continue if you can finish the steps so far in less than 30 minutes.
  6. Numbers bigger than 1000 should be ignored, so adding 2 + 1001  = 2
  7. Delimiters can be of any length with the following format:  “//[delimiter]\n” for example: “//[***]\n1***2***3” should return 6
  8. Allow multiple delimiters like this:  “//[delim1][delim2]\n” for example “//[*][%]\n1*2%3” should return 6.
  9. make sure you can also handle multiple delimiters with length longer than one char

Enjoy!

New Year resolutions

Here are my resolutions for the present year:
- Read LESS. Yes, less reading, more doing.
- Keep learning ruby.
- Practice! I'm resolved to do a kata EVERY day, one kata each month. Wanna join? Use the hashtag #12months12katas.
- Rediscover enjoyment, on many aspects. This is a tough one, since I first need to find direction, then courage to act in consequence.