github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/optimize_test.go (about) 1 /* 2 Copyright 2020 The pdfcpu Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "path/filepath" 21 "testing" 22 23 "github.com/pdfcpu/pdfcpu/pkg/cli" 24 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" 25 ) 26 27 func optimizeFile(t *testing.T, fileName string, conf *model.Configuration) error { 28 t.Helper() 29 cmd := cli.OptimizeCommand(fileName, "", conf) 30 _, err := cli.Process(cmd) 31 return err 32 } 33 34 func testOptimizeFile(t *testing.T, inFile, outFile string) { 35 t.Helper() 36 msg := "testOptimizeFile" 37 38 // Optimize inFile and write result to outFile. 39 cmd := cli.OptimizeCommand(inFile, outFile, conf) 40 if _, err := cli.Process(cmd); err != nil { 41 t.Fatalf("%s %s: %v\n", msg, inFile, err) 42 } 43 44 // Optimize outFile and write result to outFile. 45 cmd = cli.OptimizeCommand(outFile, "", conf) 46 if _, err := cli.Process(cmd); err != nil { 47 t.Fatalf("%s %s: %v\n", msg, outFile, err) 48 } 49 50 // Optimize outFile and write result to outFile. 51 if err := optimizeFile(t, outFile, nil); err != nil { 52 t.Fatalf("%s %s: %v\n", msg, outFile, err) 53 } 54 } 55 56 func TestOptimizeCommand(t *testing.T) { 57 for _, f := range allPDFs(t, inDir) { 58 inFile := filepath.Join(inDir, f) 59 outFile := filepath.Join(outDir, f) 60 testOptimizeFile(t, inFile, outFile) 61 } 62 }