Skip to content

Commit

Permalink
Added FilePath and Directory generators (#111)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron R <33096067+adrubesh@users.noreply.github.co>
  • Loading branch information
adrubesh and Aaron R authored Aug 7, 2022
1 parent f5976af commit 9d24d83
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 3 deletions.
36 changes: 36 additions & 0 deletions directory.go
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() + ":\\"
}
49 changes: 49 additions & 0 deletions directory_test.go
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()))
}
7 changes: 6 additions & 1 deletion faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 36 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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"}
)

// File is a faker struct for File
type File struct {
Faker *Faker
Faker *Faker
OSResolver OSResolver
}

// Extension returns a fake Extension file
Expand All @@ -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, "\\")
}
40 changes: 40 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package faker

import (
"os"
"strings"
"testing"
)
Expand All @@ -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)
}
14 changes: 14 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"net/http"
"os"
"runtime"
)

const (
Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package faker
import (
"net/http"
"os"
"runtime"
"strings"
"testing"
)
Expand Down Expand Up @@ -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())
}

0 comments on commit 9d24d83

Please sign in to comment.