Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby 2.0 support #357

Closed
haileys opened this issue Feb 12, 2013 · 6 comments
Closed

Ruby 2.0 support #357

haileys opened this issue Feb 12, 2013 · 6 comments
Milestone

Comments

@haileys
Copy link

haileys commented Feb 12, 2013

This patch makes some changes to support Ruby 2.0 and adds 2.0.0 to .travis.yml.

diff --git .travis.yml .travis.yml
index 661dd2b..a3297f0 100644
--- .travis.yml
+++ .travis.yml
@@ -1,4 +1,5 @@
 rvm: 
+  - 2.0.0
   - 1.9.3
   - 1.9.2

diff --git cucumber.yml cucumber.yml
index b081270..52e3727 100644
--- cucumber.yml
+++ cucumber.yml
@@ -15,6 +15,7 @@ jruby: <%= std_opts %> --tags ~@spork --tags ~@wire
 jruby_win: <%= std_opts %> --tags ~@spork --tags ~@wire CUCUMBER_FORWARD_SLASH_PATHS=true
 windows_mri: <%= std_opts %> --tags ~@jruby --tags ~@spork --tags ~@wire --tags ~@needs-many-fonts CUCUMBER_FORWARD_SLASH_PATHS=true
 ruby_1_9: <%= std_opts %> --tags ~@jruby --tags ~@fails_on_1_9
+ruby_2_0: <%= std_opts %> --tags ~@jruby --tags ~@fails_on_1_9
 wip: --tags @wip:3 --wip features
 none: --format pretty
 rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
diff --git fixtures/self_test/features/support/env.rb fixtures/self_test/features/support/env.rb
index 1ce0d3a..2dc9ff5 100644
--- fixtures/self_test/features/support/env.rb
+++ fixtures/self_test/features/support/env.rb
@@ -1,6 +1,6 @@
 require 'base64'
 begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end
-$KCODE = 'u' unless Cucumber::RUBY_1_9
+$KCODE = 'u' if Cucumber::RUBY_2_0

 Before('@not_used') do
   raise "Should never run"
diff --git gem_tasks/cucumber.rake gem_tasks/cucumber.rake
index 2789906..8bcfc82 100644
--- gem_tasks/cucumber.rake
+++ gem_tasks/cucumber.rake
@@ -3,12 +3,14 @@ require 'cucumber/platform'

 class Cucumber::Rake::Task
   def set_profile_for_current_ruby
-    self.profile = if(Cucumber::JRUBY)
+    self.profile = if Cucumber::JRUBY
       Cucumber::WINDOWS ? 'jruby_win' : 'jruby'
-    elsif(Cucumber::WINDOWS_MRI)
+    elsif Cucumber::WINDOWS_MRI
       'windows_mri'
-    elsif(Cucumber::RUBY_1_9)
+    elsif Cucumber::RUBY_1_9
       'ruby_1_9'
+    elsif Cucumber::RUBY_2_0
+      'ruby_2_0'
     end
   end
 end
diff --git legacy_features/support/env.rb legacy_features/support/env.rb
index 8a3d848..a70afef 100644
--- legacy_features/support/env.rb
+++ legacy_features/support/env.rb
@@ -101,7 +101,7 @@ class CucumberWorld
     stderr_file = Tempfile.new('cucumber')
     stderr_file.close
     in_current_dir do
-      mode = Cucumber::RUBY_1_9 ? {:external_encoding=>"UTF-8"} : 'r'
+      mode = Cucumber::RUBY_1_8_7 ? 'r' : {:external_encoding=>"UTF-8"}
       IO.popen("#{command} 2> #{stderr_file.path}", mode) do |io|
         @last_stdout = io.read
       end
diff --git lib/cucumber/core_ext/instance_exec.rb lib/cucumber/core_ext/instance_exec.rb
index 0346a34..d76c21e 100644
--- lib/cucumber/core_ext/instance_exec.rb
+++ lib/cucumber/core_ext/instance_exec.rb
@@ -54,7 +54,7 @@ class Object #:nodoc:

   def cucumber_arity(block)
     a = block.arity
-    Cucumber::RUBY_1_9 ? a : (a == -1 ? 0 : a)
+    Cucumber::RUBY_1_8_7 ? (a == -1 ? 0 : a) : a
   end

   def cucumber_compatible_arity?(args, block)
@@ -74,7 +74,7 @@ class Object #:nodoc:
     end
   end

-  INSTANCE_EXEC_OFFSET = (Cucumber::RUBY_1_9 || Cucumber::RUBY_1_8_7 || Cucumber::JRUBY) ? -3 : -4
+  INSTANCE_EXEC_OFFSET = (Cucumber::RUBY_2_0 || Cucumber::RUBY_1_9 || Cucumber::RUBY_1_8_7 || Cucumber::JRUBY) ? -3 : -4

   def replace_instance_exec_invocation_line!(backtrace, instance_exec_invocation_line, pseudo_method)
     return if Cucumber.use_full_backtrace
diff --git lib/cucumber/formatter/unicode.rb lib/cucumber/formatter/unicode.rb
index 61feaaa..0105d5e 100644
--- lib/cucumber/formatter/unicode.rb
+++ lib/cucumber/formatter/unicode.rb
@@ -2,10 +2,10 @@
 # Tips for improvement - esp. ruby 1.9: http://www.ruby-forum.com/topic/184730
 require 'cucumber/platform'
 require 'cucumber/formatter/ansicolor'
-$KCODE='u' unless Cucumber::RUBY_1_9
+$KCODE='u' if Cucumber::RUBY_2_0

 if Cucumber::WINDOWS
-  require 'iconv' unless Cucumber::RUBY_1_9
+  require 'iconv' if Cucumber::RUBY_2_0

   if ENV['CUCUMBER_OUTPUT_ENCODING']
     Cucumber::CODEPAGE = ENV['CUCUMBER_OUTPUT_ENCODING']
@@ -27,14 +27,7 @@ if Cucumber::WINDOWS
         o.instance_eval do
           alias cucumber_print print
           def print(*a)
-            if Cucumber::RUBY_1_9
-              begin
-                cucumber_print(*a.map{|arg| arg.to_s.encode(Encoding.default_external)})
-              rescue Encoding::UndefinedConversionError => e
-                STDERR.cucumber_puts("WARNING: #{e.message}")
-                cucumber_print(*a)
-              end
-            else
+            if Cucumber::RUBY_1_8_7
               begin
                 cucumber_print(*Iconv.iconv(Cucumber::CODEPAGE, "UTF-8", *a.map{|a|a.to_s}))
               rescue Iconv::InvalidEncoding => e
@@ -44,19 +37,19 @@ if Cucumber::WINDOWS
                 STDERR.cucumber_puts("WARNING: #{e.message}")
                 cucumber_print(*a)
               end
+            else
+              begin
+                cucumber_print(*a.map{|arg| arg.to_s.encode(Encoding.default_external)})
+              rescue Encoding::UndefinedConversionError => e
+                STDERR.cucumber_puts("WARNING: #{e.message}")
+                cucumber_print(*a)
+              end
             end
           end

           alias cucumber_puts puts
           def puts(*a)
-            if Cucumber::RUBY_1_9
-              begin
-                cucumber_puts(*a.map{|arg| arg.to_s.encode(Encoding.default_external)})
-              rescue Encoding::UndefinedConversionError => e
-                STDERR.cucumber_puts("WARNING: #{e.message}")
-                cucumber_puts(*a)
-              end
-            else
+            if Cucumber::RUBY_1_8_7
               begin
                 cucumber_puts(*Iconv.iconv(Cucumber::CODEPAGE, "UTF-8", *a.map{|a|a.to_s}))
               rescue Iconv::InvalidEncoding => e
@@ -66,6 +59,13 @@ if Cucumber::WINDOWS
                 STDERR.cucumber_puts("WARNING: #{e.message}")
                 cucumber_puts(*a)
               end
+            else
+              begin
+                cucumber_puts(*a.map{|arg| arg.to_s.encode(Encoding.default_external)})
+              rescue Encoding::UndefinedConversionError => e
+                STDERR.cucumber_puts("WARNING: #{e.message}")
+                cucumber_puts(*a)
+              end
             end
           end
         end
diff --git lib/cucumber/platform.rb lib/cucumber/platform.rb
index a71eea4..9b99f62 100644
--- lib/cucumber/platform.rb
+++ lib/cucumber/platform.rb
@@ -14,6 +14,7 @@ unless defined?(Cucumber::VERSION)
   WINDOWS_MRI   = WINDOWS && !JRUBY && !IRONRUBY
   RAILS         = defined?(Rails)
   RUBY_BINARY   = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
+  RUBY_2_0      = RUBY_VERSION =~ /^2\.0/
   RUBY_1_9      = RUBY_VERSION =~ /^1\.9/
   RUBY_1_8_7    = RUBY_VERSION =~ /^1\.8\.7/

@@ -21,7 +22,7 @@ unless defined?(Cucumber::VERSION)
     attr_accessor :use_full_backtrace

     def file_mode(m, encoding="UTF-8") #:nodoc:
-      RUBY_1_9 ? "#{m}:#{encoding}" : m
+      RUBY_1_8_7 ? m : "#{m}:#{encoding}"
     end
   end
   self.use_full_backtrace = false
diff --git lib/cucumber/rake/task.rb lib/cucumber/rake/task.rb
index 6596c7f..c5583ec 100644
--- lib/cucumber/rake/task.rb
+++ lib/cucumber/rake/task.rb
@@ -143,7 +143,7 @@ module Cucumber
       # true, +fork+ is implicit.
       attr_accessor :rcov
       def rcov=(flag)
-        if(flag && Cucumber::RUBY_1_9)
+        if flag && !Cucumber::RUBY_1_8_7
           raise failed + "RCov only works on Ruby 1.8.x. You may want to use SimpleCov: https://github.com/colszowka/simplecov" + reset
         end
         @rcov = flag
diff --git spec/cucumber/cli/configuration_spec.rb spec/cucumber/cli/configuration_spec.rb
index 88894ed..60293ae 100644
--- spec/cucumber/cli/configuration_spec.rb
+++ spec/cucumber/cli/configuration_spec.rb
@@ -8,9 +8,13 @@ module Cli
       attr_reader :options
     end

+    def convert_hash_to_yaml_erb(hash)
+      ["---", *hash.map { |k,v| "#{k}: #{v}" }].join "\n"
+    end
+
     def given_cucumber_yml_defined_as(hash_or_string)
       File.stub!(:exist?).and_return(true)
-      cucumber_yml = hash_or_string.is_a?(Hash) ? hash_or_string.to_yaml : hash_or_string
+      cucumber_yml = hash_or_string.is_a?(Hash) ? convert_hash_to_yaml_erb(hash_or_string) : hash_or_string
       IO.stub!(:read).with('cucumber.yml').and_return(cucumber_yml)
     end

diff --git spec/cucumber/core_ext/proc_spec.rb spec/cucumber/core_ext/proc_spec.rb
index f5fcb48..fc9fc44 100644
--- spec/cucumber/core_ext/proc_spec.rb
+++ spec/cucumber/core_ext/proc_spec.rb
@@ -35,20 +35,20 @@ describe Proc do
     }.should raise_error(Cucumber::ArityMismatchError, "Your block takes 2 arguments, but the Regexp matched 1 argument.")
   end

-  if Cucumber::RUBY_1_9
-    it "should allow varargs (expecting 0+)" do
+  if Cucumber::RUBY_1_8_7
+    # Ruby 1.8
+    it "should not allow varargs 0+ because Ruby 1.8 reports same arity as with no args, so we can't really tell the difference." do
       lambda {
         Object.new.cucumber_instance_exec(true, 'foo', 1) do |*args|
         end
-      }.should_not raise_error(Cucumber::ArityMismatchError)
+      }.should raise_error(Cucumber::ArityMismatchError, "Your block takes 0 arguments, but the Regexp matched 1 argument.")
     end
   else
-    # Ruby 1.8
-    it "should not allow varargs 0+ because Ruby 1.8 reports same arity as with no args, so we can't really tell the difference." do
+    it "should allow varargs (expecting 0+)" do
       lambda {
         Object.new.cucumber_instance_exec(true, 'foo', 1) do |*args|
         end
-      }.should raise_error(Cucumber::ArityMismatchError, "Your block takes 0 arguments, but the Regexp matched 1 argument.")
+      }.should_not raise_error(Cucumber::ArityMismatchError)
     end
   end

diff --git spec/cucumber/step_match_spec.rb spec/cucumber/step_match_spec.rb
index fb42702..35ce644 100644
--- spec/cucumber/step_match_spec.rb
+++ spec/cucumber/step_match_spec.rb
@@ -7,7 +7,7 @@ module Cucumber
   describe StepMatch do
     include RSpec::WorkInProgress

-    WORD = Cucumber::RUBY_1_9 ? '[[:word:]]' : '\w'
+    WORD = Cucumber::RUBY_1_8_7 ? '\w' : '[[:word:]]'

     before do
       @rb_language = RbSupport::RbLanguage.new(nil)
diff --git spec/spec_helper.rb spec/spec_helper.rb
index fe145ae..f7e773d 100644
--- spec/spec_helper.rb
+++ spec/spec_helper.rb
@@ -13,7 +13,7 @@ require 'bundler'
 Bundler.setup

 require 'cucumber'
-$KCODE='u' unless Cucumber::RUBY_1_9
+$KCODE='u' if Cucumber::RUBY_1_8_7

 RSpec.configure do |c|
   c.before do
@mattwynne
Copy link
Member

Thanks! Is there a reason you submitted this as an inline patch rather than a Github pull request?

@haileys
Copy link
Author

haileys commented Feb 14, 2013

I didn't want to fork the repo

@mattwynne
Copy link
Member

It does make it easier for us to integrate changes if they come as pull requests, particularly since Travis will run tests on your changes automatically for us.

Can I ask why you didn't want to fork the repo? Just too much hassle?

@haileys
Copy link
Author

haileys commented Feb 14, 2013

My repo list is already cluttered with enough forked repos, I didn't want to add to it.

mattwynne added a commit that referenced this issue Feb 25, 2013
@mattwynne
Copy link
Member

See #377

@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants