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 package exifcommon
import ( import (
"os"
"path" "path"
"go/build"
"encoding/binary" "encoding/binary"
"io/ioutil" "io/ioutil"
@ -23,63 +24,32 @@ var (
TestDefaultByteOrder = binary.BigEndian TestDefaultByteOrder = binary.BigEndian
) )
// GetModuleRootPath returns our source-path when running from source during
// tests.
func GetModuleRootPath() string { func GetModuleRootPath() string {
if moduleRootPath != "" { p, err := build.Default.Import(
return moduleRootPath "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) log.PanicIf(err)
currentPath := currentWd packagePath := p.Dir
visited := make([]string, 0) return packagePath
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 { func getTestAssetsPath() string {
if assetsPath == "" {
moduleRootPath := GetModuleRootPath() moduleRootPath := GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets") assetsPath := path.Join(moduleRootPath, "assets")
}
return assetsPath return assetsPath
} }
func getTestImageFilepath() string { func getTestImageFilepath() string {
if testImageFilepath == "" { return path.Join(assetsPath, "NDM_8901.jpg")
assetsPath := getTestAssetsPath()
testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg")
}
return testImageFilepath
} }
func getTestExifData() []byte { func getTestExifData() []byte {
assetsPath := getTestAssetsPath()
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif") filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
var err error var err error
@ -89,3 +59,7 @@ func getTestExifData() []byte {
return testExifData 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 // CIPA DC-008-2016; JEITA CP-3451D
// -> http://www.cipa.jp/std/documents/e/DC-008-Translation-2016-E.pdf // -> 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)) exifLogger.Warningf(nil, "Not enough data for EXIF header: (%d)", len(data))
return eh, ErrNoExif return eh, ErrNoExif
} }

View File

@ -14,9 +14,6 @@ import (
var ( var (
assetsPath = "" assetsPath = ""
testImageFilepath = ""
testGpsImageFilepath = ""
testExifData = make([]byte, 0) testExifData = make([]byte, 0)
) )
@ -160,25 +157,18 @@ func validateExifSimpleTestIb(exifData []byte, t *testing.T) {
} }
func getTestAssetsPath() string { func getTestAssetsPath() string {
if assetsPath == "" {
moduleRootPath := exifcommon.GetModuleRootPath() moduleRootPath := exifcommon.GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets") assetsPath := path.Join(moduleRootPath, "assets")
}
return assetsPath return assetsPath
} }
func getTestImageFilepath() string { func getTestImageFilepath() string {
if testImageFilepath == "" { testImageFilepath := path.Join(assetsPath, "NDM_8901.jpg")
assetsPath := getTestAssetsPath()
testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg")
}
return testImageFilepath return testImageFilepath
} }
func getTestExifData() []byte { func getTestExifData() []byte {
assetsPath := getTestAssetsPath()
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif") filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
var err error var err error
@ -190,10 +180,10 @@ func getTestExifData() []byte {
} }
func getTestGpsImageFilepath() string { func getTestGpsImageFilepath() string {
if testGpsImageFilepath == "" { testGpsImageFilepath := path.Join(assetsPath, "gps.jpg")
assetsPath := getTestAssetsPath()
testGpsImageFilepath = path.Join(assetsPath, "gps.jpg")
}
return testGpsImageFilepath return testGpsImageFilepath
} }
func init() {
assetsPath = getTestAssetsPath()
}