diff --git a/v2/.MODULE_ROOT b/v2/.MODULE_ROOT new file mode 100644 index 0000000..e69de29 diff --git a/v2/common/testing_common.go b/v2/common/testing_common.go index 705a7c5..dfbdf44 100644 --- a/v2/common/testing_common.go +++ b/v2/common/testing_common.go @@ -1,10 +1,9 @@ package exifcommon import ( + "os" "path" - "go/build" - "encoding/binary" "io/ioutil" @@ -24,32 +23,63 @@ var ( TestDefaultByteOrder = binary.BigEndian ) -// GetModuleRootPath returns our source-path when running from source during -// tests. func GetModuleRootPath() string { - p, err := build.Default.Import( - "github.com/dsoprea/go-exif/v2", - build.Default.GOPATH, - build.FindOnly) + if moduleRootPath != "" { + return moduleRootPath + } + moduleRootPath := os.Getenv("EXIF_MODULE_ROOT_PATH") + if moduleRootPath != "" { + return moduleRootPath + } + + currentWd, err := os.Getwd() log.PanicIf(err) - packagePath := p.Dir - return packagePath + 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 } func getTestAssetsPath() string { - moduleRootPath := GetModuleRootPath() - assetsPath := path.Join(moduleRootPath, "assets") + if assetsPath == "" { + moduleRootPath := GetModuleRootPath() + assetsPath = path.Join(moduleRootPath, "assets") + } return assetsPath } func getTestImageFilepath() string { - return path.Join(assetsPath, "NDM_8901.jpg") + if testImageFilepath == "" { + assetsPath := getTestAssetsPath() + 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 @@ -59,7 +89,3 @@ func getTestExifData() []byte { return testExifData } - -func init() { - assetsPath = getTestAssetsPath() -} diff --git a/v2/exif.go b/v2/exif.go index 20b7237..47c96c5 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) < ExifSignatureLength { + if len(data) < 8 { 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 bbc20b5..53a3b16 100644 --- a/v2/testing_common.go +++ b/v2/testing_common.go @@ -13,7 +13,10 @@ import ( ) var ( - assetsPath = "" + assetsPath = "" + testImageFilepath = "" + testGpsImageFilepath = "" + testExifData = make([]byte, 0) ) @@ -157,18 +160,25 @@ func validateExifSimpleTestIb(exifData []byte, t *testing.T) { } func getTestAssetsPath() string { - moduleRootPath := exifcommon.GetModuleRootPath() - assetsPath := path.Join(moduleRootPath, "assets") + if assetsPath == "" { + moduleRootPath := exifcommon.GetModuleRootPath() + assetsPath = path.Join(moduleRootPath, "assets") + } return assetsPath } func getTestImageFilepath() string { - testImageFilepath := path.Join(assetsPath, "NDM_8901.jpg") + if testImageFilepath == "" { + assetsPath := getTestAssetsPath() + 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 @@ -180,10 +190,10 @@ func getTestExifData() []byte { } func getTestGpsImageFilepath() string { - testGpsImageFilepath := path.Join(assetsPath, "gps.jpg") + if testGpsImageFilepath == "" { + assetsPath := getTestAssetsPath() + testGpsImageFilepath = path.Join(assetsPath, "gps.jpg") + } + return testGpsImageFilepath } - -func init() { - assetsPath = getTestAssetsPath() -}