mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-06-24 20:00:02 +02:00
test(util): MockProtect when mocking multiple times
It is fine to use MockVariableValue to change a setting such as:
defer test.MockVariableValue(&setting.Mirror.Enabled, true)()
But when testing for errors and mocking a function, multiple variants
of the functions will be used, not just one. MockProtect a function
will make sure that when the test fails it always restores a sane
version of the function. For instance:
defer test.MockProtect(&mirror_service.AddPushMirrorRemote)()
mirror_service.AddPushMirrorRemote = mockOne
do some tests that may fail
mirror_service.AddPushMirrorRemote = mockTwo
do more tests that may fail
(cherry picked from commit 884b3c0f4b
)
This commit is contained in:
parent
8fc16691e0
commit
35d5ef0649
2 changed files with 25 additions and 0 deletions
|
@ -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 }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue