Thursday, 10 October 2013

Cucumber and Ruby, Introduction


I was using RSpec, as my testing framework , as part of my daily Rails development since couple of years.  Recently I happened to try Cucumber. In that process I decided to use Cucumber Ruby than Rails.

Cucumber ?
cucumber image
 Lets us look at the exiting business practice.

Who owns the business ?
Some one who have an idea.

Then why do we need software developers or “engineers” ?

Why can not business people do development as well ? They can express their thoughts in some language, English, Hindi, Spanish etc ...

Instruction to computer:
“Let there be a Robot that can move in one direction”

magic cap

Nothing happens .. Glad that  our computers does not produce software based on instructions in spoken languages, yet. (fortunately !).

As long as this is the case, we the software engineers are safe :)


What to expect from Cucumber

  1. Cucumber lets software development teams describe how software should behave in plain text, spoken languages. 
  2. Reduces the gap between business requirement and developed product( Agile to the rescue). 
  3. Develop only what is required
  4. Documentation
  5. Acceptance test. Some defined way of proving that developer met the expectation.


TDD - In general
TDD-digram
Cucumber and TDD

Cucumber and TDD diagram
I am not sure if business people would get involved as much I expected here. 

Cucumber steps

cucumber steps - diagram


How does  a feature file look like ?

Feature: Serve coffee
  In order to earn money
  Customers should be able to
  buy coffee at all times

  Scenario: Buy last coffee
    Given there are 1 coffees left in the machine
    And I have deposited 1$
    When I press the coffee button
    Then I should be served a coffee
  Scenario: Buy first coffee
    Given there are 10 cofee left
    When I press the coffee button
    Then I should be served a coffee

    And number of coffee became 9


  • Notice indentation.
  • A line starting with the keyword Feature followed by free indented text starts a feature. A feature usually contains a list of scenarios. You can write whatever you want up until the first scenario, which starts with Scenario.
  • Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then, But or And.



Cucumber supports over 40 spoken languages and the number is steadily growing.


A # language: header on the first line of a feature file tells Cucumber what spoken language to use – for example # language: fr for French. If you omit this header, Cucumber will default to English (en).

feature file snapshot


Gherkin


Gherkin


Cucumber lets software development teams describe how software should behave in plain text.
The text is written in a business-readable domain-specific language -
The language that Cucumber understands is called Gherkin -
Gherkin is the language that Cucumber understands.
It is a Business Readable, Domain Specific Language that lets you describe software’s behavior
 without detailing how that behavior is implemented.()

Gherkin’s grammar is defined in the Treetop grammar
    Single Gherkin source file contains a description of a single feature.
Source files have .feature extension.
Like Python and YAML, Gherkin is a line-oriented language that uses indentation to define structure.
Line endings terminate statements (eg, steps).

Either spaces or tabs may be used for indentation (but spaces are more portable). Most lines start with a keyword.


Example
Robot


Moving robot, but only in one direction.

Accept 2 params, step and direction

Ex: 'F', 10 OR 'B', 20


Moves either forward or back ward, Initially at the center (0).


Installing cucumber
1.
$ gem install cucumber
Building native extensions.  This could take a while...
Successfully installed builder-3.2.2
Successfully installed diff-lcs-1.2.4
Successfully installed multi_json-1.8.0
Successfully installed gherkin-2.12.1
Successfully installed multi_test-0.0.2
Successfully installed cucumber-1.3.8
6 gems installed

2.
$cucumber
No such file or directory - features. Please create a features directory to get started. (Errno::ENOENT)

3.
Create directory and then create 'features/robot.feature'
Feature: Robot in Motion
  In order to move my robot
  As an owner
  I want the ability to create and move based on instructions
  Scenario: Create a robot
    Given Robot created
    When I check the position
    Then Robot should be at 0
  Scenario: Move forward
    Given Robot is at 0
    When I enter 'F', 10
    And I check the position
    Then Robot should be at 10

4.
Run again and see it fails.

5.
Now create 'features/step_definitions/robot_steps.rb' as

require 'robot'

Given(/^Robot created$/) do
  @robot = Robot.new
  # pending # express the regexp above with the code you wish you had
end

When(/^I check the position$/) do
  @position = @robot.position
  # pending # express the regexp above with the code you wish you had
end

Then(/^Robot should be at (\d+)$/) do |arg1|
  @position.should == arg1.to_i
  # pending # express the regexp above with the code you wish you had
end

Given(/^Robot is at (\d+)$/) do |arg1|
  @robot = Robot.new
  # pending # express the regexp above with the code you wish you had
end

When(/^I enter 'F', (\d+)$/) do |arg1|
        @robot.move('F', arg1.to_i)
  # pending # express the regexp above with the code you wish you had
end

6. This will again fail, create the actual robot class now - $ robot.rb

class Robot
        attr_accessor :location

        def initialize
                @location = 0
        end

        def move(dir, step)
                if dir=='F'
                        @location = @location + step
                elsif dir=='B'
                        @location = @location - step
                end
        end

        def position
                @location
        end
end

 7. $cucumber -f html > report.html 

This gives you some level of documentation, something like ...

cucumber report


Ref
http://cukes.info/
https://github.com/cucumber/cucumber/wiki
https://github.com/cucumber/cucumber/wiki/Given-When-Then


Wednesday, 4 September 2013

Installing and configuring Ruby and Rails in Ubuntu 13.04 . Addressing issues with libxml2, nokogiri


After upgrading Ubuntu to 13.04(Raring Ringtail), I started having problems with bundle install and starting  server, main concern was libxml2. 

I managed to solve the issue using 'rvm pkg' and 'build.config'. Finally I thought of writing those steps used. The entire steps of installing rvm, ruby, libraries with some notes about the issues with lixml2.

Libraries required:


Assuming that user has an UBUNTU machine.

Install below given libraries. ex: $sudo apt-get install curl. Multiple libraries can be installed separated by spaces. You can skip those libraries that are not required. Below given list has most of the common libraries required.

$sudo apt-get install curl build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison
mysql-server mysql-client libmysqlclient-dev libxml2-dev libxslt-dev imagemagick

$ sudo apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev(required for curb)

$ sudo apt-get install graphicsmagick-libmagick-dev-compat (required for RMagick,  libmagick9-dev doesn not exists any more)

$ sudo apt-get install libmagickcore-dev libmagickwand-dev (If you get ' wand/MAgickWand.h missing' error)

 Installing RVM(https://rvm.io/rvm/install)


 $ \curl -L https://get.rvm.io | bash -s stable

Folow instructions if any. I had instructions to add "source ~./profile" in to  '~/home/shilesh/.bash_profile'

# making sure that it is loaded for the current instance also
            $source ~/.profile

Close out your current shell or terminal session and open a new one (preferred). You may load RVM with the following command:

user$ source ~/.rvm/scripts/rvm
If installation and configuration were successful, RVM should now load whenever you open a new shell. This can be tested by executing the following command which should output 'rvm is a function' as shown below.

user$ type rvm | head -n 1
rvm is a function

Installing  ruby

$ rvm list known   #This will show the available ruby options.

$ rvm install 1.8.7 # This will install Ruby 1.8.7

$ rvm use 1.8.7

$ rvm --default 1.8.7 # makes 1.8.7 as default ruby version.

$ ruby -v  # should give you something like ruby 1.8.7

Source  code from Git or ?.


You are expected to have ssh keys in .ssh folder of your profile (ex: /home/shilesh/.ssh/id_rsa. And it should have 066 permissions).
   
$ git clone git@github.com:<project>/<name>.git
# using https will promt for user name and password for each pull and push.
   
$ git checkout <some_branch> # move to development branch, if not master.

# Edit the values in database.yml with your local mysql server details. If you do not have server ready, update yml file with a user name and password which you will give to the server.

Installing Gems using bundler

Install ruby gems from "http://rubygems.org/pages/download" and install.
$gem -v    # This might give you 2.0.6 or greater.

If you did not get the correct version to download, use
$ gem update --system 1.3.7 # This will update the gem to 1.3.7 version
 $ gem install bundler # Installing Bundler gem

We are ready to install all the gems, this might take some time based on your gems.
$bundle install --local

Database


$ bundle exec rake db:create     # This will create database for development

$bundle exec rake db:migrate    # Creates schema

$bundle exec rake db:seed         # Load default values.

Server


$ bundle exec script/server        # Your server should be up now on 3000 port.

Errors:  

Failed undefined method "source index for module"

reason could be  wrong version of gem.  
    $ gem update --system
    # gem install rubygems-update -v 1.3.7

There were cases where installing libxml-ruby fails as,

Installing libxml-ruby (1.1.3) 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/home/shilesh/.rvm/rubies/ruby-1.8.7-p374/bin/ruby extconf.rb 
checking for socket() in -lsocket... no
checking for gethostbyname() in -lnsl... yes
checking for atan() in -lm... no
checking for atan() in -lm... yes
checking for inflate() in -lz... yes
checking for iconv_open() in -liconv... no
checking for libiconv_open() in -liconv... no
checking for libiconv_open() in -llibiconv... no
checking for iconv_open() in -llibiconv... no
checking for iconv_open() in -lc... yes
checking for xmlParseDoc() in -lxml2... yes
checking for libxml/xmlversion.h... no
checking for libxml/xmlversion.h in /home/shilesh/.rvm/rubies/ruby-1.8.7-p374/include,/home/shilesh/.rvm/rubies/ruby-1.8.7-p374/include/libxml2,/opt/include/libxml2,/usr/local/include/libxml2,/usr/include/libxml2... yes
creating extconf.h
creating Makefile

And then, even after bundler successfully installed all gems, server might fail as,

$ bundle exec script/server 
=> Booting WEBrick
=> Rails 2.3.17 application starting on http://0.0.0.0:3000
/home/shilesh/.rvm/gems/ruby-1.8.7-p374@izone-1.8.7/gems/nokogiri-1.5.6/lib/nokogiri/nokogiri.so: /usr/lib/i386-linux-gnu/libxslt.so.1: symbol xmlBufUse, version LIBXML2_2.9.0 not defined in file libxml2.so.2 with link time reference - /home/shilesh/.rvm/gems/ruby-1.8.7-p374@izone-1.8.7/gems/nokogiri-1.5.6/lib/nokogiri/nokogiri.so (LoadError)
from /home/shilesh/.rvm/gems/ruby-1.8.7-p374@izone-1.8.7/gems/polyglot-0.3.3/lib/polyglot.rb:63:in `require'

In that case, try to specify the library after getting them with in rvm.

$ rvm pkg install libxml2 --verify-downloads 1
 

$ bundle config build.libxml-ruby --with-xml2-lib=${HOME}/.rvm/usr/lib --with-xml2-include=${HOME}/.rvm/usr/include/libxml2

$ bundle config build.nokogiri --with-xml2-lib=${HOME}/.rvm/usr/lib --with-xml2-include=${HOME}/.rvm/usr/include/libxml2


Then it is better to re-install all the gems using bundler. I deleted my gemset and created this again. After that installed bundler gem. Did the configurations as mentioned above, then ran bundler.

Everything was working fine then.




Friday, 3 May 2013

RMagick gem and UBUNTU 13.04


I recently upgraded my OS from Ubuntu 12.04 ro 13.04, Since then my 'rmagick' gem was not working. I used to get below error while starting my rails server ;

libMagickCore.so.4: cannot open shared object file: No such file or directory - /home/shilesh/.rvm/gems/ruby-1.8.7-p352@rails_2.3.16/gems/rmagick-2.12.2/lib/RMagick2.so (LoadError) 

Installing gem using bundler used to give below error;

$ gem install rmagickBuilding native extensions.  This could take a while...ERROR:  Error installing rmagick: ERROR: Failed to build gem native extension.
        /home/shilesh/.rvm/rubies/ruby-1.8.7-p352/bin/ruby extconf.rbchecking for Ruby version >= 1.8.5... yeschecking for gcc... yeschecking for Magick-config... yeschecking for ImageMagick version >= 6.4.9... yeschecking for HDRI disabled version of ImageMagick... yeschecking for stdint.h... yeschecking for sys/types.h... yeschecking for wand/MagickWand.h... no
Can't install RMagick 2.13.2. Can't find MagickWand.h.*** extconf.rb failed ***Could not create Makefile due to some reason, probably lack ofnecessary libraries and/or headers.  Check the mkmf.log file for moredetails.  You may need configuration options

Finally I re-installed ImageMagick and RMagick gem as given below, that worked,finally.

$ sudo apt-get install imagemagick

$sudo apt-get install libmagickwand-dev


$ gem install rmagick
Building native extensions.  This could take a while...
Successfully installed rmagick-2.13.2
1 gem installed
Installing ri documentation for rmagick-2.13.2...
Installing RDoc documentation for rmagick-2.13.2..

Wednesday, 7 March 2012

deploy rails with different user roles.

Using capistrano we can set the user running the application different from the user owning the files and folder of the application.


deploy.rb
set :app_runtime_user, "123"
set :app_runtime_group, "123group"

set :user, "abc"

"abc" will own the folders and "123" will run the application.

Friday, 16 September 2011

Scope of block variables in Ruby 1.8 and 1.9


There is a difference in the scope of the variables in blocks w.e.f ruby 1.8 and 1.9  I have pasted en example to high light the change. It created trouble when I moved my application from 1.8 to 1.9


a = 1
b = 2
r = 0
n =0

(0..10).each do |n|
  a = n
  r = r  + n
end

puts "a = #{a} ; b = #{b} ; r = #{r} ; n = #{n}"


ruby 1.8.7
  a = 10 ; b = 2 ; r = 55 ; n = 10

ruby 1.9.2
  a = 10 ; b = 2 ; r = 55 ; n = 0





Wednesday, 7 September 2011

Postfix on Ubuntu, send mail using telnet.



Post fix is said to be better than send mail as I read from many sources.

"sudo aptitude install postfix" Installed postfix for me.  Give various details when prompted for.
"/etc/postfix/main.cf" Has all the config details.

#sudo postfix start -> starts post fix if not already running.

#telnet localhost 25


ehlo localhost
mail from: user@localhost
rcpt to: someone@localhost
data
Subject: my subject

Hi,
 How are you?
regards,
Admin
. (Type the .[dot] in a new Line and press Enter )
quit

#mailq  will show you the mails to be delivered.

Monday, 13 June 2011

Rack on Rails and Custom webserver



Rails on Rack

What would be interesting
  • Where does  Rack fit in web framework
  • How to install
  • Build our own framework
  • Use our middle ware in Rails.
Rack is an interface for Ruby webserver. It provides a minimal interface between webservers supporting Ruby and Ruby frameworks.


Rack provides an interface between different web servers and your application, Making it very simple for your application to be compatible with any web server that supports Rack - Passenger, Litespeed, Mongrel, Thin, Ebb, Webrick etc ...

It is also possible to use multiple frameworks in a single application. Rails and Sinatra integration is a good example.

Think of Middle ware as a Rails before_filter/after_filter that are reusable across different rack supported frameworks. Ex: We can use the same 'anti_spamming' middle ware for our Rails app, Sinatra app etc ..

Rack handlers

Rack in our web frame work



Installation
  • gem install rack
  • Require 'rack' for further usage
  • "rake middle ware" gives you the list of classes/modules usable as middle ware in our application
Rack specification

A Rack application is a Ruby object(not a class) that responds to call. It takes exactly one argument, the environment, and returns an Array of exactly three values: The status, the headers and the body.

 class HelloWorld

     def call(env)
         [200, {"Content-Type" => "text/html" }, "Hello Rack !"]
     end

 end

sample Rack environment is pasted here

sample Rack environment


We will have another syntax to bring a Rack server up and running, Using various handlers.

require 'rubygems'
require 'rack'

Rack::Handler::WEBrick.run proc{|env| [200, {"Content-Type" => "text/html"}, "Hello Rack"]}, :Port => 9292
...
...
INFO  WEBrick::HTTPServer#start: pid=4557 port=19292

Now the server is running on 19292 port and 'http://localhost:19292/' gives "Hello Rack"

Some of the other Rack handlers are listed below,
  • Rack::Handler::CGI
  • Rack::Handler::EventedMongrel
  • Rack::Handler::FastCGI
  • Rack::Handler::LSWS
  • Rack::Handler::Mongrel
  • Rack::Handler::SCGI
  • Rack::Handler::WEBrick
  • Rack::Handler::Thin

Custom middle ware for Rails

Use an existing rails application or create a new one.
$ rackup   #will bring up the rack server. This depends on the values in config.ru file in RAILS_ROOT.

# config.ru
rub Proc.new {|env|  [200, {"Content-Type" => ""text/html"},  "Hello Rack!"]}

#environment.rb
config.middleware.use(new_middleware, args) # adds a new middle ware at the end of middle ware stack.


Example :
 Adding a middle ware to an existing Rails application. This filter redirects page to 'google' if the requested resource is not present with in rails app.

my_rack_middle_ware.rb
require 'rubygems'
require 'rack'

class MyRackMiddleWare
    def initialize(app)
        @app = app
    end

    def call(env)
        #executes the request using Rails app
        status, headers, body = @app.call(env)
        if status == 404
            [301, {"Location" => 'http://www.google.com'}, 'redirecting to google as the requested resource is not available']
        else
            [status, headers, body]
        end
    end   
end
Add middle ware details in the 'environment.rb'
  config.middleware.use(MyRackMiddleWare) # Make sure to require the file.

 Run the application and try for an invalid page in the URL, the page should get redirected to google.