mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-23 08:23:35 +00:00
WriteToDisk and SafeWriteToDisk test cleaned up
Minor cleanup to randomise the names of the temp directories used by the tests.
This commit is contained in:
parent
bf07dc9293
commit
980d0f14de
1 changed files with 9 additions and 21 deletions
|
@ -4,8 +4,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMakePath(t *testing.T) {
|
func TestMakePath(t *testing.T) {
|
||||||
|
@ -461,20 +463,22 @@ func TestSafeWriteToDisk(t *testing.T) {
|
||||||
defer deleteFileInTempDir(emptyFile)
|
defer deleteFileInTempDir(emptyFile)
|
||||||
tmpDir, _ := createEmptyTempDir()
|
tmpDir, _ := createEmptyTempDir()
|
||||||
defer deleteTempDir(tmpDir)
|
defer deleteTempDir(tmpDir)
|
||||||
os.MkdirAll(tmpDir+"/this/dir/does/not/exist/", 0644)
|
|
||||||
|
|
||||||
randomString := "This is a random string!"
|
randomString := "This is a random string!"
|
||||||
reader := strings.NewReader(randomString)
|
reader := strings.NewReader(randomString)
|
||||||
|
|
||||||
fileExists := fmt.Errorf("%v already exists", emptyFile.Name())
|
fileExists := fmt.Errorf("%v already exists", emptyFile.Name())
|
||||||
|
|
||||||
type test struct {
|
type test struct {
|
||||||
filename string
|
filename string
|
||||||
expectedErr error
|
expectedErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
now := time.Now().Unix()
|
||||||
|
nowStr := strconv.FormatInt(now, 10)
|
||||||
data := []test{
|
data := []test{
|
||||||
{emptyFile.Name(), fileExists},
|
{emptyFile.Name(), fileExists},
|
||||||
{tmpDir + "/" + emptyFile.Name(), nil},
|
{tmpDir + "/" + nowStr, nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, d := range data {
|
for i, d := range data {
|
||||||
|
@ -510,35 +514,19 @@ func TestWriteToDisk(t *testing.T) {
|
||||||
expectedErr error
|
expectedErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
now := time.Now().Unix()
|
||||||
|
nowStr := strconv.FormatInt(now, 10)
|
||||||
data := []test{
|
data := []test{
|
||||||
{emptyFile.Name(), nil},
|
{emptyFile.Name(), nil},
|
||||||
{tmpDir + "/abcd", nil},
|
{tmpDir + "/" + nowStr, nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, d := range data {
|
for i, d := range data {
|
||||||
// fmt.Printf("Writing to: %s\n", d.filename)
|
|
||||||
// dir, _ := filepath.Split(d.filename)
|
|
||||||
// ospath := filepath.FromSlash(dir)
|
|
||||||
|
|
||||||
// fmt.Printf("dir: %q, ospath: %q\n", dir, ospath)
|
|
||||||
e := WriteToDisk(d.filename, reader)
|
e := WriteToDisk(d.filename, reader)
|
||||||
// fmt.Printf("Error from WriteToDisk: %s\n", e)
|
|
||||||
// f, e := os.Open(d.filename)
|
|
||||||
// if e != nil {
|
|
||||||
// fmt.Errorf("could not open file %s, error %s\n", d.filename, e)
|
|
||||||
// }
|
|
||||||
// fi, e := f.Stat()
|
|
||||||
// if e != nil {
|
|
||||||
// fmt.Errorf("Could not stat file %s, error %s\n", d.filename, e)
|
|
||||||
// }
|
|
||||||
// fmt.Printf("file size of %s is %d bytes\n", d.filename, fi.Size())
|
|
||||||
if d.expectedErr != e {
|
if d.expectedErr != e {
|
||||||
t.Errorf("Test %d failed. WriteToDisk Error Expected %q but got %q", i, d.expectedErr, e)
|
t.Errorf("Test %d failed. WriteToDisk Error Expected %q but got %q", i, d.expectedErr, e)
|
||||||
}
|
}
|
||||||
// fmt.Printf("Reading from %s\n", d.filename)
|
|
||||||
contents, e := ioutil.ReadFile(d.filename)
|
contents, e := ioutil.ReadFile(d.filename)
|
||||||
// fmt.Printf("Error from ReadFile: %s\n", e)
|
|
||||||
// fmt.Printf("Contents: %#v, %s\n", contents, string(contents))
|
|
||||||
if e != nil {
|
if e != nil {
|
||||||
t.Error("Test %d failed. Could not read file %s. Reason: %s\n", i, d.filename, e)
|
t.Error("Test %d failed. Could not read file %s. Reason: %s\n", i, d.filename, e)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue