Module: BobTheHelper::FileSystem

Included in:
PointRb::LayoutStorageManager
Defined in:
lib/bob_the_helper/file_system.rb,
lib/bob_the_helper/file_system/exceptions.rb

Defined Under Namespace

Modules: Exceptions

Instance Method Summary collapse

Instance Method Details

#cleanup_working_directoryObject

Clean up test directory



36
37
38
39
# File 'lib/bob_the_helper/file_system.rb', line 36

def cleanup_working_directory
  delete_working_directory
  create_working_directory
end

#create_directory(*dirs) ⇒ String, Array

Create directory(ies)

Parameters:

  • dir (String, Array)

    the directories to be created, multiple arguments are possible as well

Returns:

  • (String, Array)

    returns a string if there was only one file given, and an array with muliple files



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bob_the_helper/file_system.rb', line 55

def create_directory(*dirs)
  raise_if_forbidden_path_for_create_operation(dirs)

  directories = expand_path(dirs.flatten)
  FileUtils.mkdir_p(directories)

  if directories.size == 1
    return directories.first
  else
    return directories
  end
end

#create_file(path, content = '') ⇒ String

Create a single file

Parameters:

  • file_path (String)

    the path for the new file (can include directories)

  • content (String) (defaults to: '')

    the content written to the file

Returns:

  • (String)

    the path to the created file



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bob_the_helper/file_system.rb', line 124

def create_file(path, content='')
  raise_if_forbidden_path_for_create_operation(path)

  file = expand_path(path).first
  directory = File.dirname(file)

  FileUtils.mkdir_p(directory) unless directory == '.'
  File.open(file, "wb") do |f|
    f.write content
  end

  file
end

#create_working_directoryObject

Create temporary directory



26
27
28
# File 'lib/bob_the_helper/file_system.rb', line 26

def create_working_directory
  FileUtils.mkdir_p(working_directory) unless File.exists? working_directory
end

#delete_directory(*dirs) ⇒ String, Array

Delete directory(ies)

Parameters:

  • dir (String, Array)

    the directories to be deleted, multiple arguments are possible as well

Returns:

  • (String, Array)

    returns a string if there was only one file given, and an array with muliple files



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bob_the_helper/file_system.rb', line 76

def delete_directory(*dirs)
  raise_if_forbidden_path_for_delete_operation(dirs)

  directories = expand_path(dirs.flatten)
  FileUtils.rm_r(directories)

  if directories.size == 1
    return directories.first
  else
    return directories
  end
end

#delete_file(*files) ⇒ String

Delete a single file

Parameters:

  • file_path (String)

    the path for the new file (can include directories)

  • content (String)

    the content written to the file

Returns:

  • (String)

    the path to the created file



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/bob_the_helper/file_system.rb', line 148

def delete_file(*files)
  raise_if_forbidden_path_for_delete_operation(files)

  files_to_be_deleted = expand_path(files.flatten)
  FileUtils.rm(files_to_be_deleted)

  if files_to_be_deleted.size == 1
    return files_to_be_deleted.first
  else
    return files_to_be_deleted
  end
end

#delete_working_directoryObject

Delete temporary directory



31
32
33
# File 'lib/bob_the_helper/file_system.rb', line 31

def delete_working_directory
  FileUtils.rm_rf(working_directory) if File.exists? working_directory
end

#expand_path(*paths) ⇒ Array, String

Expand path based on temporary directory

Parameters:

  • paths (String, Array, Multiple Values)

    the paths to be expanded

Returns:

  • (Array, String)

    the expanded arrays



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/bob_the_helper/file_system.rb', line 182

def expand_path(*paths)
  raise_if_forbidden_path_for_create_operation(paths)

  paths.flatten.map do |p| 
    case p 
    when /^~/
      File.expand_path(p) 
    else
      File.join(working_directory, p ) 
    end
  end
end

#path_does_not_exist?(*paths) ⇒ TrueClass, FalseClass

Check absence of path(s)

Parameters:

  • paths (String, Array)

    which path(s) should be checked, multiple arguments are possible as well

Returns:

  • (TrueClass, FalseClass)

    the result of all checks done



110
111
112
# File 'lib/bob_the_helper/file_system.rb', line 110

def path_does_not_exist?(*paths)
  not path_exists?(paths)
end

#path_exists?(*paths) ⇒ TrueClass, FalseClass

Check existence of path(s)

Parameters:

  • paths (String, Array)

    which path(s) should be checked, multiple arguments are possible as well

Returns:

  • (TrueClass, FalseClass)

    the result of all checks done



96
97
98
99
100
101
# File 'lib/bob_the_helper/file_system.rb', line 96

def path_exists?(*paths)
  raise_if_forbidden_path_for_create_operation(paths)

  paths_expanded = expand_path(paths.flatten)
  paths_expanded.flatten.all? { |p| File.exists?(p) }
end

#path_matches?(strings, regex, *paths) ⇒ Boolean

Check if path matches

Parameters:

  • strings (Array, String)

    strings which are checked against paths

  • regex (Array, String)

    regex which is checked against path

  • paths (Array, String)

    the paths to be checked

Returns:

  • (Boolean)


238
239
240
241
242
243
# File 'lib/bob_the_helper/file_system.rb', line 238

def path_matches?(strings, regex, *paths)
  flattend_paths = paths.flatten
  flattend_strings = strings.flatten

  flattend_paths.any? { |f|  f =~ regex or flattend_strings.include?(f) }
end

#raise_if_forbidden_path_for_create_operation(*paths) ⇒ TrueClass, FalseClass

Check if path is forbidden for delete operation

Parameters:

  • paths (String, Array)

    paths which should be checked

Returns:

  • (TrueClass, FalseClass)

    true if path is forbidden, false if path is not forbidden

Raises:



202
203
204
205
206
207
208
# File 'lib/bob_the_helper/file_system.rb', line 202

def raise_if_forbidden_path_for_create_operation(*paths)
  flattend_paths = paths.flatten
  strings = []
  regex = %r[\.\.]

  raise FileSystem::Exceptions::InvalidPath , "Sorry, but you cannot use paths matching \"#{strings.join(', ')}\"  or \"#{regex.to_s}\" here!" if path_matches?(strings, regex, flattend_paths)
end

#raise_if_forbidden_path_for_delete_operation(*paths) ⇒ TrueClass, FalseClass

Check if path is forbidden for delete operation

Parameters:

  • paths (String, Array)

    paths which should be checked

Returns:

  • (TrueClass, FalseClass)

    true if path is forbidden, false if path is not forbidden

Raises:



217
218
219
220
221
222
223
# File 'lib/bob_the_helper/file_system.rb', line 217

def raise_if_forbidden_path_for_delete_operation(*paths)
  flattend_paths = paths.flatten
  strings = %w[ / ]
  regex = %r[\.\.]

  raise FileSystem::Exceptions::InvalidPath , "Sorry, but you cannot use paths matching \"#{strings.join(', ')}\"  or \"#{regex.to_s}\" here!" if path_matches?(strings, regex, flattend_paths)
end

#read_file(path) ⇒ String, Binary

Read the content of a file

Parameters:

  • file_path (String)

    the path to the file

Returns:

  • (String, Binary)

    the content of the file



168
169
170
171
172
173
# File 'lib/bob_the_helper/file_system.rb', line 168

def read_file(path)
  raise_if_forbidden_path_for_create_operation(path)

  file_path = expand_path(path).first
  return File.read(file_path)
end

#root_directoryString

The root directory of the project

Returns:

  • (String)

    the root directory

Raises:



12
13
14
15
# File 'lib/bob_the_helper/file_system.rb', line 12

def root_directory
  #File.expand_path('../../../', __FILE__)
  raise Exceptions::InvalidUsageOfLibrary , "Sorry, but you need to define the root directory yourself"
end

#switch_to_working_directory(&block) ⇒ Object

Switch the current working directory to the temporary one and execute code block



43
44
45
# File 'lib/bob_the_helper/file_system.rb', line 43

def switch_to_working_directory(&block)
  Dir.chdir(working_directory, &block)
end

#working_directoryString

The temporary directory for the project

Returns:

  • (String)

    the directory created for the tests



21
22
23
# File 'lib/bob_the_helper/file_system.rb', line 21

def working_directory
  File.join(root_directory, 'tmp', 'test')
end