gitea源码

license_test.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "strings"
  6. "testing"
  7. repo_module "code.gitea.io/gitea/modules/repository"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func Test_detectLicense(t *testing.T) {
  12. type DetectLicenseTest struct {
  13. name string
  14. arg string
  15. want []string
  16. }
  17. tests := []DetectLicenseTest{
  18. {
  19. name: "empty",
  20. arg: "",
  21. want: nil,
  22. },
  23. {
  24. name: "no detected license",
  25. arg: "Copyright (c) 2023 Gitea",
  26. want: nil,
  27. },
  28. }
  29. require.NoError(t, repo_module.LoadRepoConfig())
  30. for _, licenseName := range repo_module.Licenses {
  31. license, err := repo_module.GetLicense(licenseName, &repo_module.LicenseValues{
  32. Owner: "Gitea",
  33. Email: "teabot@gitea.io",
  34. Repo: "gitea",
  35. Year: "2024",
  36. })
  37. assert.NoError(t, err)
  38. tests = append(tests, DetectLicenseTest{
  39. name: "single license test: " + licenseName,
  40. arg: string(license),
  41. want: []string{licenseName},
  42. })
  43. }
  44. require.NoError(t, InitLicenseClassifier())
  45. for _, tt := range tests {
  46. t.Run(tt.name, func(t *testing.T) {
  47. license, err := detectLicense(strings.NewReader(tt.arg))
  48. assert.NoError(t, err)
  49. assert.Equal(t, tt.want, license)
  50. })
  51. }
  52. result, err := detectLicense(strings.NewReader(tests[2].arg + tests[3].arg + tests[4].arg))
  53. assert.NoError(t, err)
  54. t.Run("multiple licenses test", func(t *testing.T) {
  55. assert.Len(t, result, 3)
  56. assert.Contains(t, result, tests[2].want[0])
  57. assert.Contains(t, result, tests[3].want[0])
  58. assert.Contains(t, result, tests[4].want[0])
  59. })
  60. }