From 0b279575e876d2bf0948e7854d57140aa14f5c64 Mon Sep 17 00:00:00 2001 From: Alexandre Tuleu Date: Wed, 10 Feb 2016 14:17:04 +0100 Subject: [PATCH] Moves testdata in test/data --- data_test.go | 29 +++++++++++++++++++++++++++-- {testdata => test/data}/.gitignore | 0 {testdata => test/data}/albums.json | 0 3 files changed, 27 insertions(+), 2 deletions(-) rename {testdata => test/data}/.gitignore (100%) rename {testdata => test/data}/albums.json (100%) diff --git a/data_test.go b/data_test.go index ed7c250..9ecadb7 100644 --- a/data_test.go +++ b/data_test.go @@ -85,7 +85,7 @@ func GetCsvData() *bytes.Buffer { func init() { start := time.Now() - albumsPath := filepath.Join("testdata", "albums.json") + albumsPath := filepath.Join("test", "data", "albums.json") f, err := os.Open(albumsPath) if err != nil { panic(fmt.Sprintf("Could not open '%s': %s", albumsPath, err)) @@ -100,9 +100,34 @@ func init() { log.Printf("loaded test data in %s", time.Since(start)) start = time.Now() dv := diskv.New(diskv.Options{ - BasePath: filepath.Join("testdata", "web-cache"), + BasePath: filepath.Join("test", "data", "web-cache"), CacheSizeMax: 100 * 1024 * 1024, // 100MB }) testGetter = httpcache.NewTransport(diskcache.NewWithDiskv(dv)).Client() log.Printf("loaded httpcache in %s", time.Since(start)) + + jsonInfo, err := os.Stat(albumsPath) + if err != nil { + panic(fmt.Sprintf("Could not stat %s: %s", albumsPath, err)) + } + csvPath := filepath.Join("test", "data", "albums.csv") + csvInfo, err := os.Stat(csvPath) + if err != nil && os.IsNotExist(err) == false { + panic(fmt.Sprintf("Could not stat %s: %s", csvPath, err)) + } + + if err == nil && csvInfo.ModTime().After(jsonInfo.ModTime()) == true { + // no need to update the file + return + } + newFile, err := os.Create(csvPath) + if err != nil { + panic(fmt.Sprintf("Could not create %s: %s", csvPath, err)) + } + defer closeOrPanic(newFile, csvPath) + _, err = io.Copy(newFile, GetCsvData()) + if err != nil { + panic(fmt.Sprintf("Could not save csv data: %s", err)) + } + } diff --git a/testdata/.gitignore b/test/data/.gitignore similarity index 100% rename from testdata/.gitignore rename to test/data/.gitignore diff --git a/testdata/albums.json b/test/data/albums.json similarity index 100% rename from testdata/albums.json rename to test/data/albums.json