-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added FilePath and Directory generators (#111)
Co-authored-by: Aaron R <33096067+adrubesh@users.noreply.github.co>
- Loading branch information
Showing
7 changed files
with
193 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package faker | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Directory is a faker struct for Directory | ||
type Directory struct { | ||
Faker *Faker | ||
OSResolver OSResolver | ||
} | ||
|
||
// Directory returns a fake directory path (the directory path style is dependent OS dependent) | ||
func (d Directory) Directory(levels int) string { | ||
switch d.OSResolver.OS() { | ||
case "windows": | ||
return d.WindowsDirectory(levels) | ||
default: | ||
return d.UnixDirectory(levels) | ||
} | ||
} | ||
|
||
// UnixDirectory returns a fake Unix directory path, regardless of the host OS | ||
func (d Directory) UnixDirectory(levels int) string { | ||
return "/" + strings.Join(d.Faker.Lorem().Words(levels), "/") | ||
} | ||
|
||
// WindowsDirectory returns a fake Windows directory path, regardless of the host OS | ||
func (d Directory) WindowsDirectory(levels int) string { | ||
return d.DriveLetter() + strings.Join(d.Faker.Lorem().Words(levels), "\\") | ||
} | ||
|
||
// DriveLetter returns a fake Win32 drive letter | ||
func (d Directory) DriveLetter() string { | ||
return d.Faker.RandomLetter() + ":\\" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package faker | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func isUnixPath(path string) bool { | ||
return path[0] == '/' | ||
} | ||
|
||
func isWindowsPath(path string) bool { | ||
return regexp.MustCompile(`^[a-zA-Z]:\\`).MatchString(path[:3]) | ||
} | ||
|
||
func TestDirectory(t *testing.T) { | ||
p := New().Directory() | ||
dir := p.Directory(2) | ||
Expect(t, true, isUnixPath(dir) || isWindowsPath(dir)) | ||
|
||
p.OSResolver = WindowsOSResolver{} | ||
Expect(t, true, isWindowsPath(p.Directory(2))) | ||
} | ||
|
||
func TestUnixDirectory(t *testing.T) { | ||
p := New().Directory() | ||
|
||
dir := p.UnixDirectory(2) | ||
parts := strings.Split(dir, "/") | ||
|
||
Expect(t, true, len(parts) == 3) | ||
Expect(t, true, isUnixPath(dir)) | ||
} | ||
|
||
func TestWindowsDirectory(t *testing.T) { | ||
p := New().Directory() | ||
|
||
dir := p.WindowsDirectory(2) | ||
parts := strings.Split(dir, "\\") | ||
|
||
Expect(t, true, len(parts) == 3) | ||
Expect(t, true, isWindowsPath(dir)) | ||
} | ||
|
||
func TestDriveLetter(t *testing.T) { | ||
p := New().Directory() | ||
Expect(t, true, isWindowsPath(p.DriveLetter())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters