| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- // Copyright 2024 The Gitea Authors. All rights reserved.
- // SPDX-License-Identifier: MIT
-
- package integration
-
- import (
- "net/url"
- "strconv"
- "strings"
- "testing"
- "time"
-
- repo_model "code.gitea.io/gitea/models/repo"
- "code.gitea.io/gitea/models/unittest"
- user_model "code.gitea.io/gitea/models/user"
- "code.gitea.io/gitea/modules/indexer/stats"
- "code.gitea.io/gitea/modules/queue"
- repo_service "code.gitea.io/gitea/services/repository"
- files_service "code.gitea.io/gitea/services/repository/files"
- "code.gitea.io/gitea/tests"
-
- "github.com/stretchr/testify/assert"
- )
-
- func TestLinguist(t *testing.T) {
- onGiteaRun(t, func(t *testing.T, _ *url.URL) {
- user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
-
- cppContent := "#include <iostream>\nint main() {\nstd::cout << \"Hello Gitea!\";\nreturn 0;\n}"
- pyContent := "print(\"Hello Gitea!\")"
- phpContent := "<?php\necho 'Hallo Welt';\n?>"
- lockContent := "# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand."
- mdContent := "markdown"
-
- cases := []struct {
- GitAttributesContent string
- FilesToAdd []*files_service.ChangeRepoFile
- ExpectedLanguageOrder []string
- }{
- // case 0
- {
- ExpectedLanguageOrder: []string{},
- },
- // case 1
- {
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "cplusplus.cpp",
- ContentReader: strings.NewReader(cppContent),
- },
- {
- TreePath: "python.py",
- ContentReader: strings.NewReader(pyContent),
- },
- {
- TreePath: "php.php",
- ContentReader: strings.NewReader(phpContent),
- },
- },
- ExpectedLanguageOrder: []string{"C++", "PHP", "Python"},
- },
- // case 2
- {
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: ".cplusplus.cpp",
- ContentReader: strings.NewReader(cppContent),
- },
- {
- TreePath: "python.py",
- ContentReader: strings.NewReader(pyContent),
- },
- {
- TreePath: "vendor/php.php",
- ContentReader: strings.NewReader(phpContent),
- },
- },
- ExpectedLanguageOrder: []string{"Python"},
- },
- // case 3
- {
- GitAttributesContent: "*.cpp linguist-language=Go",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "cplusplus.cpp",
- ContentReader: strings.NewReader(cppContent),
- },
- },
- ExpectedLanguageOrder: []string{"Go"},
- },
- // case 4
- {
- GitAttributesContent: "*.cpp gitlab-language=Go?parent=json",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "cplusplus.cpp",
- ContentReader: strings.NewReader(cppContent),
- },
- },
- ExpectedLanguageOrder: []string{"Go"},
- },
- // case 5
- {
- GitAttributesContent: "*.cpp linguist-language=HTML gitlab-language=Go?parent=json",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "cplusplus.cpp",
- ContentReader: strings.NewReader(cppContent),
- },
- },
- ExpectedLanguageOrder: []string{"HTML"},
- },
- // case 6
- {
- GitAttributesContent: "vendor/** linguist-vendored=false",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "vendor/php.php",
- ContentReader: strings.NewReader(phpContent),
- },
- },
- ExpectedLanguageOrder: []string{"PHP"},
- },
- // case 7
- {
- GitAttributesContent: "*.cpp linguist-vendored=true\n*.py linguist-vendored\nvendor/** -linguist-vendored",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "cplusplus.cpp",
- ContentReader: strings.NewReader(cppContent),
- },
- {
- TreePath: "python.py",
- ContentReader: strings.NewReader(pyContent),
- },
- {
- TreePath: "vendor/php.php",
- ContentReader: strings.NewReader(phpContent),
- },
- },
- ExpectedLanguageOrder: []string{"PHP"},
- },
- // case 8
- {
- GitAttributesContent: "poetry.lock linguist-language=Go",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "poetry.lock",
- ContentReader: strings.NewReader(lockContent),
- },
- },
- ExpectedLanguageOrder: []string{"Go"},
- },
- // case 9
- {
- GitAttributesContent: "poetry.lock linguist-generated=false",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "poetry.lock",
- ContentReader: strings.NewReader(lockContent),
- },
- },
- ExpectedLanguageOrder: []string{"TOML"},
- },
- // case 10
- {
- GitAttributesContent: "*.cpp -linguist-detectable",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "cplusplus.cpp",
- ContentReader: strings.NewReader(cppContent),
- },
- },
- ExpectedLanguageOrder: []string{},
- },
- // case 11
- {
- GitAttributesContent: "*.md linguist-detectable",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "test.md",
- ContentReader: strings.NewReader(mdContent),
- },
- },
- ExpectedLanguageOrder: []string{"Markdown"},
- },
- // case 12
- {
- GitAttributesContent: "test2.md linguist-detectable",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "cplusplus.cpp",
- ContentReader: strings.NewReader(cppContent),
- },
- {
- TreePath: "test.md",
- ContentReader: strings.NewReader(mdContent),
- },
- {
- TreePath: "test2.md",
- ContentReader: strings.NewReader(mdContent),
- },
- },
- ExpectedLanguageOrder: []string{"C++", "Markdown"},
- },
- // case 13
- {
- GitAttributesContent: "README.md linguist-documentation=false",
- FilesToAdd: []*files_service.ChangeRepoFile{
- {
- TreePath: "README.md",
- ContentReader: strings.NewReader(mdContent),
- },
- },
- ExpectedLanguageOrder: []string{"Markdown"},
- },
- }
-
- for i, c := range cases {
- t.Run("Case-"+strconv.Itoa(i), func(t *testing.T) {
- defer tests.PrintCurrentTest(t)()
- repo, err := repo_service.CreateRepository(t.Context(), user, user, repo_service.CreateRepoOptions{
- Name: "linguist-test-" + strconv.Itoa(i),
- })
- assert.NoError(t, err)
-
- files := []*files_service.ChangeRepoFile{
- {
- TreePath: ".gitattributes",
- ContentReader: strings.NewReader(c.GitAttributesContent),
- },
- }
- files = append(files, c.FilesToAdd...)
- for _, f := range files {
- f.Operation = "create"
- }
-
- _, err = files_service.ChangeRepoFiles(t.Context(), repo, user, &files_service.ChangeRepoFilesOptions{
- Files: files,
- OldBranch: repo.DefaultBranch,
- NewBranch: repo.DefaultBranch,
- })
- assert.NoError(t, err)
-
- assert.NoError(t, stats.UpdateRepoIndexer(repo))
- assert.NoError(t, queue.GetManager().FlushAll(t.Context(), 10*time.Second))
-
- stats, err := repo_model.GetTopLanguageStats(t.Context(), repo, len(c.FilesToAdd))
- assert.NoError(t, err)
-
- languages := make([]string, 0, len(stats))
- for _, s := range stats {
- languages = append(languages, s.Language)
- }
- assert.Equal(t, c.ExpectedLanguageOrder, languages, "case %d: unexpected language stats", i)
- })
- }
- })
- }
|