From 07ed25a870198b15dc3629b26895c9ad05057507 Mon Sep 17 00:00:00 2001 From: Calvin Lobo Date: Wed, 28 Aug 2024 10:29:22 -0400 Subject: [PATCH] Improved code coverage and fixed nil error when copying references --- index/rolodex.go | 6 ++++++ index/rolodex_test.go | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/index/rolodex.go b/index/rolodex.go index 512ceb01..020dbfba 100644 --- a/index/rolodex.go +++ b/index/rolodex.go @@ -436,6 +436,9 @@ func (r *Rolodex) BuildIndexes() { func (r *Rolodex) GetAllReferences() map[string]*Reference { allRefs := make(map[string]*Reference) for _, idx := range append(r.GetIndexes(), r.GetRootIndex()) { + if idx == nil { + continue + } refs := idx.GetAllReferences() maps.Copy(allRefs, refs) } @@ -446,6 +449,9 @@ func (r *Rolodex) GetAllReferences() map[string]*Reference { func (r *Rolodex) GetAllMappedReferences() map[string]*Reference { mappedRefs := make(map[string]*Reference) for _, idx := range append(r.GetIndexes(), r.GetRootIndex()) { + if idx == nil { + continue + } refs := idx.GetMappedReferences() maps.Copy(mappedRefs, refs) } diff --git a/index/rolodex_test.go b/index/rolodex_test.go index 77692d73..f20df5a6 100644 --- a/index/rolodex_test.go +++ b/index/rolodex_test.go @@ -24,6 +24,8 @@ import ( func TestRolodex_NewRolodex(t *testing.T) { c := CreateOpenAPIIndexConfig() rolo := NewRolodex(c) + assert.Len(t, rolo.GetAllReferences(), 0) + assert.Len(t, rolo.GetAllMappedReferences(), 0) assert.NotNil(t, rolo) assert.NotNil(t, rolo.indexConfig) assert.Nil(t, rolo.GetIgnoredCircularReferences()) @@ -1520,6 +1522,7 @@ func TestRolodex_SimpleTest_OneDoc(t *testing.T) { } cf := CreateOpenAPIIndexConfig() + cf.SpecFilePath = filepath.Join(baseDir, "doc1.yaml") cf.BasePath = baseDir cf.IgnoreArrayCircularReferences = true cf.IgnorePolymorphicCircularReferences = true @@ -1527,11 +1530,19 @@ func TestRolodex_SimpleTest_OneDoc(t *testing.T) { rolo := NewRolodex(cf) rolo.AddLocalFS(baseDir, fileFS) + rootBytes, err := os.ReadFile(cf.SpecFilePath) + assert.NoError(t, err) + var rootNode yaml.Node + _ = yaml.Unmarshal(rootBytes, &rootNode) + rolo.SetRootNode(&rootNode) + err = rolo.IndexTheRolodex() //assert.NotZero(t, rolo.GetIndexingDuration()) comes back as 0 on windows. - assert.Nil(t, rolo.GetRootIndex()) + assert.NotNil(t, rolo.GetRootIndex()) assert.Len(t, rolo.GetIndexes(), 10) + assert.Len(t, rolo.GetAllReferences(), 7) + assert.Len(t, rolo.GetAllMappedReferences(), 7) lineCount := rolo.GetFullLineCount() assert.Equal(t, int64(158), lineCount, "total line count in the rolodex is wrong")