[PORT] Refactor tests to prevent from unnecessary preparations (gitea#32398)

Some preparations are only used by a few tests, so to make the tests fast, they should only be prepared when they are used.

By the way, this PR splits PrepareTestEnv into small functions to make it simple.

---

Conflict resolution: Mostly magical and just re-pasting the code into
the right places.
Done differently: use `require.NoError` instead of `assert.NoError`.

(cherry picked from commit ec2d1593c269e06655525deb96f74b8094221b6f)
This commit is contained in:
wxiaoguang 2024-11-01 23:18:29 +08:00 committed by Gusted
parent 019083ed5a
commit 3c4153b195
No known key found for this signature in database
GPG key ID: FD821B732837125F
7 changed files with 96 additions and 59 deletions

View file

@ -275,3 +275,16 @@ func TestGeneratingEd25519Keypair(t *testing.T) {
assert.EqualValues(t, testPublicKey, string(publicKey))
assert.EqualValues(t, testPrivateKey, string(privateKey))
}
func TestOptionalArg(t *testing.T) {
foo := func(other any, optArg ...int) int {
return util.OptionalArg(optArg)
}
bar := func(other any, optArg ...int) int {
return util.OptionalArg(optArg, 42)
}
assert.Equal(t, 0, foo(nil))
assert.Equal(t, 100, foo(nil, 100))
assert.Equal(t, 42, bar(nil))
assert.Equal(t, 100, bar(nil, 100))
}