chore: loosen assertions in SetOrderBeginBlockers() and SetOrderEndBlockers() (#14415)
This commit is contained in:
+23
-7
@@ -293,32 +293,42 @@ func NewManagerFromMap(moduleMap map[string]appmodule.AppModule) *Manager {
|
||||
|
||||
// SetOrderInitGenesis sets the order of init genesis calls
|
||||
func (m *Manager) SetOrderInitGenesis(moduleNames ...string) {
|
||||
m.assertNoForgottenModules("SetOrderInitGenesis", moduleNames)
|
||||
m.assertNoForgottenModules("SetOrderInitGenesis", moduleNames, nil)
|
||||
m.OrderInitGenesis = moduleNames
|
||||
}
|
||||
|
||||
// SetOrderExportGenesis sets the order of export genesis calls
|
||||
func (m *Manager) SetOrderExportGenesis(moduleNames ...string) {
|
||||
m.assertNoForgottenModules("SetOrderExportGenesis", moduleNames)
|
||||
m.assertNoForgottenModules("SetOrderExportGenesis", moduleNames, nil)
|
||||
m.OrderExportGenesis = moduleNames
|
||||
}
|
||||
|
||||
// SetOrderBeginBlockers sets the order of set begin-blocker calls
|
||||
func (m *Manager) SetOrderBeginBlockers(moduleNames ...string) {
|
||||
m.assertNoForgottenModules("SetOrderBeginBlockers", moduleNames)
|
||||
m.assertNoForgottenModules("SetOrderBeginBlockers", moduleNames,
|
||||
func(moduleName string) bool {
|
||||
module := m.Modules[moduleName]
|
||||
_, hasBeginBlock := module.(BeginBlockAppModule)
|
||||
return !hasBeginBlock
|
||||
})
|
||||
m.OrderBeginBlockers = moduleNames
|
||||
}
|
||||
|
||||
// SetOrderEndBlockers sets the order of set end-blocker calls
|
||||
func (m *Manager) SetOrderEndBlockers(moduleNames ...string) {
|
||||
m.assertNoForgottenModules("SetOrderEndBlockers", moduleNames)
|
||||
m.assertNoForgottenModules("SetOrderEndBlockers", moduleNames,
|
||||
func(moduleName string) bool {
|
||||
module := m.Modules[moduleName]
|
||||
_, hasEndBlock := module.(EndBlockAppModule)
|
||||
return !hasEndBlock
|
||||
})
|
||||
m.OrderEndBlockers = moduleNames
|
||||
}
|
||||
|
||||
// SetOrderMigrations sets the order of migrations to be run. If not set
|
||||
// then migrations will be run with an order defined in `DefaultMigrationsOrder`.
|
||||
func (m *Manager) SetOrderMigrations(moduleNames ...string) {
|
||||
m.assertNoForgottenModules("SetOrderMigrations", moduleNames)
|
||||
m.assertNoForgottenModules("SetOrderMigrations", moduleNames, nil)
|
||||
m.OrderMigrations = moduleNames
|
||||
}
|
||||
|
||||
@@ -425,13 +435,19 @@ func (m *Manager) checkModulesExists(moduleName []string) error {
|
||||
|
||||
// assertNoForgottenModules checks that we didn't forget any modules in the
|
||||
// SetOrder* functions.
|
||||
func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []string) {
|
||||
// `pass` is a closure which allows one to omit modules from `moduleNames`. If you provide non-nil `pass` and it returns true, the module would not be subject of the assertion.
|
||||
func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []string, pass func(moduleName string) bool) {
|
||||
ms := make(map[string]bool)
|
||||
for _, m := range moduleNames {
|
||||
ms[m] = true
|
||||
}
|
||||
var missing []string
|
||||
for m := range m.Modules {
|
||||
m := m
|
||||
if pass != nil && pass(m) {
|
||||
continue
|
||||
}
|
||||
|
||||
if !ms[m] {
|
||||
missing = append(missing, m)
|
||||
}
|
||||
@@ -439,7 +455,7 @@ func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []
|
||||
if len(missing) != 0 {
|
||||
sort.Strings(missing)
|
||||
panic(fmt.Sprintf(
|
||||
"%s: all modules must be defined when setting %s, missing: %v", setOrderFnName, setOrderFnName, missing))
|
||||
"all modules must be defined when setting %s, missing: %v", setOrderFnName, missing))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,16 +23,19 @@ func (s TestSuite) TestAssertNoForgottenModules() { //nolint:govet
|
||||
name string
|
||||
positive bool
|
||||
modules []string
|
||||
pass func(string) bool
|
||||
}{
|
||||
{"same modules", true, []string{"a", "b"}},
|
||||
{"more modules", true, []string{"a", "b", "c"}},
|
||||
{"less modules", false, []string{"a"}, nil},
|
||||
{"same modules", true, []string{"a", "b"}, nil},
|
||||
{"more modules", true, []string{"a", "b", "c"}, nil},
|
||||
{"pass module b", true, []string{"a"}, func(moduleName string) bool { return moduleName == "b" }},
|
||||
}
|
||||
|
||||
for _, tc := range tcs {
|
||||
if tc.positive {
|
||||
m.assertNoForgottenModules("x", tc.modules)
|
||||
m.assertNoForgottenModules("x", tc.modules, tc.pass)
|
||||
} else {
|
||||
s.Panics(func() { m.assertNoForgottenModules("x", tc.modules) })
|
||||
s.Panics(func() { m.assertNoForgottenModules("x", tc.modules, tc.pass) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user