Skip to content

Commit

Permalink
fix(buf): set correct prefix for api (#4198)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1316d69)

# Conflicts:
#	ignite/templates/app/files/proto/buf.gen.pulsar.yaml
#	ignite/templates/app/files/proto/buf.gen.pulsar.yaml.plush
#	ignite/templates/app/files/{{protoDir}}/buf.gen.pulsar.yaml
#	ignite/templates/app/proto_test.go
#	integration/app/cmd_proto_path_test.go
  • Loading branch information
julienrbrt authored and mergify[bot] committed Jun 21, 2024
1 parent 8cebfc3 commit 0300f27
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
### Fixes

- [#4184](https://github.com/ignite/cli/pull/4184) Set custom `InitChainer` because of manually registered modules
- [#4198](https://github.com/ignite/cli/pull/4198) Set correct prefix overwriting in `buf.gen.pulsar.yaml`

## [`v28.4.0`](https://github.com/ignite/cli/releases/tag/v28.4.0)

Expand Down
3 changes: 2 additions & 1 deletion ignite/templates/app/files/proto/buf.gen.pulsar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ version: v1
managed:
enabled: true
go_package_prefix:
default: cosmossdk.io/api
default: <%= ModulePath %>/api
except:
- buf.build/googleapis/googleapis
- buf.build/cosmos/gogo-proto
- buf.build/cosmos/cosmos-proto
override:
buf.build/cosmos/cosmos-sdk: cosmossdk.io/api
plugins:
- name: go-pulsar
out: ./api
Expand Down
23 changes: 23 additions & 0 deletions ignite/templates/app/files/proto/buf.gen.pulsar.yaml.plush
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This file is auto-generated from Ignite. You can edit
# the file content but do not change the file name or path.
#
# buf.gen.pulsar.yaml
#
version: v1
managed:
enabled: true
go_package_prefix:
default: <%= ModulePath %>/api
except:
- buf.build/googleapis/googleapis
- buf.build/cosmos/gogo-proto
- buf.build/cosmos/cosmos-proto
override:
buf.build/cosmos/cosmos-sdk: cosmossdk.io/api
plugins:
- name: go-pulsar
out: ./api
opt: paths=source_relative
- name: go-grpc
out: ./api
opt: paths=source_relative
58 changes: 58 additions & 0 deletions ignite/templates/app/proto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package app

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestBufFiles(t *testing.T) {
want := []string{"buf.work.yaml"}
protoDir, err := os.ReadDir("files/{{protoDir}}")
require.NoError(t, err)
for _, e := range protoDir {
want = append(want, filepath.Join("{{protoDir}}", strings.TrimSuffix(e.Name(), ".plush")))
}

got, err := BufFiles()

Check failure on line 20 in ignite/templates/app/proto_test.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: BufFiles

Check failure on line 20 in ignite/templates/app/proto_test.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: BufFiles
require.NoError(t, err)
require.ElementsMatch(t, want, got)
}

func TestCutTemplatePrefix(t *testing.T) {
tests := []struct {
name string
arg string
want string
ok bool
}{
{
name: "with prefix",
arg: "{{protoDir}}/myvalue",
want: "myvalue",
ok: true,
},
{
name: "with 2 prefix",
arg: "{{protoDir}}/{{protoDir}}/myvalue",
want: "{{protoDir}}/myvalue",
ok: true,
},
{
name: "without prefix",
arg: "myvalue",
want: "myvalue",
ok: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := CutTemplatePrefix(tt.arg)

Check failure on line 53 in ignite/templates/app/proto_test.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: CutTemplatePrefix

Check failure on line 53 in ignite/templates/app/proto_test.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: CutTemplatePrefix
require.Equal(t, tt.ok, ok)
require.Equal(t, tt.want, got)
})
}
}
104 changes: 104 additions & 0 deletions integration/app/cmd_proto_path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//go:build !relayer

package app_test

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

"github.com/ignite/cli/v29/ignite/config/chain"

Check failure on line 14 in integration/app/cmd_proto_path_test.go

View workflow job for this annotation

GitHub Actions / test app on ubuntu-latest

no required module provides package github.com/ignite/cli/v29/ignite/config/chain; to add it:

Check failure on line 14 in integration/app/cmd_proto_path_test.go

View workflow job for this annotation

GitHub Actions / test app on macos-latest

no required module provides package github.com/ignite/cli/v29/ignite/config/chain; to add it:
"github.com/ignite/cli/v29/ignite/config/chain/base"
v1 "github.com/ignite/cli/v29/ignite/config/chain/v1"
"github.com/ignite/cli/v29/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/v29/ignite/pkg/xyaml"
envtest "github.com/ignite/cli/v29/integration"
)

const newProtoPath = "myProto"

var (
bobName = "bob"
cfg = v1.Config{
Config: base.Config{
Version: 1,
Build: base.Build{
Proto: base.Proto{
Path: newProtoPath,
},
},
Accounts: []base.Account{
{
Name: "alice",
Coins: []string{"100000000000token", "10000000000000000000stake"},
Mnemonic: "slide moment original seven milk crawl help text kick fluid boring awkward doll wonder sure fragile plate grid hard next casual expire okay body",
},
{
Name: bobName,
Coins: []string{"100000000000token", "10000000000000000000stake"},
Mnemonic: "trap possible liquid elite embody host segment fantasy swim cable digital eager tiny broom burden diary earn hen grow engine pigeon fringe claim program",
},
},
Faucet: base.Faucet{
Name: &bobName,
Coins: []string{"500token", "100000000stake"},
Host: ":4501",
},
Genesis: xyaml.Map{"chain_id": "mars-1"},
},
Validators: []v1.Validator{
{
Name: "alice",
Bonded: "100000000stake",
},
},
}
)

// TestGenerapAppCheckBufPulsarPath tests scaffolding a new chain and checks if the buf.gen.pulsar.yaml file is correct
func TestGenerapAppCheckBufPulsarPath(t *testing.T) {
var (
env = envtest.New(t)
app = env.Scaffold("github.com/test/blog")
)

bufGenPulsarPath := filepath.Join(app.SourcePath(), "proto", "buf.gen.pulsar.yaml")
_, statErr := os.Stat(bufGenPulsarPath)
require.False(t, os.IsNotExist(statErr), "buf.gen.pulsar.yaml should be scaffolded")

result, err := os.ReadFile(bufGenPulsarPath)
require.NoError(t, err)

require.True(t, strings.Contains(string(result), "default: github.com/test/blog/api"), "buf.gen.pulsar.yaml should contain the correct api override")

app.EnsureSteady()
}

func TestChangeProtoPath(t *testing.T) {
var (
env = envtest.New(t)
app = env.Scaffold("github.com/test/protopath", "--proto-dir", newProtoPath)
appPath = app.SourcePath()
cfgPath = filepath.Join(appPath, chain.ConfigFilenames[0])
)

// set the custom config path.
file, err := os.Create(cfgPath)
require.NoError(t, err)
require.NoError(t, yaml.NewEncoder(file).Encode(cfg))
require.NoError(t, file.Close())
app.SetConfigPath(cfgPath)

env.Must(env.Exec("create a list with a custom proto path from config",
step.NewSteps(step.New(
step.Exec(envtest.IgniteApp, "s", "list", "--yes", "listUser", "email"),
step.Workdir(app.SourcePath()),
)),
))

app.EnsureSteady()
}

0 comments on commit 0300f27

Please sign in to comment.