summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorKurt Jung <kurt.w.jung@gmail.com>2015-10-08 14:19:39 -0400
committerKurt Jung <kurt.w.jung@gmail.com>2015-10-08 14:19:39 -0400
commitfd73b28bfe68c6b20d2ce446de94819bdcfe8d36 (patch)
tree28bdede25505f7adbb039d385fb1c9cca932eba5 /internal
parent12304a427f93e86aca8265ebc2c8ca47d5e6d046 (diff)
Compare bytes of PDFs generated as examples with reference copies. Differences in the CreationDate values are effectively ignored in the comparison.
Diffstat (limited to 'internal')
-rw-r--r--internal/example/example.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/example/example.go b/internal/example/example.go
index d6a114c..3a60a1e 100644
--- a/internal/example/example.go
+++ b/internal/example/example.go
@@ -18,9 +18,12 @@
package example
import (
+ "bytes"
"fmt"
+ "io/ioutil"
"os"
"path/filepath"
+ "regexp"
"strings"
)
@@ -87,12 +90,64 @@ func Filename(baseStr string) string {
return PdfFile(baseStr + ".pdf")
}
+var (
+ // 00000230 44 46 20 31 2e 37 29 0a 2f 43 72 65 61 74 69 6f |DF 1.7)./Creatio|
+ // 00000240 6e 44 61 74 65 20 28 44 3a 32 30 31 35 31 30 30 |nDate (D:2015100|
+ // 00000250 38 31 32 33 30 34 35 29 0a 3e 3e 0a 65 6e 64 6f |8123045).>>.endo|
+ creationDateRe *regexp.Regexp = regexp.MustCompile("/CreationDate \\(D:\\d{14}\\)")
+ fixDate []byte = []byte("/CreationDate (D:20000101000000)")
+)
+
+// referenceCompare compares the specified file with the file's reference copy
+// located in the 'reference' subdirectory. All bytes of the two files are
+// compared except for the value of the /CreationDate field in the PDF. An
+// error is returned if the two files do not match. If the file does not exist,
+// a copy of the specified file is made and a non-nil error is returned only if
+// this copy fails.
+func referenceCompare(fileStr string) (err error) {
+ var fileBuf, refFileBuf []byte
+ var refFileStr, refDirStr, dirStr, baseFileStr string
+ dirStr, baseFileStr = filepath.Split(fileStr)
+ refDirStr = filepath.Join(dirStr, "reference")
+ err = os.MkdirAll(refDirStr, 0755)
+ if err == nil {
+ refFileStr = filepath.Join(refDirStr, baseFileStr)
+ fileBuf, err = ioutil.ReadFile(fileStr)
+ if err == nil {
+ // Replace the creation timestamp of this PDF with a fixed value
+ fileBuf = creationDateRe.ReplaceAll(fileBuf, fixDate)
+ refFileBuf, err = ioutil.ReadFile(refFileStr)
+ if err == nil {
+ if len(fileBuf) == len(refFileBuf) {
+ if bytes.Equal(fileBuf, refFileBuf) {
+ // Files match
+ } else {
+ err = fmt.Errorf("%s differs from %s", fileStr, refFileStr)
+ }
+ } else {
+ err = fmt.Errorf("size of %s (%d) does not match size of %s (%d)",
+ fileStr, len(fileBuf), refFileStr, len(refFileBuf))
+ }
+ } else {
+ // Reference file is missing. Create it with a copy of the newly produced
+ // file in which the creation date has been fixed. Overwrite error with copy
+ // error.
+ err = ioutil.WriteFile(refFileStr, fileBuf, 0644)
+ }
+ }
+ }
+ return
+}
+
// Summary generates a predictable report for use by test examples. If the
// specified error is nil, the filename delimiters are normalized and the
// filename printed to standard output with a success message. If the specified
// error is not nil, its String() value is printed to standard output.
func Summary(err error, fileStr string) {
if err == nil {
+ err = referenceCompare(fileStr)
+ }
+ if err == nil {
fileStr = filepath.ToSlash(fileStr)
fmt.Printf("Successfully generated %s\n", fileStr)
} else {