diff --git a/official/wasm/cmd/cmd_test.go b/official/wasm/cmd/cmd_test.go index d883f261..eb8d49d0 100644 --- a/official/wasm/cmd/cmd_test.go +++ b/official/wasm/cmd/cmd_test.go @@ -1,6 +1,7 @@ package cmd import ( + "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -11,18 +12,33 @@ func Test_relativePath(t *testing.T) { name string appPath string want string - err error }{ - // TODO: Add test cases. + { + name: "Relative path within current directory", + appPath: "subdir/file.txt", + want: "subdir/file.txt", + }, + { + name: "Relative path outside current directory", + appPath: "/path/file.txt", + want: "../../../../../../../../../../../path/file.txt", + }, + { + name: "App path is current directory", + appPath: ".", + want: ".", + }, + { + name: "App path is parent directory", + appPath: "..", + want: "..", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := relativePath(tt.appPath) - if tt.err != nil { - require.Error(t, err) - require.ErrorIs(t, err, tt.err) - return - } + absPath, err := filepath.Abs(tt.appPath) + require.NoError(t, err) + got, err := relativePath(absPath) require.NoError(t, err) require.Equal(t, tt.want, got) }) diff --git a/official/wasm/pkg/config/config_test.go b/official/wasm/pkg/config/config_test.go index 1c856724..68952158 100644 --- a/official/wasm/pkg/config/config_test.go +++ b/official/wasm/pkg/config/config_test.go @@ -1,8 +1,11 @@ package config import ( + "os" + "strings" "testing" + "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/stretchr/testify/require" ) @@ -16,32 +19,92 @@ func TestAddWasm(t *testing.T) { args args err error }{ - // TODO: Add test cases. + { + name: "Add wasm parameters to the config file", + args: args{ + configPath: "testdata/config_without_wasm.toml", + options: []Option{ + WithSmartQueryGasLimit(77), + WithMemoryCacheSize(888), + WithSimulationGasLimit(9999), + }, + }, + }, + { + name: "Config file already has wasm section", + args: args{ + configPath: "testdata/config_with_wasm.toml", + options: []Option{ + WithSmartQueryGasLimit(77), + WithMemoryCacheSize(888), + WithSimulationGasLimit(9999), + }, + }, + err: errors.New("config file already have wasm testdata/config_with_wasm.toml"), + }, + { + name: "Invalid config file path", + args: args{ + configPath: "nonexistent_directory/nonexistent_config.toml", + options: []Option{ + WithSmartQueryGasLimit(77), + WithMemoryCacheSize(888), + WithSimulationGasLimit(9999), + }, + }, + err: errors.New("open nonexistent_directory/nonexistent_config.toml: no such file or directory"), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + content, _ := os.ReadFile(tt.args.configPath) err := AddWasm(tt.args.configPath, tt.args.options...) - require.Equal(t, tt.err, err) + if tt.err != nil { + require.Error(t, err) + require.Equal(t, tt.err.Error(), err.Error()) + return + } + require.NoError(t, err) + require.True(t, hasWasm(tt.args.configPath)) + + withWasm, err := os.ReadFile("testdata/config_with_wasm.toml") + require.NoError(t, err) + noWasm, err := os.ReadFile(tt.args.configPath) + require.NoError(t, err) + require.Equal(t, strings.TrimSpace(string(withWasm)), strings.TrimSpace(string(noWasm))) + + require.NoError(t, os.WriteFile(tt.args.configPath, content, 0o644)) + require.False(t, hasWasm(tt.args.configPath)) }) } } func Test_hasWasm(t *testing.T) { - type args struct { - configPath string - } tests := []struct { - name string - args args - want bool + name string + configPath string + want bool }{ - // TODO: Add test cases. + { + name: "Config file with wasm section", + configPath: "testdata/config_with_wasm.toml", + want: true, + }, + { + name: "Config file without wasm section", + configPath: "testdata/config_without_wasm.toml", + want: false, + }, + { + name: "Non-existent config file", + configPath: "testdata/nonexistent_config.toml", + want: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := hasWasm(tt.args.configPath); got != tt.want { - t.Errorf("hasWasm() = %v, want %v", got, tt.want) - } + got := hasWasm(tt.configPath) + require.Equal(t, tt.want, got) }) } } diff --git a/official/wasm/pkg/config/testdata/config_with_wasm.toml b/official/wasm/pkg/config/testdata/config_with_wasm.toml new file mode 100644 index 00000000..42ae0c30 --- /dev/null +++ b/official/wasm/pkg/config/testdata/config_with_wasm.toml @@ -0,0 +1,30 @@ +[instrumentation] + +# When true, Prometheus metrics are served under /metrics on +# PrometheusListenAddr. +# Check out the documentation for the list of available metrics. +prometheus = false + +# Address to listen for Prometheus collector(s) connections +prometheus_listen_addr = ":26660" + +# Maximum number of simultaneous connections. +# If you want to accept a larger number than the default, make sure +# you increase your OS limits. +# 0 - unlimited. +max_open_connections = 3 + +# Instrumentation namespace +namespace = "cometbft" + +[wasm] +# Smart query gas limit is the max gas to be used in a smart query contract call +query_gas_limit = 77 + +# in-memory cache for Wasm contracts. Set to 0 to disable. +# The value is in MiB not bytes +memory_cache_size = 888 + +# Simulation gas limit is the max gas to be used in a tx simulation call. +# When not set the consensus max block gas is used instead +simulation_gas_limit = 9999 \ No newline at end of file diff --git a/official/wasm/pkg/config/testdata/config_without_wasm.toml b/official/wasm/pkg/config/testdata/config_without_wasm.toml new file mode 100644 index 00000000..f061633b --- /dev/null +++ b/official/wasm/pkg/config/testdata/config_without_wasm.toml @@ -0,0 +1,18 @@ +[instrumentation] + +# When true, Prometheus metrics are served under /metrics on +# PrometheusListenAddr. +# Check out the documentation for the list of available metrics. +prometheus = false + +# Address to listen for Prometheus collector(s) connections +prometheus_listen_addr = ":26660" + +# Maximum number of simultaneous connections. +# If you want to accept a larger number than the default, make sure +# you increase your OS limits. +# 0 - unlimited. +max_open_connections = 3 + +# Instrumentation namespace +namespace = "cometbft" diff --git a/official/wasm/services/scaffolder/scaffolder_test.go b/official/wasm/services/scaffolder/scaffolder_test.go index af947094..86146658 100644 --- a/official/wasm/services/scaffolder/scaffolder_test.go +++ b/official/wasm/services/scaffolder/scaffolder_test.go @@ -4,21 +4,53 @@ import ( "testing" "github.com/ignite/cli/v28/ignite/pkg/cosmosver" + "github.com/pkg/errors" "github.com/stretchr/testify/require" ) func Test_assertSupportedCosmosSDKVersion(t *testing.T) { + v100, err := cosmosver.Parse("v1.0.0") + require.NoError(t, err) + v0501, err := cosmosver.Parse("v0.50.1") + require.NoError(t, err) + v0450, err := cosmosver.Parse("v0.45.0") + require.NoError(t, err) + v0391, err := cosmosver.Parse("v0.39.1") + require.NoError(t, err) + tests := []struct { name string v cosmosver.Version err error }{ - // TODO: Add test cases. + { + name: "Supported Cosmos SDK version (equal)", + v: v0501, + }, + { + name: "Supported Cosmos SDK version (greater than)", + v: v100, + }, + { + name: "Unsupported Cosmos SDK version", + v: v0450, + err: errors.Errorf(errOldCosmosSDKVersionStr, v0450), + }, + { + name: "Unsupported Cosmos SDK version", + v: v0391, + err: errors.Errorf(errOldCosmosSDKVersionStr, v0391), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := assertSupportedCosmosSDKVersion(tt.v) - require.Equal(t, tt.err, err) + if tt.err != nil { + require.Error(t, err) + require.Equal(t, tt.err.Error(), err.Error()) + return + } + require.NoError(t, err) }) } }