Module: Aruba::Api::Filesystem
- Included in:
- Aruba::Api
- Defined in:
- lib/aruba/api/filesystem.rb
Overview
Filesystem methods
Instance Method Summary collapse
-
#absolute?(path) ⇒ Boolean
Check if path is absolute.
-
#all_directories ⇒ Array
Return all existing directories in current directory.
-
#all_files ⇒ Array
Return all existing files in current directory.
-
#all_paths ⇒ Array
Return all existing paths (directories, files) in current dir.
-
#append_to_file(file_name, file_content) ⇒ Object
Append data to file.
-
#chmod(*args) ⇒ Object
Change file system permissions of file.
-
#copy(*args) ⇒ Object
Copy a file and/or directory.
-
#create_directory(directory_name) ⇒ Object
Create a directory in current directory.
-
#directory(path) ⇒ Dir
Create directory object.
-
#directory?(file) ⇒ Boolean
Check if directory exist and is directory.
-
#disk_usage(*paths) ⇒ FileSize
Calculate disk usage for file(s) and/or directories.
-
#executable?(path) ⇒ Boolean
Check if file exist and is executable.
-
#exist?(file_or_directory) ⇒ Boolean
Check if file or directory exist.
-
#file?(file) ⇒ Boolean
Check if file exist and is file.
-
#file_size(name) ⇒ Numeric
Get size of file.
-
#list(name) ⇒ Array
Return content of directory.
-
#move(*args) ⇒ Object
Move a file and/or directory.
-
#overwrite_file(name, content) ⇒ Object
Create a file with given content.
-
#read(name) ⇒ Array
Return content of file.
-
#relative?(path) ⇒ Boolean
Check if path is relative.
-
#remove(*args) ⇒ Object
Remove file or directory.
-
#touch(*args) ⇒ Object
Create an empty file.
-
#with_file_content(file) { ... } ⇒ Object
Read content of file and yield the content to block.
-
#write_file(name, content) ⇒ Object
Create a file with given content.
-
#write_fixed_size_file(name, size) ⇒ Object
Create a file with the given size.
Instance Method Details
#absolute?(path) ⇒ Boolean
Check if path is absolute
55 56 57 |
# File 'lib/aruba/api/filesystem.rb', line 55 def absolute?(path) Aruba.platform.absolute_path?(path) end |
#all_directories ⇒ Array
Return all existing directories in current directory
86 87 88 |
# File 'lib/aruba/api/filesystem.rb', line 86 def all_directories list('.').select { |p| directory? p }.map { |p| (p) } end |
#all_files ⇒ Array
Return all existing files in current directory
78 79 80 |
# File 'lib/aruba/api/filesystem.rb', line 78 def all_files list('.').select { |p| file? p }.map { |p| (p) } end |
#all_paths ⇒ Array
Return all existing paths (directories, files) in current dir
70 71 72 |
# File 'lib/aruba/api/filesystem.rb', line 70 def all_paths list('.').map { |path| (path) } end |
#append_to_file(file_name, file_content) ⇒ Object
Append data to file
327 328 329 330 331 332 |
# File 'lib/aruba/api/filesystem.rb', line 327 def append_to_file(file_name, file_content) file_name = (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
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/aruba/api/filesystem.rb', line 296 def chmod(*args) args = args.flatten = if args.last.is_a? Hash args.pop else {} end mode = args.shift mode = if mode.is_a? String mode.to_i(8) else mode end args.each { |path| raise "Expected #{path} to be present" unless exist?(path) } paths = args.map { |path| (path) } Aruba.platform.chmod(mode, paths, ) self end |
#copy(*args) ⇒ Object
Copy a file and/or directory
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/aruba/api/filesystem.rb', line 180 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 if destination.start_with? aruba.config.fixtures_path_prefix raise ArgumentError, "Using a fixture as destination (#{destination}) is not supported" end if source.count > 1 && exist?(destination) && !directory?(destination) raise ArgumentError, 'Multiples sources can only be copied to a directory' end source_paths = source.map { |f| (f) } destination_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
338 339 340 341 342 |
# File 'lib/aruba/api/filesystem.rb', line 338 def create_directory(directory_name) Aruba.platform.mkdir (directory_name) self end |
#directory(path) ⇒ Dir
Create directory object
94 95 96 97 98 |
# File 'lib/aruba/api/filesystem.rb', line 94 def directory(path) raise ArgumentError, %(Path "#{name}" does not exist.) unless exist? name Dir.new((path)) end |
#directory?(file) ⇒ Boolean
Check if directory exist and is directory
36 37 38 |
# File 'lib/aruba/api/filesystem.rb', line 36 def directory?(file) Aruba.platform.directory? (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.
387 388 389 390 391 392 393 |
# File 'lib/aruba/api/filesystem.rb', line 387 def disk_usage(*paths) paths = paths.flatten expect(paths).to Aruba::Matchers.all be_an_existing_path = paths.map { |path| (path) } Aruba.platform.determine_disk_usage() end |
#executable?(path) ⇒ Boolean
Check if file exist and is executable
46 47 48 49 50 |
# File 'lib/aruba/api/filesystem.rb', line 46 def executable?(path) path = (path) Aruba.platform.file?(path) && Aruba.platform.executable?(path) end |
#exist?(file_or_directory) ⇒ Boolean
Check if file or directory exist
20 21 22 |
# File 'lib/aruba/api/filesystem.rb', line 20 def exist?(file_or_directory) Aruba.platform.exist? (file_or_directory) end |
#file?(file) ⇒ Boolean
Check if file exist and is file
28 29 30 |
# File 'lib/aruba/api/filesystem.rb', line 28 def file?(file) Aruba.platform.file? (file) end |
#file_size(name) ⇒ Numeric
Get size of 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 (name) end |
#list(name) ⇒ Array
Return content of directory
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/aruba/api/filesystem.rb', line 104 def list(name) raise ArgumentError, %(Path "#{name}" does not exist.) unless exist? name unless directory? name raise ArgumentError, %(Only directories are supported. Path "#{name}" is not a directory.) end existing_files = Dir.glob((File.join(name, '**', '*'))) current_working_directory = Pathname.new(('.')) existing_files.map do |d| Pathname.new(d).relative_path_from(current_working_directory).to_s end end |
#move(*args) ⇒ Object
Move a file and/or directory
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/aruba/api/filesystem.rb', line 223 def move(*args) args = args.flatten destination = args.pop source = args source.each do |s| if s.start_with? aruba.config.fixtures_path_prefix raise ArgumentError, "Using a fixture as source (#{source}) is not supported" end end if destination.start_with? aruba.config.fixtures_path_prefix raise ArgumentError, "Using a fixture as destination (#{destination}) is not supported" end source.each do |s| raise ArgumentError, %(The following source "#{s}" does not exist.) unless exist? s end if source.count > 1 && exist?(destination) && !directory?(destination) raise ArgumentError, 'Multiple sources can only be copied to a directory' end source_paths = source.map { |f| (f) } destination_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.
283 284 285 286 287 |
# File 'lib/aruba/api/filesystem.rb', line 283 def overwrite_file(name, content) Aruba.platform.create_file((name), content, true) self end |
#read(name) ⇒ Array
Return content of file
125 126 127 128 129 130 131 132 |
# File 'lib/aruba/api/filesystem.rb', line 125 def read(name) raise ArgumentError, %(Path "#{name}" does not exist.) unless exist? name unless file? name raise ArgumentError, %(Only files are supported. Path "#{name}" is not a file.) end File.readlines((name)).map(&:chomp) end |
#relative?(path) ⇒ Boolean
Check if path is relative
62 63 64 |
# File 'lib/aruba/api/filesystem.rb', line 62 def relative?(path) Aruba.platform.relative_path?(path) end |
#remove(*args) ⇒ Object
Remove file or directory
348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/aruba/api/filesystem.rb', line 348 def remove(*args) args = args.flatten = if args.last.is_a? Hash args.pop else {} end args = args.map { |path| (path) } Aruba.platform.rm(args, ) end |
#touch(*args) ⇒ Object
Create an empty file
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/aruba/api/filesystem.rb', line 154 def touch(*args) args = args.flatten = if args.last.is_a? Hash args.pop else {} end args.each { |p| create_directory(File.dirname(p)) } Aruba.platform.touch(args.map { |p| (p) }, ) self end |
#with_file_content(file) { ... } ⇒ Object
Read content of file and yield the content to block
369 370 371 372 373 374 375 |
# File 'lib/aruba/api/filesystem.rb', line 369 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.
144 145 146 147 148 |
# File 'lib/aruba/api/filesystem.rb', line 144 def write_file(name, content) Aruba.platform.create_file((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.
272 273 274 275 276 |
# File 'lib/aruba/api/filesystem.rb', line 272 def write_fixed_size_file(name, size) Aruba.platform.create_fixed_size_file((name), size, false) self end |