Skip to content

Commit

Permalink
Modules.Parse errors out if ms.add(n) fails (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus authored Jul 29, 2021
1 parent 4a6d186 commit 1f1b463
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/yang/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (ms *Modules) Read(name string) error {

// Parse parses data as YANG source and adds it to ms. The name should reflect
// the source of data.
// Note: If an error is returned, valid modules might still have been added to
// the Modules cache.
func (ms *Modules) Parse(data, name string) error {
ss, err := Parse(data, name)
if err != nil {
Expand All @@ -69,7 +71,9 @@ func (ms *Modules) Parse(data, name string) error {
if err != nil {
return err
}
ms.add(n)
if err := ms.add(n); err != nil {
return err
}
}
return nil
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/yang/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ var testdataFindModulesText = map[string]string{
"dup-ns-two": `module dup-ns-two { prefix ns-two; namespace urn:duplicate; }`,
}

func TestDupModule(t *testing.T) {
tests := []struct {
desc string
inModules map[string]string
wantErr bool
}{{
desc: "two modules with the same name",
inModules: map[string]string{
"foo": `module foo { prefix "foo"; namespace "urn:foo"; }`,
"bar": `module foo { prefix "foo"; namespace "urn:foo"; }`,
},
wantErr: true,
}}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
ms := NewModules()
var err error
for name, modtext := range tt.inModules {
if err = ms.Parse(modtext, name+".yang"); err != nil {
break
}
}
if gotErr := err != nil; gotErr != tt.wantErr {
t.Fatalf("wantErr: %v, got error: %v", tt.wantErr, err)
}
})
}
}

func testModulesForTestdataModulesText(t *testing.T) *Modules {
ms := NewModules()
for name, modtext := range testdataFindModulesText {
Expand Down

0 comments on commit 1f1b463

Please sign in to comment.