Permalink
Browse files

bootstrap Cucumber test suite with Aruba

  • Loading branch information...
mislav committed May 4, 2012
1 parent d90ff65 commit fb5fdbb342a1a7671aa0a0a7592d944215135797
Showing with 142 additions and 42 deletions.
  1. +1 −0 .gitignore
  2. +3 −1 Gemfile
  3. +73 −0 features/support/env.rb
  4. +19 −0 features/support/fakebin/git
  5. +46 −41 lib/hub/context.rb
View
@@ -2,3 +2,4 @@
*~
/hub
Gemfile.lock
+tmp/
View
@@ -1,5 +1,7 @@
source 'https://rubygems.org'
-gem 'ronn', :group => :development
+gem 'ronn'
+gem 'aruba'
+gem 'cucumber'
gemspec
View
@@ -0,0 +1,73 @@
+require 'aruba/cucumber'
+require 'fileutils'
+require 'hub/context'
+
+unless system_git = Hub::Context.which('git')
+ abort "Error: `git` not found in PATH"
+end
+
+lib_dir = File.expand_path('../../../lib', __FILE__)
+bin_dir = File.expand_path('../fakebin', __FILE__)
+
+Before do
+ # don't want hub to run in bundle
+ unset_bundler_env_vars
+ # have bin/hub load code from the current project
+ set_env 'RUBYLIB', lib_dir
+ # put fakebin on the PATH
+ set_env 'PATH', "#{bin_dir}:#{ENV['PATH']}"
+ # exclude this project's git directory from use in testing
+ set_env 'GIT_CEILING_DIRECTORIES', File.dirname(lib_dir)
+ # sabotage git commands that might try to access a remote host
+ set_env 'GIT_PROXY_COMMAND', 'echo'
+ # avoids reading from current user's "~/.gitconfig"
+ set_env 'HOME', File.expand_path(File.join(current_dir, 'home'))
+ # used in fakebin/git
+ set_env 'HUB_SYSTEM_GIT', system_git
+
+ FileUtils.mkdir_p ENV['HOME']
+end
+
+Before '~@noexec' do
+ set_env 'GIT', nil
+end
+
+Before '@noexec' do
+ set_env 'GIT', 'echo'
+end
+
+World Module.new {
+ def history
+ histfile = File.join(ENV['HOME'], '.history')
+ if File.exist? histfile
+ File.readlines histfile
+ else
+ []
+ end
+ end
+
+ def assert_command_run cmd
+ cmd += "\n" unless cmd[-1..-1] == "\n"
+ history.should include(cmd)
+ end
+
+ def edit_hub_config
+ config = File.join(ENV['HOME'], '.config/hub')
+ FileUtils.mkdir_p File.dirname(config)
+ if File.exist? config
+ data = YAML.load File.read(config)
+ else
+ data = {}
+ end
+ yield data
+ File.open(config, 'w') { |cfg| cfg << YAML.dump(data) }
+ end
+
+ def run_silent cmd
+ in_current_dir do
+ output = `#{cmd} 2>&1`.chomp
+ $?.should be_success
+ output
+ end
+ end
+}
@@ -0,0 +1,19 @@
+#!/bin/sh
+# A wrapper for system git that prevents commands such as `clone` or `fetch` to be
+# executed in testing. It logs commands to "~/.history" so afterwards it can be
+# asserted that they ran.
+
+command="$1"
+[ "$command" == "config" ] || echo git "$@" >> "$HOME"/.history
+
+case "$command" in
+ "clone" | "fetch" | "pull" | "push" )
+ exit
+ ;;
+ * )
+ [ "$command $2 $3" == "remote add -f" ] && exit
+ # note: `submodule add` also initiates a clone, but we work around it
+ ;;
+ esac
+
+exec "$HUB_SYSTEM_GIT" "$@"
View
@@ -409,50 +409,55 @@ def git_editor
editor.shellsplit
end
- # Cross-platform web browser command; respects the value set in $BROWSER.
- #
- # Returns an array, e.g.: ['open']
- def browser_launcher
- browser = ENV['BROWSER'] || (
- osx? ? 'open' : windows? ? 'start' :
- %w[xdg-open cygstart x-www-browser firefox opera mozilla netscape].find { |comm| which comm }
- )
-
- abort "Please set $BROWSER to a web launcher to use this command." unless browser
- Array(browser)
- end
-
- def osx?
- require 'rbconfig'
- RbConfig::CONFIG['host_os'].to_s.include?('darwin')
- end
-
- def windows?
- require 'rbconfig'
- RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw|windows/
- end
+ module System
+ # Cross-platform web browser command; respects the value set in $BROWSER.
+ #
+ # Returns an array, e.g.: ['open']
+ def browser_launcher
+ browser = ENV['BROWSER'] || (
+ osx? ? 'open' : windows? ? 'start' :
+ %w[xdg-open cygstart x-www-browser firefox opera mozilla netscape].find { |comm| which comm }
+ )
+
+ abort "Please set $BROWSER to a web launcher to use this command." unless browser
+ Array(browser)
+ end
+
+ def osx?
+ require 'rbconfig'
+ RbConfig::CONFIG['host_os'].to_s.include?('darwin')
+ end
+
+ def windows?
+ require 'rbconfig'
+ RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw|windows/
+ end
+
+ # Cross-platform way of finding an executable in the $PATH.
+ #
+ # which('ruby') #=> /usr/bin/ruby
+ def which(cmd)
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
+ exts.each { |ext|
+ exe = "#{path}/#{cmd}#{ext}"
+ return exe if File.executable? exe
+ }
+ end
+ return nil
+ end
- # Cross-platform way of finding an executable in the $PATH.
- #
- # which('ruby') #=> /usr/bin/ruby
- def which(cmd)
- exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
- ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
- exts.each { |ext|
- exe = "#{path}/#{cmd}#{ext}"
- return exe if File.executable? exe
- }
+ # Checks whether a command exists on this system in the $PATH.
+ #
+ # name - The String name of the command to check for.
+ #
+ # Returns a Boolean.
+ def command?(name)
+ !which(name).nil?
end
- return nil
end
- # Checks whether a command exists on this system in the $PATH.
- #
- # name - The String name of the command to check for.
- #
- # Returns a Boolean.
- def command?(name)
- !which(name).nil?
- end
+ include System
+ extend System
end
end

0 comments on commit fb5fdbb

Please sign in to comment.