From 9d24d83d1a8f4f162f7084ce82cd7d85aa846f61 Mon Sep 17 00:00:00 2001 From: Aaron Rubesh <33096067+adrubesh@users.noreply.github.com> Date: Sun, 7 Aug 2022 08:58:05 -0500 Subject: [PATCH] Added FilePath and Directory generators (#111) Co-authored-by: Aaron R <33096067+adrubesh@users.noreply.github.co> --- directory.go | 36 ++++++++++++++++++++++++++++++++++ directory_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ faker.go | 7 ++++++- file.go | 38 ++++++++++++++++++++++++++++++++++-- file_test.go | 40 ++++++++++++++++++++++++++++++++++++++ utils.go | 14 ++++++++++++++ utils_test.go | 12 ++++++++++++ 7 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 directory.go create mode 100644 directory_test.go diff --git a/directory.go b/directory.go new file mode 100644 index 0000000..9610750 --- /dev/null +++ b/directory.go @@ -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() + ":\\" +} diff --git a/directory_test.go b/directory_test.go new file mode 100644 index 0000000..1e40f10 --- /dev/null +++ b/directory_test.go @@ -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())) +} diff --git a/faker.go b/faker.go index 7a9d1d0..dc97f98 100644 --- a/faker.go +++ b/faker.go @@ -433,7 +433,12 @@ func (f Faker) Image() Image { // File returns a fake File instance for Faker func (f Faker) File() File { - return File{&f} + return File{&f, OSResolverImpl{}} +} + +// Directory returns a fake Directory instance for Faker +func (f Faker) Directory() Directory { + return Directory{&f, OSResolverImpl{}} } // YouTube returns a fake YouTube instance for Faker diff --git a/file.go b/file.go index e17cab7..bdd420f 100644 --- a/file.go +++ b/file.go @@ -1,6 +1,9 @@ package faker -import "fmt" +import ( + "fmt" + "strings" +) var ( extensions = []string{"ods", "xls", "xlsx", "csv", "ics", "vcf", "3dm", "3ds", "max", "bmp", "dds", "gif", "jpg", "jpeg", "png", "psd", "xcf", "tga", "thm", "tif", "tiff", "yuv", "ai", "eps", "ps", "svg", "dwg", "dxf", "gpx", "kml", "kmz", "webp", "3g2", "3gp", "aaf", "asf", "avchd", "avi", "drc", "flv", "m2v", "m4p", "m4v", "mkv", "mng", "mov", "mp2", "mp4", "mpe", "mpeg", "mpg", "mpv", "mxf", "nsv", "ogg", "ogv", "ogm", "qt", "rm", "rmvb", "roq", "srt", "svi", "vob", "webm", "wmv", "yuv", "aac", "aiff", "ape", "au", "flac", "gsm", "it", "m3u", "m4a", "mid", "mod", "mp3", "mpa", "pls", "ra", "s3m", "sid", "wav", "wma", "xm", "7z", "a", "apk", "ar", "bz2", "cab", "cpio", "deb", "dmg", "egg", "gz", "iso", "jar", "lha", "mar", "pea", "rar", "rpm", "s7z", "shar", "tar", "tbz2", "tgz", "tlz", "war", "whl", "xpi", "zip", "zipx", "xz", "pak", "exe", "msi", "bin", "command", "sh", "bat", "crx", "c", "cc", "class", "clj", "cpp", "cs", "cxx", "el", "go", "h", "java", "lua", "m", "m4", "php", "pl", "po", "py", "rb", "rs", "sh", "swift", "vb", "vcxproj", "xcodeproj", "xml", "diff", "patch", "html", "js", "html", "htm", "css", "js", "jsx", "less", "scss", "wasm", "php", "eot", "otf", "ttf", "woff", "woff2", "ppt", "odp", "doc", "docx", "ebook", "log", "md", "msg", "odt", "org", "pages", "pdf", "rtf", "rst", "tex", "txt", "wpd", "wps", "mobi", "epub", "azw1", "azw3", "azw4", "azw6", "azw", "cbr", "cbz"} @@ -8,7 +11,8 @@ var ( // File is a faker struct for File type File struct { - Faker *Faker + Faker *Faker + OSResolver OSResolver } // Extension returns a fake Extension file @@ -23,3 +27,33 @@ func (f File) FilenameWithExtension() string { return fmt.Sprintf("%s.%s", text, extension) } + +// AbsoluteFilePath returns a fake absolute path to a fake file (style is dependent on OS) +func (f File) AbsoluteFilePath(levels int) string { + switch f.OSResolver.OS() { + case "windows": + return f.AbsoluteFilePathForWindows(levels) + default: + return f.AbsoluteFilePathForUnix(levels) + } +} + +// AbsoluteFilePathForUnix returns a fake absolute unix-style path to a fake file +func (f File) AbsoluteFilePathForUnix(levels int) string { + path := []string{ + f.Faker.Directory().UnixDirectory(levels), + f.FilenameWithExtension(), + } + + return strings.Join(path, "/") +} + +// AbsoluteFilePathForWindows returns a fake absolute win32-style path to a fake file +func (f File) AbsoluteFilePathForWindows(levels int) string { + path := []string{ + f.Faker.Directory().WindowsDirectory(levels), + f.FilenameWithExtension(), + } + + return strings.Join(path, "\\") +} diff --git a/file_test.go b/file_test.go index 43daf44..02daf67 100644 --- a/file_test.go +++ b/file_test.go @@ -1,6 +1,7 @@ package faker import ( + "os" "strings" "testing" ) @@ -14,3 +15,42 @@ func TestFileWithExtension(t *testing.T) { p := New().File() Expect(t, true, len(strings.Split(p.FilenameWithExtension(), ".")) == 2) } + +func TestAbsolutePath(t *testing.T) { + p := New().File() + + path := p.AbsoluteFilePath(2) + parts := strings.Split(path, string(os.PathSeparator)) + Expect(t, true, isUnixPath(path) || isWindowsPath(path)) + Expect(t, true, len(parts) == 4) + Expect(t, true, len(strings.Split(parts[len(parts)-1], ".")) == 2) + + p.OSResolver = WindowsOSResolver{} + path = p.AbsoluteFilePath(2) + parts = strings.Split(path, "\\") + Expect(t, true, isWindowsPath(p.AbsoluteFilePath(2))) + Expect(t, true, len(parts) == 4) + Expect(t, true, len(strings.Split(parts[len(parts)-1], ".")) == 2) +} + +func TestAbsoluteFilePathForUnix(t *testing.T) { + p := New().File() + + path := p.AbsoluteFilePathForUnix(2) + parts := strings.Split(path, "/") + + Expect(t, true, isUnixPath(path)) + Expect(t, true, len(parts) == 4) + Expect(t, true, len(strings.Split(parts[len(parts)-1], ".")) == 2) +} + +func TestAbsoluteFilePathForWindows(t *testing.T) { + p := New().File() + + path := p.AbsoluteFilePathForWindows(2) + parts := strings.Split(path, "\\") + + Expect(t, true, isWindowsPath(path)) + Expect(t, true, len(parts) == 4) + Expect(t, true, len(strings.Split(parts[len(parts)-1], ".")) == 2) +} diff --git a/utils.go b/utils.go index 9d6a0eb..aec3f93 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "net/http" "os" + "runtime" ) const ( @@ -42,3 +43,16 @@ type TempFileCreatorImpl struct{} func (TempFileCreatorImpl) TempFile(prefix string) (f *os.File, err error) { return ioutil.TempFile(os.TempDir(), prefix) } + +// OSResolver returns the GOOS value for operating an operating system +type OSResolver interface { + OS() string +} + +// OSResolverImpl is the default implementation of OSResolver +type OSResolverImpl struct{} + +// OS returns the runtime.GOOS value for the host operating system +func (OSResolverImpl) OS() string { + return runtime.GOOS +} diff --git a/utils_test.go b/utils_test.go index 93701d5..e8795e5 100644 --- a/utils_test.go +++ b/utils_test.go @@ -3,6 +3,7 @@ package faker import ( "net/http" "os" + "runtime" "strings" "testing" ) @@ -43,3 +44,14 @@ func TestTempFileCreatorImplCanCreateTempFiles(t *testing.T) { Expect(t, true, strings.Contains(f.Name(), "prefix")) Expect(t, f.Close(), nil) } + +type WindowsOSResolver struct{} + +func (WindowsOSResolver) OS() string { + return "windows" +} + +func TestOSResolverImplReturnsGOOS(t *testing.T) { + resolver := OSResolverImpl{} + Expect(t, runtime.GOOS, resolver.OS()) +}