From ca8584a0e1c4e4adaad5362aee1e1b3ca2b789cb Mon Sep 17 00:00:00 2001 From: Dustin Oprea Date: Wed, 3 Jun 2020 22:06:15 -0400 Subject: [PATCH] Minor sensible testing refactors --- v2/common/testing_common.go | 90 ++++++++++++++++++------------------- v2/ifd_enumerate_test.go | 3 +- v2/testing_common.go | 41 +++++------------ 3 files changed, 57 insertions(+), 77 deletions(-) diff --git a/v2/common/testing_common.go b/v2/common/testing_common.go index dfbdf44..ed1da0a 100644 --- a/v2/common/testing_common.go +++ b/v2/common/testing_common.go @@ -11,10 +11,9 @@ import ( ) var ( - assetsPath = "" - testImageFilepath = "" - testExifData = make([]byte, 0) - moduleRootPath = "" + moduleRootPath = "" + + testExifData []byte = nil // EncodeDefaultByteOrder is the default byte-order for encoding operations. EncodeDefaultByteOrder = binary.BigEndian @@ -24,68 +23,65 @@ var ( ) func GetModuleRootPath() string { - if moduleRootPath != "" { - return moduleRootPath - } - - moduleRootPath := os.Getenv("EXIF_MODULE_ROOT_PATH") - if moduleRootPath != "" { - return moduleRootPath - } - - currentWd, err := os.Getwd() - log.PanicIf(err) - - currentPath := currentWd - visited := make([]string, 0) - - for { - tryStampFilepath := path.Join(currentPath, ".MODULE_ROOT") - - _, err := os.Stat(tryStampFilepath) - if err != nil && os.IsNotExist(err) != true { - log.Panic(err) - } else if err == nil { - break + if moduleRootPath == "" { + moduleRootPath := os.Getenv("EXIF_MODULE_ROOT_PATH") + if moduleRootPath != "" { + return moduleRootPath } - visited = append(visited, tryStampFilepath) + currentWd, err := os.Getwd() + log.PanicIf(err) - currentPath = path.Dir(currentPath) - if currentPath == "/" { - log.Panicf("could not find module-root: %v", visited) + currentPath := currentWd + visited := make([]string, 0) + + for { + tryStampFilepath := path.Join(currentPath, ".MODULE_ROOT") + + _, err := os.Stat(tryStampFilepath) + if err != nil && os.IsNotExist(err) != true { + log.Panic(err) + } else if err == nil { + break + } + + visited = append(visited, tryStampFilepath) + + currentPath = path.Dir(currentPath) + if currentPath == "/" { + log.Panicf("could not find module-root: %v", visited) + } } + + moduleRootPath = currentPath } - return currentPath + return moduleRootPath } -func getTestAssetsPath() string { - if assetsPath == "" { - moduleRootPath := GetModuleRootPath() - assetsPath = path.Join(moduleRootPath, "assets") - } +func GetTestAssetsPath() string { + moduleRootPath := GetModuleRootPath() + assetsPath := path.Join(moduleRootPath, "assets") return assetsPath } func getTestImageFilepath() string { - if testImageFilepath == "" { - assetsPath := getTestAssetsPath() - testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg") - } - + assetsPath := GetTestAssetsPath() + testImageFilepath := path.Join(assetsPath, "NDM_8901.jpg") return testImageFilepath } func getTestExifData() []byte { - assetsPath := getTestAssetsPath() - filepath := path.Join(assetsPath, "NDM_8901.jpg.exif") + if testExifData == nil { + assetsPath := GetTestAssetsPath() + filepath := path.Join(assetsPath, "NDM_8901.jpg.exif") - var err error + var err error - testExifData, err = ioutil.ReadFile(filepath) - log.PanicIf(err) + testExifData, err = ioutil.ReadFile(filepath) + log.PanicIf(err) + } return testExifData } diff --git a/v2/ifd_enumerate_test.go b/v2/ifd_enumerate_test.go index 38f7396..9d2e544 100644 --- a/v2/ifd_enumerate_test.go +++ b/v2/ifd_enumerate_test.go @@ -217,6 +217,7 @@ func TestIfd_Thumbnail(t *testing.T) { actual, err := ifd.NextIfd.Thumbnail() log.PanicIf(err) + assetsPath := exifcommon.GetTestAssetsPath() expectedFilepath := path.Join(assetsPath, "NDM_8901.jpg.thumbnail") expected, err := ioutil.ReadFile(expectedFilepath) @@ -277,7 +278,7 @@ func TestIfd_GpsInfo__2_0_0_0(t *testing.T) { } }() - assetsPath := getTestAssetsPath() + assetsPath := exifcommon.GetTestAssetsPath() filepath := path.Join(assetsPath, "gps-2000-scaled.jpg") rawExif, err := SearchFileAndExtractExif(filepath) diff --git a/v2/testing_common.go b/v2/testing_common.go index 53a3b16..1783466 100644 --- a/v2/testing_common.go +++ b/v2/testing_common.go @@ -13,11 +13,7 @@ import ( ) var ( - assetsPath = "" - testImageFilepath = "" - testGpsImageFilepath = "" - - testExifData = make([]byte, 0) + testExifData []byte = nil ) func getExifSimpleTestIb() *IfdBuilder { @@ -159,41 +155,28 @@ func validateExifSimpleTestIb(exifData []byte, t *testing.T) { } } -func getTestAssetsPath() string { - if assetsPath == "" { - moduleRootPath := exifcommon.GetModuleRootPath() - assetsPath = path.Join(moduleRootPath, "assets") - } - - return assetsPath -} - func getTestImageFilepath() string { - if testImageFilepath == "" { - assetsPath := getTestAssetsPath() - testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg") - } - + assetsPath := exifcommon.GetTestAssetsPath() + testImageFilepath := path.Join(assetsPath, "NDM_8901.jpg") return testImageFilepath } func getTestExifData() []byte { - assetsPath := getTestAssetsPath() - filepath := path.Join(assetsPath, "NDM_8901.jpg.exif") + if testExifData == nil { + assetsPath := exifcommon.GetTestAssetsPath() + filepath := path.Join(assetsPath, "NDM_8901.jpg.exif") - var err error + var err error - testExifData, err = ioutil.ReadFile(filepath) - log.PanicIf(err) + testExifData, err = ioutil.ReadFile(filepath) + log.PanicIf(err) + } return testExifData } func getTestGpsImageFilepath() string { - if testGpsImageFilepath == "" { - assetsPath := getTestAssetsPath() - testGpsImageFilepath = path.Join(assetsPath, "gps.jpg") - } - + assetsPath := exifcommon.GetTestAssetsPath() + testGpsImageFilepath := path.Join(assetsPath, "gps.jpg") return testGpsImageFilepath }