]> gitweb.fluxo.info Git - puppet-mailalias_core.git/commitdiff
Get unit tests passing
authorMelissa Stone <melissa@puppet.com>
Fri, 13 Apr 2018 21:04:01 +0000 (14:04 -0700)
committerMelissa Stone <melissa@puppet.com>
Mon, 16 Apr 2018 23:19:34 +0000 (16:19 -0700)
spec/lib/puppet_spec/files.rb [new file with mode: 0644]
spec/shared_behaviours/all_parsedfile_providers.rb [new file with mode: 0644]
spec/spec_helper.rb

diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb
new file mode 100644 (file)
index 0000000..b02637e
--- /dev/null
@@ -0,0 +1,103 @@
+require 'fileutils'
+require 'tempfile'
+require 'tmpdir'
+require 'pathname'
+
+# A support module for testing files.
+module PuppetSpec::Files
+  def self.cleanup
+    $global_tempfiles ||= []
+    while path = $global_tempfiles.pop do
+      begin
+        Dir.unstub(:entries)
+        FileUtils.rm_rf path, :secure => true
+      rescue Errno::ENOENT
+        # nothing to do
+      end
+    end
+  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 self.tmpfile(name, dir = nil)
+    # Generate a temporary file, just for the name...
+    source = dir ? Tempfile.new(name, dir) : Tempfile.new(name)
+    path = Puppet::FileSystem.expand_path(source.path.encode(Encoding::UTF_8))
+    source.close!
+
+    record_tmp(File.expand_path(path))
+
+    path
+  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 self.script_containing(name, contents)
+    file = tmpfile(name)
+    if Puppet.features.microsoft_windows?
+      file += '.bat'
+      text = contents[:windows]
+    else
+      text = contents[:posix]
+    end
+    File.open(file, 'wb') { |f| f.write(text) }
+    Puppet::FileSystem.chmod(0755, file)
+    file
+  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))
+
+    record_tmp(dir)
+
+    dir
+  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 self.dir_contained_in(dir, contents_hash)
+    contents_hash.each do |k,v|
+      if v.is_a?(Hash)
+        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) }
+      end
+    end
+    dir
+  end
+
+  def self.record_tmp(tmp)
+    # ...record it for cleanup,
+    $global_tempfiles ||= []
+    $global_tempfiles << tmp
+  end
+
+  def expect_file_mode(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
+    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
new file mode 100644 (file)
index 0000000..9fdf54b
--- /dev/null
@@ -0,0 +1,21 @@
+shared_examples_for "all parsedfile providers" do |provider, *files|
+  if files.empty? then
+    files = my_fixtures
+  end
+
+  files.flatten.each do |file|
+    it "should rewrite #{file} reasonably unchanged" do
+      provider.stubs(:default_target).returns(file)
+      provider.prefetch
+
+      text = provider.to_file(provider.target_records(file))
+      text.gsub!(/^# 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+/, ''))
+      end
+    end
+  end
+end
index efd225b540fcb886c0b5ecdd291eb96afd8cd648..0747894fdf4df8f93c57e1aee008d41ad9fd8274 100644 (file)
@@ -28,3 +28,17 @@ end
 RSpec.configure do |c|
   c.default_facts = default_facts
 end
+
+dir = File.expand_path(File.dirname(__FILE__))
+$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)
+end
+
+require 'puppet_spec/files'
+
+Pathname.glob("#{dir}/shared_behaviours/**/*.rb") do |behaviour|
+  require behaviour.relative_path_from(Pathname.new(dir))
+end