mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 08:32:16 +01:00
167 lines
3.5 KiB
Go
167 lines
3.5 KiB
Go
package pdftk
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/gotenberg/gotenberg/v7/pkg/gotenberg"
|
|
)
|
|
|
|
func TestPdfTk_Descriptor(t *testing.T) {
|
|
descriptor := new(PdfTk).Descriptor()
|
|
|
|
actual := reflect.TypeOf(descriptor.New())
|
|
expect := reflect.TypeOf(new(PdfTk))
|
|
|
|
if actual != expect {
|
|
t.Errorf("expected '%s' but got '%s'", expect, actual)
|
|
}
|
|
}
|
|
|
|
func TestPdfTk_Provision(t *testing.T) {
|
|
engine := new(PdfTk)
|
|
ctx := gotenberg.NewContext(gotenberg.ParsedFlags{}, nil)
|
|
|
|
err := engine.Provision(ctx)
|
|
if err != nil {
|
|
t.Errorf("expected no error but got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPdfTk_Validate(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
scenario string
|
|
binPath string
|
|
expectError bool
|
|
}{
|
|
{
|
|
scenario: "empty bin path",
|
|
binPath: "",
|
|
expectError: true,
|
|
},
|
|
{
|
|
scenario: "bin path does not exist",
|
|
binPath: "/foo",
|
|
expectError: true,
|
|
},
|
|
{
|
|
scenario: "validate success",
|
|
binPath: os.Getenv("PDFTK_BIN_PATH"),
|
|
expectError: false,
|
|
},
|
|
} {
|
|
t.Run(tc.scenario, func(t *testing.T) {
|
|
engine := new(PdfTk)
|
|
engine.binPath = tc.binPath
|
|
err := engine.Validate()
|
|
|
|
if !tc.expectError && err != nil {
|
|
t.Fatalf("expected no error but got: %v", err)
|
|
}
|
|
|
|
if tc.expectError && err == nil {
|
|
t.Fatal("expected error but got none")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPdfTk_Metrics(t *testing.T) {
|
|
metrics, err := new(PdfTk).Metrics()
|
|
if err != nil {
|
|
t.Fatalf("expected no error but got: %v", err)
|
|
}
|
|
|
|
if len(metrics) != 1 {
|
|
t.Fatalf("expected %d metrics, but got %d", 1, len(metrics))
|
|
}
|
|
|
|
actual := metrics[0].Read()
|
|
if actual != 0 {
|
|
t.Errorf("expected %d PDFtk instances, but got %f", 0, actual)
|
|
}
|
|
}
|
|
|
|
func TestPdfTk_Merge(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
scenario string
|
|
ctx context.Context
|
|
inputPaths []string
|
|
expectError bool
|
|
}{
|
|
{
|
|
scenario: "invalid context",
|
|
ctx: nil,
|
|
expectError: true,
|
|
},
|
|
{
|
|
scenario: "invalid input path",
|
|
ctx: context.TODO(),
|
|
inputPaths: []string{
|
|
"foo",
|
|
},
|
|
expectError: true,
|
|
},
|
|
{
|
|
scenario: "single file success",
|
|
ctx: context.TODO(),
|
|
inputPaths: []string{
|
|
"/tests/test/testdata/pdfengines/sample1.pdf",
|
|
},
|
|
},
|
|
{
|
|
scenario: "many files success",
|
|
ctx: context.TODO(),
|
|
inputPaths: []string{
|
|
"/tests/test/testdata/pdfengines/sample1.pdf",
|
|
"/tests/test/testdata/pdfengines/sample2.pdf",
|
|
},
|
|
},
|
|
} {
|
|
t.Run(tc.scenario, func(t *testing.T) {
|
|
engine := new(PdfTk)
|
|
err := engine.Provision(nil)
|
|
if err != nil {
|
|
t.Fatalf("expected error but got: %v", err)
|
|
}
|
|
|
|
fs := gotenberg.NewFileSystem()
|
|
outputDir, err := fs.MkdirAll()
|
|
if err != nil {
|
|
t.Fatalf("expected error but got: %v", err)
|
|
}
|
|
|
|
defer func() {
|
|
err = os.RemoveAll(fs.WorkingDirPath())
|
|
if err != nil {
|
|
t.Fatalf("expected no error while cleaning up but got: %v", err)
|
|
}
|
|
}()
|
|
|
|
err = engine.Merge(tc.ctx, zap.NewNop(), tc.inputPaths, outputDir+"/foo.pdf")
|
|
|
|
if !tc.expectError && err != nil {
|
|
t.Fatalf("expected no error but got: %v", err)
|
|
}
|
|
|
|
if tc.expectError && err == nil {
|
|
t.Fatal("expected error but got none")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPdfTk_Convert(t *testing.T) {
|
|
engine := new(PdfTk)
|
|
err := engine.Convert(context.TODO(), zap.NewNop(), gotenberg.PdfFormats{}, "", "")
|
|
|
|
if !errors.Is(err, gotenberg.ErrPdfEngineMethodNotSupported) {
|
|
t.Errorf("expected error %v, but got: %v", gotenberg.ErrPdfEngineMethodNotSupported, err)
|
|
}
|
|
}
|