main 80d5067

license_test.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5import (
6	"context"
7	"errors"
8	"io"
9	"strings"
10	"testing"
11
12	"github.com/stretchr/testify/assert"
13	"github.com/stretchr/testify/mock"
14	"github.com/stretchr/testify/require"
15
16	"bnema.dev/ermite/internal/domain"
17	"bnema.dev/ermite/internal/ports"
18	"bnema.dev/ermite/internal/ports/mocks"
19)
20
21func TestLicenseID(t *testing.T) {
22	t.Parallel()
23	for _, tc := range []struct{ text, want string }{
24		{"// SPDX-License-Identifier: MIT", "MIT"},
25		{"<!-- SPDX-License-Identifier: Apache-2.0 OR MIT -->", "Apache-2.0 OR MIT"},
26		{"/* SPDX-License-Identifier: GPL-3.0-or-later */", "GPL-3.0-or-later"},
27		{"GNU AFFERO GENERAL PUBLIC LICENSE\nVersion 3, 19 November 2007", "AGPL-3.0"},
28		{"GNU GENERAL PUBLIC LICENSE Version 3" + strings.Repeat(" padding", 100) + "GNU Affero General Public License version 3", "GPL-3.0"},
29		{"GNU GENERAL PUBLIC LICENSE Version 2", "GPL-2.0"},
30		{"GNU LESSER GENERAL PUBLIC LICENSE Version 2.1", "LGPL-2.1"},
31		{"GNU LESSER GENERAL PUBLIC LICENSE Version 3", "LGPL-3.0"},
32		{"Apache License\nVersion 2.0, January 2004", "Apache-2.0"},
33		{"Mozilla Public License Version 2.0", "MPL-2.0"},
34		{"Permission is hereby granted, free of charge\nTHE SOFTWARE IS PROVIDED \"AS IS\"", "MIT-0"},
35		{"Redistribution and use in source and binary forms ... neither the name", "BSD-3-Clause"},
36		{"Redistribution and use in source and binary forms ... this list of conditions", "BSD-2-Clause"},
37		{"Redistribution and use in source and binary forms ... all advertising materials ... neither the name", "Other"},
38		{"Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee", "ISC"},
39		{"Custom license mentioning MIT or Apache-2.0", "Other"},
40		{"", "Other"},
41	} {
42		t.Run(tc.want+tc.text[:min(20, len(tc.text))], func(t *testing.T) { t.Parallel(); assert.Equal(t, tc.want, licenseID(tc.text)) })
43	}
44}
45
46func TestRepositoryLicenseCache(t *testing.T) {
47	t.Parallel()
48	for _, filename := range append(append([]string{}, licenseNames...), "LiCeNsE.MD", "missing", "directory") {
49		t.Run(filename, func(t *testing.T) {
50			t.Parallel()
51			browse := mocks.NewBrowseService(t)
52			repo := repoName(t, "proj")
53			head := strings.Repeat("a", 40)
54			entry := domain.TreeEntry{Name: filename, Path: filename, Kind: domain.EntryFile}
55			if filename == "directory" {
56				entry.Name, entry.Kind = "LICENSE", domain.EntryDirectory
57			}
58			browse.EXPECT().GetTree(mock.Anything, mock.Anything, repo, mock.Anything, mock.Anything, false).Return(domain.Tree{Entries: []domain.TreeEntry{entry}}, nil).Once()
59			found := filename != "missing" && filename != "directory"
60			if found {
61				browse.EXPECT().OpenBlob(mock.Anything, mock.Anything, repo, mock.Anything, mock.Anything).Return(domain.BlobInfo{}, io.NopCloser(strings.NewReader("SPDX-License-Identifier: MIT")), nil).Once()
62			}
63			srv := newTestServer(t, Config{Repos: reposServing(t, publicRepo(t, "proj")), Browse: browse})
64			for range 2 {
65				badge := srv.repositoryLicense(t.Context(), repo, head)
66				if !found {
67					assert.Nil(t, badge)
68					continue
69				}
70				require.NotNil(t, badge)
71				assert.Equal(t, "MIT", badge.ID)
72				assert.Equal(t, "/proj/blob/"+head+"/"+filename, badge.Href)
73			}
74		})
75	}
76}
77
78func TestRepositoryLicenseRetriesErrorsAndNewHeads(t *testing.T) {
79	t.Parallel()
80	browse := mocks.NewBrowseService(t)
81	repo := repoName(t, "proj")
82	browse.EXPECT().GetTree(mock.Anything, mock.Anything, repo, mock.Anything, mock.Anything, false).Return(domain.Tree{}, errors.New("unavailable")).Once()
83	browse.EXPECT().GetTree(mock.Anything, mock.Anything, repo, mock.Anything, mock.Anything, false).Return(domain.Tree{}, nil).Twice()
84	srv := newTestServer(t, Config{Repos: reposServing(t, publicRepo(t, "proj")), Browse: browse})
85	for _, head := range []string{strings.Repeat("a", 40), strings.Repeat("a", 40), strings.Repeat("b", 40)} {
86		assert.Nil(t, srv.repositoryLicense(context.Background(), repo, head))
87	}
88}
89
90func TestSummaryLicense(t *testing.T) {
91	t.Parallel()
92	assert.NotContains(t, renderRail(t, SummaryPage{}), "<dt>license</dt>")
93	out := renderRail(t, SummaryPage{License: &LicenseBadge{ID: "MIT", Href: "/proj/blob/main/LICENSE"}})
94	assert.Contains(t, out, `<dt>license</dt><dd><a href="/proj/blob/main/LICENSE">MIT</a></dd>`)
95}
96
97func TestHomeLicenseFromDefaultHead(t *testing.T) {
98	t.Parallel()
99	for _, binary := range []bool{false, true} {
100		name := "unknown text"
101		if binary {
102			name = "binary"
103		}
104		t.Run(name, func(t *testing.T) {
105			t.Parallel()
106			browse := mocks.NewBrowseService(t)
107			repo := pushedRepo(t, "proj")
108			commits := homeCommits(t, 1)
109			head := commits[0].ID.String()
110			revision, err := domain.ParseRevision(head)
111			require.NoError(t, err)
112			browse.EXPECT().ListCommits(mock.Anything, mock.Anything, repo.Name, mock.Anything).Return(ports.HistoryPage{Commits: commits}, nil).Once()
113			browse.EXPECT().RepoStats(mock.Anything, mock.Anything, repo.Name, commits[0].ID, mock.Anything).Return(ports.RepoStats{}, nil).Once()
114			browse.EXPECT().GetTree(mock.Anything, mock.Anything, repo.Name, revision, mock.Anything, false).Return(domain.Tree{Entries: []domain.TreeEntry{{Name: "COPYING", Path: "COPYING", Kind: domain.EntryFile}}}, nil).Once()
115			browse.EXPECT().OpenBlob(mock.Anything, mock.Anything, repo.Name, revision, mock.Anything).Return(domain.BlobInfo{Binary: binary}, io.NopCloser(strings.NewReader("A custom license")), nil).Once()
116			srv := newTestServer(t, Config{Repos: reposServing(t, repo), Browse: browse})
117			row := srv.homeRowOf(t.Context(), repo, fixedNow, false)
118			require.NotNil(t, row.License)
119			assert.Equal(t, "Other", row.License.ID)
120			assert.Contains(t, renderRow(t, row), `href="/proj/blob/`+head+`/COPYING" title="License: Other">Other</a>`)
121			assert.Contains(t, renderRail(t, SummaryPage{License: row.License}), `<dt>license</dt><dd><a href="/proj/blob/`+head+`/COPYING">Other</a></dd>`)
122		})
123	}
124}
125
126func TestLicenseFullTexts(t *testing.T) {
127	t.Parallel()
128	const grant = `Copyright (c) 2026 Example
129
130Permission is hereby granted, free of charge, to any person obtaining a copy
131of this software and associated documentation files (the "Software"), to deal
132in the Software without restriction, including without limitation the rights
133to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
134copies of the Software, and to permit persons to whom the Software is
135furnished to do so.`
136	const notice = `The above copyright notice and this permission notice shall be included in
137all copies or substantial portions of the Software.`
138	const warranty = `THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
139IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
140FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
141AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
142LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
143OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
144THE SOFTWARE.`
145	const bsd = `Copyright (c) 2026 Example. All rights reserved.
146Redistribution and use in source and binary forms, with or without
147modification, are permitted provided that the following conditions are met:
1481. Redistributions of source code must retain the above copyright notice,
149   this list of conditions and the following disclaimer.
1502. Redistributions in binary form must reproduce the above copyright notice,
151   this list of conditions and the following disclaimer in the documentation
152   and/or other materials provided with the distribution.`
153	const endorsement = `3. Neither the name of the copyright holder nor the names of its
154   contributors may be used to endorse or promote products derived from
155   this software without specific prior written permission.`
156	const disclaimer = `THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
157AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
158IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
159ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
160LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
161CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
162SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
163INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
164CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
165ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
166POSSIBILITY OF SUCH DAMAGE.`
167	const gnu = `Copyright (C) 2007 Free Software Foundation, Inc.
168Everyone is permitted to copy and distribute verbatim copies of this license
169document, but changing it is not allowed.
170
171Preamble
172The licenses for most software and other practical works are designed to
173take away your freedom to share and change the works. By contrast, the GNU
174General Public License is intended to guarantee your freedom to share and
175change all versions of a program--to make sure it remains free software
176for all its users.`
177	for _, tc := range []struct{ name, text, want string }{
178		{"MIT", "MIT License\n" + grant + "\n" + notice + "\n" + warranty, "MIT"},
179		{"MIT-0", "MIT No Attribution\n" + grant + "\n" + warranty, "MIT-0"},
180		{"BSD-2", bsd + "\n" + disclaimer, "BSD-2-Clause"},
181		{"BSD-3", bsd + "\n" + endorsement + "\n" + disclaimer, "BSD-3-Clause"},
182		{"GPL", "GNU GENERAL PUBLIC LICENSE\nVersion 3, 29 June 2007\n" + gnu + strings.Repeat("\nTerms and conditions for copying, distribution and modification.", 10) + "\nUse with the GNU Affero General Public License version 3.", "GPL-3.0"},
183		{"LGPL", "GNU LESSER GENERAL PUBLIC LICENSE\nVersion 3, 29 June 2007\n" + gnu + "\nThis version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by additional permissions.", "LGPL-3.0"},
184		{"AGPL", "GNU AFFERO GENERAL PUBLIC LICENSE\nVersion 3, 19 November 2007\n" + gnu + "\nNotwithstanding any other provision of this License, if you modify the Program, your modified version must prominently offer all users interacting with it remotely through a computer network an opportunity to receive the Corresponding Source.", "AGPL-3.0"},
185	} {
186		t.Run(tc.name, func(t *testing.T) { t.Parallel(); assert.Equal(t, tc.want, licenseID(tc.text)) })
187	}
188}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 3

Tags 44

↑↓ moveenter openesc close