diff --git a/v2/.MODULE_ROOT b/v2/.MODULE_ROOT deleted file mode 100644 index e69de29..0000000 diff --git a/v2/common/testing_common.go b/v2/common/testing_common.go index dfbdf44..705a7c5 100644 --- a/v2/common/testing_common.go +++ b/v2/common/testing_common.go @@ -1,9 +1,10 @@ package exifcommon import ( - "os" "path" + "go/build" + "encoding/binary" "io/ioutil" @@ -23,63 +24,32 @@ var ( TestDefaultByteOrder = binary.BigEndian ) +// GetModuleRootPath returns our source-path when running from source during +// tests. func GetModuleRootPath() string { - if moduleRootPath != "" { - return moduleRootPath - } + p, err := build.Default.Import( + "github.com/dsoprea/go-exif/v2", + build.Default.GOPATH, + build.FindOnly) - 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 - } - - visited = append(visited, tryStampFilepath) - - currentPath = path.Dir(currentPath) - if currentPath == "/" { - log.Panicf("could not find module-root: %v", visited) - } - } - - return currentPath + packagePath := p.Dir + return packagePath } func getTestAssetsPath() string { - if assetsPath == "" { - moduleRootPath := GetModuleRootPath() - assetsPath = path.Join(moduleRootPath, "assets") - } + moduleRootPath := GetModuleRootPath() + assetsPath := path.Join(moduleRootPath, "assets") return assetsPath } func getTestImageFilepath() string { - if testImageFilepath == "" { - assetsPath := getTestAssetsPath() - testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg") - } - - return testImageFilepath + return path.Join(assetsPath, "NDM_8901.jpg") } func getTestExifData() []byte { - assetsPath := getTestAssetsPath() filepath := path.Join(assetsPath, "NDM_8901.jpg.exif") var err error @@ -89,3 +59,7 @@ func getTestExifData() []byte { return testExifData } + +func init() { + assetsPath = getTestAssetsPath() +} diff --git a/v2/exif.go b/v2/exif.go index 47c96c5..20b7237 100644 --- a/v2/exif.go +++ b/v2/exif.go @@ -173,7 +173,7 @@ func ParseExifHeader(data []byte) (eh ExifHeader, err error) { // CIPA DC-008-2016; JEITA CP-3451D // -> http://www.cipa.jp/std/documents/e/DC-008-Translation-2016-E.pdf - if len(data) < 8 { + if len(data) < ExifSignatureLength { exifLogger.Warningf(nil, "Not enough data for EXIF header: (%d)", len(data)) return eh, ErrNoExif } diff --git a/v2/testing_common.go b/v2/testing_common.go index 53a3b16..bbc20b5 100644 --- a/v2/testing_common.go +++ b/v2/testing_common.go @@ -13,10 +13,7 @@ import ( ) var ( - assetsPath = "" - testImageFilepath = "" - testGpsImageFilepath = "" - + assetsPath = "" testExifData = make([]byte, 0) ) @@ -160,25 +157,18 @@ func validateExifSimpleTestIb(exifData []byte, t *testing.T) { } func getTestAssetsPath() string { - if assetsPath == "" { - moduleRootPath := exifcommon.GetModuleRootPath() - assetsPath = path.Join(moduleRootPath, "assets") - } + moduleRootPath := exifcommon.GetModuleRootPath() + assetsPath := path.Join(moduleRootPath, "assets") return assetsPath } func getTestImageFilepath() string { - if testImageFilepath == "" { - assetsPath := getTestAssetsPath() - testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg") - } - + testImageFilepath := path.Join(assetsPath, "NDM_8901.jpg") return testImageFilepath } func getTestExifData() []byte { - assetsPath := getTestAssetsPath() filepath := path.Join(assetsPath, "NDM_8901.jpg.exif") var err error @@ -190,10 +180,10 @@ func getTestExifData() []byte { } func getTestGpsImageFilepath() string { - if testGpsImageFilepath == "" { - assetsPath := getTestAssetsPath() - testGpsImageFilepath = path.Join(assetsPath, "gps.jpg") - } - + testGpsImageFilepath := path.Join(assetsPath, "gps.jpg") return testGpsImageFilepath } + +func init() { + assetsPath = getTestAssetsPath() +}