From e5c933674350825d60c347a91ceae10778161a18 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 15 Sep 2022 12:29:18 -0400 Subject: [PATCH] feat(wasmer): Add `SetTestVersion` method to `Config` struct (#2823) --- lib/runtime/wasmer/config.go | 13 ++++++++++++ lib/runtime/wasmer/config_test.go | 35 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 lib/runtime/wasmer/config_test.go diff --git a/lib/runtime/wasmer/config.go b/lib/runtime/wasmer/config.go index 20dc703e1e..46b484f9d9 100644 --- a/lib/runtime/wasmer/config.go +++ b/lib/runtime/wasmer/config.go @@ -4,6 +4,8 @@ package wasmer import ( + "testing" + "github.com/ChainSafe/gossamer/internal/log" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/keystore" @@ -23,3 +25,14 @@ type Config struct { CodeHash common.Hash testVersion *runtime.Version } + +// SetTestVersion sets the test version for the runtime. +// WARNING: This should only be used for testing purposes. +// The *testing.T argument is only required to enforce this function +// to be used in tests only. +func (c *Config) SetTestVersion(t *testing.T, version runtime.Version) { + if t == nil { + panic("*testing.T argument cannot be nil. Please don't use this function outside of Go tests.") + } + c.testVersion = &version +} diff --git a/lib/runtime/wasmer/config_test.go b/lib/runtime/wasmer/config_test.go new file mode 100644 index 0000000000..cdf2e2dcad --- /dev/null +++ b/lib/runtime/wasmer/config_test.go @@ -0,0 +1,35 @@ +// Copyright 2022 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + +package wasmer + +import ( + "testing" + + "github.com/ChainSafe/gossamer/lib/runtime" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_Config_SetTestVersion(t *testing.T) { + t.Run("panics with nil *testing.T", func(t *testing.T) { + var c Config + assert.PanicsWithValue(t, + "*testing.T argument cannot be nil. Please don't use this function outside of Go tests.", + func() { + c.SetTestVersion(nil, runtime.Version{}) + }) + }) + + t.Run("set test version", func(t *testing.T) { + var c Config + testVersion := runtime.Version{ + StateVersion: 1, + } + + c.SetTestVersion(t, testVersion) + + require.NotNil(t, c.testVersion) + assert.Equal(t, testVersion, *c.testVersion) + }) +}