| OLD | NEW |
| (Empty) | |
| 1 package pdf |
| 2 |
| 3 import ( |
| 4 "bytes" |
| 5 "crypto/md5" |
| 6 "io" |
| 7 "io/ioutil" |
| 8 "os" |
| 9 "path" |
| 10 "testing" |
| 11 |
| 12 "github.com/skia-dev/glog" |
| 13 assert "github.com/stretchr/testify/require" |
| 14 "go.skia.org/infra/go/fileutil" |
| 15 "go.skia.org/infra/go/testutils" |
| 16 "go.skia.org/infra/go/util" |
| 17 ) |
| 18 |
| 19 func md5OfFile(path string) (sum []byte, err error) { |
| 20 md5 := md5.New() |
| 21 f, err := os.Open(path) |
| 22 if err != nil { |
| 23 return |
| 24 } |
| 25 defer util.Close(f) |
| 26 if _, err = io.Copy(md5, f); err != nil { |
| 27 return |
| 28 } |
| 29 sum = md5.Sum(nil) |
| 30 return |
| 31 } |
| 32 |
| 33 func filesEqual(path1, path2 string) bool { |
| 34 checksum1, err := md5OfFile(path1) |
| 35 if err != nil { |
| 36 glog.Infof("%v\n", err) |
| 37 return false |
| 38 } |
| 39 checksum2, err := md5OfFile(path2) |
| 40 if err != nil { |
| 41 glog.Infof("%v\n", err) |
| 42 return false |
| 43 } |
| 44 return 0 == bytes.Compare(checksum1, checksum2) |
| 45 } |
| 46 |
| 47 func testRasterizer(t *testing.T, rasterizer Rasterizer, expectation string) { |
| 48 assert.True(t, rasterizer.Enabled(), "%s.Enabled() failed.", rasterizer.
String()) |
| 49 |
| 50 testDataDir, err := testutils.TestDataDir() |
| 51 assert.Nil(t, err, "TestDataDir missing: %v", err) |
| 52 |
| 53 tempDir, err := ioutil.TempDir("", "pdf_test_") |
| 54 assert.Nil(t, err, "ioutil.TempDir failed") |
| 55 defer util.RemoveAll(tempDir) |
| 56 |
| 57 pdfSrcPath := path.Join(testDataDir, "minimal.pdf") |
| 58 assert.True(t, fileutil.FileExists(pdfSrcPath), "Path '%s' does not exis
t", pdfSrcPath) |
| 59 pdfInputPath := path.Join(tempDir, "minimal.pdf") |
| 60 |
| 61 err = os.Symlink(pdfSrcPath, pdfInputPath) |
| 62 assert.Nil(t, err, "Symlink failed") |
| 63 assert.True(t, fileutil.FileExists(pdfInputPath), "Path '%s' does not ex
ist", pdfInputPath) |
| 64 |
| 65 outputFileName := path.Join(tempDir, "test.png") |
| 66 |
| 67 badPath := path.Join(tempDir, "this_file_should_really_not_exist.pdf") |
| 68 |
| 69 if err := rasterizer.Rasterize(badPath, outputFileName); err == nil { |
| 70 t.Errorf(": Got '%v' Want '%v'", err, nil) |
| 71 } |
| 72 |
| 73 if err := rasterizer.Rasterize(pdfInputPath, outputFileName); err != nil
{ |
| 74 t.Errorf(": Got '%v' Want '!nil'", err) |
| 75 } |
| 76 |
| 77 expectedOutput := path.Join(testDataDir, expectation) |
| 78 assert.True(t, filesEqual(outputFileName, expectedOutput), "png output n
ot correct") |
| 79 } |
| 80 |
| 81 func TestRasterizePdfium(t *testing.T) { |
| 82 testRasterizer(t, Pdfium{}, "minimalPdfium.png") |
| 83 } |
| 84 |
| 85 func TestRasterizePoppler(t *testing.T) { |
| 86 testRasterizer(t, Poppler{}, "minimalPoppler.png") |
| 87 } |
| OLD | NEW |