diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | 95e8c48dd3348503a8c7db5d0498894a1b676395 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/mspec/spec/guards | |
parent | ed7d803500de38186c74bce94d233e85ef51e503 (diff) |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/mspec/spec/guards')
-rw-r--r-- | spec/mspec/spec/guards/block_device_spec.rb | 46 | ||||
-rw-r--r-- | spec/mspec/spec/guards/bug_spec.rb | 151 | ||||
-rw-r--r-- | spec/mspec/spec/guards/conflict_spec.rb | 51 | ||||
-rw-r--r-- | spec/mspec/spec/guards/endian_spec.rb | 55 | ||||
-rw-r--r-- | spec/mspec/spec/guards/feature_spec.rb | 80 | ||||
-rw-r--r-- | spec/mspec/spec/guards/guard_spec.rb | 180 | ||||
-rw-r--r-- | spec/mspec/spec/guards/platform_spec.rb | 331 | ||||
-rw-r--r-- | spec/mspec/spec/guards/quarantine_spec.rb | 35 | ||||
-rw-r--r-- | spec/mspec/spec/guards/superuser_spec.rb | 35 | ||||
-rw-r--r-- | spec/mspec/spec/guards/support_spec.rb | 69 | ||||
-rw-r--r-- | spec/mspec/spec/guards/user_spec.rb | 20 | ||||
-rw-r--r-- | spec/mspec/spec/guards/version_spec.rb | 83 |
12 files changed, 1136 insertions, 0 deletions
diff --git a/spec/mspec/spec/guards/block_device_spec.rb b/spec/mspec/spec/guards/block_device_spec.rb new file mode 100644 index 0000000000..3b437b6d74 --- /dev/null +++ b/spec/mspec/spec/guards/block_device_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' +require 'mspec/guards' + +describe Object, "#with_block_device" do + before :each do + ScratchPad.clear + + @guard = BlockDeviceGuard.new + BlockDeviceGuard.stub(:new).and_return(@guard) + end + + platform_is_not :freebsd, :windows do + it "yields if block device is available" do + @guard.should_receive(:`).and_return("block devices") + with_block_device { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "does not yield if block device is not available" do + @guard.should_receive(:`).and_return(nil) + with_block_device { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + end + + platform_is :freebsd, :windows do + it "does not yield, since platform does not support block devices" do + @guard.should_not_receive(:`) + with_block_device { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + end + + it "sets the name of the guard to :with_block_device" do + with_block_device { } + @guard.name.should == :with_block_device + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:match?).and_return(true) + @guard.should_receive(:unregister) + lambda do + with_block_device { raise Exception } + end.should raise_error(Exception) + end +end diff --git a/spec/mspec/spec/guards/bug_spec.rb b/spec/mspec/spec/guards/bug_spec.rb new file mode 100644 index 0000000000..93c549041a --- /dev/null +++ b/spec/mspec/spec/guards/bug_spec.rb @@ -0,0 +1,151 @@ +require 'spec_helper' +require 'mspec/guards' + +describe BugGuard, "#match? when #implementation? is 'ruby'" do + before :all do + @verbose = $VERBOSE + $VERBOSE = nil + end + + after :all do + $VERBOSE = @verbose + end + + before :each do + hide_deprecation_warnings + stub_const "VersionGuard::FULL_RUBY_VERSION", SpecVersion.new('1.8.6') + @ruby_name = Object.const_get :RUBY_NAME + Object.const_set :RUBY_NAME, 'ruby' + end + + after :each do + Object.const_set :RUBY_NAME, @ruby_name + end + + it "returns false when version argument is less than RUBY_VERSION" do + BugGuard.new("#1", "1.8.5").match?.should == false + end + + it "returns true when version argument is equal to RUBY_VERSION" do + BugGuard.new("#1", "1.8.6").match?.should == true + end + + it "returns true when version argument is greater than RUBY_VERSION" do + BugGuard.new("#1", "1.8.7").match?.should == true + end + + it "returns true when version argument implicitly includes RUBY_VERSION" do + BugGuard.new("#1", "1.8").match?.should == true + BugGuard.new("#1", "1.8.6").match?.should == true + end + + it "returns true when the argument range includes RUBY_VERSION" do + BugGuard.new("#1", '1.8.5'..'1.8.7').match?.should == true + BugGuard.new("#1", '1.8'..'1.9').match?.should == true + BugGuard.new("#1", '1.8'...'1.9').match?.should == true + BugGuard.new("#1", '1.8'..'1.8.6').match?.should == true + BugGuard.new("#1", '1.8.5'..'1.8.6').match?.should == true + BugGuard.new("#1", ''...'1.8.7').match?.should == true + end + + it "returns false when the argument range does not include RUBY_VERSION" do + BugGuard.new("#1", '1.8.7'..'1.8.9').match?.should == false + BugGuard.new("#1", '1.8.4'..'1.8.5').match?.should == false + BugGuard.new("#1", '1.8.4'...'1.8.6').match?.should == false + BugGuard.new("#1", '1.8.5'...'1.8.6').match?.should == false + BugGuard.new("#1", ''...'1.8.6').match?.should == false + end + + it "returns false when MSpec.mode?(:no_ruby_bug) is true" do + MSpec.should_receive(:mode?).with(:no_ruby_bug).twice.and_return(:true) + BugGuard.new("#1", "1.8.5").match?.should == false + BugGuard.new("#1", "1.8").match?.should == false + end +end + +describe BugGuard, "#match? when #implementation? is not 'ruby'" do + before :all do + @verbose = $VERBOSE + $VERBOSE = nil + end + + after :all do + $VERBOSE = @verbose + end + + before :each do + hide_deprecation_warnings + @ruby_version = Object.const_get :RUBY_VERSION + @ruby_name = Object.const_get :RUBY_NAME + + Object.const_set :RUBY_VERSION, '1.8.6' + Object.const_set :RUBY_NAME, 'jruby' + end + + after :each do + Object.const_set :RUBY_VERSION, @ruby_version + Object.const_set :RUBY_NAME, @ruby_name + end + + it "returns false when version argument is less than RUBY_VERSION" do + BugGuard.new("#1", "1.8").match?.should == false + BugGuard.new("#1", "1.8.6").match?.should == false + end + + it "returns false when version argument is equal to RUBY_VERSION" do + BugGuard.new("#1", "1.8.6").match?.should == false + end + + it "returns false when version argument is greater than RUBY_VERSION" do + BugGuard.new("#1", "1.8.7").match?.should == false + end + + it "returns false no matter if the argument range includes RUBY_VERSION" do + BugGuard.new("#1", '1.8'...'1.9').match?.should == false + BugGuard.new("#1", '1.8.5'...'1.8.7').match?.should == false + BugGuard.new("#1", '1.8.4'...'1.8.6').match?.should == false + end + + it "returns false when MSpec.mode?(:no_ruby_bug) is true" do + MSpec.stub(:mode?).and_return(:true) + BugGuard.new("#1", "1.8.6").match?.should == false + end +end + +describe Object, "#ruby_bug" do + before :each do + hide_deprecation_warnings + @guard = BugGuard.new "#1234", "x.x.x" + BugGuard.stub(:new).and_return(@guard) + ScratchPad.clear + end + + it "yields when #match? returns false" do + @guard.stub(:match?).and_return(false) + ruby_bug("#1234", "1.8.6") { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "does not yield when #match? returns true" do + @guard.stub(:match?).and_return(true) + ruby_bug("#1234", "1.8.6") { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "requires a bug tracker number and a version number" do + lambda { ruby_bug { } }.should raise_error(ArgumentError) + lambda { ruby_bug("#1234") { } }.should raise_error(ArgumentError) + end + + it "sets the name of the guard to :ruby_bug" do + ruby_bug("#1234", "1.8.6") { } + @guard.name.should == :ruby_bug + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:unregister) + lambda do + ruby_bug("", "") { raise Exception } + end.should raise_error(Exception) + end +end diff --git a/spec/mspec/spec/guards/conflict_spec.rb b/spec/mspec/spec/guards/conflict_spec.rb new file mode 100644 index 0000000000..e06a2809ee --- /dev/null +++ b/spec/mspec/spec/guards/conflict_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' +require 'mspec/guards' + +describe Object, "#conflicts_with" do + before :each do + ScratchPad.clear + end + + it "does not yield if Object.constants includes any of the arguments" do + Object.stub(:constants).and_return(["SomeClass", "OtherClass"]) + conflicts_with(:SomeClass, :AClass, :BClass) { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "does not yield if Object.constants (as Symbols) includes any of the arguments" do + Object.stub(:constants).and_return([:SomeClass, :OtherClass]) + conflicts_with(:SomeClass, :AClass, :BClass) { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "yields if Object.constants does not include any of the arguments" do + Object.stub(:constants).and_return(["SomeClass", "OtherClass"]) + conflicts_with(:AClass, :BClass) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "yields if Object.constants (as Symbols) does not include any of the arguments" do + Object.stub(:constants).and_return([:SomeClass, :OtherClass]) + conflicts_with(:AClass, :BClass) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end +end + +describe Object, "#conflicts_with" do + before :each do + @guard = ConflictsGuard.new + ConflictsGuard.stub(:new).and_return(@guard) + end + + it "sets the name of the guard to :conflicts_with" do + conflicts_with(:AClass, :BClass) { } + @guard.name.should == :conflicts_with + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:unregister) + lambda do + conflicts_with(:AClass, :BClass) { raise Exception } + end.should raise_error(Exception) + end +end diff --git a/spec/mspec/spec/guards/endian_spec.rb b/spec/mspec/spec/guards/endian_spec.rb new file mode 100644 index 0000000000..5b40c203ab --- /dev/null +++ b/spec/mspec/spec/guards/endian_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' +require 'mspec/guards' + +describe Object, "#big_endian" do + before :each do + @guard = BigEndianGuard.new + BigEndianGuard.stub(:new).and_return(@guard) + ScratchPad.clear + end + + it "yields on big-endian platforms" do + @guard.stub(:pattern).and_return([?\001]) + big_endian { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "does not yield on little-endian platforms" do + @guard.stub(:pattern).and_return([?\000]) + big_endian { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "sets the name of the guard to :big_endian" do + big_endian { } + @guard.name.should == :big_endian + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.stub(:pattern).and_return([?\001]) + @guard.should_receive(:unregister) + lambda do + big_endian { raise Exception } + end.should raise_error(Exception) + end +end + +describe Object, "#little_endian" do + before :each do + @guard = BigEndianGuard.new + BigEndianGuard.stub(:new).and_return(@guard) + ScratchPad.clear + end + + it "yields on little-endian platforms" do + @guard.stub(:pattern).and_return([?\000]) + little_endian { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "does not yield on big-endian platforms" do + @guard.stub(:pattern).and_return([?\001]) + little_endian { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end +end diff --git a/spec/mspec/spec/guards/feature_spec.rb b/spec/mspec/spec/guards/feature_spec.rb new file mode 100644 index 0000000000..d14e5f8e67 --- /dev/null +++ b/spec/mspec/spec/guards/feature_spec.rb @@ -0,0 +1,80 @@ +require 'spec_helper' +require 'mspec/guards' + +describe FeatureGuard, ".enabled?" do + it "returns true if the feature is enabled" do + MSpec.should_receive(:feature_enabled?).with(:encoding).and_return(true) + FeatureGuard.enabled?(:encoding).should be_true + end + + it "returns false if the feature is not enabled" do + MSpec.should_receive(:feature_enabled?).with(:encoding).and_return(false) + FeatureGuard.enabled?(:encoding).should be_false + end + + it "returns true if all the features are enabled" do + MSpec.should_receive(:feature_enabled?).with(:one).and_return(true) + MSpec.should_receive(:feature_enabled?).with(:two).and_return(true) + FeatureGuard.enabled?(:one, :two).should be_true + end + + it "returns false if any of the features are not enabled" do + MSpec.should_receive(:feature_enabled?).with(:one).and_return(true) + MSpec.should_receive(:feature_enabled?).with(:two).and_return(false) + FeatureGuard.enabled?(:one, :two).should be_false + end +end + +describe Object, "#with_feature" do + before :each do + ScratchPad.clear + + @guard = FeatureGuard.new :encoding + FeatureGuard.stub(:new).and_return(@guard) + end + + it "sets the name of the guard to :with_feature" do + with_feature(:encoding) { } + @guard.name.should == :with_feature + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:match?).and_return(true) + @guard.should_receive(:unregister) + lambda do + with_feature { raise Exception } + end.should raise_error(Exception) + end +end + +describe Object, "#with_feature" do + before :each do + ScratchPad.clear + end + + it "yields if the feature is enabled" do + MSpec.should_receive(:feature_enabled?).with(:encoding).and_return(true) + with_feature(:encoding) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "yields if all the features are enabled" do + MSpec.should_receive(:feature_enabled?).with(:one).and_return(true) + MSpec.should_receive(:feature_enabled?).with(:two).and_return(true) + with_feature(:one, :two) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "does not yield if the feature is not enabled" do + MSpec.should_receive(:feature_enabled?).with(:encoding).and_return(false) + with_feature(:encoding) { ScratchPad.record :yield } + ScratchPad.recorded.should be_nil + end + + it "does not yield if any of the features are not enabled" do + MSpec.should_receive(:feature_enabled?).with(:one).and_return(true) + MSpec.should_receive(:feature_enabled?).with(:two).and_return(false) + with_feature(:one, :two) { ScratchPad.record :yield } + ScratchPad.recorded.should be_nil + end +end diff --git a/spec/mspec/spec/guards/guard_spec.rb b/spec/mspec/spec/guards/guard_spec.rb new file mode 100644 index 0000000000..ca7dba6455 --- /dev/null +++ b/spec/mspec/spec/guards/guard_spec.rb @@ -0,0 +1,180 @@ +require 'spec_helper' +require 'mspec/guards' +require 'rbconfig' + +describe SpecGuard, ".ruby_version" do + before :each do + @ruby_version = Object.const_get :RUBY_VERSION + Object.const_set :RUBY_VERSION, "8.2.3" + end + + after :each do + Object.const_set :RUBY_VERSION, @ruby_version + end + + it "returns the full version for :full" do + SpecGuard.ruby_version(:full).should == "8.2.3" + end + + it "returns major.minor.tiny for :tiny" do + SpecGuard.ruby_version(:tiny).should == "8.2.3" + end + + it "returns major.minor.tiny for :teeny" do + SpecGuard.ruby_version(:tiny).should == "8.2.3" + end + + it "returns major.minor for :minor" do + SpecGuard.ruby_version(:minor).should == "8.2" + end + + it "defaults to :minor" do + SpecGuard.ruby_version.should == "8.2" + end + + it "returns major for :major" do + SpecGuard.ruby_version(:major).should == "8" + end +end + +describe SpecGuard, "#yield?" do + before :each do + MSpec.clear_modes + @guard = SpecGuard.new + @guard.stub(:match?).and_return(false) + end + + after :each do + MSpec.unregister :add, @guard + MSpec.clear_modes + SpecGuard.clear_guards + end + + it "returns true if MSpec.mode?(:unguarded) is true" do + MSpec.register_mode :unguarded + @guard.yield?.should == true + end + + it "returns true if MSpec.mode?(:verify) is true" do + MSpec.register_mode :verify + @guard.yield?.should == true + end + + it "returns true if MSpec.mode?(:verify) is true regardless of invert being true" do + MSpec.register_mode :verify + @guard.yield?(true).should == true + end + + it "returns true if MSpec.mode?(:report) is true" do + MSpec.register_mode :report + @guard.yield?.should == true + end + + it "returns true if MSpec.mode?(:report) is true regardless of invert being true" do + MSpec.register_mode :report + @guard.yield?(true).should == true + end + + it "returns true if MSpec.mode?(:report_on) is true and SpecGuards.guards contains the named guard" do + MSpec.register_mode :report_on + SpecGuard.guards << :guard_name + @guard.yield?.should == false + @guard.name = :guard_name + @guard.yield?.should == true + end + + it "returns #match? if neither report nor verify mode are true" do + @guard.stub(:match?).and_return(false) + @guard.yield?.should == false + @guard.stub(:match?).and_return(true) + @guard.yield?.should == true + end + + it "returns #match? if invert is true and neither report nor verify mode are true" do + @guard.stub(:match?).and_return(false) + @guard.yield?(true).should == true + @guard.stub(:match?).and_return(true) + @guard.yield?(true).should == false + end +end + +describe SpecGuard, "#match?" do + before :each do + @guard = SpecGuard.new + end + + it "must be implemented in subclasses" do + lambda { + @guard.match? + }.should raise_error("must be implemented by the subclass") + end +end + +describe SpecGuard, "#unregister" do + before :each do + MSpec.stub(:unregister) + @guard = SpecGuard.new + end + + it "unregisters from MSpec :add actions" do + MSpec.should_receive(:unregister).with(:add, @guard) + @guard.unregister + end +end + +describe SpecGuard, "#record" do + after :each do + SpecGuard.clear + end + + it "saves the name of the guarded spec under the name of the guard" do + guard = SpecGuard.new "a", "1.8"..."1.9" + guard.name = :named_guard + guard.record "SomeClass#action returns true" + SpecGuard.report.should == { + 'named_guard a, 1.8...1.9' => ["SomeClass#action returns true"] + } + end +end + +describe SpecGuard, ".guards" do + it "returns an Array" do + SpecGuard.guards.should be_kind_of(Array) + end +end + +describe SpecGuard, ".clear_guards" do + it "resets the array to empty" do + SpecGuard.guards << :guard + SpecGuard.guards.should == [:guard] + SpecGuard.clear_guards + SpecGuard.guards.should == [] + end +end + +describe SpecGuard, ".finish" do + before :each do + $stdout = @out = IOStub.new + end + + after :each do + $stdout = STDOUT + SpecGuard.clear + end + + it "prints the descriptions of the guarded specs" do + guard = SpecGuard.new "a", "1.8"..."1.9" + guard.name = :named_guard + guard.record "SomeClass#action returns true" + guard.record "SomeClass#reverse returns false" + SpecGuard.finish + $stdout.should == %[ + +2 specs omitted by guard: named_guard a, 1.8...1.9: + +SomeClass#action returns true +SomeClass#reverse returns false + +] + end +end diff --git a/spec/mspec/spec/guards/platform_spec.rb b/spec/mspec/spec/guards/platform_spec.rb new file mode 100644 index 0000000000..578773e476 --- /dev/null +++ b/spec/mspec/spec/guards/platform_spec.rb @@ -0,0 +1,331 @@ +require 'spec_helper' +require 'mspec/guards' + +describe Object, "#platform_is" do + before :each do + @guard = PlatformGuard.new :dummy + PlatformGuard.stub(:new).and_return(@guard) + ScratchPad.clear + end + + it "does not yield when #os? returns false" do + PlatformGuard.stub(:os?).and_return(false) + platform_is(:ruby) { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "yields when #os? returns true" do + PlatformGuard.stub(:os?).and_return(true) + platform_is(:solarce) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "sets the name of the guard to :platform_is" do + platform_is(:solarce) { } + @guard.name.should == :platform_is + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:match?).and_return(true) + @guard.should_receive(:unregister) + lambda do + platform_is(:solarce) { raise Exception } + end.should raise_error(Exception) + end +end + +describe Object, "#platform_is_not" do + before :each do + @guard = PlatformGuard.new :dummy + PlatformGuard.stub(:new).and_return(@guard) + ScratchPad.clear + end + + it "does not yield when #os? returns true" do + PlatformGuard.stub(:os?).and_return(true) + platform_is_not(:ruby) { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "yields when #os? returns false" do + PlatformGuard.stub(:os?).and_return(false) + platform_is_not(:solarce) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "sets the name of the guard to :platform_is_not" do + platform_is_not(:solarce) { } + @guard.name.should == :platform_is_not + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:match?).and_return(false) + @guard.should_receive(:unregister) + lambda do + platform_is_not(:solarce) { raise Exception } + end.should raise_error(Exception) + end +end + +describe Object, "#platform_is :wordsize => SIZE_SPEC" do + before :each do + @guard = PlatformGuard.new :darwin, :wordsize => 32 + PlatformGuard.stub(:os?).and_return(true) + PlatformGuard.stub(:new).and_return(@guard) + ScratchPad.clear + end + + it "yields when #wordsize? returns true" do + PlatformGuard.stub(:wordsize?).and_return(true) + platform_is(:wordsize => 32) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "doesn not yield when #wordsize? returns false" do + PlatformGuard.stub(:wordsize?).and_return(false) + platform_is(:wordsize => 32) { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end +end + +describe Object, "#platform_is_not :wordsize => SIZE_SPEC" do + before :each do + @guard = PlatformGuard.new :darwin, :wordsize => 32 + PlatformGuard.stub(:os?).and_return(true) + PlatformGuard.stub(:new).and_return(@guard) + ScratchPad.clear + end + + it "yields when #wordsize? returns false" do + PlatformGuard.stub(:wordsize?).and_return(false) + platform_is_not(:wordsize => 32) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "doesn not yield when #wordsize? returns true" do + PlatformGuard.stub(:wordsize?).and_return(true) + platform_is_not(:wordsize => 32) { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end +end + +describe PlatformGuard, ".implementation?" do + before :all do + @verbose = $VERBOSE + $VERBOSE = nil + end + + after :all do + $VERBOSE = @verbose + end + + before :each do + @ruby_name = Object.const_get :RUBY_NAME + end + + after :each do + Object.const_set :RUBY_NAME, @ruby_name + end + + it "returns true if passed :ruby and RUBY_NAME == 'ruby'" do + Object.const_set :RUBY_NAME, 'ruby' + PlatformGuard.implementation?(:ruby).should == true + end + + it "returns true if passed :rubinius and RUBY_NAME == 'rbx'" do + Object.const_set :RUBY_NAME, 'rbx' + PlatformGuard.implementation?(:rubinius).should == true + end + + it "returns true if passed :jruby and RUBY_NAME == 'jruby'" do + Object.const_set :RUBY_NAME, 'jruby' + PlatformGuard.implementation?(:jruby).should == true + end + + it "returns true if passed :ironruby and RUBY_NAME == 'ironruby'" do + Object.const_set :RUBY_NAME, 'ironruby' + PlatformGuard.implementation?(:ironruby).should == true + end + + it "returns true if passed :maglev and RUBY_NAME == 'maglev'" do + Object.const_set :RUBY_NAME, 'maglev' + PlatformGuard.implementation?(:maglev).should == true + end + + it "returns true if passed :topaz and RUBY_NAME == 'topaz'" do + Object.const_set :RUBY_NAME, 'topaz' + PlatformGuard.implementation?(:topaz).should == true + end + + it "returns true if passed :ruby and RUBY_NAME matches /^ruby/" do + Object.const_set :RUBY_NAME, 'ruby' + PlatformGuard.implementation?(:ruby).should == true + + Object.const_set :RUBY_NAME, 'ruby1.8' + PlatformGuard.implementation?(:ruby).should == true + + Object.const_set :RUBY_NAME, 'ruby1.9' + PlatformGuard.implementation?(:ruby).should == true + end + + it "raises an error when passed an unrecognized name" do + Object.const_set :RUBY_NAME, 'ruby' + lambda { + PlatformGuard.implementation?(:python) + }.should raise_error(/unknown implementation/) + end +end + +describe PlatformGuard, ".standard?" do + it "returns true if implementation? returns true" do + PlatformGuard.should_receive(:implementation?).with(:ruby).and_return(true) + PlatformGuard.standard?.should be_true + end + + it "returns false if implementation? returns false" do + PlatformGuard.should_receive(:implementation?).with(:ruby).and_return(false) + PlatformGuard.standard?.should be_false + end +end + +describe PlatformGuard, ".wordsize?" do + it "returns true when arg is 32 and 1.size is 4" do + PlatformGuard.wordsize?(32).should == (1.size == 4) + end + + it "returns true when arg is 64 and 1.size is 8" do + PlatformGuard.wordsize?(64).should == (1.size == 8) + end +end + +describe PlatformGuard, ".os?" do + before :each do + stub_const 'PlatformGuard::HOST_OS', 'solarce' + end + + it "returns false when arg does not match the platform" do + PlatformGuard.os?(:ruby).should == false + end + + it "returns false when no arg matches the platform" do + PlatformGuard.os?(:ruby, :jruby, :rubinius, :maglev).should == false + end + + it "returns true when arg matches the platform" do + PlatformGuard.os?(:solarce).should == true + end + + it "returns true when any arg matches the platform" do + PlatformGuard.os?(:ruby, :jruby, :solarce, :rubinius, :maglev).should == true + end + + it "returns true when arg is :windows and the platform contains 'mswin'" do + stub_const 'PlatformGuard::HOST_OS', 'mswin32' + PlatformGuard.os?(:windows).should == true + end + + it "returns true when arg is :windows and the platform contains 'mingw'" do + stub_const 'PlatformGuard::HOST_OS', 'i386-mingw32' + PlatformGuard.os?(:windows).should == true + end + + it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mswin'" do + stub_const 'PlatformGuard::HOST_OS', 'i386-mswin32' + PlatformGuard.os?(:linux).should == false + end + + it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mingw'" do + stub_const 'PlatformGuard::HOST_OS', 'i386-mingw32' + PlatformGuard.os?(:linux).should == false + end +end + +describe PlatformGuard, ".os? on JRuby" do + before :all do + @verbose = $VERBOSE + $VERBOSE = nil + end + + after :all do + $VERBOSE = @verbose + end + + before :each do + @ruby_platform = Object.const_get :RUBY_PLATFORM + Object.const_set :RUBY_PLATFORM, 'java' + end + + after :each do + Object.const_set :RUBY_PLATFORM, @ruby_platform + end + + it "raises an error when testing for a :java platform" do + lambda { + PlatformGuard.os?(:java) + }.should raise_error(":java is not a valid OS") + end + + it "returns true when arg is :windows and RUBY_PLATFORM contains 'java' and os?(:windows) is true" do + stub_const 'PlatformGuard::HOST_OS', 'mswin32' + PlatformGuard.os?(:windows).should == true + end + + it "returns true when RUBY_PLATFORM contains 'java' and os?(argument) is true" do + stub_const 'PlatformGuard::HOST_OS', 'amiga' + PlatformGuard.os?(:amiga).should == true + end +end + +describe PlatformGuard, ".os?" do + before :each do + stub_const 'PlatformGuard::HOST_OS', 'unreal' + end + + it "returns true if argument matches RbConfig::CONFIG['host_os']" do + PlatformGuard.os?(:unreal).should == true + end + + it "returns true if any argument matches RbConfig::CONFIG['host_os']" do + PlatformGuard.os?(:bsd, :unreal, :amiga).should == true + end + + it "returns false if no argument matches RbConfig::CONFIG['host_os']" do + PlatformGuard.os?(:bsd, :netbsd, :amiga, :msdos).should == false + end + + it "returns false if argument does not match RbConfig::CONFIG['host_os']" do + PlatformGuard.os?(:amiga).should == false + end + + it "returns true when arg is :windows and RbConfig::CONFIG['host_os'] contains 'mswin'" do + stub_const 'PlatformGuard::HOST_OS', 'i386-mswin32' + PlatformGuard.os?(:windows).should == true + end + + it "returns true when arg is :windows and RbConfig::CONFIG['host_os'] contains 'mingw'" do + stub_const 'PlatformGuard::HOST_OS', 'i386-mingw32' + PlatformGuard.os?(:windows).should == true + end + + it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mswin'" do + stub_const 'PlatformGuard::HOST_OS', 'i386-mingw32' + PlatformGuard.os?(:linux).should == false + end + + it "returns false when arg is not :windows and RbConfig::CONFIG['host_os'] contains 'mingw'" do + stub_const 'PlatformGuard::HOST_OS', 'i386-mingw32' + PlatformGuard.os?(:linux).should == false + end +end + +describe PlatformGuard, ".windows?" do + it "returns true on windows" do + stub_const 'PlatformGuard::HOST_OS', 'i386-mingw32' + PlatformGuard.windows?.should == true + end + + it "returns false on non-windows" do + stub_const 'PlatformGuard::HOST_OS', 'i586-linux' + PlatformGuard.windows?.should == false + end +end diff --git a/spec/mspec/spec/guards/quarantine_spec.rb b/spec/mspec/spec/guards/quarantine_spec.rb new file mode 100644 index 0000000000..e5c7da7939 --- /dev/null +++ b/spec/mspec/spec/guards/quarantine_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' +require 'mspec/guards' + +describe QuarantineGuard, "#match?" do + it "returns true" do + QuarantineGuard.new.match?.should == true + end +end + +describe Object, "#quarantine!" do + before :each do + ScratchPad.clear + + @guard = QuarantineGuard.new + QuarantineGuard.stub(:new).and_return(@guard) + end + + it "does not yield" do + quarantine! { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "sets the name of the guard to :quarantine!" do + quarantine! { } + @guard.name.should == :quarantine! + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:match?).and_return(false) + @guard.should_receive(:unregister) + lambda do + quarantine! { raise Exception } + end.should raise_error(Exception) + end +end diff --git a/spec/mspec/spec/guards/superuser_spec.rb b/spec/mspec/spec/guards/superuser_spec.rb new file mode 100644 index 0000000000..f8815057e1 --- /dev/null +++ b/spec/mspec/spec/guards/superuser_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' +require 'mspec/guards' + +describe Object, "#as_superuser" do + before :each do + @guard = SuperUserGuard.new + SuperUserGuard.stub(:new).and_return(@guard) + ScratchPad.clear + end + + it "does not yield when Process.euid is not 0" do + Process.stub(:euid).and_return(501) + as_superuser { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "yields when Process.euid is 0" do + Process.stub(:euid).and_return(0) + as_superuser { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "sets the name of the guard to :as_superuser" do + as_superuser { } + @guard.name.should == :as_superuser + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:match?).and_return(true) + @guard.should_receive(:unregister) + lambda do + as_superuser { raise Exception } + end.should raise_error(Exception) + end +end diff --git a/spec/mspec/spec/guards/support_spec.rb b/spec/mspec/spec/guards/support_spec.rb new file mode 100644 index 0000000000..43a7e76f06 --- /dev/null +++ b/spec/mspec/spec/guards/support_spec.rb @@ -0,0 +1,69 @@ +require 'spec_helper' +require 'mspec/guards' + +describe Object, "#not_supported_on" do + before :all do + @verbose = $VERBOSE + $VERBOSE = nil + @ruby_name = Object.const_get :RUBY_NAME if Object.const_defined? :RUBY_NAME + end + + after :all do + $VERBOSE = @verbose + if @ruby_name + Object.const_set :RUBY_NAME, @ruby_name + else + Object.send :remove_const, :RUBY_NAME + end + end + + before :each do + ScratchPad.clear + end + + it "raises an Exception when passed :ruby" do + Object.const_set :RUBY_NAME, "jruby" + lambda { + not_supported_on(:ruby) { ScratchPad.record :yield } + }.should raise_error(Exception) + ScratchPad.recorded.should_not == :yield + end + + it "does not yield when #implementation? returns true" do + Object.const_set :RUBY_NAME, "jruby" + not_supported_on(:jruby) { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "yields when #standard? returns true" do + Object.const_set :RUBY_NAME, "ruby" + not_supported_on(:rubinius) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "yields when #implementation? returns false" do + Object.const_set :RUBY_NAME, "jruby" + not_supported_on(:rubinius) { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end +end + +describe Object, "#not_supported_on" do + before :each do + @guard = SupportedGuard.new + SupportedGuard.stub(:new).and_return(@guard) + end + + it "sets the name of the guard to :not_supported_on" do + not_supported_on(:rubinius) { } + @guard.name.should == :not_supported_on + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:match?).and_return(false) + @guard.should_receive(:unregister) + lambda do + not_supported_on(:rubinius) { raise Exception } + end.should raise_error(Exception) + end +end diff --git a/spec/mspec/spec/guards/user_spec.rb b/spec/mspec/spec/guards/user_spec.rb new file mode 100644 index 0000000000..2de4db7390 --- /dev/null +++ b/spec/mspec/spec/guards/user_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' +require 'mspec/guards' + +describe Object, "#as_user" do + before :each do + ScratchPad.clear + end + + it "yields when the Process.euid is not 0" do + Process.stub(:euid).and_return(501) + as_user { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "does not yield when the Process.euid is 0" do + Process.stub(:euid).and_return(0) + as_user { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end +end diff --git a/spec/mspec/spec/guards/version_spec.rb b/spec/mspec/spec/guards/version_spec.rb new file mode 100644 index 0000000000..f11e3dbd94 --- /dev/null +++ b/spec/mspec/spec/guards/version_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' +require 'mspec/guards' + +# The VersionGuard specifies a version of Ruby with a String of +# the form: v = 'major.minor.tiny'. +# +# A VersionGuard instance can be created with a single String, +# which means any version >= each component of v. +# Or, the guard can be created with a Range, a..b, or a...b, +# where a, b are of the same form as v. The meaning of the Range +# is as typically understood: a..b means v >= a and v <= b; +# a...b means v >= a and v < b. + +describe VersionGuard, "#match?" do + before :each do + hide_deprecation_warnings + stub_const "VersionGuard::FULL_RUBY_VERSION", SpecVersion.new('1.8.6') + end + + it "returns true when the argument is equal to RUBY_VERSION" do + VersionGuard.new('1.8.6').match?.should == true + end + + it "returns true when the argument is less than RUBY_VERSION" do + VersionGuard.new('1.8').match?.should == true + VersionGuard.new('1.8.5').match?.should == true + end + + it "returns false when the argument is greater than RUBY_VERSION" do + VersionGuard.new('1.8.7').match?.should == false + VersionGuard.new('1.9.2').match?.should == false + end + + it "returns true when the argument range includes RUBY_VERSION" do + VersionGuard.new('1.8.5'..'1.8.7').match?.should == true + VersionGuard.new('1.8'..'1.9').match?.should == true + VersionGuard.new('1.8'...'1.9').match?.should == true + VersionGuard.new('1.8'..'1.8.6').match?.should == true + VersionGuard.new('1.8.5'..'1.8.6').match?.should == true + VersionGuard.new(''...'1.8.7').match?.should == true + end + + it "returns false when the argument range does not include RUBY_VERSION" do + VersionGuard.new('1.8.7'..'1.8.9').match?.should == false + VersionGuard.new('1.8.4'..'1.8.5').match?.should == false + VersionGuard.new('1.8.4'...'1.8.6').match?.should == false + VersionGuard.new('1.8.5'...'1.8.6').match?.should == false + VersionGuard.new(''...'1.8.6').match?.should == false + end +end + +describe Object, "#ruby_version_is" do + before :each do + @guard = VersionGuard.new 'x.x.x' + VersionGuard.stub(:new).and_return(@guard) + ScratchPad.clear + end + + it "yields when #match? returns true" do + @guard.stub(:match?).and_return(true) + ruby_version_is('x.x.x') { ScratchPad.record :yield } + ScratchPad.recorded.should == :yield + end + + it "does not yield when #match? returns false" do + @guard.stub(:match?).and_return(false) + ruby_version_is('x.x.x') { ScratchPad.record :yield } + ScratchPad.recorded.should_not == :yield + end + + it "sets the name of the guard to :ruby_version_is" do + ruby_version_is("") { } + @guard.name.should == :ruby_version_is + end + + it "calls #unregister even when an exception is raised in the guard block" do + @guard.should_receive(:match?).and_return(true) + @guard.should_receive(:unregister) + lambda do + ruby_version_is("") { raise Exception } + end.should raise_error(Exception) + end +end |