code.gitea.io/gitea@v1.21.7/models/unit/unit_test.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package unit
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/modules/setting"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestLoadUnitConfig(t *testing.T) {
    15  	t.Run("regular", func(t *testing.T) {
    16  		defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []Type) {
    17  			DisabledRepoUnits = disabledRepoUnits
    18  			DefaultRepoUnits = defaultRepoUnits
    19  			DefaultForkRepoUnits = defaultForkRepoUnits
    20  		}(DisabledRepoUnits, DefaultRepoUnits, DefaultForkRepoUnits)
    21  		defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []string) {
    22  			setting.Repository.DisabledRepoUnits = disabledRepoUnits
    23  			setting.Repository.DefaultRepoUnits = defaultRepoUnits
    24  			setting.Repository.DefaultForkRepoUnits = defaultForkRepoUnits
    25  		}(setting.Repository.DisabledRepoUnits, setting.Repository.DefaultRepoUnits, setting.Repository.DefaultForkRepoUnits)
    26  
    27  		setting.Repository.DisabledRepoUnits = []string{"repo.issues"}
    28  		setting.Repository.DefaultRepoUnits = []string{"repo.code", "repo.releases", "repo.issues", "repo.pulls"}
    29  		setting.Repository.DefaultForkRepoUnits = []string{"repo.releases"}
    30  		assert.NoError(t, LoadUnitConfig())
    31  		assert.Equal(t, []Type{TypeIssues}, DisabledRepoUnits)
    32  		assert.Equal(t, []Type{TypeCode, TypeReleases, TypePullRequests}, DefaultRepoUnits)
    33  		assert.Equal(t, []Type{TypeReleases}, DefaultForkRepoUnits)
    34  	})
    35  	t.Run("invalid", func(t *testing.T) {
    36  		defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []Type) {
    37  			DisabledRepoUnits = disabledRepoUnits
    38  			DefaultRepoUnits = defaultRepoUnits
    39  			DefaultForkRepoUnits = defaultForkRepoUnits
    40  		}(DisabledRepoUnits, DefaultRepoUnits, DefaultForkRepoUnits)
    41  		defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []string) {
    42  			setting.Repository.DisabledRepoUnits = disabledRepoUnits
    43  			setting.Repository.DefaultRepoUnits = defaultRepoUnits
    44  			setting.Repository.DefaultForkRepoUnits = defaultForkRepoUnits
    45  		}(setting.Repository.DisabledRepoUnits, setting.Repository.DefaultRepoUnits, setting.Repository.DefaultForkRepoUnits)
    46  
    47  		setting.Repository.DisabledRepoUnits = []string{"repo.issues", "invalid.1"}
    48  		setting.Repository.DefaultRepoUnits = []string{"repo.code", "invalid.2", "repo.releases", "repo.issues", "repo.pulls"}
    49  		setting.Repository.DefaultForkRepoUnits = []string{"invalid.3", "repo.releases"}
    50  		assert.NoError(t, LoadUnitConfig())
    51  		assert.Equal(t, []Type{TypeIssues}, DisabledRepoUnits)
    52  		assert.Equal(t, []Type{TypeCode, TypeReleases, TypePullRequests}, DefaultRepoUnits)
    53  		assert.Equal(t, []Type{TypeReleases}, DefaultForkRepoUnits)
    54  	})
    55  	t.Run("duplicate", func(t *testing.T) {
    56  		defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []Type) {
    57  			DisabledRepoUnits = disabledRepoUnits
    58  			DefaultRepoUnits = defaultRepoUnits
    59  			DefaultForkRepoUnits = defaultForkRepoUnits
    60  		}(DisabledRepoUnits, DefaultRepoUnits, DefaultForkRepoUnits)
    61  		defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []string) {
    62  			setting.Repository.DisabledRepoUnits = disabledRepoUnits
    63  			setting.Repository.DefaultRepoUnits = defaultRepoUnits
    64  			setting.Repository.DefaultForkRepoUnits = defaultForkRepoUnits
    65  		}(setting.Repository.DisabledRepoUnits, setting.Repository.DefaultRepoUnits, setting.Repository.DefaultForkRepoUnits)
    66  
    67  		setting.Repository.DisabledRepoUnits = []string{"repo.issues", "repo.issues"}
    68  		setting.Repository.DefaultRepoUnits = []string{"repo.code", "repo.releases", "repo.issues", "repo.pulls", "repo.code"}
    69  		setting.Repository.DefaultForkRepoUnits = []string{"repo.releases", "repo.releases"}
    70  		assert.NoError(t, LoadUnitConfig())
    71  		assert.Equal(t, []Type{TypeIssues}, DisabledRepoUnits)
    72  		assert.Equal(t, []Type{TypeCode, TypeReleases, TypePullRequests}, DefaultRepoUnits)
    73  		assert.Equal(t, []Type{TypeReleases}, DefaultForkRepoUnits)
    74  	})
    75  	t.Run("empty_default", func(t *testing.T) {
    76  		defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []Type) {
    77  			DisabledRepoUnits = disabledRepoUnits
    78  			DefaultRepoUnits = defaultRepoUnits
    79  			DefaultForkRepoUnits = defaultForkRepoUnits
    80  		}(DisabledRepoUnits, DefaultRepoUnits, DefaultForkRepoUnits)
    81  		defer func(disabledRepoUnits, defaultRepoUnits, defaultForkRepoUnits []string) {
    82  			setting.Repository.DisabledRepoUnits = disabledRepoUnits
    83  			setting.Repository.DefaultRepoUnits = defaultRepoUnits
    84  			setting.Repository.DefaultForkRepoUnits = defaultForkRepoUnits
    85  		}(setting.Repository.DisabledRepoUnits, setting.Repository.DefaultRepoUnits, setting.Repository.DefaultForkRepoUnits)
    86  
    87  		setting.Repository.DisabledRepoUnits = []string{"repo.issues", "repo.issues"}
    88  		setting.Repository.DefaultRepoUnits = []string{}
    89  		setting.Repository.DefaultForkRepoUnits = []string{"repo.releases", "repo.releases"}
    90  		assert.NoError(t, LoadUnitConfig())
    91  		assert.Equal(t, []Type{TypeIssues}, DisabledRepoUnits)
    92  		assert.ElementsMatch(t, []Type{TypeCode, TypePullRequests, TypeReleases, TypeWiki, TypePackages, TypeProjects}, DefaultRepoUnits)
    93  		assert.Equal(t, []Type{TypeReleases}, DefaultForkRepoUnits)
    94  	})
    95  }