Module: Aruba::Api::Filesystem

Included in:
Aruba::Api
Defined in:
lib/aruba/api/filesystem.rb

Overview

Filesystem methods

Instance Method Summary collapse

Instance Method Details

#absolute?(path) ⇒ Boolean

Check if path is absolute

Returns:

  • (Boolean)

56
57
58
# File 'lib/aruba/api/filesystem.rb', line 56

def absolute?(path)
  ArubaPath.new(path).absolute?
end

#all_directoriesArray

Return all existing directories in current directory

Returns:

  • (Array)

    List of files


87
88
89
# File 'lib/aruba/api/filesystem.rb', line 87

def all_directories
  list('.').select { |p| directory? p }.map { |p| expand_path(p) }
end

#all_filesArray

Return all existing files in current directory

Returns:

  • (Array)

    List of files


79
80
81
# File 'lib/aruba/api/filesystem.rb', line 79

def all_files
  list('.').select { |p| file? p }.map { |p| expand_path(p) }
end

#all_pathsArray

Return all existing paths (directories, files) in current dir

Returns:

  • (Array)

    List of files and directories


71
72
73
# File 'lib/aruba/api/filesystem.rb', line 71

def all_paths
  list('.').map { |path| expand_path(path) }
end

#append_to_file(file_name, file_content) ⇒ Object

Append data to file

Parameters:

  • file_name (String)

    The name of the file to be used

  • file_content (String)

    The content which should be appended to file


315
316
317
318
319
320
# File 'lib/aruba/api/filesystem.rb', line 315

def append_to_file(file_name, file_content)
  file_name = expand_path(file_name)

  Aruba.platform.mkdir(File.dirname(file_name))
  File.open(file_name, 'a') { |f| f << file_content }
end

#chmod(*args) ⇒ Object

Change file system permissions of file

Parameters:

  • mode (Octal)

    File system mode, eg. 0755

  • file_name (String)

    Name of file to be modified. This file needs to be present to succeed


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/aruba/api/filesystem.rb', line 284

def chmod(*args)
  args = args.flatten

  options = if args.last.kind_of? Hash
              args.pop
            else
              {}
            end

  mode = args.shift
  mode = if mode.kind_of? String
           mode.to_i(8)
         else
           mode
         end

  args.each { |path| raise "Expected #{path} to be present" unless exist?(path) }
  paths = args.map { |path| expand_path(path) }

  Aruba.platform.chmod(mode, paths, options)

  self
end

#copy(*args) ⇒ Object

Copy a file and/or directory

rubocop:disable Metrics/CyclomaticComplexity def copy(*source, destination)

Parameters:

  • source (String, Array)

    A single file or directory, multiple files or directories or multiple files and directories. If multiple sources are given the destination needs to be a directory

  • destination (String)

    A file or directory name. If multiple sources are given the destination needs to be a directory

Raises:

  • (ArgumentError)

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/aruba/api/filesystem.rb', line 175

def copy(*args)
  args = args.flatten
  destination = args.pop
  source = args

  source.each do |s|
    raise ArgumentError, %(The following source "#{s}" does not exist.) unless exist? s
  end

  raise ArgumentError, "Using a fixture as destination (#{destination}) is not supported" if destination.start_with? aruba.config.fixtures_path_prefix
  raise ArgumentError, "Multiples sources can only be copied to a directory" if source.count > 1 && exist?(destination) && !directory?(destination)

  source_paths     = source.map { |f| expand_path(f) }
  destination_path = expand_path(destination)

  if source_paths.count > 1
    Aruba.platform.mkdir(destination_path)
  else
    Aruba.platform.mkdir(File.dirname(destination_path))
    source_paths = source_paths.first
  end

  Aruba.platform.cp source_paths, destination_path

  self
end

#create_directory(directory_name) ⇒ Object

Create a directory in current directory

Parameters:

  • directory_name (String)

    The name of the directory which should be created


326
327
328
329
330
# File 'lib/aruba/api/filesystem.rb', line 326

def create_directory(directory_name)
  Aruba.platform.mkdir expand_path(directory_name)

  self
end

#directory(path) ⇒ Dir

Create directory object

Returns:

  • (Dir)

    The directory object


95
96
97
98
99
# File 'lib/aruba/api/filesystem.rb', line 95

def directory(path)
  fail ArgumentError, %(Path "#{name}" does not exist.) unless exist? name

  Dir.new(expand_path(path))
end

#directory?(file) ⇒ Boolean

Check if directory exist and is directory

Parameters:

  • file (String)

    The file/directory which should exist

Returns:

  • (Boolean)

37
38
39
# File 'lib/aruba/api/filesystem.rb', line 37

def directory?(file)
  Aruba.platform.directory? expand_path(file)
end

#disk_usage(*paths) ⇒ FileSize

Calculate disk usage for file(s) and/or directories

It shows the disk usage for a single file/directory. If multiple paths are given, it sum their size up.

Parameters:

  • paths (Array, Path)

    The paths

Returns:


375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/aruba/api/filesystem.rb', line 375

def disk_usage(*paths)
  expect(paths.flatten).to Aruba::Matchers.all be_an_existing_path
  expanded = paths.flatten.map { |path| ArubaPath.new(expand_path(path)) }

  # TODO: change the API so that you could set something like
  # aruba.config.fs_allocation_unit_size directly

  typical_fs_unit = 4096 # very typical, except for giant or embedded filesystems
  typical_dev_bsize = 512 # google dev_bsize for more info

  block_multiplier = typical_fs_unit / typical_dev_bsize
  fs_unit_size = aruba.config.physical_block_size * block_multiplier

  # TODO: the size argument here is unnecessary - ArubaPath should decide
  # what the disk usage of a file is (even if Aruba.config needs to be
  # read)
  deprecated_block_count = fs_unit_size / block_multiplier
  Aruba.platform.determine_disk_usage(expanded, deprecated_block_count)
end

#executable?(path) ⇒ Boolean

Check if file exist and is executable

Parameters:

  • path (String)

    The path which should exist and be executable

Returns:

  • (Boolean)

47
48
49
50
51
# File 'lib/aruba/api/filesystem.rb', line 47

def executable?(path)
  path = expand_path(path)

  Aruba.platform.file?(path) && Aruba.platform.executable?(path)
end

#exist?(file_or_directory) ⇒ Boolean

Check if file or directory exist

Parameters:

  • file_or_directory (String)

    The file/directory which should exist

Returns:

  • (Boolean)

21
22
23
# File 'lib/aruba/api/filesystem.rb', line 21

def exist?(file_or_directory)
  Aruba.platform.exist? expand_path(file_or_directory)
end

#file?(file) ⇒ Boolean

Check if file exist and is file

Parameters:

  • file (String)

    The file/directory which should exist

Returns:

  • (Boolean)

29
30
31
# File 'lib/aruba/api/filesystem.rb', line 29

def file?(file)
  Aruba.platform.file? expand_path(file)
end

#file_size(name) ⇒ Numeric

Get size of file

Parameters:

Returns:

  • (Numeric)

    The size of the file


402
403
404
405
406
# File 'lib/aruba/api/filesystem.rb', line 402

def file_size(name)
  expect(name).to be_an_existing_file

  Aruba.platform.determine_file_size expand_path(name)
end

#list(name) ⇒ Array

Return content of directory

Returns:

  • (Array)

    The content of directory


105
106
107
108
109
110
111
112
113
# File 'lib/aruba/api/filesystem.rb', line 105

def list(name)
  fail ArgumentError, %(Path "#{name}" does not exist.) unless exist? name
  fail ArgumentError, %(Only directories are supported. Path "#{name}" is not a directory.) unless directory? name

  existing_files            = Dir.glob(expand_path(File.join(name, '**', '*')))
  current_working_directory = ArubaPath.new(expand_path('.'))

  existing_files.map { |d| ArubaPath.new(d).relative_path_from(current_working_directory).to_s }
end

#move(*args) ⇒ Object

Move a file and/or directory

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength

Parameters:

  • source (String, Array)

    A single file or directory, multiple files or directories or multiple files and directories. If multiple sources are given the destination needs to be a directory

  • destination (String)

    A file or directory name. If multiple sources are given the destination needs to be a directory

Raises:

  • (ArgumentError)

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/aruba/api/filesystem.rb', line 216

def move(*args)
  args = args.flatten
  destination = args.pop
  source = args

  source.each do |s|
    raise ArgumentError, "Using a fixture as source (#{source}) is not supported" if s.start_with? aruba.config.fixtures_path_prefix
  end

  raise ArgumentError, "Using a fixture as destination (#{destination}) is not supported" if destination.start_with? aruba.config.fixtures_path_prefix

  source.each do |s|
    raise ArgumentError, %(The following source "#{s}" does not exist.) unless exist? s
  end

  raise ArgumentError, "Multiple sources can only be copied to a directory" if source.count > 1 && exist?(destination) && !directory?(destination)

  source_paths     = source.map { |f| expand_path(f) }
  destination_path = expand_path(destination)

  if source_paths.count > 1
    Aruba.platform.mkdir(destination_path)
  else
    Aruba.platform.mkdir(File.dirname(destination_path))
    source_paths = source_paths.first
  end

  Aruba.platform.mv source_paths, destination_path

  self
end

#overwrite_file(name, content) ⇒ Object

Create a file with given content

The method does check if file already exists and fails if the file is missing. If the file name is a path the method will create all neccessary directories.


271
272
273
274
275
# File 'lib/aruba/api/filesystem.rb', line 271

def overwrite_file(name, content)
  Aruba.platform.create_file(expand_path(name), content, true)

  self
end

#read(name) ⇒ Array

Return content of file

Returns:

  • (Array)

    The content of file, without “n” or “rn” at the end. To rebuild the file use `content.join(“n”)`


119
120
121
122
123
124
# File 'lib/aruba/api/filesystem.rb', line 119

def read(name)
  fail ArgumentError, %(Path "#{name}" does not exist.) unless exist? name
  fail ArgumentError, %(Only files are supported. Path "#{name}" is not a file.) unless file? name

  File.readlines(expand_path(name)).map(&:chomp)
end

#relative?(path) ⇒ Boolean

Check if path is relative

Returns:

  • (Boolean)

63
64
65
# File 'lib/aruba/api/filesystem.rb', line 63

def relative?(path)
  ArubaPath.new(path).relative?
end

#remove(*args) ⇒ Object

Remove file or directory

Parameters:

  • name (Array, String)

    The name of the file / directory which should be removed


336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/aruba/api/filesystem.rb', line 336

def remove(*args)
  args = args.flatten

  options = if args.last.kind_of? Hash
              args.pop
            else
              {}
            end

  args = args.map { |path| expand_path(path) }

  Aruba.platform.rm(args, options)
end

#touch(*args) ⇒ Object

Create an empty file

Parameters:

  • file_name (String)

    The name of the file


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/aruba/api/filesystem.rb', line 146

def touch(*args)
  args = args.flatten

  options = if args.last.kind_of? Hash
              args.pop
            else
              {}
            end

  args.each { |p| create_directory(File.dirname(p)) }

  Aruba.platform.touch(args.map { |p| expand_path(p) }, options)

  self
end

#with_file_content(file) { ... } ⇒ Object

Read content of file and yield the content to block

Parameters:

  • file (String)

    The name of file which should be read from

Yields:

  • Pass the content of the given file to this block


357
358
359
360
361
362
363
# File 'lib/aruba/api/filesystem.rb', line 357

def with_file_content(file)
  expect(file).to be_an_existing_path

  content = read(file).join("\n")

  yield(content)
end

#write_file(name, content) ⇒ Object

Create a file with given content

The method does not check if file already exists. If the file name is a path the method will create all neccessary directories.

Parameters:

  • name (String)

    The name of the file

  • content (String)

    The content which should be written to the file


136
137
138
139
140
# File 'lib/aruba/api/filesystem.rb', line 136

def write_file(name, content)
  Aruba.platform.create_file(expand_path(name), content, false)

  self
end

#write_fixed_size_file(name, size) ⇒ Object

Create a file with the given size

The method does not check if file already exists. If the file name is a path the method will create all neccessary directories.

Parameters:

  • name (String)

    The name of the file

  • size (Integer)

    The size of the file


260
261
262
263
264
# File 'lib/aruba/api/filesystem.rb', line 260

def write_fixed_size_file(name, size)
  Aruba.platform.create_fixed_size_file(expand_path(name), size, false)

  self
end