Removed init()'s from tests so they don't run when imported

Otherwise they'll break for other projects' tests. We now initialize our
paths on-demand, which will only happen when running from our own tests.
for/master
Dustin Oprea 2020-01-13 12:00:23 -05:00
parent 0d58b5da07
commit eee9ff13c7
5 changed files with 91 additions and 12 deletions

View File

@ -13,8 +13,8 @@ import (
var (
assetsPath = ""
testImageFilepath = ""
testExifData = make([]byte, 0)
testExifData = make([]byte, 0)
moduleRootPath = ""
// EncodeDefaultByteOrder is the default byte-order for encoding operations.
EncodeDefaultByteOrder = binary.BigEndian
@ -24,6 +24,10 @@ var (
)
func GetModuleRootPath() string {
if moduleRootPath != "" {
return moduleRootPath
}
moduleRootPath := os.Getenv("EXIF_MODULE_ROOT_PATH")
if moduleRootPath != "" {
return moduleRootPath
@ -56,17 +60,32 @@ func GetModuleRootPath() string {
return currentPath
}
func init() {
moduleRootPath := GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets")
func getTestAssetsPath() string {
if assetsPath == "" {
moduleRootPath := GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets")
}
testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg")
return assetsPath
}
// Load test EXIF data.
func getTestImageFilepath() string {
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
testExifData, err = ioutil.ReadFile(filepath)
log.PanicIf(err)
return testExifData
}

View File

@ -39,6 +39,8 @@ func TestVisit(t *testing.T) {
// Open the file.
testImageFilepath := getTestImageFilepath()
f, err := os.Open(testImageFilepath)
log.PanicIf(err)
@ -194,23 +196,31 @@ func TestVisit(t *testing.T) {
}
func TestSearchFileAndExtractExif(t *testing.T) {
testImageFilepath := getTestImageFilepath()
// Returns a slice starting with the EXIF data and going to the end of the
// image.
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
testExifData := getTestExifData()
if bytes.Compare(rawExif[:len(testExifData)], testExifData) != 0 {
t.Fatalf("found EXIF data not correct")
}
}
func TestSearchAndExtractExif(t *testing.T) {
testImageFilepath := getTestImageFilepath()
imageData, err := ioutil.ReadFile(testImageFilepath)
log.PanicIf(err)
rawExif, err := SearchAndExtractExif(imageData)
log.PanicIf(err)
testExifData := getTestExifData()
if bytes.Compare(rawExif[:len(testExifData)], testExifData) != 0 {
t.Fatalf("found EXIF data not correct")
}
@ -226,6 +236,8 @@ func TestCollect(t *testing.T) {
}
}()
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -341,6 +353,8 @@ func TestCollect(t *testing.T) {
}
func TestParseExifHeader(t *testing.T) {
testExifData := getTestExifData()
eh, err := ParseExifHeader(testExifData)
log.PanicIf(err)

View File

@ -1293,6 +1293,8 @@ func TestIfdBuilder_CreateIfdBuilderFromExistingChain(t *testing.T) {
}
}()
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -1386,6 +1388,8 @@ func TestIfdBuilder_CreateIfdBuilderFromExistingChain(t *testing.T) {
// TODO(dustin): !! Test with an actual GPS-attached image.
func TestIfdBuilder_CreateIfdBuilderFromExistingChain_RealData(t *testing.T) {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -1507,6 +1511,8 @@ func TestIfdBuilder_CreateIfdBuilderFromExistingChain_RealData(t *testing.T) {
}
// func TestIfdBuilder_CreateIfdBuilderFromExistingChain_RealData_WithUpdate(t *testing.T) {
// testImageFilepath := getTestImageFilepath()
// rawExif, err := SearchFileAndExtractExif(testImageFilepath)
// log.PanicIf(err)
@ -1644,6 +1650,8 @@ func TestIfdBuilder_CreateIfdBuilderFromExistingChain_RealData(t *testing.T) {
// }
func ExampleIfd_Thumbnail() {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -1665,6 +1673,8 @@ func ExampleIfd_Thumbnail() {
}
func ExampleBuilderTag_SetValue() {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -1718,6 +1728,8 @@ func ExampleBuilderTag_SetValue() {
// encodes down to a new EXIF block, reparses, and validates that the value for
// that tag is what we set it to.
func ExampleIfdBuilder_SetStandardWithName() {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)

View File

@ -22,6 +22,8 @@ func TestIfdTagEntry_RawBytes_RealData(t *testing.T) {
}
}()
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -61,6 +63,7 @@ func TestIfdTagEntry_RawBytes_RealData(t *testing.T) {
}
func TestIfd_FindTagWithId_Hit(t *testing.T) {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -85,6 +88,8 @@ func TestIfd_FindTagWithId_Hit(t *testing.T) {
}
func TestIfd_FindTagWithId_Miss(t *testing.T) {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -109,6 +114,8 @@ func TestIfd_FindTagWithId_Miss(t *testing.T) {
}
func TestIfd_FindTagWithName_Hit(t *testing.T) {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -133,6 +140,8 @@ func TestIfd_FindTagWithName_Hit(t *testing.T) {
}
func TestIfd_FindTagWithName_Miss(t *testing.T) {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -157,6 +166,8 @@ func TestIfd_FindTagWithName_Miss(t *testing.T) {
}
func TestIfd_FindTagWithName_NonStandard(t *testing.T) {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -181,6 +192,8 @@ func TestIfd_FindTagWithName_NonStandard(t *testing.T) {
}
func TestIfd_Thumbnail(t *testing.T) {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -257,6 +270,8 @@ func TestIfd_GpsInfo(t *testing.T) {
}
func TestIfd_EnumerateTagsRecursively(t *testing.T) {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -413,6 +428,8 @@ func TestIfd_EnumerateTagsRecursively(t *testing.T) {
}
func ExampleIfd_EnumerateTagsRecursively() {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)
@ -468,6 +485,8 @@ func ExampleIfd_GpsInfo() {
}
func ExampleIfd_FindTagWithName() {
testImageFilepath := getTestImageFilepath()
rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)

View File

@ -158,17 +158,32 @@ func validateExifSimpleTestIb(exifData []byte, t *testing.T) {
}
}
func init() {
moduleRootPath := exifcommon.GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets")
func getTestAssetsPath() string {
if assetsPath == "" {
moduleRootPath := exifcommon.GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets")
}
testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg")
return assetsPath
}
// Load test EXIF data.
func getTestImageFilepath() string {
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
testExifData, err = ioutil.ReadFile(filepath)
log.PanicIf(err)
return testExifData
}