mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-16 22:42:42 +00:00
Merge pull request '[v7.0/forgejo] test(util): MockProtect when mocking multiple times' (#3993) from bp-v7.0/forgejo-884b3c0 into v7.0/forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3993 Reviewed-by: Victoria <efertone@noreply.codeberg.org>
This commit is contained in:
commit
8da2acff43
|
@ -34,8 +34,15 @@ func IsNormalPageCompleted(s string) bool {
|
||||||
return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
|
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()) {
|
func MockVariableValue[T any](p *T, v T) (reset func()) {
|
||||||
old := *p
|
old := *p
|
||||||
*p = v
|
*p = v
|
||||||
return func() { *p = old }
|
return func() { *p = old }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use for global variables only
|
||||||
|
func MockProtect[T any](p *T) (reset func()) {
|
||||||
|
old := *p
|
||||||
|
return func() { *p = old }
|
||||||
|
}
|
||||||
|
|
18
modules/test/utils_test.go
Normal file
18
modules/test/utils_test.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in a new issue