|
1 | 1 | package scaffolder |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
4 | 7 | "testing" |
5 | 8 |
|
6 | 9 | "github.com/stretchr/testify/require" |
@@ -105,3 +108,42 @@ func TestContainsCustomTypes(t *testing.T) { |
105 | 108 | }) |
106 | 109 | } |
107 | 110 | } |
| 111 | + |
| 112 | +func TestCheckTypeProtoCreated(t *testing.T) { |
| 113 | + t.Run("should fail when proto type already exists", func(t *testing.T) { |
| 114 | + tmp := t.TempDir() |
| 115 | + protoFile := filepath.Join(tmp, "proto", "blog", "blog", "v1", "post.proto") |
| 116 | + require.NoError(t, os.MkdirAll(filepath.Dir(protoFile), 0o755)) |
| 117 | + |
| 118 | + content := `syntax = "proto3"; |
| 119 | +package blog.blog.v1; |
| 120 | +
|
| 121 | +message Post {} |
| 122 | +` |
| 123 | + require.NoError(t, os.WriteFile(protoFile, []byte(content), 0o644)) |
| 124 | + |
| 125 | + name, err := multiformatname.NewName("post") |
| 126 | + require.NoError(t, err) |
| 127 | + |
| 128 | + err = checkTypeProtoCreated(context.Background(), tmp, "blog", "proto", "blog", name) |
| 129 | + require.EqualError(t, err, "component type with name post is already created (type Post exists)") |
| 130 | + }) |
| 131 | + |
| 132 | + t.Run("should pass when proto type does not exist", func(t *testing.T) { |
| 133 | + tmp := t.TempDir() |
| 134 | + protoFile := filepath.Join(tmp, "proto", "blog", "blog", "v1", "comment.proto") |
| 135 | + require.NoError(t, os.MkdirAll(filepath.Dir(protoFile), 0o755)) |
| 136 | + |
| 137 | + content := `syntax = "proto3"; |
| 138 | +package blog.blog.v1; |
| 139 | +
|
| 140 | +message Comment {} |
| 141 | +` |
| 142 | + require.NoError(t, os.WriteFile(protoFile, []byte(content), 0o644)) |
| 143 | + |
| 144 | + name, err := multiformatname.NewName("post") |
| 145 | + require.NoError(t, err) |
| 146 | + |
| 147 | + require.NoError(t, checkTypeProtoCreated(context.Background(), tmp, "blog", "proto", "blog", name)) |
| 148 | + }) |
| 149 | +} |
0 commit comments