BigDiver

Dive away, explore the net…

Archive for November 2011

Working with Ruby Version Manager

leave a comment »

You should first install the Ruby Version Manager (RVM), you can follow my other article on how to do it and get the latest version of Ruby and Rails working on your Mac.

Once you have RVM you can change Ruby versions at will. For a full documentation of RVM you can go the the website.

This article is in no way a full on manual but a set of quick tips on how to get going, and best practices. So we’ll see how to:

  1. Choose a Ruby version
  2. Gemsets and their basic functionality
  3. How to use gemsets with rails projects

Choose a Ruby Version

To select a working version of Ruby you can do


bigdiver$ rvm use 1.9.3
Using /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0

Then you can verify that you’re really using that version by running


bigdiver$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]
bigdiver$ which ruby
/Users/bigdiver/.rvm/rubies/ruby-1.9.3-p0/bin/ruby

Notice that we’re using the ruby installed in the user’s home directory (~/.rvm)and not the system’s which is in /usr/bin.

Gemsets

The next thing to get familiar with are gemsets. RVM gives you a compartmentalized ruby environment that is independent of the other versions on the system. This is great because it gives you full control over the gem, or package, versions your projects needs to function properly.
A huge part of that compartmentalization are the RVM gemsets. Gemsets are groups of Ruby Gems, that are associated with a version of ruby managed by RVM. You can think of them as gem repositories. You should have one gemset per project, and that’s where RVM really shines, because it makes the process really easy.

You can list the existing gemsets with the command


bigdiver$ rvm gemset list

gemsets for ruby-1.9.3-p0 (found in /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0)
global

At this point, and if you just installed RVM you should only have one global gemset, you can choose by using the command gemset use


bigdiver$ rvm gemset use global
Using /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0 with gemset global
bigdiver$ rvm gemset name
global
bigdiver$ rvm gemset list

gemsets for ruby-1.9.3-p0 (found in /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0)
=> global

Both gemset name and gemset list show the current gemset.

To create a gemset you run the command


bigdiver$ rvm gemset create repo1
'repo1' gemset created (/Users/bigdiver/.rvm/gems/ruby-1.9.3-p0@repo1).
bigdiver$ rvm gemset use repo1
Using /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0 with gemset app4

Easy. Repo1 gemset was created and selected for use. Notice the notation 1.9.3@repo1? This indicates that we created the repo1 gemset “under” or “at” the “1.9.3″ ruby version. By default RVM creates the gemset under the current ruby version (the one you chose with rvm use command).

You can abbreviate the two commands above with the –create option.


bigdiver$ rvm --create 1.9.3@repo2
bigdiver$ rvm gemset list

gemsets for ruby-1.9.3-p0 (found in /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0)
repo1
=> repo2
global

As you can see the hash-rocket (=>) now points to the newly created repo2 indicating that it is the selected gemset.

All the gem operations you make, like gem install, etc will only affect the current gemset.

The exception to this is the global gemset, which as the name indicates, is global to the ruby version in use. So all the gems installed to global will be available in all other gemsets created under this version. This is a very good thing since it saves us a lot of time and work when creating new projects.

Using Gemsets with Rails

I usually create a rails directory where all the projects go. And create a gemset per project. You should create the gemset and select it before creating the new rails application.

Let’s do it:

bigdiver$ mkdir ~/work/rails/
bigdiver$ cd ~/work/rails
bigdiver$ rvm --create app1
bigdiver$ rails new app1

If rvm, ruby and rails were properly installed all the commands should run without error, and the Bundler will install a bunch of new gems into the app1 gemset repository.

Now you’re ready for some RVM magic! Lets create a .rvmrc file so that RVM changes to the app1 gemset as soon as we enter, or cd, to the app1 directory.


bigdiver$ cd app1
bigdiver$ rvm --rvmrc 1.9.3@app1

Again we’re using the @ syntax. This creates a .rvmrc file in the current directory, which was ~/work/rails/app1. Now all you need to do is exit from the directory and jump back in again.


bigdiver$ cd ..
bigdiver$ cd app1

==============================================================================
= NOTICE =
==============================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory =
= This is a shell script and therefore may contain any shell commands. =
= =
= Examine the contents of this file carefully to be sure the contents are =
= safe before trusting it! ( Choose v[iew] below to view the contents ) =
==============================================================================
Do you wish to trust this .rvmrc file? (/Users/bigdiver/work/rails/app1/.rvmrc)
y[es], n[o], v[iew], c[ancel]> Yes

Answer Yes to trust the current .rvmrc file, and you’re done. Now every time you change to the app1 directory the 1.9.3@app1 gemset is selected automatically. Nice!

I also create a .rvmrc file in the rails directory that points to the global gemset, so when I want to install gems globally all i need to do is cd .. from any project.


bigdiver$ cd ..
bigdiver$ rvm --rvmrc 1.9.3@global
bigdiver$ cd ..
bigdiver$ cd rails
==============================================================================
= NOTICE =
==============================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory =
= This is a shell script and therefore may contain any shell commands. =
= =
= Examine the contents of this file carefully to be sure the contents are =
= safe before trusting it! ( Choose v[iew] below to view the contents ) =
==============================================================================
Do you wish to trust this .rvmrc file? (/Users/bigdiver/work/rails/global/.rvmrc)
y[es], n[o], v[iew], c[ancel]> yes
Using: /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0@global

Try it out


bigdiver$ cd app1
Using: /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0@app1
bigdiver$ cd ..
Using: /Users/bigdiver/.rvm/gems/ruby-1.9.3-p0@global

Neat.

Written by bigdiver

November 24, 2011 at 6:10 pm

Posted in Apple, Ruby on Rails

Tagged with , , , ,

Update Ruby and Rails on Mac OS X

with one comment

The system version of Ruby is a bit outdated. In order to update it you should install the Ruby Version Manager or RVM, and not touch the System’s Ruby version. Trying to install it through MacPorts also gave me a bunch of problems, especially in OS X Lion (10.7).

This install also requires Git Version Control system.
If you’re using MacPorts you can install it by running

sudo port install git

But like I said some Ruby and Rails applications do not play well with MacPorts so you will sooner or later should migrate to Homebrew. Here is one of such problems due to mismatched OpenSSL MacPort library versions, that caused me to have to reinstall the whole thing…

Well using the system’s version of ruby it is easy to install Homebrew:

ruby -e "$(curl -fsSL https://gist.github.com/raw/323731/install_homebrew.rb)"

and then install Git

brew install git

Now you can install RVM. Just Copy & Paste the whole line and run it as a single line in your terminal

bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

I usually install RVM in my home directory, and run the previous command with my regular user. This is ok since we mostly use our Macs for development, with only one developer on that machine.

If you need to install RVM system wide you can run:

sudo bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

Now you need to add RVM to your shell so it can do its magic.

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bash_profile

Close your Terminal and reopen it. This will force it to process the code we just entered into .bash_profile. You can also run the following:

. ~/.bash_profile

Now you need to make sure you have the latest Xcode from Apple. You can download it from the App Store for free, or install it from your Mac OS X installation DVD.

If you are a die hard fan of MacPorts, please remove it from the PATH so Ruby does not compile against its libraries causing all sorts of errors later on. It usually suffices to remove all references to /opt/local from the /etc/profile, but your configuration maybe a little bit different.

Now that you have installed Xcode and removed the MacPorts from the PATH you can continue :)

At the time of this writing the current stable version of Ruby was 1.9.3 so that is what I use, you should probably use the latest sable version, and not 1.9.3, by the time you follow this procedure…

rvm install 1.9.3

If you’re on Mac OS X Lion 10.7.2 with Xcode 4.2.1 you may get a compilation error. In that case run

rvm install 1.9.3 --with-gcc=clang

Once it finishes downloading, compiling and installing ruby you can do

rvm --default use 1.9.3

To use the ruby version we just installed, and to make it the default version.

RVM is kind of neat since it does not disturb the system version of Ruby and allows you to maintain and work with different versions of Ruby so you can make sure your older projects that require older versions of Ruby, Rails or some other Gem still run even when you upgrade to newer versions.

RVM is kind of like a virtual machine, or environment that allows you to keep separate Ruby installs that will coexist peacefully, and that is why you should use it and not replace the system Ruby version.

RVM allows for the creation and use of several Gem Repositories or gemsets, that you can keep for each of your projects, and this is an invaluable feature so you don’t get stuck into gem dependency issues later on.

By default a global gemset is created and all gems installed to global will be shared among all the other gemsets that you create for a specific ruby version maintained by RVM.
You should install gems, that like Rails, will be shared by all projects running under a Ruby version. So lets install some of the basic gems to get us going.


rvm gemset list

You should get something like:


gemsets for ruby-1.9.3-p0 (found in /Users/pedro/.rvm/gems/ruby-1.9.3-p0)
global

Lets double check that we’re using the Ruby version we just installed:


bigdiver$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]
bigdiver$ gem -v
1.8.10

Nice!
Install sQlite3, JSON, and Rails


gem install sqlite3 json rdoc rails cheat

If you get UTF-8 errors just set the LANG and LC_CTYPE environment variables. I usually do it in the /etc/profile, but you can do it in the .bash_profile in your home directory.
Add the following lines to one of them and “source” them into your environment


export LC_CTYPE="utf-8"
export LANG=en_US.UTF-8

. /etc/profile

or

. ~/.bash_profile

And then install your gems.

Written by bigdiver

November 24, 2011 at 11:54 am

Get Coverage Reports working with PHPUnit and MAMP

leave a comment »

If you get a “The Xdebug extension is not loaded. No code coverage will be generated.” error message when running phpunit –coverage-html test, you need to add Xdebug to the php.ini as follows:


zend_extension="/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
;xdebug.idekey=

The actual path may actual be different as I am using ActiveState’s Komodo version of Xdebug that I manually copied to the PHP extensions directory.

If you are running MAMP Pro you should also add the above lines to the php.ini template. Do this by opening MAMP Pro Application and click “Edit Template” from the File menu, then click “PHP 5.3.6 php.ini”.

Save and restart your servers.

Written by bigdiver

November 10, 2011 at 3:56 pm

Posted in komodo, MAC OS, PHP

Tagged with , , ,

PHPUnit 3.6 Tests and Komodo IDE Unit Testing

leave a comment »

The latest version of PHPUnit, which at the time of this writing is 3.6.3 breaks the Komodo IDE Test Plan feature due to the wrong PHPUnit include in the drive_testrunner.php file.

My php_error.log file had the following:


Fatal error: require_once(): Failed opening required 'PHPUnit/Framework.php' (include_path='.:/Applications/MAMP/bin/php/php5.3.6/lib/php') in /Applications/Komodo IDE.app/Contents/MacOS/python/komodo/harnesses/php/drive_testrunner.php on line 16
...
require_once() /Applications/Komodo IDE.app/Contents/MacOS/python/komodo/harnesses/php/drive_testrunner.php:57

All that I needed to do was to edit the file /Applications/Komodo IDE.app/Contents/MacOS/python/komodo/harnesses/php/drive_testrunner.php and replace the outdated

require_once 'PHPUnit/Framework.php'

With

require_once 'PHPUnit/Autoload.php'

right at the top and now all is working.

If your tests stil don’t run under Komodo set the PHP interpreter to the MAMP binary in the PHP Languages Properties in the Komodo Preferences.
In my case /Applications/MAMP/bin/php/php5.3.6/bin/php.

Happy testing.

If you’re having problems in getting PHPUnit to work in the first place check my other article to get PHPUnit to work with MAMP on Mac OSX Snow Leopard and Lion.

Written by bigdiver

November 10, 2011 at 1:50 pm

Posted in komodo, PHP

Tagged with , , , , ,

MAMP 2.0 Pear and PHPUnit Installation

with 10 comments

I have been struggling to get PHPUnit working under MAMP 2.0 on OSX Lion, and Snow Leopard. After a lot of searching and googling, and combing through countless forums I figured it out and my tests are finally running.

Here are the steps I took for MAMP using PHP 5.3.6, if your PHP is different adjust accordingly, but basically you have to be in the bin directory of MAMP’s PHP install. If you are you should see the binary for php, for pear, in this directory.


cd /Applications/MAMP/bin/php/php5.3.6/bin
sudo ./pear upgrade pear

In my brand new, fresh, install of MAMP i got the following error:


Notice: unserialize(): Error at offset 276 of 1133 bytes in Config.php on line 1050
ERROR: The default config file is not a valid config file or is corrupted.

If you get this error as well just delete the pear.conf file, and rerun the pear upgrade


rm /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf
sudo ./pear upgrade pear

You should now see something like (truncated the output for space):

downloading PEAR-1.9.4.tgz ...
Starting to download PEAR-1.9.4.tgz (296,332 bytes)
.............................................................done: 296,332 bytes
downloading Archive_Tar-1.3.8.tgz ...
Starting to download Archive_Tar-1.3.8.tgz (17,995 bytes)
...done: 17,995 bytes
downloading Console_Getopt-1.3.1.tgz ...
Starting to download Console_Getopt-1.3.1.tgz (4,471 bytes)
...done: 4,471 bytes
upgrade ok: channel://pear.php.net/Archive_Tar-1.3.8
upgrade ok: channel://pear.php.net/Console_Getopt-1.3.1
upgrade ok: channel://pear.php.net/PEAR-1.9.4
...

Now that you’ve upgraded Pear you can install PHPUnit and its dependencies. Following instructions from the PHPUnit Github repository, i.e. directly from the developer, you can install PHPUnit by running:


./pear config-set auto_discover 1
./pear install --alldeps pear.phpunit.de/PHPUnit

DO NOT RUN ./pear install phppunit as that will install some old and useless version that will not work! There are a lot of tutorials out on the web that are outdated and will not work for the latest version of MAMP and PHPUnit.
You should get something similar to the following (truncated for space):


Attempting to discover channel "pear.phpunit.de"...
downloading channel.xml ...
Starting to download channel.xml (804 bytes)
...
install ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.1.1
install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.1.0
install ok: channel://pear.phpunit.de/PHPUnit-3.6.3

Notice that PHPUnit_MockObject and PHP_CodeCoverage are also installed.
You are now ready to run phpunit and see if it works…

./phpunit

Should print usage information without any errors, on the screen your your PHP Error log file. If there are any include errors in you PHP Error file, you may have multiple PHPUnit versions installed and you need to remove them all by using the pear uninstall command.

My php_error.log had a bunch of entries that looked like this:


PHP Fatal error: require_once(): Failed opening required 'PHPUnit/Framework/MockObject/Autoload.php' (include_path='.:/Applications/MAMP/bin/php/php5.3.6/lib/php') in /Applications/MAMP/bin/php/php5.3.6/lib/php/PHPUnit/Autoload.php on line 48
...
PHP Warning: require_once(PHPUnit/TestCase.php): failed to open stream: No such file or directory in /Applications/MAMP/bin/php/php5.3.6/lib/php/PHPUnit.php on line 49

To resolve this I uninstalled all PHPUnit, and PHPUnit_MockObject.


./pear uninstall phpunit
./pear uninstall pear.phpunit.de/PHPUnit
./pear uninstall pear.phpunit.de/PHPUnit_MockObject

Then reinstalled PHPUnit as explained earlier in this article.

One final comment on Using PHPUnit in your tests. A lot of the tutorials I found on the web, including the PHPUnit manual tell you to include PHPUnit in your code as:


require_once 'PHPUnit/Framework.php'

For the version at the time of this writing which was PHPUnit 3.6.3 you must do the following instead


require_once 'PHPUnit/Autoload.php';

Happy testing!

Written by bigdiver

November 10, 2011 at 1:40 pm

Follow

Get every new post delivered to your Inbox.