spec/support/random.rb in imw-0.1.1 vs spec/support/random.rb in imw-0.2.0

- old
+ new

@@ -1,40 +1,27 @@ require 'fileutils' module IMWTest module Random - STRING_CHARS = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + [' ',' ',' ',' ',' '] - TEXT_CHARS = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + [' ',' ',' ',' ',' ',"\n"] - FILENAME_CHARS = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + ["-","_"] - FILENAME_MAX_LENGTH = 9 - TEXT_MAX_LENGTH = 1024 - EXTENSIONS = { - /\.csv$/ => :csv_file, - /\.xml$/ => :xml_file, - /\.html$/ => :html_file, - /\.tar$/ => :tar_file, - /\.tar\.gz$/ => :targz_file, - /\.tar\.bz2$/ => :tarbz2_file, - /\.rar$/ => :rar_file, - /\.zip$/ => :zip_file - } - EXTERNAL_PROGRAMS = if defined?(IMW) && defined?(IMW::EXTERNAL_PROGRAMS) - IMW::EXTERNAL_PROGRAMS - else - { - :tar => "tar", - :rar => "rar", - :zip => "zip", - :unzip => "unzip", - :gzip => "gzip", - :bzip2 => "bzip2", - :wget => "wget" - } - end - - private + STRING_CHARS = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + [' ',' ',' ',' ',' '] unless defined?(STRING_CHARS) + TEXT_CHARS = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + [' ',' ',' ',' ',' ',"\n"] unless defined?(TEXT_CHARS) + FILENAME_CHARS = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + ["-","_",' '] unless defined?(FILENAME_CHARS) + FILENAME_MAX_LENGTH = 9 unless defined?(FILENAME_MAX_LENGTH) + TEXT_MAX_LENGTH = 1024 unless defined?(TEXT_MAX_LENGTH) + EXTENSIONS = [ + [/\.csv$/ , :csv_file], + [/\.xml$/ , :xml_file], + [/\.html$/ , :html_file], + [/\.tar\.gz$/ , :targz_file], + [/\.tar\.bz2$/ , :tarbz2_file], + [/\.bz2$/ , :bz2_file], + [/\.gz$/ , :gz_file], + [/\.tar$/ , :tar_file], + [/\.rar$/ , :rar_file], + [/\.zip$/ , :zip_file] + ] unless defined?(EXTENSIONS) # Return a random filename. Optional +length+ to set the maximum # length of the filename returned. def self.basename options = {} length = (options[:length] or FILENAME_MAX_LENGTH) filename = (1..length).map { |i| FILENAME_CHARS.random }.join @@ -52,11 +39,10 @@ length = (options[:length] or TEXT_MAX_LENGTH) char_pool = options[:newlines] ? TEXT_CHARS : STRING_CHARS (1..length).map { |i| char_pool.random }.join end - public # Create a random file by matching the extension of the given # +filename+ or a text file if no match is found. def self.file filename match = EXTENSIONS.find { |regex,func| regex.match filename } match ? self.send(match.last,filename) : self.text_file(filename) @@ -113,52 +99,69 @@ # Create a tar archive at the given +filename+ containing random # files. def self.tar_file filename tmpd = File.dirname(filename) + '/dir' directory_with_files(tmpd) - FileUtils.cd(tmpd) {|dir| system("#{EXTERNAL_PROGRAMS[:tar]} -cf file.tar *") } + FileUtils.cd(tmpd) {|dir| system("tar -cf file.tar *") } FileUtils.cp(tmpd + "/file.tar",filename) FileUtils.rm_rf(tmpd) end # Create a tar.gz archive at the given +filename+ containing # random files. def self.targz_file filename tar = File.dirname(filename) + "/file.tar" targz = tar + ".gz" tar_file tar - system("#{EXTERNAL_PROGRAMS[:gzip]} #{tar}") + system("gzip #{tar}") FileUtils.cp(targz,filename) FileUtils.rm(targz) end # Create a tar.bz2 archive at the given +filename+ containing # random files. def self.tarbz2_file filename tar = File.dirname(filename) + "/file.tar" tarbz2 = tar + ".bz2" tar_file tar - system("#{EXTERNAL_PROGRAMS[:bzip2]} #{tar}") + system("bzip2 #{tar}") FileUtils.cp(tarbz2,filename) FileUtils.rm(tarbz2) end + # Create a .bz2 file at the given +filename+. + def self.bz2_file filename + text_path = File.dirname(filename) + "/fake_file" + text_file(text_path) + system("bzip2 #{text_path}") + FileUtils.mv(text_path + ".bz2", filename) + end + + # Create a .gz file at the given +filename+. + def self.gz_file filename + text_path = File.dirname(filename) + "/fake_file" + text_file(text_path) + system("gzip #{text_path}") + FileUtils.mv(text_path + ".gz", filename) + end + + # Create a compressed rar archive at the given +filename+ # containing random files. def self.rar_file filename tmpd = File.dirname(filename) + '/dir' directory_with_files(tmpd) - FileUtils.cd(tmpd) {|dir| system("#{EXTERNAL_PROGRAMS[:rar]} a -r -o+ file.rar *") } + FileUtils.cd(tmpd) {|dir| system("rar a -o+ -inul file.rar *") } FileUtils.cp(tmpd + "/file.rar",filename) FileUtils.rm_rf(tmpd) end # Create a compressed zip archive at the given +filename+ # containing random files. def self.zip_file filename tmpd = File.dirname(filename) + '/dir' directory_with_files(tmpd) - FileUtils.cd(tmpd) {|dir| system("#{EXTERNAL_PROGRAMS[:zip]} -r file.zip *") } + FileUtils.cd(tmpd) {|dir| system("zip -qqr file.zip *") } FileUtils.cp(tmpd + "/file.zip",filename) FileUtils.rm_rf(tmpd) end # Creates +directory+ and fills it with random files containing