diff options
Diffstat (limited to 'spec')
-rwxr-xr-x[-rw-r--r--] | spec/integration/provider/mailalias/aliases_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/puppet_spec/files.rb | 66 | ||||
-rw-r--r-- | spec/shared_behaviours/all_parsedfile_providers.rb | 8 | ||||
-rw-r--r-- | spec/spec_helper.rb | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | spec/unit/type/mailalias_spec.rb | 38 |
5 files changed, 71 insertions, 45 deletions
diff --git a/spec/integration/provider/mailalias/aliases_spec.rb b/spec/integration/provider/mailalias/aliases_spec.rb index 64cef1c..29098f8 100644..100755 --- a/spec/integration/provider/mailalias/aliases_spec.rb +++ b/spec/integration/provider/mailalias/aliases_spec.rb @@ -6,5 +6,5 @@ provider_class = Puppet::Type.type(:mailalias).provider(:aliases) describe provider_class do # #1560, in which we corrupt the format of complex mail aliases. - it_should_behave_like "all parsedfile providers", provider_class + it_behaves_like 'all parsedfile providers', provider_class end diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb index b02637e..5fef530 100644 --- a/spec/lib/puppet_spec/files.rb +++ b/spec/lib/puppet_spec/files.rb @@ -5,26 +5,32 @@ require 'pathname' # A support module for testing files. module PuppetSpec::Files + @global_tempfiles = [] + def self.cleanup - $global_tempfiles ||= [] - while path = $global_tempfiles.pop do + until @global_tempfiles.empty? + path = @global_tempfiles.pop begin Dir.unstub(:entries) - FileUtils.rm_rf path, :secure => true - rescue Errno::ENOENT - # nothing to do + FileUtils.rm_rf path, secure: true end end end - def make_absolute(path) PuppetSpec::Files.make_absolute(path) end + def make_absolute(path) + PuppetSpec::Files.make_absolute(path) + end + def self.make_absolute(path) path = File.expand_path(path) path[0] = 'c' if Puppet.features.microsoft_windows? path end - def tmpfile(name, dir = nil) PuppetSpec::Files.tmpfile(name, dir) end + def tmpfile(name, dir = nil) + PuppetSpec::Files.tmpfile(name, dir) + end + def self.tmpfile(name, dir = nil) # Generate a temporary file, just for the name... source = dir ? Tempfile.new(name, dir) : Tempfile.new(name) @@ -36,14 +42,20 @@ module PuppetSpec::Files path end - def file_containing(name, contents) PuppetSpec::Files.file_containing(name, contents) end + def file_containing(name, contents) + PuppetSpec::Files.file_containing(name, contents) + end + def self.file_containing(name, contents) file = tmpfile(name) File.open(file, 'wb') { |f| f.write(contents) } file end - def script_containing(name, contents) PuppetSpec::Files.script_containing(name, contents) end + def script_containing(name, contents) + PuppetSpec::Files.script_containing(name, contents) + end + def self.script_containing(name, contents) file = tmpfile(name) if Puppet.features.microsoft_windows? @@ -53,11 +65,14 @@ module PuppetSpec::Files text = contents[:posix] end File.open(file, 'wb') { |f| f.write(text) } - Puppet::FileSystem.chmod(0755, file) + Puppet::FileSystem.chmod(0o755, file) file end - def tmpdir(name) PuppetSpec::Files.tmpdir(name) end + def tmpdir(name) + PuppetSpec::Files.tmpdir(name) + end + def self.tmpdir(name) dir = Puppet::FileSystem.expand_path(Dir.mktmpdir(name).encode!(Encoding::UTF_8)) @@ -66,20 +81,26 @@ module PuppetSpec::Files dir end - def dir_containing(name, contents_hash) PuppetSpec::Files.dir_containing(name, contents_hash) end + def dir_containing(name, contents_hash) + PuppetSpec::Files.dir_containing(name, contents_hash) + end + def self.dir_containing(name, contents_hash) dir_contained_in(tmpdir(name), contents_hash) end - def dir_contained_in(dir, contents_hash) PuppetSpec::Files.dir_contained_in(dir, contents_hash) end + def dir_contained_in(dir, contents_hash) + PuppetSpec::Files.dir_contained_in(dir, contents_hash) + end + def self.dir_contained_in(dir, contents_hash) - contents_hash.each do |k,v| + contents_hash.each do |k, v| if v.is_a?(Hash) - Dir.mkdir(tmp = File.join(dir,k)) + Dir.mkdir(tmp = File.join(dir, k)) dir_contained_in(tmp, v) else file = File.join(dir, k) - File.open(file, 'wb') {|f| f.write(v) } + File.open(file, 'wb') { |f| f.write(v) } end end dir @@ -87,17 +108,16 @@ module PuppetSpec::Files def self.record_tmp(tmp) # ...record it for cleanup, - $global_tempfiles ||= [] - $global_tempfiles << tmp + @global_tempfiles << tmp end def expect_file_mode(file, mode) - actual_mode = "%o" % Puppet::FileSystem.stat(file).mode + actual_mode = '%o' % Puppet::FileSystem.stat(file).mode target_mode = if Puppet.features.microsoft_windows? - mode - else - "10" + "%04i" % mode.to_i - end + mode + else + '10' + '%04i' % mode.to_i + end expect(actual_mode).to eq(target_mode) end end diff --git a/spec/shared_behaviours/all_parsedfile_providers.rb b/spec/shared_behaviours/all_parsedfile_providers.rb index 9fdf54b..d697a14 100644 --- a/spec/shared_behaviours/all_parsedfile_providers.rb +++ b/spec/shared_behaviours/all_parsedfile_providers.rb @@ -1,5 +1,5 @@ -shared_examples_for "all parsedfile providers" do |provider, *files| - if files.empty? then +shared_examples_for 'all parsedfile providers' do |provider, *files| + if files.empty? files = my_fixtures end @@ -9,12 +9,12 @@ shared_examples_for "all parsedfile providers" do |provider, *files| provider.prefetch text = provider.to_file(provider.target_records(file)) - text.gsub!(/^# HEADER.+\n/, '') + text.gsub!(%r{^# HEADER.+\n}, '') oldlines = File.readlines(file) newlines = text.chomp.split "\n" oldlines.zip(newlines).each do |old, new| - expect(new.gsub(/\s+/, '')).to eq(old.chomp.gsub(/\s+/, '')) + expect(new.gsub(%r{\s+}, '')).to eq(old.chomp.gsub(%r{\s+}, '')) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0747894..9885e2d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -34,7 +34,7 @@ $LOAD_PATH.unshift File.join(dir, 'lib') # So everyone else doesn't have to include this base constant. module PuppetSpec - FIXTURE_DIR = File.join(File.expand_path(File.dirname(__FILE__)), "fixtures") unless defined?(FIXTURE_DIR) + FIXTURE_DIR = File.join(File.expand_path(File.dirname(__FILE__)), 'fixtures') unless defined?(FIXTURE_DIR) end require 'puppet_spec/files' diff --git a/spec/unit/type/mailalias_spec.rb b/spec/unit/type/mailalias_spec.rb index eb701c6..2a079ba 100644..100755 --- a/spec/unit/type/mailalias_spec.rb +++ b/spec/unit/type/mailalias_spec.rb @@ -4,46 +4,52 @@ require 'spec_helper' describe Puppet::Type.type(:mailalias) do include PuppetSpec::Files - let :tmpfile_path do tmpfile('afile') end - let :target do tmpfile('mailalias') end + let :tmpfile_path do + tmpfile('afile') + end + + let :target do + tmpfile('mailalias') + end + let :recipient_resource do - described_class.new(:name => "luke", :recipient => "yay", :target => target) + described_class.new(name: 'luke', recipient: 'yay', target: target) end let :file_resource do - described_class.new(:name => "lukefile", :file => tmpfile_path, :target => target) + described_class.new(name: 'lukefile', file: tmpfile_path, target: target) end - it "should be initially absent as a recipient" do + it 'is initially absent as a recipient' do expect(recipient_resource.retrieve_resource[:recipient]).to eq(:absent) end - it "should be initially absent as an included file" do + it 'is initially absent as an included file' do expect(file_resource.retrieve_resource[:file]).to eq(:absent) end - it "should try and set the recipient when it does the sync" do + it 'tries and set the recipient when it does the sync' do expect(recipient_resource.retrieve_resource[:recipient]).to eq(:absent) - recipient_resource.property(:recipient).expects(:set).with(["yay"]) + recipient_resource.property(:recipient).expects(:set).with(['yay']) recipient_resource.property(:recipient).sync end - it "should try and set the included file when it does the sync" do + it 'tries and set the included file when it does the sync' do expect(file_resource.retrieve_resource[:file]).to eq(:absent) file_resource.property(:file).expects(:set).with(tmpfile_path) file_resource.property(:file).sync end - it "should fail when file is not an absolute path" do + it 'fails when file is not an absolute path' do expect { - Puppet::Type.type(:mailalias).new(:name => 'x', :file => 'afile') - }.to raise_error Puppet::Error, /File paths must be fully qualified/ + Puppet::Type.type(:mailalias).new(name: 'x', file: 'afile') + }.to raise_error Puppet::Error, %r{File paths must be fully qualified} end - it "should fail when both file and recipient are specified" do + it 'fails when both file and recipient are specified' do expect { - Puppet::Type.type(:mailalias).new(:name => 'x', :file => tmpfile_path, - :recipient => 'foo@example.com') - }.to raise_error Puppet::Error, /cannot specify both a recipient and a file/ + Puppet::Type.type(:mailalias).new(name: 'x', file: tmpfile_path, + recipient: 'foo@example.com') + }.to raise_error Puppet::Error, %r{cannot specify both a recipient and a file} end end |