diff --git a/internal/app/xhttp/pkg/resource/arg_test.go b/internal/app/xhttp/pkg/resource/arg_test.go index 3c7f6b02..f42fd1d0 100644 --- a/internal/app/xhttp/pkg/resource/arg_test.go +++ b/internal/app/xhttp/pkg/resource/arg_test.go @@ -24,6 +24,7 @@ func TestArgKeys(t *testing.T) { MarginLeftArgKey, MarginRightArgKey, LandscapeArgKey, + PageRangesArgKey, } assert.Equal(t, expected, ArgKeys()) } diff --git a/internal/pkg/printer/chrome.go b/internal/pkg/printer/chrome.go index f7cca718..41b4c99d 100644 --- a/internal/pkg/printer/chrome.go +++ b/internal/pkg/printer/chrome.go @@ -161,15 +161,15 @@ func (p chromePrinter) Print(destination string) error { printToPdfArgs.SetPageRanges(p.opts.PageRanges) } // print the page to PDF. - // TODO catch page range error? print, err := targetClient.Page.PrintToPDF( ctx, printToPdfArgs, ) if err != nil { + // TODO: find a way to check it in the handlers. if strings.Contains(err.Error(), "Page range syntax error") { return xerror.Invalid( - "", + op, fmt.Sprintf("'%s' is not a valid Google Chrome page ranges", p.opts.PageRanges), err, ) diff --git a/internal/pkg/printer/html_test.go b/internal/pkg/printer/html_test.go index 4a233163..e69539fa 100644 --- a/internal/pkg/printer/html_test.go +++ b/internal/pkg/printer/html_test.go @@ -38,6 +38,26 @@ func TestHTMLPrinter(t *testing.T) { assert.Nil(t, err) err = os.RemoveAll(dest) assert.Nil(t, err) + // options with a page ranges. + opts = DefaultChromePrinterOptions(config) + opts.PageRanges = "1" + p = NewHTMLPrinter(logger, fpath, opts) + dest = test.GenerateDestination() + err = p.Print(dest) + assert.Nil(t, err) + err = os.RemoveAll(dest) + assert.Nil(t, err) + // should not be OK as options have + // a wrong page ranges. + opts = DefaultChromePrinterOptions(config) + opts.PageRanges = "foo" + p = NewHTMLPrinter(logger, fpath, opts) + dest = test.GenerateDestination() + err = p.Print(dest) + test.AssertError(t, err) + assert.Equal(t, xerror.InvalidCode, xerror.Code(err)) + err = os.RemoveAll(dest) + assert.Nil(t, err) // should not be OK as context.Context // should timeout. opts = DefaultChromePrinterOptions(config) diff --git a/internal/pkg/printer/markdown_test.go b/internal/pkg/printer/markdown_test.go index c5115a3a..9627cdea 100644 --- a/internal/pkg/printer/markdown_test.go +++ b/internal/pkg/printer/markdown_test.go @@ -40,6 +40,28 @@ func TestMarkdownPrinter(t *testing.T) { assert.Nil(t, err) err = os.RemoveAll(dest) assert.Nil(t, err) + // options with a page ranges. + opts = DefaultChromePrinterOptions(config) + opts.PageRanges = "1" + p, err = NewMarkdownPrinter(logger, fpath, opts) + assert.Nil(t, err) + dest = test.GenerateDestination() + err = p.Print(dest) + assert.Nil(t, err) + err = os.RemoveAll(dest) + assert.Nil(t, err) + // should not be OK as options have + // a wrong page ranges. + opts = DefaultChromePrinterOptions(config) + opts.PageRanges = "foo" + p, err = NewMarkdownPrinter(logger, fpath, opts) + assert.Nil(t, err) + dest = test.GenerateDestination() + err = p.Print(dest) + test.AssertError(t, err) + assert.Equal(t, xerror.InvalidCode, xerror.Code(err)) + err = os.RemoveAll(dest) + assert.Nil(t, err) // should not be OK as context.Context // should timeout. opts = DefaultChromePrinterOptions(config) diff --git a/internal/pkg/printer/office.go b/internal/pkg/printer/office.go index 7112acda..e0f4f20b 100644 --- a/internal/pkg/printer/office.go +++ b/internal/pkg/printer/office.go @@ -91,9 +91,6 @@ func (p officePrinter) Print(destination string) error { func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) error { const op string = "printer.unoconv" resolver := func() error { - hasPageRanges := func() bool { - return p.opts.PageRanges != "" && len(p.fpaths) == 1 - } port, err := freeport.GetFreePort() if err != nil { return err @@ -109,14 +106,15 @@ func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) e if p.opts.Landscape { args = append(args, "--printer", "PaperOrientation=landscape") } - if hasPageRanges() { + if p.opts.PageRanges != "" { args = append(args, "--export", fmt.Sprintf("PageRange=%s", p.opts.PageRanges)) } args = append(args, "--output", destination, fpath) if err := xexec.Run(ctx, p.logger, "unoconv", args...); err != nil { - if hasPageRanges() && strings.Contains(err.Error(), "exit status 5") { + // TODO: find a way to check it in the handlers. + if p.opts.PageRanges != "" && strings.Contains(err.Error(), "exit status 5") { return xerror.Invalid( - "", + op, fmt.Sprintf("'%s' is not a valid LibreOffice page ranges", p.opts.PageRanges), err, ) diff --git a/internal/pkg/printer/office_test.go b/internal/pkg/printer/office_test.go index b6a0235b..5eb8992e 100644 --- a/internal/pkg/printer/office_test.go +++ b/internal/pkg/printer/office_test.go @@ -46,6 +46,26 @@ func TestOfficePrinter(t *testing.T) { assert.Nil(t, err) err = os.RemoveAll(dest) assert.Nil(t, err) + // options with page ranges. + opts = DefaultOfficePrinterOptions(config) + opts.PageRanges = "1-1" + p = NewOfficePrinter(logger, []string{fpaths[0]}, opts) + dest = test.GenerateDestination() + err = p.Print(dest) + assert.Nil(t, err) + err = os.RemoveAll(dest) + assert.Nil(t, err) + // should not be OK as options have + // a wrong page ranges. + opts = DefaultOfficePrinterOptions(config) + opts.PageRanges = "foo" + p = NewOfficePrinter(logger, []string{fpaths[0]}, opts) + dest = test.GenerateDestination() + err = p.Print(dest) + test.AssertError(t, err) + assert.Equal(t, xerror.InvalidCode, xerror.Code(err)) + err = os.RemoveAll(dest) + assert.Nil(t, err) // should not be OK as context.Context // should timeout. opts = DefaultOfficePrinterOptions(config) diff --git a/internal/pkg/printer/url_test.go b/internal/pkg/printer/url_test.go index 2c0b99f7..ef477e75 100644 --- a/internal/pkg/printer/url_test.go +++ b/internal/pkg/printer/url_test.go @@ -38,6 +38,26 @@ func TestURLPrinter(t *testing.T) { assert.Nil(t, err) err = os.RemoveAll(dest) assert.Nil(t, err) + // options with a pages ranges. + opts = DefaultChromePrinterOptions(config) + opts.PageRanges = "1" + p = NewURLPrinter(logger, URL, opts) + dest = test.GenerateDestination() + err = p.Print(dest) + assert.Nil(t, err) + err = os.RemoveAll(dest) + assert.Nil(t, err) + // should not be OK as options have + // a wrong page ranges. + opts = DefaultChromePrinterOptions(config) + opts.PageRanges = "foo" + p = NewURLPrinter(logger, URL, opts) + dest = test.GenerateDestination() + err = p.Print(dest) + test.AssertError(t, err) + assert.Equal(t, xerror.InvalidCode, xerror.Code(err)) + err = os.RemoveAll(dest) + assert.Nil(t, err) // should not be OK as context.Context // should timeout. opts = DefaultChromePrinterOptions(config)