Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Psych fix #36

Closed
wants to merge 1 commit into from

3 participants

@myronmarston
Owner

Our fix for YAML serialization works fine when you use syck (the YAML engine on 1.8, and the default on 1.9 unless you compile with a special option), but I found that it didn't work for Psych. This fixes it.

@myronmarston myronmarston Fix yaml serialization when using Psych.
Without these changes, I was getting SystemStackErrors when calling YAML.dump(stubbed_object) or stubbed_object.to_yaml.

To test this, you may need to reinstall ruby 1.9.2 with psych support.  On OSX with homebrew and rvm, this will do the trick:

brew install libyaml
rvm install ruby-1.9.2 --with-libyaml-dir=/usr/local
b6e77f5
@dchelimsky
Owner

Can you think of a way to automate a test for this short of two ruby 1.9 installs, one compiled w/ Psych and one without?

@myronmarston

Yes, I found a way actually. I pushed a new branch with the original commit and my changes for testing squashed into one commit. It's here. Please pull from that commit instead of the original one from this pull request.

@myronmarston

Fix yaml serialization when using Psych.

Without these changes, I was getting SystemStackErrors when calling YAML.dump(stubbed_object) or stubbed_object.to_yaml.

To test this, you may need to reinstall ruby 1.9.2 with psych support. On OSX with homebrew and rvm, this will do the trick:

brew install libyaml
rvm install ruby-1.9.2 --with-libyaml-dir=/usr/local

Closed by 8ce0c12.

@dchelimsky
Owner

Hey @myronmarston. When I pulled this, I couldn't even run rspec-core's specs on 1.9.2 without recompiling w/ Psych. I don't think this is an acceptable solution, since it means rspec-1.2.6 will break for all users on 1.9.2.

Can you think of a way to exclude serialization support on 1.9.2 without Psych and then warn users who need it that they'd have to recompile 1.9.2?

@myronmarston

When I pulled this, I couldn't even run rspec-core's specs on 1.9.2 without recompiling w/ Psych.

:(. Sorry about that. I made sure the specs test with syck set as the YAML engine and psych set as the YAML engine, but I only ran the specs on a 1.9.2 installation with psych. I didn't stop to think about the fact that it might not work when you use 1.9.2 without psych.

I'll try to see if I can fix this. What error are you getting?

@dchelimsky
Owner
/Users/david/projects/ruby/rspec2/repos/rspec-mocks/lib/rspec/mocks/extensions/psych.rb:18:in `alias_method': undefined method `dump' for class `Module' (NameError)
@myronmarston

Thanks. I'm compiling a new 1.9.2 w/o psych so I can repro this. You've suggested excluding psych serialization support on 1.9.2 w/ a warning--is there any reason to prefer this over simply getting it work both when psych support is included and excluded?

@dchelimsky
Owner

Not at all. "Just working" is a far better option. I just wasn't sure that was feasible.

@myronmarston

I just pushed a fix. Let me know if you're still seeing problems. All rspec-mocks are passing fine for me on a 1.9.2 installation with psych an one without psych.

@dchelimsky
Owner

All good here, too. Thanks, Myron!

@rdp

I get this, wonder if it's related...

1) requires faster! should save a file as a cache in a dir
Failure/Error: Unable to find matching line from backtrace
SystemStackError:
stack level too deep
# C:/installs/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-mocks-2.6.0.rc2/lib/rspec/mocks/extensions/marshal.rb:7

@rdp

oh wait I may have been using an older version. Sorry for the noise.

@myronmarston myronmarston referenced this pull request from a commit in myronmarston/rspec-mocks
@myronmarston myronmarston Fix yaml serialization when using Psych.
Without these changes, I was getting SystemStackErrors when calling YAML.dump(stubbed_object) or stubbed_object.to_yaml.

To test this, you may need to reinstall ruby 1.9.2 with psych support.  On OSX with homebrew and rvm, this will do the trick:

brew install libyaml
rvm install ruby-1.9.2 --with-libyaml-dir=/usr/local

Closes #36.
8ce0c12
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 8, 2011
  1. @myronmarston

    Fix yaml serialization when using Psych.

    myronmarston authored
    Without these changes, I was getting SystemStackErrors when calling YAML.dump(stubbed_object) or stubbed_object.to_yaml.
    
    To test this, you may need to reinstall ruby 1.9.2 with psych support.  On OSX with homebrew and rvm, this will do the trick:
    
    brew install libyaml
    rvm install ruby-1.9.2 --with-libyaml-dir=/usr/local
This page is out of date. Refresh to see the latest.
View
21 lib/rspec/mocks/extensions/psych.rb
@@ -0,0 +1,21 @@
+module Psych
+ class << self
+ def dump_with_mocks(object, *args)
+ return dump_without_mocks(object, *args) unless object.instance_variable_defined?(:@mock_proxy)
+
+ mp = object.instance_variable_get(:@mock_proxy)
+ return dump_without_mocks(object, *args) unless mp.is_a?(::RSpec::Mocks::Proxy)
+
+ object.send(:remove_instance_variable, :@mock_proxy)
+
+ begin
+ dump_without_mocks(object, *args)
+ ensure
+ object.instance_variable_set(:@mock_proxy,mp)
+ end
+ end
+
+ alias_method :dump_without_mocks, :dump
+ alias_method :dump, :dump_with_mocks
+ end
+end
View
8 lib/rspec/mocks/serialization.rb
@@ -1,4 +1,5 @@
require 'rspec/mocks/extensions/marshal'
+require 'rspec/mocks/extensions/psych' if defined?(::Psych)
module RSpec
module Mocks
@@ -8,14 +9,15 @@ def self.fix_for(object)
end
module YAML
- def to_yaml(*a)
- return super(*a) unless instance_variable_defined?(:@mock_proxy)
+ def to_yaml(options = {})
+ return nil if defined?(::Psych) && options[:nodump]
+ return super(options) unless instance_variable_defined?(:@mock_proxy)
mp = @mock_proxy
remove_instance_variable(:@mock_proxy)
begin
- super(*a)
+ super(options)
ensure
@mock_proxy = mp
end
Something went wrong with that request. Please try again.