Skip to content

Commit

Permalink
Removed init()'s from tests so they don't run when imported
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dsoprea committed Jan 13, 2020
1 parent 0d58b5d commit eee9ff1
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
33 changes: 26 additions & 7 deletions v2/common/testing_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +24,10 @@ var (
)

func GetModuleRootPath() string {
if moduleRootPath != "" {
return moduleRootPath
}

moduleRootPath := os.Getenv("EXIF_MODULE_ROOT_PATH")
if moduleRootPath != "" {
return moduleRootPath
Expand Down Expand Up @@ -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")
}

return assetsPath
}

testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg")
func getTestImageFilepath() string {
if testImageFilepath == "" {
assetsPath := getTestAssetsPath()
testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg")
}

// Load test EXIF data.
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
}
14 changes: 14 additions & 0 deletions v2/exif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func TestVisit(t *testing.T) {

// Open the file.

testImageFilepath := getTestImageFilepath()

f, err := os.Open(testImageFilepath)
log.PanicIf(err)

Expand Down Expand Up @@ -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")
}
Expand All @@ -226,6 +236,8 @@ func TestCollect(t *testing.T) {
}
}()

testImageFilepath := getTestImageFilepath()

rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)

Expand Down Expand Up @@ -341,6 +353,8 @@ func TestCollect(t *testing.T) {
}

func TestParseExifHeader(t *testing.T) {
testExifData := getTestExifData()

eh, err := ParseExifHeader(testExifData)
log.PanicIf(err)

Expand Down
12 changes: 12 additions & 0 deletions v2/ifd_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,8 @@ func TestIfdBuilder_CreateIfdBuilderFromExistingChain(t *testing.T) {
}
}()

testImageFilepath := getTestImageFilepath()

rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -1644,6 +1650,8 @@ func TestIfdBuilder_CreateIfdBuilderFromExistingChain_RealData(t *testing.T) {
// }

func ExampleIfd_Thumbnail() {
testImageFilepath := getTestImageFilepath()

rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)

Expand All @@ -1665,6 +1673,8 @@ func ExampleIfd_Thumbnail() {
}

func ExampleBuilderTag_SetValue() {
testImageFilepath := getTestImageFilepath()

rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)

Expand Down Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions v2/ifd_enumerate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func TestIfdTagEntry_RawBytes_RealData(t *testing.T) {
}
}()

testImageFilepath := getTestImageFilepath()

rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -413,6 +428,8 @@ func TestIfd_EnumerateTagsRecursively(t *testing.T) {
}

func ExampleIfd_EnumerateTagsRecursively() {
testImageFilepath := getTestImageFilepath()

rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)

Expand Down Expand Up @@ -468,6 +485,8 @@ func ExampleIfd_GpsInfo() {
}

func ExampleIfd_FindTagWithName() {
testImageFilepath := getTestImageFilepath()

rawExif, err := SearchFileAndExtractExif(testImageFilepath)
log.PanicIf(err)

Expand Down
25 changes: 20 additions & 5 deletions v2/testing_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit eee9ff1

Please sign in to comment.