Files

71 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-10-02 04:25:08 +09:00
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
import (
"strings"
"testing"
repo_module "code.gitea.io/gitea/modules/repository"
"github.com/stretchr/testify/assert"
2025-03-10 06:40:37 +08:00
"github.com/stretchr/testify/require"
2024-10-02 04:25:08 +09:00
)
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,
},
}
2025-03-10 06:40:37 +08:00
require.NoError(t, repo_module.LoadRepoConfig())
2024-10-02 04:25:08 +09:00
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{
2025-04-01 12:14:01 +02:00
name: "single license test: " + licenseName,
2024-10-02 04:25:08 +09:00
arg: string(license),
2025-03-10 06:40:37 +08:00
want: []string{licenseName},
2024-10-02 04:25:08 +09:00
})
}
2025-03-10 06:40:37 +08:00
require.NoError(t, InitLicenseClassifier())
2024-10-02 04:25:08 +09:00
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) {
2024-12-15 11:41:29 +01:00
assert.Len(t, result, 3)
2024-10-02 04:25:08 +09:00
assert.Contains(t, result, tests[2].want[0])
assert.Contains(t, result, tests[3].want[0])
assert.Contains(t, result, tests[4].want[0])
})
}