Module: Cuken::Api::File
- Includes:
- Etc
- Defined in:
- lib/cuken/api/file.rb
Instance Method Summary collapse
- #check_amtime_change(filename, time_type) ⇒ Object
- #check_modes(expected_mode, filename, octal = false) ⇒ Object
- #check_placed_directory_presence(paths, expect_presence) ⇒ Object
- #check_placed_file_content(file, partial_content, expect_match) ⇒ Object
- #check_placed_file_presence(paths, expect_presence) ⇒ Object
- #check_uid(dirname, owner) ⇒ Object
-
#parse_mode(mode, octal = false) ⇒ Object
def check_file_content(file, partial_content, expect_match, times = 1) regexp = regexp(partial_content) seen_count = 0 prep_for_fs_check do content = IO.read(::File.expand_path(file)) while (seen_count < times.to_i || content =~ regexp)do if content =~ regexp content = content.sub(regexp,”) seen_count+=1 end end if expect_match seen_count.should == times.to_i else seen_count.should_not == times.to_i end end end.
- #place_file(src, dst) ⇒ Object
- #place_folder_contents(dest_dir_name, src_dir_name) ⇒ Object
- #prep_for_placed_fs_check(&block) ⇒ Object
- #record_amtimes(filename) ⇒ Object
Instance Method Details
#check_amtime_change(filename, time_type) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/cuken/api/file.rb', line 59 def check_amtime_change(filename, time_type) in_dir do case time_type when "m" current_mtime = ::File.mtime(filename) current_mtime.should_not == @recorded_mtime when "a" current_atime = ::File.atime(filename) current_atime.should_not == @recorded_atime end end end |
#check_modes(expected_mode, filename, octal = false) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/cuken/api/file.rb', line 44 def check_modes(expected_mode, filename, octal = false) in_dir do cstats = ::File.stat(filename) parse_mode(cstats.mode, octal).should match /#{expected_mode}\Z/# ].should_not be_nil end end |
#check_placed_directory_presence(paths, expect_presence) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cuken/api/file.rb', line 126 def check_placed_directory_presence(paths, expect_presence) prep_for_placed_fs_check do paths.each do |path| if expect_presence dest = Pathname(path). Pathname(dest).mkpath Pathname(path)..should be_directory else Pathname(path)..should_not be_directory end end end end |
#check_placed_file_content(file, partial_content, expect_match) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cuken/api/file.rb', line 98 def check_placed_file_content(file, partial_content, expect_match) prep_for_placed_fs_check do content = IO.read(::File.(file)) if expect_match content.should include partial_content else content.should_not include partial_content end end end |
#check_placed_file_presence(paths, expect_presence) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/cuken/api/file.rb', line 114 def check_placed_file_presence(paths, expect_presence) prep_for_placed_fs_check do paths.each do |path| if expect_presence Pathname(path)..should be_file else Pathname(path)..should_not be_file end end end end |
#check_uid(dirname, owner) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/cuken/api/file.rb', line 51 def check_uid(dirname, owner) in_dir do uid = ::Etc.getpwnam(owner).uid cstats = ::File.stat(dirname) cstats.uid.should == uid end end |
#parse_mode(mode, octal = false) ⇒ Object
def check_file_content(file, partial_content, expect_match, times = 1)
regexp = regexp(partial_content)
seen_count = 0
prep_for_fs_check do
content = IO.read(::File.expand_path(file))
while (seen_count < times.to_i || content =~ regexp)do
if content =~ regexp
content = content.sub(regexp,'')
seen_count+=1
end
end
if expect_match
seen_count.should == times.to_i
else
seen_count.should_not == times.to_i
end
end
end
28 29 30 31 32 33 34 35 |
# File 'lib/cuken/api/file.rb', line 28 def parse_mode(mode, octal=false) return mode.to_s if octal if mode.respond_to?(:oct) mode.oct.to_s(8) else '%o' % mode end end |
#place_file(src, dst) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/cuken/api/file.rb', line 72 def place_file(src, dst) dest = Pathname(dst). FileUtils.mkdir_p((dest+src).dirname.to_s) in_dir do opts = {:verbose => true, :preserve => true, :remove_destination => true} FileUtils.cp_r(src, (dest+src).to_s, opts).should be_nil FileUtils.compare_file(src, (dest+src).to_s).should be_true end end |
#place_folder_contents(dest_dir_name, src_dir_name) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/cuken/api/file.rb', line 82 def place_folder_contents(dest_dir_name, src_dir_name) dest = Pathname(dest_dir_name)..realdirpath dest_parent = dest.dirname.to_s ::FileUtils.mkdir_p((dest+src_dir_name).to_s) in_dir do opts = {:verbose => true, :preserve => true, :remove_destination => true} ::FileUtils.cp_r(src_dir_name+'/.', dest.to_s, opts).should be_nil end ::Dir.glob("#{src_dir_name}/**/*").each do |fn| if ::File.file?(fn.to_s) fn2 = ::File.join(dest_parent, fn) ::FileUtils.compare_file(fn, fn2).should be_true if ::File.file?(fn2.to_s) end end end |
#prep_for_placed_fs_check(&block) ⇒ Object
109 110 111 112 |
# File 'lib/cuken/api/file.rb', line 109 def prep_for_placed_fs_check(&block) stop_processes! block.call end |
#record_amtimes(filename) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/cuken/api/file.rb', line 37 def record_amtimes(filename) in_dir do @recorded_mtime = ::File.mtime(filename) @recorded_atime = ::File.atime(filename) end end |