1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 12:54:53 -05:00

Merge pull request 'test(util): MockProtect when mocking multiple times' (#3959) from earl-warren/forgejo:wip-mock-protect into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3959
Reviewed-by: Mai-Lapyst <mai-lapyst@noreply.codeberg.org>
Reviewed-by: Victoria <efertone@noreply.codeberg.org>
This commit is contained in:
Earl Warren 2024-06-02 14:27:35 +00:00
commit 16e8dfe417
2 changed files with 25 additions and 0 deletions

View file

@ -34,8 +34,15 @@ func IsNormalPageCompleted(s string) bool {
return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
}
// use for global variables only
func MockVariableValue[T any](p *T, v T) (reset func()) {
old := *p
*p = v
return func() { *p = old }
}
// use for global variables only
func MockProtect[T any](p *T) (reset func()) {
old := *p
return func() { *p = old }
}

View file

@ -0,0 +1,18 @@
// Copyright 2024 The Forgejo Authors
// SPDX-License-Identifier: MIT
package test
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMockProtect(t *testing.T) {
mockable := "original"
restore := MockProtect(&mockable)
mockable = "tainted"
restore()
assert.Equal(t, "original", mockable)
}