Module: Siba::TestFiles

Extended by:
FilePlug
Defined in:
lib/siba/test_files.rb

Class Method Summary collapse

Methods included from FilePlug

siba_file, siba_file, siba_file=

Class Method Details

.generate_path(dest_file_name_part, tmp_dir = nil) ⇒ Object



34
35
36
37
# File 'lib/siba/test_files.rb', line 34

def generate_path(dest_file_name_part, tmp_dir=nil)
  tmp_dir ||= Siba.tmp_dir
  File.join(tmp_dir,"#{dest_file_name_part}#{random_suffix}")
end

.mkdir_in_tmp_dir(prefix, tmp_dir = nil) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/siba/test_files.rb', line 39

def mkdir_in_tmp_dir(prefix, tmp_dir=nil)
  tmp_dir ||= Siba.tmp_dir
  dir = "#{prefix}#{random_suffix}"
  dir_path = File.join(tmp_dir, dir)
  siba_file.file_utils_mkpath dir_path
  dir_path
end

.prepare_options(src_yml_path, replace_data) ⇒ Object



60
61
62
# File 'lib/siba/test_files.rb', line 60

def prepare_options(src_yml_path, replace_data)
  YAML.load prepare_options_text(src_yml_path, replace_data)
end

.prepare_options_text(src_yml_path, replace_data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/siba/test_files.rb', line 47

def prepare_options_text(src_yml_path, replace_data)
  replace_prefix = "#_replace_"
  options_text = Siba::FileHelper.read src_yml_path
  replace_data.each_pair do |key, value|
    str_to_replace = "#{replace_prefix}#{key.to_s}"
    raise "YML does not contain #{str_to_replace} text" unless options_text.include? str_to_replace
    options_text.gsub! str_to_replace, value
  end

  raise "Test YML still has unreplaced text" if options_text.include? "#{replace_prefix}"
  options_text
end

.prepare_test_dir(dest_dir_name_part, tmp_dir = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/siba/test_files.rb', line 18

def prepare_test_dir(dest_dir_name_part, tmp_dir=nil)
  tmp_dir ||= Siba.tmp_dir
  dest_dir = File.join(tmp_dir,"#{dest_dir_name_part}#{random_suffix}")
  siba_file.file_utils_mkpath tmp_dir unless siba_file.file_directory? tmp_dir
  siba_file.file_utils_cp_r test_dir, dest_dir
  dest_dir
end

.prepare_test_file(dest_file_name_part, tmp_dir = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/siba/test_files.rb', line 26

def prepare_test_file(dest_file_name_part, tmp_dir=nil)
  tmp_dir ||= Siba.tmp_dir
  dest_file = File.join(tmp_dir,"#{dest_file_name_part}#{random_suffix}")
  siba_file.file_utils_mkpath tmp_dir unless siba_file.file_directory? tmp_dir
  siba_file.file_utils_cp test_file, dest_file
  dest_file
end

.prepare_yml(src_yml_path, replace_data, tmp_dir = nil) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/siba/test_files.rb', line 64

def prepare_yml(src_yml_path, replace_data, tmp_dir=nil)
  tmp_dir ||= Siba.tmp_dir
  options_text = prepare_options_text src_yml_path, replace_data
  dest_file = File.join tmp_dir,"#{File.basename(src_yml_path,".yml")}#{random_suffix}.yml"
  siba_file.file_open(dest_file, "w") { |f| f.write options_text }
  dest_file
end

.random_suffixObject



72
73
74
# File 'lib/siba/test_files.rb', line 72

def random_suffix
  "-#{Siba::SecurityHelper.alphanumeric_password(8,true)}"
end

.test_dirObject



14
15
16
# File 'lib/siba/test_files.rb', line 14

def test_dir
  File.join test_files_dir, "files_and_dirs"
end

.test_fileObject



10
11
12
# File 'lib/siba/test_files.rb', line 10

def test_file
  File.join test_files_dir, "a_file"
end

.test_files_dirObject



6
7
8
# File 'lib/siba/test_files.rb', line 6

def test_files_dir
  siba_file.file_expand_path("../test_files/", __FILE__)
end