From 9bbbe87cba8a49cc68ce28f3ae6629b2e6b90d94 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Mon, 9 Jul 2018 22:14:52 -0700 Subject: Actually delete tempfiles Call cleanup after each test completes The record_tmp method needs to record the files to be deleted in the same class variable that the cleanup method uses. --- spec/lib/puppet_spec/files.rb | 69 +++---------------------------------------- spec/spec_helper_local.rb | 6 ++++ 2 files changed, 10 insertions(+), 65 deletions(-) diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb index 28413cd..fa774ef 100644 --- a/spec/lib/puppet_spec/files.rb +++ b/spec/lib/puppet_spec/files.rb @@ -1,12 +1,12 @@ require 'fileutils' require 'tempfile' require 'tmpdir' -require 'pathname' # A support module for testing files. module PuppetSpec::Files + @global_tempfiles = [] + def self.cleanup - @global_tempfiles ||= [] until @global_tempfiles.empty? path = @global_tempfiles.pop Dir.unstub(:entries) @@ -16,47 +16,14 @@ module PuppetSpec::Files module_function - def make_absolute(path) - path = File.expand_path(path) - path[0] = 'c' if Puppet.features.microsoft_windows? - path - end - def tmpfile(name, dir = nil) dir ||= Dir.tmpdir path = Puppet::FileSystem.expand_path(make_tmpname(name, nil).encode(Encoding::UTF_8), dir) - record_tmp(File.expand_path(path)) + PuppetSpec::Files.record_tmp(File.expand_path(path)) path end - def file_containing(name, contents) - file = tmpfile(name) - File.open(file, 'wb') { |f| f.write(contents) } - file - end - - def 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(0o755, file) - file - end - - def tmpdir(name) - dir = Puppet::FileSystem.expand_path(Dir.mktmpdir(name).encode!(Encoding::UTF_8)) - - record_tmp(dir) - - dir - end - # Copied from ruby 2.4 source def make_tmpname((prefix, suffix), n) prefix = (String.try_convert(prefix) || @@ -70,36 +37,8 @@ module PuppetSpec::Files path end - def dir_containing(name, contents_hash) - dir_contained_in(tmpdir(name), contents_hash) - end - - def 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 record_tmp(tmp) + 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/spec_helper_local.rb b/spec/spec_helper_local.rb index 0fcc231..86c87f0 100644 --- a/spec/spec_helper_local.rb +++ b/spec/spec_helper_local.rb @@ -11,3 +11,9 @@ require 'puppet_spec/files' Pathname.glob("#{dir}/shared_behaviours/**/*.rb") do |behaviour| require behaviour.relative_path_from(Pathname.new(dir)) end + +RSpec.configure do |c| + c.after :each do + PuppetSpec::Files.cleanup + end +end -- cgit v1.2.3