Skip to content

Commit

Permalink
feat: giving error info when having duplicated names (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Apr 28, 2023
1 parent 5af79c7 commit 15c4792
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/testing/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,19 @@ func Parse(configFile string) (testSuite *TestSuite, err error) {
// ParseFromData parses data and returns the test suite
func ParseFromData(data []byte) (testSuite *TestSuite, err error) {
testSuite = &TestSuite{}
err = yaml.Unmarshal(data, testSuite)
if err = yaml.Unmarshal(data, testSuite); err != nil {
return
}

names := map[string]struct{}{}
for _, item := range testSuite.Items {
if _, ok := names[item.Name]; !ok {
names[item.Name] = struct{}{}
} else {
err = fmt.Errorf("having duplicated name '%s'", item.Name)
break
}
}
return
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/testing/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func TestParse(t *testing.T) {
assert.NotNil(t, err)
}

func TestDuplicatedNames(t *testing.T) {
_, err := Parse("testdata/duplicated-names.yaml")
assert.NotNil(t, err)

_, err = ParseFromData([]byte("fake"))
assert.NotNil(t, err)
}

func TestRequestRender(t *testing.T) {
tests := []struct {
name string
Expand Down
8 changes: 8 additions & 0 deletions pkg/testing/testdata/duplicated-names.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: duplicated names
items:
- name: projects
request:
api: https://foo
- name: projects
request:
api: https://foo

0 comments on commit 15c4792

Please sign in to comment.