2023-04-03 16:42:38 +08:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
2026-03-07 02:03:22 +01:00
|
|
|
package unit_test
|
2023-04-03 16:42:38 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-03-07 02:03:22 +01:00
|
|
|
unit_model "forgejo.org/models/unit"
|
|
|
|
|
"forgejo.org/models/unit/tests"
|
2025-03-27 19:40:14 +00:00
|
|
|
"forgejo.org/modules/setting"
|
2023-04-03 16:42:38 +08:00
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 19:41:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-04-03 16:42:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestLoadUnitConfig(t *testing.T) {
|
|
|
|
|
t.Run("regular", func(t *testing.T) {
|
2026-03-07 02:03:22 +01:00
|
|
|
defer tests.SaveUnits()()
|
2023-05-06 17:39:06 +08:00
|
|
|
|
2023-04-03 16:42:38 +08:00
|
|
|
setting.Repository.DisabledRepoUnits = []string{"repo.issues"}
|
|
|
|
|
setting.Repository.DefaultRepoUnits = []string{"repo.code", "repo.releases", "repo.issues", "repo.pulls"}
|
|
|
|
|
setting.Repository.DefaultForkRepoUnits = []string{"repo.releases"}
|
2026-03-07 02:03:22 +01:00
|
|
|
require.NoError(t, unit_model.LoadUnitConfig())
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeIssues}, unit_model.DisabledRepoUnitsGet())
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeCode, unit_model.TypeReleases, unit_model.TypePullRequests}, unit_model.DefaultRepoUnits)
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeReleases}, unit_model.DefaultForkRepoUnits)
|
2023-04-03 16:42:38 +08:00
|
|
|
})
|
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
2026-03-07 02:03:22 +01:00
|
|
|
defer tests.SaveUnits()()
|
2023-05-06 17:39:06 +08:00
|
|
|
|
2023-04-03 16:42:38 +08:00
|
|
|
setting.Repository.DisabledRepoUnits = []string{"repo.issues", "invalid.1"}
|
|
|
|
|
setting.Repository.DefaultRepoUnits = []string{"repo.code", "invalid.2", "repo.releases", "repo.issues", "repo.pulls"}
|
|
|
|
|
setting.Repository.DefaultForkRepoUnits = []string{"invalid.3", "repo.releases"}
|
2026-03-07 02:03:22 +01:00
|
|
|
require.NoError(t, unit_model.LoadUnitConfig())
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeIssues}, unit_model.DisabledRepoUnitsGet())
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeCode, unit_model.TypeReleases, unit_model.TypePullRequests}, unit_model.DefaultRepoUnits)
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeReleases}, unit_model.DefaultForkRepoUnits)
|
2023-04-03 16:42:38 +08:00
|
|
|
})
|
|
|
|
|
t.Run("duplicate", func(t *testing.T) {
|
2026-03-07 02:03:22 +01:00
|
|
|
defer tests.SaveUnits()()
|
2023-05-06 17:39:06 +08:00
|
|
|
|
2023-04-03 16:42:38 +08:00
|
|
|
setting.Repository.DisabledRepoUnits = []string{"repo.issues", "repo.issues"}
|
|
|
|
|
setting.Repository.DefaultRepoUnits = []string{"repo.code", "repo.releases", "repo.issues", "repo.pulls", "repo.code"}
|
|
|
|
|
setting.Repository.DefaultForkRepoUnits = []string{"repo.releases", "repo.releases"}
|
2026-03-07 02:03:22 +01:00
|
|
|
require.NoError(t, unit_model.LoadUnitConfig())
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeIssues}, unit_model.DisabledRepoUnitsGet())
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeCode, unit_model.TypeReleases, unit_model.TypePullRequests}, unit_model.DefaultRepoUnits)
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeReleases}, unit_model.DefaultForkRepoUnits)
|
2023-05-06 17:39:06 +08:00
|
|
|
})
|
|
|
|
|
t.Run("empty_default", func(t *testing.T) {
|
2026-03-07 02:03:22 +01:00
|
|
|
defer tests.SaveUnits()()
|
2023-05-06 17:39:06 +08:00
|
|
|
|
|
|
|
|
setting.Repository.DisabledRepoUnits = []string{"repo.issues", "repo.issues"}
|
|
|
|
|
setting.Repository.DefaultRepoUnits = []string{}
|
|
|
|
|
setting.Repository.DefaultForkRepoUnits = []string{"repo.releases", "repo.releases"}
|
2026-03-07 02:03:22 +01:00
|
|
|
require.NoError(t, unit_model.LoadUnitConfig())
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeIssues}, unit_model.DisabledRepoUnitsGet())
|
|
|
|
|
assert.ElementsMatch(t, []unit_model.Type{unit_model.TypeCode, unit_model.TypePullRequests, unit_model.TypeReleases, unit_model.TypeWiki, unit_model.TypePackages, unit_model.TypeProjects, unit_model.TypeActions}, unit_model.DefaultRepoUnits)
|
|
|
|
|
assert.Equal(t, []unit_model.Type{unit_model.TypeReleases}, unit_model.DefaultForkRepoUnits)
|
2023-04-03 16:42:38 +08:00
|
|
|
})
|
|
|
|
|
}
|