From fd73b28bfe68c6b20d2ce446de94819bdcfe8d36 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Thu, 8 Oct 2015 14:19:39 -0400 Subject: Compare bytes of PDFs generated as examples with reference copies. Differences in the CreationDate values are effectively ignored in the comparison. --- internal/example/example.go | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'internal') 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,11 +90,63 @@ 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) -- cgit v1.2.1-24-ge1ad From 4bed0d1e5b814845432a73f62b9462a27fd53bda Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Thu, 8 Oct 2015 14:52:13 -0400 Subject: Clarify a comment --- internal/example/example.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'internal') diff --git a/internal/example/example.go b/internal/example/example.go index 3a60a1e..6a3c65a 100644 --- a/internal/example/example.go +++ b/internal/example/example.go @@ -101,9 +101,9 @@ var ( // 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. +// error is returned if the two files do not match. If the reference 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 -- cgit v1.2.1-24-ge1ad From 47143d5c2dabe888df2ff3e586c911468881b55c Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Thu, 8 Oct 2015 15:05:15 -0400 Subject: Omit type in declaration assignment --- internal/example/example.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'internal') diff --git a/internal/example/example.go b/internal/example/example.go index 6a3c65a..9e2c736 100644 --- a/internal/example/example.go +++ b/internal/example/example.go @@ -94,8 +94,8 @@ 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)") + creationDateRe = regexp.MustCompile("/CreationDate \\(D:\\d{14}\\)") + fixDate = []byte("/CreationDate (D:20000101000000)") ) // referenceCompare compares the specified file with the file's reference copy -- cgit v1.2.1-24-ge1ad From 25d0813f8f9210fcaf3a5797d72756a332ef2dbe Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Fri, 9 Oct 2015 15:52:07 -0400 Subject: Implement PDF comparison with rudimentary byte-difference display. If a reference file exists, it will be compared with its associated example file. If the reference file is missing, the associated example file is considered to be without differences. PDF files in any subdirectory named reference are not deleted when the test begins. This commit is broken -- it illustrates how the use of maps for fonts, images and other resources lead to different dictionary tables in the PDF document. --- internal/example/example.go | 52 ++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) (limited to 'internal') diff --git a/internal/example/example.go b/internal/example/example.go index 9e2c736..b95972c 100644 --- a/internal/example/example.go +++ b/internal/example/example.go @@ -18,13 +18,14 @@ package example import ( - "bytes" "fmt" - "io/ioutil" + // "io/ioutil" "os" "path/filepath" "regexp" "strings" + + "github.com/jung-kurt/gofpdf" ) var gofpdfDir string @@ -39,8 +40,7 @@ func setRoot() { wdStr, err := os.Getwd() if err == nil { gofpdfDir = "" - sepStr := string(os.PathSeparator) - list := strings.Split(wdStr, sepStr) + list := strings.Split(filepath.ToSlash(wdStr), "/") for j := len(list) - 1; j >= 0 && list[j] != "gofpdf"; j-- { gofpdfDir = filepath.Join(gofpdfDir, "..") } @@ -103,38 +103,32 @@ var ( // 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 reference file does // not exist, a copy of the specified file is made and a non-nil error is -// returned only if this copy fails. +// returned only if this copy fails. If the reference file exists but has zero +// length, the file will not be overwritten and will be considered to be equal +// to the example file. This is intended to facilitate initial example +// development. func referenceCompare(fileStr string) (err error) { - var fileBuf, refFileBuf []byte var refFileStr, refDirStr, dirStr, baseFileStr string + // var fileBuf []byte + // var info os.FileInfo 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) - } - } + // info, err = os.Stat(refFileStr) + // if err == nil { + // if info.Size() > 0 { + err = gofpdf.ComparePDFFiles(fileStr, refFileStr) + // } + // } else { + // // Reference file is missing. Create it with a copy of the example file. + // // Overwrite error with copy error. + // fileBuf, err = ioutil.ReadFile(fileStr) + // if err == nil { + // err = ioutil.WriteFile(refFileStr, fileBuf, 0644) + // } + // } } return } -- cgit v1.2.1-24-ge1ad From b0f3b60a71b8ca327da859e5f4a7cd8eb5bea4e7 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Fri, 9 Oct 2015 16:07:19 -0400 Subject: Remove some development comments --- internal/example/example.go | 34 +++------------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) (limited to 'internal') diff --git a/internal/example/example.go b/internal/example/example.go index b95972c..b29561c 100644 --- a/internal/example/example.go +++ b/internal/example/example.go @@ -19,10 +19,8 @@ package example import ( "fmt" - // "io/ioutil" "os" "path/filepath" - "regexp" "strings" "github.com/jung-kurt/gofpdf" @@ -90,45 +88,19 @@ 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.MustCompile("/CreationDate \\(D:\\d{14}\\)") - fixDate = []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 reference file does -// not exist, a copy of the specified file is made and a non-nil error is -// returned only if this copy fails. If the reference file exists but has zero -// length, the file will not be overwritten and will be considered to be equal -// to the example file. This is intended to facilitate initial example -// development. +// compared except for the value of the /CreationDate field in the PDF. This +// function succeeds if both files are equivalent except for their +// /CreationDate values or if the reference file does not exist. func referenceCompare(fileStr string) (err error) { var refFileStr, refDirStr, dirStr, baseFileStr string - // var fileBuf []byte - // var info os.FileInfo dirStr, baseFileStr = filepath.Split(fileStr) refDirStr = filepath.Join(dirStr, "reference") err = os.MkdirAll(refDirStr, 0755) if err == nil { refFileStr = filepath.Join(refDirStr, baseFileStr) - // info, err = os.Stat(refFileStr) - // if err == nil { - // if info.Size() > 0 { err = gofpdf.ComparePDFFiles(fileStr, refFileStr) - // } - // } else { - // // Reference file is missing. Create it with a copy of the example file. - // // Overwrite error with copy error. - // fileBuf, err = ioutil.ReadFile(fileStr) - // if err == nil { - // err = ioutil.WriteFile(refFileStr, fileBuf, 0644) - // } - // } } return } -- cgit v1.2.1-24-ge1ad From eca9f96fbf9b6be62915d111546f4798ce3fac62 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Sun, 11 Oct 2015 09:16:21 -0400 Subject: Move default assignments to init function of example file from various test files. Now any package that imports the example package will generate PDFs with sorted catalogs and a fixed creation timestamp. --- internal/example/example.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'internal') diff --git a/internal/example/example.go b/internal/example/example.go index b29561c..56ffb23 100644 --- a/internal/example/example.go +++ b/internal/example/example.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/jung-kurt/gofpdf" ) @@ -30,6 +31,8 @@ var gofpdfDir string func init() { setRoot() + gofpdf.SetDefaultCatalogSort(true) + gofpdf.SetDefaultCreationDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) } // Assign the relative path to the gofpdfDir directory based on current working -- cgit v1.2.1-24-ge1ad