5
5
package frontend
6
6
7
7
import (
8
+ "bytes"
8
9
"context"
9
10
"sort"
11
+ "strings"
10
12
"testing"
11
13
12
14
"github.com/google/go-cmp/cmp"
13
- "github.com/google/go-cmp/cmp/cmpopts"
14
15
"github.com/google/safehtml"
15
16
"golang.org/x/pkgsite/internal/licenses"
16
17
"golang.org/x/pkgsite/internal/postgres"
17
18
"golang.org/x/pkgsite/internal/stdlib"
18
19
"golang.org/x/pkgsite/internal/testing/sample"
20
+ "golang.org/x/pkgsite/internal/testing/testhelper"
19
21
)
20
22
21
23
func TestLicenseAnchors (t * testing.T ) {
@@ -43,12 +45,27 @@ func TestLicenseAnchors(t *testing.T) {
43
45
func TestFetchLicensesDetails (t * testing.T ) {
44
46
testModule := sample .Module (sample .ModulePath , "v1.2.3" , "A/B" )
45
47
stdlibModule := sample .Module (stdlib .ModulePath , "v1.13.0" , "cmd/go" )
48
+ crlfPath := "github.com/crlf/module_name"
49
+ crlfModule := sample .Module (crlfPath , "v1.2.3" , "A" )
50
+
46
51
mit := & licenses.Metadata {Types : []string {"MIT" }, FilePath : "LICENSE" }
47
52
bsd := & licenses.Metadata {Types : []string {"BSD-3-Clause" }, FilePath : "A/B/LICENSE" }
48
53
49
- mitLicense := & licenses.License {Metadata : mit }
50
- bsdLicense := & licenses.License {Metadata : bsd }
54
+ mitLicense := & licenses.License {
55
+ Metadata : mit ,
56
+ Contents : []byte (testhelper .MITLicense ),
57
+ }
58
+ mitLicenseCRLF := & licenses.License {
59
+ Metadata : mit ,
60
+ Contents : []byte (strings .ReplaceAll (testhelper .MITLicense , "\n " , "\r \n " )),
61
+ }
62
+ bsdLicense := & licenses.License {
63
+ Metadata : bsd ,
64
+ Contents : []byte (testhelper .BSD0License ),
65
+ }
66
+
51
67
testModule .Licenses = []* licenses.License {bsdLicense , mitLicense }
68
+ crlfModule .Licenses = []* licenses.License {mitLicenseCRLF }
52
69
sort .Slice (testModule .Directories , func (i , j int ) bool {
53
70
return testModule .Directories [i ].Path < testModule .Directories [j ].Path
54
71
})
@@ -68,6 +85,9 @@ func TestFetchLicensesDetails(t *testing.T) {
68
85
if err := testDB .InsertModule (ctx , stdlibModule ); err != nil {
69
86
t .Fatal (err )
70
87
}
88
+ if err := testDB .InsertModule (ctx , crlfModule ); err != nil {
89
+ t .Fatal (err )
90
+ }
71
91
for _ , test := range []struct {
72
92
err error
73
93
name , fullPath , modulePath , version string
@@ -115,6 +135,13 @@ func TestFetchLicensesDetails(t *testing.T) {
115
135
version : stdlibModule .Version ,
116
136
want : stdlibModule .Licenses ,
117
137
},
138
+ {
139
+ name : "module with CRLF line terminators" ,
140
+ fullPath : crlfPath ,
141
+ modulePath : crlfPath ,
142
+ version : crlfModule .Version ,
143
+ want : crlfModule .Licenses ,
144
+ },
118
145
} {
119
146
t .Run (test .name , func (t * testing.T ) {
120
147
wantDetails := & LicensesDetails {Licenses : transformLicenses (
@@ -124,11 +151,15 @@ func TestFetchLicensesDetails(t *testing.T) {
124
151
t .Fatal (err )
125
152
}
126
153
if diff := cmp .Diff (wantDetails , got ,
127
- cmpopts .IgnoreFields (licenses.License {}, "Contents" ),
128
154
cmp .AllowUnexported (safehtml.HTML {}, safehtml.Identifier {}),
129
155
); diff != "" {
130
156
t .Errorf ("mismatch (-want +got):\n %s" , diff )
131
157
}
158
+ for _ , l := range got .Licenses {
159
+ if bytes .Contains (l .Contents , []byte ("\r " )) {
160
+ t .Errorf ("license %s contains \\ r line terminators" , l .Metadata .FilePath )
161
+ }
162
+ }
132
163
})
133
164
}
134
165
}
0 commit comments