diff options
author | Cyrill Schumacher <cyrill@schumacher.fm> | 2014-12-27 14:40:10 +1100 |
---|---|---|
committer | spf13 <steve.francia@gmail.com> | 2015-02-17 12:14:39 -0500 |
commit | 5e2d3d2e10a4cf1935c2226b023bde2f3ce9e7c0 (patch) | |
tree | 9e47a2aead8bbddeda8991b874afd628df005dbe /helpers | |
parent | 9bf223e584e115569bf2ffe64c3118e54d021a90 (diff) |
Merge to stay up to date with current dev
Feature: GetJson and GetJson in short codes or other layout files.
For more details please see: http://cyrillschumacher.com/2014/12/21/dynamic-pages-with-gohugo.io/
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/path.go | 25 | ||||
-rw-r--r-- | helpers/path_test.go | 28 |
2 files changed, 53 insertions, 0 deletions
diff --git a/helpers/path.go b/helpers/path.go index 9c3c2ba15..f6c77f9c3 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -437,3 +437,28 @@ func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) { _, err = io.Copy(file, r) return } + +// GetTempDir returns the OS default temp directory with trailing slash +// if subPath is not empty then it will be created recursively +func GetTempDir(subPath string, fs afero.Fs) string { + dir := os.TempDir() + if FilePathSeparator != dir[len(dir)-1:] { + dir = dir + FilePathSeparator + } + if subPath != "" { + dir = dir + MakePath(subPath) + + if exists, _ := Exists(dir, fs); exists { + return dir + } + + err := fs.MkdirAll(dir, 0777) // rwx, rw, r + if err != nil { + panic(err) + } + if FilePathSeparator != dir[len(dir)-1:] { + dir = dir + FilePathSeparator + } + } + return dir +} diff --git a/helpers/path_test.go b/helpers/path_test.go index bc0a54790..9be1c9fc4 100644 --- a/helpers/path_test.go +++ b/helpers/path_test.go @@ -647,3 +647,31 @@ func TestWriteToDisk(t *testing.T) { reader.Seek(0, 0) } } + +func TestGetTempDir(t *testing.T) { + dir := os.TempDir() + if FilePathSeparator != dir[len(dir)-1:] { + dir = dir + FilePathSeparator + } + testDir := "hugoTestFolder" + FilePathSeparator + tests := []struct { + input string + expected string + }{ + {"", dir}, + {testDir + " Foo bar ", dir + testDir + "--Foo-bar" + FilePathSeparator}, + {testDir + "Foo.Bar/foo_Bar-Foo", dir + testDir + "Foo.Bar/foo_Bar-Foo" + FilePathSeparator}, + {testDir + "fOO,bar:foo%bAR", dir + testDir + "fOObarfoobAR" + FilePathSeparator}, + {testDir + "FOo/BaR.html", dir + testDir + "FOo/BaR.html" + FilePathSeparator}, + {testDir + "трям/трям", dir + testDir + "трям/трям" + FilePathSeparator}, + {testDir + "은행", dir + testDir + "은행" + FilePathSeparator}, + {testDir + "Банковский кассир", dir + testDir + "Банковский-кассир" + FilePathSeparator}, + } + + for _, test := range tests { + output := GetTempDir(test.input, new(afero.MemMapFs)) + if output != test.expected { + t.Errorf("Expected %#v, got %#v\n", test.expected, output) + } + } +} |