From 884b3c0f4bbdccb3d2dcb76ff323697375cda5ae Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Wed, 29 May 2024 19:47:15 +0200 Subject: [PATCH] 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 --- modules/test/utils.go | 7 +++++++ modules/test/utils_test.go | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 modules/test/utils_test.go diff --git a/modules/test/utils.go b/modules/test/utils.go index 4a0c2f1b3b..3d884b6cbe 100644 --- a/modules/test/utils.go +++ b/modules/test/utils.go @@ -34,8 +34,15 @@ func IsNormalPageCompleted(s string) bool { return strings.Contains(s, `