Bumping coverage on 0.6.0

This commit is contained in:
Dave Shanley
2023-02-18 17:10:52 -05:00
parent e6626fe22e
commit c5e9dbc360
5 changed files with 114 additions and 23 deletions

View File

@@ -296,11 +296,9 @@ func (index *SpecIndex) performExternalLookup(uri []string, componentId string,
} else {
j, _ = url.Parse(uri[0])
}
path := GenerateCleanSpecConfigBaseURL(j, uri[0], false)
newUrl, e := url.Parse(path)
if e == nil {
newUrl, _ := url.Parse(path)
if newUrl != nil {
newConfig := &SpecIndexConfig{
BaseURL: newUrl,
AllowRemoteLookup: index.config.AllowRemoteLookup,
@@ -318,9 +316,6 @@ func (index *SpecIndex) performExternalLookup(uri []string, componentId string,
index.AddChild(newIndex)
index.refLock.Unlock()
externalSpecIndex = newIndex
} else {
return nil
}
}

View File

@@ -0,0 +1,101 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package index
import (
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestSpecIndex_performExternalLookup(t *testing.T) {
yml := `{
"openapi": "3.1.0",
"paths": [
{"/": {
"get": {}
}}
]
}`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
index := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, index.GetPathsNode().Content, 1)
}
func TestSpecIndex_performExternalLookup_invalidURL(t *testing.T) {
yml := `openapi: 3.1.0
components:
schemas:
thing:
properties:
thong:
$ref: 'httpssss://not-gonna-work.com'`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
index := NewSpecIndexWithConfig(&rootNode, c)
assert.Len(t, index.GetReferenceIndexErrors(), 2)
}
func TestSpecIndex_FindComponentInRoot(t *testing.T) {
yml := `openapi: 3.1.0
components:
schemas:
thing:
properties:
thong: hi!`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
index := NewSpecIndexWithConfig(&rootNode, c)
thing := index.FindComponentInRoot("#/$splish/$.../slash#$///./")
assert.Nil(t, thing)
assert.Len(t, index.GetReferenceIndexErrors(), 0)
}
func TestSpecIndex_FailLookupRemoteComponent_badPath(t *testing.T) {
yml := `openapi: 3.1.0
components:
schemas:
thing:
properties:
thong:
$ref: 'https://pb33f.io/site.webmanifest#/....$.ok../oh#/$$_-'`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
index := NewSpecIndexWithConfig(&rootNode, c)
thing := index.FindComponentInRoot("#/$splish/$.../slash#$///./")
assert.Nil(t, thing)
assert.Len(t, index.GetReferenceIndexErrors(), 2)
}
func TestSpecIndex_FailLookupRemoteComponent_Ok_butNotFound(t *testing.T) {
yml := `openapi: 3.1.0
components:
schemas:
thing:
properties:
thong:
$ref: 'https://pb33f.io/site.webmanifest#/valid-but-missing'`
var rootNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &rootNode)
c := CreateOpenAPIIndexConfig()
index := NewSpecIndexWithConfig(&rootNode, c)
thing := index.FindComponentInRoot("#/valid-but-missing")
assert.Nil(t, thing)
assert.Len(t, index.GetReferenceIndexErrors(), 1)
}

View File

@@ -23,7 +23,7 @@ func TestSpecIndex_SearchIndexForReference(t *testing.T) {
assert.NotNil(t, ref)
}
func TestSpecIndex_SearchIndexForReferene_ExternalSpecs(t *testing.T) {
func TestSpecIndex_SearchIndexForReference_ExternalSpecs(t *testing.T) {
// load up an index with lots of references
petstore, _ := ioutil.ReadFile("../test_specs/digitalocean.yaml")
@@ -46,4 +46,8 @@ func TestSpecIndex_SearchIndexForReferene_ExternalSpecs(t *testing.T) {
assert.NotNil(t, ref)
assert.Equal(t, "description", ref[0].Node.Content[0].Value)
ref = idx.SearchIndexForReference("../models/options.yml")
assert.NotNil(t, ref)
assert.Equal(t, "kubernetes_options", ref[0].Node.Content[0].Value)
}

View File

@@ -546,21 +546,6 @@ func TestSpecIndex_FindComponenth(t *testing.T) {
assert.Nil(t, index.FindComponent("I-do-not-exist", nil))
}
func TestSpecIndex_performExternalLookup(t *testing.T) {
yml := `{
"openapi": "3.1.0",
"paths": [
{"/": {
"get": {}
}}
]
}`
var rootNode yaml.Node
yaml.Unmarshal([]byte(yml), &rootNode)
index := NewSpecIndex(&rootNode)
assert.Len(t, index.GetPathsNode().Content, 1)
}
func TestSpecIndex_TestPathsNodeAsArray(t *testing.T) {
yml := `components:

View File

@@ -25,3 +25,9 @@ func TestGenerateCleanSpecConfigBaseURL_RelativeDeep(t *testing.T) {
assert.Equal(t, "https://pb33f.io/things/stuff/foo/bar/baz/crap.yaml#thang",
GenerateCleanSpecConfigBaseURL(u, path, true))
}
func TestSpecIndex_extractDefinitionRequiredRefProperties(t *testing.T) {
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(nil, c)
assert.Nil(t, idx.extractDefinitionRequiredRefProperties(nil, nil))
}