aboutsummaryrefslogtreecommitdiffstats
path: root/services/repository/license_test.go
blob: 9d3e0f36e3654bba7f9bb465209030d4ace1110b (plain)
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
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repository

import (
	"fmt"
	"strings"
	"testing"

	repo_module "code.gitea.io/gitea/modules/repository"

	"github.com/stretchr/testify/assert"
)

func Test_detectLicense(t *testing.T) {
	type DetectLicenseTest struct {
		name string
		arg  string
		want []string
	}

	tests := []DetectLicenseTest{
		{
			name: "empty",
			arg:  "",
			want: nil,
		},
		{
			name: "no detected license",
			arg:  "Copyright (c) 2023 Gitea",
			want: nil,
		},
	}

	repo_module.LoadRepoConfig()
	err := loadLicenseAliases()
	assert.NoError(t, err)
	for _, licenseName := range repo_module.Licenses {
		license, err := repo_module.GetLicense(licenseName, &repo_module.LicenseValues{
			Owner: "Gitea",
			Email: "teabot@gitea.io",
			Repo:  "gitea",
			Year:  "2024",
		})
		assert.NoError(t, err)

		tests = append(tests, DetectLicenseTest{
			name: fmt.Sprintf("single license test: %s", licenseName),
			arg:  string(license),
			want: []string{ConvertLicenseName(licenseName)},
		})
	}

	err = InitLicenseClassifier()
	assert.NoError(t, err)
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			license, err := detectLicense(strings.NewReader(tt.arg))
			assert.NoError(t, err)
			assert.Equal(t, tt.want, license)
		})
	}

	result, err := detectLicense(strings.NewReader(tests[2].arg + tests[3].arg + tests[4].arg))
	assert.NoError(t, err)
	t.Run("multiple licenses test", func(t *testing.T) {
		assert.Len(t, result, 3)
		assert.Contains(t, result, tests[2].want[0])
		assert.Contains(t, result, tests[3].want[0])
		assert.Contains(t, result, tests[4].want[0])
	})
}