github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/attachment_test.go (about) 1 /* 2 Copyright 2019 The pdf 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 ) 25 26 func prepareForAttachmentTest(t *testing.T) error { 27 t.Helper() 28 for _, fileName := range []string{"go.pdf", "golang.pdf", "T4.pdf", "go-lecture.pdf"} { 29 inFile := filepath.Join(inDir, fileName) 30 outFile := filepath.Join(outDir, fileName) 31 if err := copyFile(t, inFile, outFile); err != nil { 32 return err 33 } 34 } 35 return copyFile(t, filepath.Join(resDir, "test.wav"), filepath.Join(outDir, "test.wav")) 36 } 37 38 func listAttachments(t *testing.T, msg, fileName string, want int) []string { 39 t.Helper() 40 cmd := cli.ListAttachmentsCommand(fileName, conf) 41 list, err := cli.Process(cmd) 42 if err != nil { 43 t.Fatalf("%s list attachments: %v\n", msg, err) 44 } 45 // # of attachments must be want 46 if len(list) != want { 47 t.Fatalf("%s: list attachments %s: want %d got %d\n", msg, fileName, want, len(list)) 48 } 49 return list 50 } 51 52 func TestAttachments(t *testing.T) { 53 msg := "testAttachments" 54 55 if err := prepareForAttachmentTest(t); err != nil { 56 t.Fatalf("%s prepare for attachments: %v\n", msg, err) 57 } 58 59 fileName := filepath.Join(outDir, "go.pdf") 60 61 // # of attachments must be 0 62 listAttachments(t, msg, fileName, 0) 63 64 // attach add 4 files 65 files := []string{ 66 filepath.Join(outDir, "golang.pdf"), 67 filepath.Join(outDir, "T4.pdf"), 68 filepath.Join(outDir, "go-lecture.pdf"), 69 filepath.Join(outDir, "test.wav"), 70 } 71 72 cmd := cli.AddAttachmentsCommand(fileName, "", files, conf) 73 if _, err := cli.Process(cmd); err != nil { 74 t.Fatalf("%s add attachments: %v\n", msg, err) 75 } 76 list := listAttachments(t, msg, fileName, 4) 77 for _, s := range list { 78 t.Log(s) 79 } 80 81 // Extract all attachments. 82 cmd = cli.ExtractAttachmentsCommand(fileName, outDir, nil, conf) 83 if _, err := cli.Process(cmd); err != nil { 84 t.Fatalf("%s extract all attachments: %v\n", msg, err) 85 } 86 87 // Extract 1 attachment. 88 cmd = cli.ExtractAttachmentsCommand(fileName, outDir, []string{"golang.pdf"}, conf) 89 if _, err := cli.Process(cmd); err != nil { 90 t.Fatalf("%s extract one attachment: %v\n", msg, err) 91 } 92 93 // Remove 1 attachment. 94 cmd = cli.RemoveAttachmentsCommand(fileName, "", []string{"golang.pdf"}, conf) 95 if _, err := cli.Process(cmd); err != nil { 96 t.Fatalf("%s remove one attachment: %v\n", msg, err) 97 } 98 listAttachments(t, msg, fileName, 3) 99 100 // Remove all attachments. 101 cmd = cli.RemoveAttachmentsCommand(fileName, "", nil, conf) 102 if _, err := cli.Process(cmd); err != nil { 103 t.Fatalf("%s remove all attachments: %v\n", msg, err) 104 } 105 listAttachments(t, msg, fileName, 0) 106 107 // Validate the processed file. 108 if err := validateFile(t, fileName, conf); err != nil { 109 t.Fatalf("%s: validate: %v\n", msg, err) 110 } 111 }