mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-28 09:12:13 +01:00
138942c09e
- Go's deadcode eliminator is quite simple, if you put a public function in a package `aa/bb` that is used only by tests, it would still be built if package `aa/bb` was imported. This means that if such functions use libraries relevant only to tests that those libraries would still be be built and increase the binary size of a Go binary. - This is also the case with Forgejo, `models/migrations/base/tests.go` contained functions exclusively used by tests which (skipping some steps here) imports https://github.com/ClickHouse/clickhouse-go, which is 2MiB. The `code.gitea.io/gitea/models/migrations/base` package is imported by `cmd/doctor` and thus the code of the clickhouse library is also built and included in the Forgejo binary, although entirely unused and not reachable. - This patch moves the test-related functions to their own package, so Go's deadcode eliminator knows not to build the test-related functions and thus reduces the size of the Forgejo binary. - It is not possible to move this to a `_test.go` file because Go does not allow importing functions from such files, so any test helper function must be in a non-test package and file. - Reduction of size (built with `TAGS="sqlite sqlite_unlock_notify" make build`): - Before: 95912040 bytes (92M) - After: 92306888 bytes (89M)
110 lines
2.8 KiB
Go
110 lines
2.8 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package v1_20 //nolint
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
migration_tests "code.gitea.io/gitea/models/migrations/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type testCase struct {
|
|
Old OldAccessTokenScope
|
|
New AccessTokenScope
|
|
}
|
|
|
|
func createOldTokenScope(scopes ...OldAccessTokenScope) OldAccessTokenScope {
|
|
s := make([]string, 0, len(scopes))
|
|
for _, os := range scopes {
|
|
s = append(s, string(os))
|
|
}
|
|
return OldAccessTokenScope(strings.Join(s, ","))
|
|
}
|
|
|
|
func createNewTokenScope(scopes ...AccessTokenScope) AccessTokenScope {
|
|
s := make([]string, 0, len(scopes))
|
|
for _, os := range scopes {
|
|
s = append(s, string(os))
|
|
}
|
|
return AccessTokenScope(strings.Join(s, ","))
|
|
}
|
|
|
|
func Test_ConvertScopedAccessTokens(t *testing.T) {
|
|
tests := []testCase{
|
|
{
|
|
createOldTokenScope(OldAccessTokenScopeRepo, OldAccessTokenScopeUserFollow),
|
|
createNewTokenScope(AccessTokenScopeWriteRepository, AccessTokenScopeWriteUser),
|
|
},
|
|
{
|
|
createOldTokenScope(OldAccessTokenScopeUser, OldAccessTokenScopeWritePackage, OldAccessTokenScopeSudo),
|
|
createNewTokenScope(AccessTokenScopeWriteAdmin, AccessTokenScopeWritePackage, AccessTokenScopeWriteUser),
|
|
},
|
|
{
|
|
createOldTokenScope(),
|
|
createNewTokenScope(),
|
|
},
|
|
{
|
|
createOldTokenScope(OldAccessTokenScopeReadGPGKey, OldAccessTokenScopeReadOrg, OldAccessTokenScopeAll),
|
|
createNewTokenScope(AccessTokenScopeAll),
|
|
},
|
|
{
|
|
createOldTokenScope(OldAccessTokenScopeReadGPGKey, "invalid"),
|
|
createNewTokenScope("invalid", AccessTokenScopeReadUser),
|
|
},
|
|
}
|
|
|
|
// add a test for each individual mapping
|
|
for oldScope, newScope := range accessTokenScopeMap {
|
|
tests = append(tests, testCase{
|
|
oldScope,
|
|
createNewTokenScope(newScope...),
|
|
})
|
|
}
|
|
|
|
x, deferable := migration_tests.PrepareTestEnv(t, 0, new(AccessToken))
|
|
defer deferable()
|
|
if x == nil || t.Failed() {
|
|
t.Skip()
|
|
return
|
|
}
|
|
|
|
// verify that no fixtures were loaded
|
|
count, err := x.Count(&AccessToken{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), count)
|
|
|
|
for _, tc := range tests {
|
|
_, err = x.Insert(&AccessToken{
|
|
Scope: string(tc.Old),
|
|
})
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
// migrate the scopes
|
|
err = ConvertScopedAccessTokens(x)
|
|
assert.NoError(t, err)
|
|
|
|
// migrate the scopes again (migration should be idempotent)
|
|
err = ConvertScopedAccessTokens(x)
|
|
assert.NoError(t, err)
|
|
|
|
tokens := make([]AccessToken, 0)
|
|
err = x.Find(&tokens)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, len(tests), len(tokens))
|
|
|
|
// sort the tokens (insertion order by auto-incrementing primary key)
|
|
sort.Slice(tokens, func(i, j int) bool {
|
|
return tokens[i].ID < tokens[j].ID
|
|
})
|
|
|
|
// verify that the converted scopes are equal to the expected test result
|
|
for idx, newToken := range tokens {
|
|
assert.Equal(t, string(tests[idx].New), newToken.Scope)
|
|
}
|
|
}
|