Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Faker::File.dir #1361

Merged
merged 2 commits into from Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion doc/default/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"
```
8 changes: 7 additions & 1 deletion doc/unreleased/default/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/default/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/faker/default/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