|
| 1 | +package cosmosgen |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestFindInnerProtoFolder(t *testing.T) { |
| 12 | + tmpDir, err := os.MkdirTemp("", "proto-test") |
| 13 | + require.NoError(t, err) |
| 14 | + defer os.RemoveAll(tmpDir) |
| 15 | + |
| 16 | + // create dummy files |
| 17 | + create := func(path string) { |
| 18 | + dir := filepath.Dir(path) |
| 19 | + err := os.MkdirAll(dir, 0o755) |
| 20 | + require.NoError(t, err) |
| 21 | + _, err = os.Create(path) |
| 22 | + require.NoError(t, err) |
| 23 | + } |
| 24 | + |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + setup func(root string) |
| 28 | + expectedPath string |
| 29 | + expectError bool |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "no proto files", |
| 33 | + setup: func(root string) { |
| 34 | + // No files created |
| 35 | + }, |
| 36 | + expectError: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "single proto file in root", |
| 40 | + setup: func(root string) { |
| 41 | + create(filepath.Join(root, "a.proto")) |
| 42 | + }, |
| 43 | + expectedPath: ".", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "single proto file in proto dir", |
| 47 | + setup: func(root string) { |
| 48 | + create(filepath.Join(root, "proto", "a.proto")) |
| 49 | + }, |
| 50 | + expectedPath: "proto", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "multiple proto files in same proto dir", |
| 54 | + setup: func(root string) { |
| 55 | + create(filepath.Join(root, "proto", "a.proto")) |
| 56 | + create(filepath.Join(root, "proto", "b.proto")) |
| 57 | + }, |
| 58 | + expectedPath: "proto", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "nested proto directories", |
| 62 | + setup: func(root string) { |
| 63 | + create(filepath.Join(root, "proto", "a.proto")) |
| 64 | + create(filepath.Join(root, "proto", "api", "v1", "b.proto")) |
| 65 | + }, |
| 66 | + expectedPath: "proto", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "highest level proto directory", |
| 70 | + setup: func(root string) { |
| 71 | + create(filepath.Join(root, "proto", "a.proto")) |
| 72 | + create(filepath.Join(root, "foo", "proto", "b.proto")) |
| 73 | + }, |
| 74 | + expectedPath: "proto", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "no dir named proto", |
| 78 | + setup: func(root string) { |
| 79 | + create(filepath.Join(root, "api", "a.proto")) |
| 80 | + }, |
| 81 | + expectedPath: "api", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "deeply nested with no proto dir name", |
| 85 | + setup: func(root string) { |
| 86 | + create(filepath.Join(root, "foo", "bar", "a.proto")) |
| 87 | + }, |
| 88 | + expectedPath: "foo/bar", |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tt := range tests { |
| 93 | + t.Run(tt.name, func(t *testing.T) { |
| 94 | + caseRoot := filepath.Join(tmpDir, tt.name) |
| 95 | + err := os.MkdirAll(caseRoot, 0o755) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + tt.setup(caseRoot) |
| 99 | + |
| 100 | + result, err := findInnerProtoFolder(caseRoot) |
| 101 | + |
| 102 | + if tt.expectError { |
| 103 | + require.Error(t, err) |
| 104 | + return |
| 105 | + } |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + expected := filepath.Join(caseRoot, tt.expectedPath) |
| 109 | + require.Equal(t, expected, result) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments