Allow ModuleConfig.WithName("") to clear the module name (#1277)

We currently allow clearing other config with nil, such as FSConfig.
However, we missed a spot as internally we couldn't differentiate
between name never set, or explicitly set to empty. Now, when someone
sets the module name to empty, the name in the binary section is
ignored.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2023-03-23 08:53:14 +01:00
committed by GitHub
parent d8f356e644
commit 53bb95eeaa
4 changed files with 37 additions and 9 deletions

View File

@@ -455,7 +455,7 @@ type ModuleConfig interface {
WithFSConfig(FSConfig) ModuleConfig WithFSConfig(FSConfig) ModuleConfig
// WithName configures the module name. Defaults to what was decoded from // WithName configures the module name. Defaults to what was decoded from
// the name section. // the name section. Empty string ("") clears any name.
WithName(string) ModuleConfig WithName(string) ModuleConfig
// WithStartFunctions configures the functions to call after the module is // WithStartFunctions configures the functions to call after the module is
@@ -597,6 +597,7 @@ type ModuleConfig interface {
type moduleConfig struct { type moduleConfig struct {
name string name string
nameSet bool
startFunctions []string startFunctions []string
stdin io.Reader stdin io.Reader
stdout io.Writer stdout io.Writer
@@ -685,6 +686,7 @@ func (c *moduleConfig) WithFSConfig(config FSConfig) ModuleConfig {
// WithName implements ModuleConfig.WithName // WithName implements ModuleConfig.WithName
func (c *moduleConfig) WithName(name string) ModuleConfig { func (c *moduleConfig) WithName(name string) ModuleConfig {
ret := c.clone() ret := c.clone()
ret.nameSet = true
ret.name = name ret.name = name
return ret return ret
} }

View File

@@ -100,29 +100,50 @@ func TestRuntimeConfig(t *testing.T) {
func TestModuleConfig(t *testing.T) { func TestModuleConfig(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
with func(ModuleConfig) ModuleConfig with func(ModuleConfig) ModuleConfig
expected string expectNameSet bool
expectedName string
}{ }{
{
name: "WithName default",
with: func(c ModuleConfig) ModuleConfig {
return c
},
expectNameSet: false,
expectedName: "",
},
{ {
name: "WithName", name: "WithName",
with: func(c ModuleConfig) ModuleConfig { with: func(c ModuleConfig) ModuleConfig {
return c.WithName("wazero") return c.WithName("wazero")
}, },
expected: "wazero", expectNameSet: true,
expectedName: "wazero",
}, },
{ {
name: "WithName empty", name: "WithName empty",
with: func(c ModuleConfig) ModuleConfig { with: func(c ModuleConfig) ModuleConfig {
return c.WithName("") return c.WithName("")
}, },
expectNameSet: true,
expectedName: "",
}, },
{ {
name: "WithName twice", name: "WithName twice",
with: func(c ModuleConfig) ModuleConfig { with: func(c ModuleConfig) ModuleConfig {
return c.WithName("wazero").WithName("wa0") return c.WithName("wazero").WithName("wa0")
}, },
expected: "wa0", expectNameSet: true,
expectedName: "wa0",
},
{
name: "WithName can clear",
with: func(c ModuleConfig) ModuleConfig {
return c.WithName("wazero").WithName("")
},
expectNameSet: true,
expectedName: "",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@@ -131,7 +152,8 @@ func TestModuleConfig(t *testing.T) {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
input := NewModuleConfig() input := NewModuleConfig()
rc := tc.with(input) rc := tc.with(input)
require.Equal(t, tc.expected, rc.(*moduleConfig).name) require.Equal(t, tc.expectNameSet, rc.(*moduleConfig).nameSet)
require.Equal(t, tc.expectedName, rc.(*moduleConfig).name)
// The source wasn't modified // The source wasn't modified
require.Equal(t, NewModuleConfig(), input) require.Equal(t, NewModuleConfig(), input)
}) })

View File

@@ -283,7 +283,7 @@ func (r *runtime) InstantiateModule(
} }
name := config.name name := config.name
if name == "" && code.module.NameSection != nil && code.module.NameSection.ModuleName != "" { if !config.nameSet && code.module.NameSection != nil && code.module.NameSection.ModuleName != "" {
name = code.module.NameSection.ModuleName name = code.module.NameSection.ModuleName
} }

View File

@@ -446,19 +446,23 @@ func TestRuntime_InstantiateModule_WithName(t *testing.T) {
internal := r.(*runtime) internal := r.(*runtime)
m1, err := r.InstantiateModule(testCtx, base, NewModuleConfig().WithName("1")) m1, err := r.InstantiateModule(testCtx, base, NewModuleConfig().WithName("1"))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "1", m1.Name())
require.Nil(t, internal.Module("0")) require.Nil(t, internal.Module("0"))
require.Equal(t, internal.Module("1"), m1) require.Equal(t, internal.Module("1"), m1)
m2, err := r.InstantiateModule(testCtx, base, NewModuleConfig().WithName("2")) m2, err := r.InstantiateModule(testCtx, base, NewModuleConfig().WithName("2"))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "2", m2.Name())
require.Nil(t, internal.Module("0")) require.Nil(t, internal.Module("0"))
require.Equal(t, internal.Module("2"), m2) require.Equal(t, internal.Module("2"), m2)
// Empty name module shouldn't be returned via Module() for future optimization. // Empty name module shouldn't be returned via Module() for future optimization.
_, err = r.InstantiateModule(testCtx, base, NewModuleConfig().WithName("")) m3, err := r.InstantiateModule(testCtx, base, NewModuleConfig().WithName(""))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "", m3.Name())
ret := internal.Module("") ret := internal.Module("")
require.Nil(t, ret) require.Nil(t, ret)
} }