diff options
| author | Nick Lewis <nick@puppetlabs.com> | 2010-11-23 16:42:06 -0800 |
|---|---|---|
| committer | Nick Lewis <nick@puppetlabs.com> | 2010-11-23 16:42:06 -0800 |
| commit | 4b35402ba85d8842d757becec5c8a7bf4d6f6654 (patch) | |
| tree | 5da174c4c1c106b99e8c464e45e89d7b5ba2b5a2 /spec/unit/util | |
| parent | a7bd1630622cace01d0e4c974c76366e4b3b886c (diff) | |
| parent | 2bc6727b6ac7348dbac98099f1fe0aeb3cd1295f (diff) | |
Merge branch 'next'
Diffstat (limited to 'spec/unit/util')
| -rw-r--r-- | spec/unit/util/command_line_spec.rb | 4 | ||||
| -rwxr-xr-x | spec/unit/util/file_locking_spec.rb | 52 | ||||
| -rwxr-xr-x | spec/unit/util/log_spec.rb | 4 | ||||
| -rw-r--r-- | spec/unit/util/monkey_patches_spec.rb | 26 | ||||
| -rwxr-xr-x | spec/unit/util/pson_spec.rb | 15 | ||||
| -rwxr-xr-x | spec/unit/util/rdoc_spec.rb | 10 | ||||
| -rwxr-xr-x | spec/unit/util/storage_spec.rb | 5 | ||||
| -rwxr-xr-x | spec/unit/util/zaml_spec.rb | 1 |
8 files changed, 84 insertions, 33 deletions
diff --git a/spec/unit/util/command_line_spec.rb b/spec/unit/util/command_line_spec.rb index a83ad968d..7ba965249 100644 --- a/spec/unit/util/command_line_spec.rb +++ b/spec/unit/util/command_line_spec.rb @@ -86,7 +86,7 @@ describe Puppet::Util::CommandLine do describe "when the subcommand is not implemented" do it "should find and invoke an executable with a hyphenated name" do commandline = Puppet::Util::CommandLine.new("puppet", ['whatever', 'argument'], @tty) - Puppet::Util.expects(:binary).with('puppet-whatever').returns('/dev/null/puppet-whatever') + Puppet::Util.expects(:which).with('puppet-whatever').returns('/dev/null/puppet-whatever') commandline.expects(:system).with('/dev/null/puppet-whatever', 'argument') commandline.execute @@ -95,7 +95,7 @@ describe Puppet::Util::CommandLine do describe "and an external implementation cannot be found" do it "should abort and show the usage message" do commandline = Puppet::Util::CommandLine.new("puppet", ['whatever', 'argument'], @tty) - Puppet::Util.expects(:binary).with('puppet-whatever').returns(nil) + Puppet::Util.expects(:which).with('puppet-whatever').returns(nil) commandline.expects(:system).never commandline.expects(:usage_message).returns("the usage message") diff --git a/spec/unit/util/file_locking_spec.rb b/spec/unit/util/file_locking_spec.rb index 10051060c..8fafb1d52 100755 --- a/spec/unit/util/file_locking_spec.rb +++ b/spec/unit/util/file_locking_spec.rb @@ -32,17 +32,12 @@ describe Puppet::Util::FileLocking do end it "should use a global shared mutex" do - @sync = mock 'sync' - @sync.expects(:synchronize).with(Sync::SH).once - Puppet::Util.expects(:sync).with('/file').returns @sync - + Puppet::Util.expects(:synchronize_on).with('/file',Sync::SH).once Puppet::Util::FileLocking.readlock '/file' end it "should use a shared lock on the file" do - @sync = mock 'sync' - @sync.stubs(:synchronize).yields - Puppet::Util.expects(:sync).with('/file').returns @sync + Puppet::Util.expects(:synchronize_on).with('/file',Sync::SH).yields fh = mock 'filehandle' File.expects(:open).with("/file").yields fh @@ -59,9 +54,7 @@ describe Puppet::Util::FileLocking do end it "should create missing files" do - @sync = mock 'sync' - @sync.stubs(:synchronize).yields - Puppet::Util.expects(:sync).with('/file').returns @sync + Puppet::Util.expects(:synchronize_on).with('/file',Sync::SH).yields File.expects(:exists?).with('/file').returns false File.expects(:open).with('/file').once @@ -72,9 +65,7 @@ describe Puppet::Util::FileLocking do describe "when acquiring a write lock" do before do - @sync = mock 'sync' - Puppet::Util.stubs(:sync).returns @sync - @sync.stubs(:synchronize).yields + Puppet::Util.stubs(:synchronize_on).yields File.stubs(:file?).with('/file').returns true File.stubs(:exists?).with('/file').returns true end @@ -88,29 +79,26 @@ describe Puppet::Util::FileLocking do end it "should use a global exclusive mutex" do - sync = mock 'sync' - sync.expects(:synchronize).with(Sync::EX) - Puppet::Util.expects(:sync).with("/file").returns sync - + Puppet::Util.expects(:synchronize_on).with("/file",Sync::EX) Puppet::Util::FileLocking.writelock '/file' end it "should use any specified mode when opening the file" do - File.expects(:open).with("/file", "w", :mymode) + File.expects(:open).with("/file", File::Constants::CREAT | File::Constants::WRONLY , :mymode) Puppet::Util::FileLocking.writelock('/file', :mymode) end it "should use the mode of the existing file if no mode is specified" do File.expects(:stat).with("/file").returns(mock("stat", :mode => 0755)) - File.expects(:open).with("/file", "w", 0755) + File.expects(:open).with("/file", File::Constants::CREAT | File::Constants::WRONLY, 0755) Puppet::Util::FileLocking.writelock('/file') end it "should use 0600 as the mode if no mode is specified and the file does not exist" do File.expects(:stat).raises(Errno::ENOENT) - File.expects(:open).with("/file", "w", 0600) + File.expects(:open).with("/file", File::Constants::CREAT | File::Constants::WRONLY, 0600) Puppet::Util::FileLocking.writelock('/file') end @@ -130,6 +118,8 @@ describe Puppet::Util::FileLocking do lfh = mock 'locked_filehandle' fh.expects(:lock_exclusive).yields(lfh) + lfh.stubs(:seek) + lfh.stubs(:truncate) lfh.expects(:print).with "foo" Puppet::Util::FileLocking.writelock('/file') do |f| @@ -137,18 +127,32 @@ describe Puppet::Util::FileLocking do end end + it "should truncate the file under an exclusive lock" do + fh = mock 'fh' + File.expects(:open).yields fh + + lfh = mock 'locked_filehandle' + fh.expects(:lock_exclusive).yields(lfh) + + lfh.expects(:seek).with(0, IO::SEEK_SET) + lfh.expects(:truncate).with(0) + lfh.stubs(:print) + + Puppet::Util::FileLocking.writelock('/file') do |f| + f.print "foo" + end + end + it "should only work on regular files" do File.expects(:file?).with('/file').returns false proc { Puppet::Util::FileLocking.writelock('/file') }.should raise_error(ArgumentError) end it "should create missing files" do - @sync = mock 'sync' - @sync.stubs(:synchronize).yields - Puppet::Util.expects(:sync).with('/file').returns @sync + Puppet::Util.expects(:synchronize_on).with('/file',Sync::EX).yields File.expects(:exists?).with('/file').returns false - File.expects(:open).with('/file', 'w', 0600).once + File.expects(:open).with('/file', File::Constants::CREAT | File::Constants::WRONLY, 0600).once Puppet::Util::FileLocking.writelock('/file') end diff --git a/spec/unit/util/log_spec.rb b/spec/unit/util/log_spec.rb index 7d96fe190..ea5d59859 100755 --- a/spec/unit/util/log_spec.rb +++ b/spec/unit/util/log_spec.rb @@ -7,7 +7,7 @@ require 'puppet/util/log' describe Puppet::Util::Log do it "should write a given message to the specified destination" do arraydest = [] - Puppet::Util::Log.newdestination(arraydest) + Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(arraydest)) Puppet::Util::Log.new(:level => :notice, :message => "foo") message = arraydest.last.message message.should == "foo" @@ -87,7 +87,7 @@ describe Puppet::Util::Log do it "should flush the log queue when the first destination is specified" do Puppet::Util::Log.close_all Puppet::Util::Log.expects(:flushqueue) - Puppet::Util::Log.newdestination([]) + Puppet::Util::Log.newdestination(:console) end it "should convert the level to a symbol if it's passed in as a string" do diff --git a/spec/unit/util/monkey_patches_spec.rb b/spec/unit/util/monkey_patches_spec.rb index b0f61c808..049ed1044 100644 --- a/spec/unit/util/monkey_patches_spec.rb +++ b/spec/unit/util/monkey_patches_spec.rb @@ -5,3 +5,29 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f require 'puppet/util/monkey_patches' + +describe "yaml deserialization" do + it "should call yaml_initialize when deserializing objects that have that method defined" do + class Puppet::TestYamlInitializeClass + attr_reader :foo + + def yaml_initialize(tag, var) + var.should == {'foo' => 100} + instance_variables.should == [] + @foo = 200 + end + end + + obj = YAML.load("--- !ruby/object:Puppet::TestYamlInitializeClass\n foo: 100") + obj.foo.should == 200 + end + + it "should not call yaml_initialize if not defined" do + class Puppet::TestYamlNonInitializeClass + attr_reader :foo + end + + obj = YAML.load("--- !ruby/object:Puppet::TestYamlNonInitializeClass\n foo: 100") + obj.foo.should == 100 + end +end diff --git a/spec/unit/util/pson_spec.rb b/spec/unit/util/pson_spec.rb index d02d28517..474ddafa4 100755 --- a/spec/unit/util/pson_spec.rb +++ b/spec/unit/util/pson_spec.rb @@ -35,4 +35,19 @@ describe Puppet::Util::Pson do bin_string = (1..20000).collect { |i| ((17*i+13*i*i) % 255).chr }.join PSON.parse(%Q{{ "type": "foo", "data": #{bin_string.to_pson} }})["data"].should == bin_string end + + it "should be able to handle UTF8 that isn't a real unicode character" do + s = ["\355\274\267"] + PSON.parse( [s].to_pson ).should == [s] + end + + it "should be able to handle UTF8 for \\xFF" do + s = ["\xc3\xbf"] + PSON.parse( [s].to_pson ).should == [s] + end + + it "should be able to handle invalid UTF8 bytes" do + s = ["\xc3\xc3"] + PSON.parse( [s].to_pson ).should == [s] + end end diff --git a/spec/unit/util/rdoc_spec.rb b/spec/unit/util/rdoc_spec.rb index 58c2034ee..41d4b9cd0 100755 --- a/spec/unit/util/rdoc_spec.rb +++ b/spec/unit/util/rdoc_spec.rb @@ -43,12 +43,20 @@ describe Puppet::Util::RDoc do Puppet::Util::RDoc.rdoc("output", [], "utf-8") end - it "should tell RDoc to force updates of indices" do + it "should tell RDoc to force updates of indices when RDoc supports it" do + Options::OptionList.stubs(:options).returns([["--force-update", "-U", 0 ]]) @rdoc.expects(:document).with { |args| args.include?("--force-update") } Puppet::Util::RDoc.rdoc("output", []) end + it "should not tell RDoc to force updates of indices when RDoc doesn't support it" do + Options::OptionList.stubs(:options).returns([]) + @rdoc.expects(:document).never.with { |args| args.include?("--force-update") } + + Puppet::Util::RDoc.rdoc("output", []) + end + it "should tell RDoc to use the given outputdir" do @rdoc.expects(:document).with { |args| args.include?("--op") and args.include?("myoutputdir") } diff --git a/spec/unit/util/storage_spec.rb b/spec/unit/util/storage_spec.rb index 6c8baba1f..ae3cbc2ae 100755 --- a/spec/unit/util/storage_spec.rb +++ b/spec/unit/util/storage_spec.rb @@ -3,15 +3,14 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'yaml' -require 'tempfile' - require 'puppet/util/storage' describe Puppet::Util::Storage do + include PuppetSpec::Files before(:all) do @basepath = Puppet.features.posix? ? "/somepath" : "C:/somepath" - Puppet[:statedir] = Dir.tmpdir + Puppet[:statedir] = tmpdir("statedir") end after(:all) do diff --git a/spec/unit/util/zaml_spec.rb b/spec/unit/util/zaml_spec.rb index b223f89d4..358c6aa11 100755 --- a/spec/unit/util/zaml_spec.rb +++ b/spec/unit/util/zaml_spec.rb @@ -61,4 +61,3 @@ describe "Pure ruby yaml implementation" do x2[2].should equal(x2) end end - |
