-
Notifications
You must be signed in to change notification settings - Fork 143
/
release_test.go
131 lines (118 loc) · 3.03 KB
/
release_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package wkhtmltopdf
import (
"reflect"
"regexp"
"strings"
"testing"
)
// flags which are defaults are not present in the code
var ignoredFlags = []string{
"top",
"enable-javascript",
"background",
"no-debug-javascript",
"enable-external-links",
"outline",
"disable-toc-back-links",
"no-footer-line",
"no-header-line",
"no-print-media-type",
"include-in-outline",
"enable-smart-shrinking",
"resolve-relative-links",
"collate",
"disable-forms",
"disable-plugins",
"stop-slow-scripts",
"images",
"enable-internal-links",
}
// TestIfArgsAreIncluded checks if all args in the help output of wkhtmltopdf are present in the code.
// If checks if the arguments are in any option set, it currently is not smart enough to check if they are
// in the correct options set (like TOC options, page options etc.).
// This test is skipped by default.
func TestIfArgsAreIncluded(t *testing.T) {
t.SkipNow() // remove if you want to run this test
// run wkhtmltopdf with extended-help argument
wkpdf, err := NewPDFGenerator()
if err != nil {
t.Fatal(err)
}
wkpdf.ExtendedHelp.Set(true)
err = wkpdf.Create()
if err != nil {
t.Fatal(err)
}
// cut off the buffer after "Page sizes:', this is where the arguments end
bufStr := wkpdf.Buffer().String()
bufStr = bufStr[:strings.Index(bufStr, "Page sizes:")]
// use simple regex to get all long arument flags
argRegex, err := regexp.Compile(`--[a-z-]+\s`)
if err != nil {
t.Fatal(err)
}
helpFlags := argRegex.FindAllString(bufStr, -1)
// put the flags in a map to remove duplicates
mHelpFlags := make(map[string]bool)
for _, flag := range helpFlags {
mHelpFlags[strings.TrimSpace(strings.TrimPrefix(flag, "--"))] = true
}
t.Logf("found %d arguments in extended help", len(mHelpFlags))
var unusedFlags []string
// for each option, check if it is in the code
global := newGlobalOptions()
headerFooter := newHeaderAndFooterOptions()
outline := newOutlineOptions()
page := newPageOptions()
toc := newTocOptions()
for flag, _ := range mHelpFlags {
// check if flag is ignored
ignored := false
for _, ignoredFlag := range ignoredFlags {
if flag == ignoredFlag {
ignored = true
continue
}
}
if ignored {
continue
}
// check if flag is present
found := false
found = optionsHaveArg(global, flag)
if !found {
found = optionsHaveArg(headerFooter, flag)
}
if !found {
found = optionsHaveArg(outline, flag)
}
if !found {
found = optionsHaveArg(page, flag)
}
if !found {
found = optionsHaveArg(toc, flag)
}
if !found {
unusedFlags = append(unusedFlags, flag)
}
}
if len(unusedFlags) > 0 {
t.Errorf("%d unused flags:\n%s", len(unusedFlags), strings.Join(unusedFlags, "\n"))
}
}
func optionsHaveArg(opts interface{}, arg string) bool {
rv := reflect.Indirect(reflect.ValueOf(opts))
if rv.Kind() != reflect.Struct {
return false
}
for i := 0; i < rv.NumField(); i++ {
optionField := rv.Field(i).FieldByName("option")
if optionField.IsZero() {
continue
}
if optionField.String() == arg {
return true
}
}
return false
}