Saturday, October 4, 2008

Regex Changes in RSpec 1.1.5 Stories

RSpec 1.1.5 changed how the Given, When, and Then match statements are parsed and can break some of your stories.

One of my Then statements was as follows:

Then("the event should have $count artists?") do |count|
   #...
end

The match statements are converted to regular expressions so the question mark would just make the 's' on artists optional so this would match both:

Then the event should have 4 artists

and...

Then the event should have 1 artist

After updating RSpec to 1.1.8 from 1.1.4 (the versions jumped so fast to align with Rspec-rails but 1.1.5 has most of the changes) I was seeing a bunch of my steps were now pending.

I ran a diff on the lib/spec/story/step.rb file between tags 1.1.4 and 1.1.5 in rspec and noticed the method building the matcher was updated.

-    def assign_expression(string_or_regexp)
-      if String === string_or_regexp
-        expression = string_or_regexp.dup
-        expression.gsub! '(', '\('
-        expression.gsub! ')', '\)'
-      elsif Regexp === string_or_regexp
-        expression = string_or_regexp.source
-      end
-      while expression =~ PARAM_PATTERN
-        expression.gsub!($2, "(.*?)")
-      end
-      @expression = Regexp.new("^#{expression}$")
+  def init_name(name)
+    @name = name
+  end
+
+  def init_expression(string_or_regexp)
+    if String === string_or_regexp
+      expression = string_or_regexp.dup
+      %w<? ( ) [ ] { } ^ !>.each {|c| expression.gsub! c, "\\#{c}"}
+    elsif Regexp === string_or_regexp
+      expression = string_or_regexp.source
+    end
+    while expression =~ PARAM_PATTERN
+      expression.sub!($2, "(.*?)")
     end
+    @expression = Regexp.new("\\A#{expression}\\Z", Regexp::MULTILINE)
+  end

I didnt realize the matcher was parsed differently based on if it was passed in as a String or a Regexp. Now if you give a string to match with any of the %w<? ( ) [ ] { } ^ !> characters they will be escaped to match those actual characters. Previously it was just parenthesis.

Anyway to fix your current matchers just send them in as regexs instead of strings.

Then(/the event should have $count artists?/) do |count|
   #...
end

Don't worry about escaping the dollar sign, the PARAM_PATTERN strips that out before it gets converted back into a regex.

Tuesday, April 29, 2008

gitweb + apache + gentoo

This is meant as a quick tutorial to get gitweb up and running on gentoo with apache as the web server.

USE FLAGS

since git version 1.4, gitweb is bundled with git and can be installed by building with the cgi and perl use flags.

add the following to your package.use file:

[/etc/portage/package.use]

dev-util/git perl cgi
EMERGE...

emerge all the required packages...

> sudo emerge dev-util/git
> sudo emerge apache
INSTALL

install a copy of gitweb to apache's cgi-bin directory

> sudo cp -r /usr/share/git/gitweb /var/www/localhost/cgi-bin
> sudo chown -R apache:apache /var/www/localhost/cgi-bin/gitweb

note: change the 'apache' user and group to whatever you have apache configured to run as.

APACHE CONFIG

create a new virtual host file in your apache vhosts.d directory to be used for accessing gitweb.

[/etc/apache2/vhosts.d/01_gitweb_vhost.conf]

<VirtualHost *:80>
     ServerName git.mydomain.com
     DocumentRoot /var/www/localhost/cgi-bin/gitweb 
     <Directory /var/www/localhost/cgi-bin/gitweb>
          Allow from all
          AllowOverride all
          Order allow,deny
          Options ExecCGI
          <Files gitweb.cgi>
               SetHandler cgi-script
          </Files>
     </Directory>
     DirectoryIndex gitweb.cgi
     SetEnv  GITWEB_CONFIG  /etc/gitweb.conf
</VirtualHost>

note: if you are just using this for localhost and/or you are not setting this up as a subdomain you can just overwrite the default VirtualHost defined in 00_default_vhost.conf with the above and change 'ServerName' to localhost

GITWEB CONFIG

create and edit your gitweb configuration file to point to the root of your projects directory. (/var/git in my case)

[/etc/gitweb.conf]

$projectroot = '/var/git';

note: additional configuration options can be found here: http://repo.or.cz

CREATE A TEST PROJECT
> cd /var/git
> mkdir test.git
> cd test.git
> git --bare init
> echo "this is a test" > description
GIT PUSH

push a project to the gitweb repo

> git push phil@mydomain.com:/var/git/test.git

note: if there is a permissions issue check the test.git repo permissions and ensure you have write access.

if you need more security or are looking to manage access and permissions without using system accounts you might want to check out gitosis for the shared repo.

FIRE IT UP
> sudo /etc/init.d/apache2 start

point your browser to the address you put in for the vhost config and you should be in business.

<update>
if your projects are not being listed in gitweb, try adding your apache user to the git group or whatever group the project dir belongs to. also try this if you updated git and your projects are now showing "no commits". i believe the default group permissions for a shared repo were changed/fixed. i noticed this at v.1.5.6.1 but didn't track down the actual commit.
</update>

if the owner names for the projects are not showing up you can add a name through the usermod command.

> sudo usermod -c "phil sergi" phil