Skip to content

Commit cf09a0a

Browse files
committed
Test that verapdf validate path exists
1 parent 45a69de commit cf09a0a

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

internal/fvalidate/verapdf_validator.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package fvalidate
22

33
import (
44
"errors"
5+
"fmt"
56
"os/exec"
67

78
"github.com/go-logr/logr"
9+
10+
"github.com/artefactual-sdps/preprocessing-sfa/internal/fsutil"
811
)
912

1013
// pdfaPUIDs are the https://www.nationalarchives.gov.uk/pronom/ IDs of the
@@ -48,6 +51,10 @@ func (v *veraPDFValidator) Validate(path string) (string, error) {
4851
return "", nil
4952
}
5053

54+
if !fsutil.FileExists(path) {
55+
return "", fmt.Errorf("validate: file not found: %s", path)
56+
}
57+
5158
cmd := exec.Command(v.cmd, "--recurse", path) // #nosec: G204 -- trusted path.
5259

5360
_, err := cmd.Output()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package fvalidate_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/go-logr/logr"
8+
"gotest.tools/v3/assert"
9+
10+
"github.com/artefactual-sdps/preprocessing-sfa/internal/fvalidate"
11+
)
12+
13+
func TestFormatIDs(t *testing.T) {
14+
t.Parallel()
15+
16+
v := fvalidate.NewVeraPDFValidator("", logr.Discard())
17+
got := v.FormatIDs()
18+
19+
assert.DeepEqual(t, got, []string{
20+
"fmt/95", // PDF/A 1a
21+
"fmt/354", // PDF/A 1b
22+
"fmt/476", // PDF/A 2a
23+
"fmt/477", // PDF/A 2b
24+
"fmt/478", // PDF/A 2u
25+
"fmt/479", // PDF/A 3a
26+
"fmt/480", // PDF/A 3b
27+
"fmt/481", // PDF/A 3u
28+
"fmt/1910", // PDF/A 4
29+
"fmt/1911", // PDF/A 4e
30+
"fmt/1912", // PDF/A 4f
31+
})
32+
}
33+
34+
func TestName(t *testing.T) {
35+
t.Parallel()
36+
37+
v := fvalidate.NewVeraPDFValidator("", logr.Discard())
38+
got := v.Name()
39+
40+
assert.Equal(t, got, "veraPDF")
41+
}
42+
43+
func empty(td string) string {
44+
return ""
45+
}
46+
47+
func TestValidate(t *testing.T) {
48+
t.Parallel()
49+
50+
type test struct {
51+
name string
52+
cmd string
53+
path func(td string) string
54+
want func(td string) string
55+
wantErr func(td string) string
56+
}
57+
for _, tt := range []test{
58+
{
59+
name: "Does nothing when cmd is not set",
60+
path: empty,
61+
},
62+
{
63+
name: "Errors when path doesn't exist",
64+
cmd: "echo",
65+
path: func(td string) string { return td + "/foo" },
66+
wantErr: func(td string) string {
67+
return fmt.Sprintf("validate: file not found: %s/foo", td)
68+
},
69+
},
70+
{
71+
name: "Returns nothing when no error",
72+
cmd: "echo",
73+
path: func(td string) string { return td },
74+
},
75+
} {
76+
t.Run(tt.name, func(t *testing.T) {
77+
t.Parallel()
78+
79+
v := fvalidate.NewVeraPDFValidator(tt.cmd, logr.Discard())
80+
td := t.TempDir()
81+
got, err := v.Validate(tt.path(td))
82+
if tt.wantErr != nil {
83+
assert.Error(t, err, tt.wantErr(td))
84+
return
85+
}
86+
87+
assert.NilError(t, err)
88+
89+
if tt.want != nil {
90+
assert.Equal(t, got, tt.want(td))
91+
} else {
92+
assert.Equal(t, got, "")
93+
}
94+
})
95+
}
96+
}

0 commit comments

Comments
 (0)