github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/cli_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 package test 17 18 import ( 19 "fmt" 20 "io" 21 "os" 22 "path/filepath" 23 "strings" 24 "testing" 25 26 "github.com/pdfcpu/pdfcpu/pkg/api" 27 "github.com/pdfcpu/pdfcpu/pkg/cli" 28 "github.com/pdfcpu/pdfcpu/pkg/log" 29 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" 30 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types" 31 ) 32 33 var inDir, outDir, resDir, fontDir, samplesDir string 34 var conf *model.Configuration 35 36 func isTrueType(filename string) bool { 37 s := strings.ToLower(filename) 38 return strings.HasSuffix(s, ".ttf") || strings.HasSuffix(s, ".ttc") 39 } 40 41 func userFonts(dir string) ([]string, error) { 42 files, err := os.ReadDir(dir) 43 if err != nil { 44 return nil, err 45 } 46 ff := []string(nil) 47 for _, f := range files { 48 if isTrueType(f.Name()) { 49 fn := filepath.Join(dir, f.Name()) 50 ff = append(ff, fn) 51 } 52 } 53 return ff, nil 54 } 55 56 func TestMain(m *testing.M) { 57 inDir = filepath.Join("..", "..", "testdata") 58 fontDir = filepath.Join(inDir, "fonts") 59 resDir = filepath.Join(inDir, "resources") 60 samplesDir = filepath.Join("..", "..", "samples") 61 62 conf = api.LoadConfiguration() 63 64 // Install test user fonts from pkg/testdata/fonts. 65 fonts, err := userFonts(filepath.Join(inDir, "fonts")) 66 if err != nil { 67 fmt.Printf("%v", err) 68 os.Exit(1) 69 } 70 71 if err := api.InstallFonts(fonts); err != nil { 72 fmt.Printf("%v", err) 73 os.Exit(1) 74 } 75 76 if outDir, err = os.MkdirTemp("", "pdfcpu_cli_tests"); err != nil { 77 fmt.Printf("%v", err) 78 os.Exit(1) 79 } 80 //fmt.Printf("outDir = %s\n", outDir) 81 82 exitCode := m.Run() 83 84 if err = os.RemoveAll(outDir); err != nil { 85 fmt.Printf("%v", err) 86 os.Exit(1) 87 } 88 89 os.Exit(exitCode) 90 } 91 92 func copyFile(t *testing.T, srcFileName, destFileName string) error { 93 t.Helper() 94 from, err := os.Open(srcFileName) 95 if err != nil { 96 return err 97 } 98 defer from.Close() 99 to, err := os.Create(destFileName) 100 if err != nil { 101 return err 102 } 103 defer to.Close() 104 _, err = io.Copy(to, from) 105 return err 106 } 107 108 func imageFileNames(t *testing.T, dir string) []string { 109 t.Helper() 110 fn, err := model.ImageFileNames(dir, types.MB) 111 if err != nil { 112 t.Fatal(err) 113 } 114 return fn 115 } 116 117 func isPDF(filename string) bool { 118 return strings.HasSuffix(strings.ToLower(filename), ".pdf") 119 } 120 121 func allPDFs(t *testing.T, dir string) []string { 122 t.Helper() 123 files, err := os.ReadDir(dir) 124 if err != nil { 125 t.Fatalf("pdfFiles from %s: %v\n", dir, err) 126 } 127 ff := []string(nil) 128 for _, f := range files { 129 if isPDF(f.Name()) { 130 ff = append(ff, f.Name()) 131 } 132 } 133 return ff 134 } 135 136 func validateFile(t *testing.T, fileName string, conf *model.Configuration) error { 137 t.Helper() 138 _, err := cli.Process(cli.ValidateCommand([]string{fileName}, conf)) 139 return err 140 } 141 142 func TestValidate(t *testing.T) { 143 msg := "TestValidateCommand" 144 for _, f := range allPDFs(t, inDir) { 145 inFile := filepath.Join(inDir, f) 146 if err := validateFile(t, inFile, conf); err != nil { 147 t.Fatalf("%s: %s: %v\n", msg, inFile, err) 148 } 149 } 150 } 151 152 func TestInfoCommand(t *testing.T) { 153 msg := "TestInfoCommand" 154 inFile := filepath.Join(inDir, "5116.DCT_Filter.pdf") 155 156 cmd := cli.InfoCommand([]string{inFile}, nil, true, true, conf) 157 if _, err := cli.Process(cmd); err != nil { 158 t.Fatalf("%s: %v\n", msg, err) 159 } 160 } 161 162 func TestUnknownCommand(t *testing.T) { 163 msg := "TestUnknownCommand" 164 inFile := filepath.Join(outDir, "go.pdf") 165 166 cmd := &cli.Command{ 167 Mode: 99, 168 InFile: &inFile, 169 Conf: conf} 170 171 if _, err := cli.Process(cmd); err == nil { 172 t.Fatalf("%s: %v\n", msg, err) 173 } 174 } 175 176 // Enable this test for debugging of a specific file. 177 func XTestSomeCommand(t *testing.T) { 178 msg := "TestSomeCommand" 179 180 log.SetDefaultTraceLogger() 181 //log.SetDefaultParseLogger() 182 log.SetDefaultReadLogger() 183 log.SetDefaultValidateLogger() 184 log.SetDefaultOptimizeLogger() 185 log.SetDefaultWriteLogger() 186 //log.SetDefaultStatsLogger() 187 188 inFile := filepath.Join(inDir, "test.pdf") 189 190 cmd := cli.ValidateCommand([]string{inFile}, conf) 191 192 if _, err := cli.Process(cmd); err != nil { 193 t.Fatalf("%s %s: %v\n", msg, inFile, err) 194 } 195 }