Skip to content

Commit

Permalink
Add file dir
Browse files Browse the repository at this point in the history
Add Faker::File.dir for generating directory paths.

Fix an imprecise test on Faker::File.file_name and update it to use the
new Faker::File.dir method.
  • Loading branch information
tylerhunt committed Sep 13, 2018
1 parent 2ae4fc6 commit 3b81499
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 7 additions & 1 deletion doc/file.md
Expand Up @@ -11,5 +11,11 @@ Faker::File.mime_type #=> "application/pdf"
Faker::File.file_name('path/to') #=> "path/to/something_random.jpg"
Faker::File.file_name('foo/bar', 'baz') #=> "foo/bar/baz.zip"
Faker::File.file_name('foo/bar', 'baz', 'doc') #=> "foo/bar/baz.doc"
Faker::File.file_name('foo/bar', 'baz', 'mp3', '\') #=> "foo\bar\baz.mp3"
Faker::File.file_name('foo/bar', 'baz', 'mp3', '\\') #=> "foo\bar\baz.mp3"

# Optional arguments: segment_count, root, directory_separator
Faker::File.dir #=> "path/to/something_random"
Faker::File.dir(2) #=> "foo/bar"
Faker::File.dir(3, '/') #=> "/foo/bar/baz"
Faker::File.dir(3, nil, '\\') #=> "foo\bar\baz"
```
11 changes: 10 additions & 1 deletion lib/faker/file.rb
Expand Up @@ -3,6 +3,15 @@
module Faker
class File < Base
class << self
def dir(segment_count = 3, root = nil, directory_separator = '/')
Array
.new(segment_count) { Faker::Internet.slug }
.unshift(root)
.compact
.join(directory_separator)
.squeeze(directory_separator)
end

def extension
fetch('file.extension')
end
Expand All @@ -12,7 +21,7 @@ def mime_type
end

def file_name(dir = nil, name = nil, ext = nil, directory_separator = '/')
dir ||= Faker::Internet.slug
dir ||= dir(1)
name ||= Faker::Lorem.word.downcase
ext ||= extension

Expand Down
14 changes: 13 additions & 1 deletion test/test_faker_file.rb
Expand Up @@ -16,6 +16,18 @@ def test_mime_type_format
end

def test_file_name
assert @tester.file_name.match(%r{([a-z\-_]+)(\\|\/)([a-z\-_]+)\.([a-z]+)})
assert @tester
.file_name
.match(%r{^([a-z\-_.]+)(\\|\/)([a-z\-_]+)\.([a-z]+)$})
end

def test_dir
assert @tester.dir.match(%r{^(([a-z\-_.]+)(\\|\/)){2}([a-z\-_.]+)$})
end

def test_dir_with_args
assert @tester
.dir(2, '\\root\\', '\\')
.match(%r{^\\root(\\([a-z\-_.]+)){2}$})
end
end

0 comments on commit 3b81499

Please sign in to comment.