Refactor module-detection to use go/build instead of static file

dustin/master
Dustin Oprea 2020-06-02 04:23:08 -04:00
parent e46688a494
commit c23f7a1066
4 changed files with 27 additions and 63 deletions

View File

View File

@ -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()
}

View File

@ -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
}

View File

@ -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()
}