more coverage bumps to rolodex

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2023-10-31 14:10:48 -04:00
parent 0b08a63e63
commit b37b9a2fb9
3 changed files with 138 additions and 118 deletions

View File

@@ -44,7 +44,6 @@ func (index *SpecIndex) FindComponent(componentId string) *Reference {
// root search // root search
return index.FindComponentInRoot(componentId) return index.FindComponentInRoot(componentId)
} }
return nil
} }
func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index *SpecIndex) *Reference { func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index *SpecIndex) *Reference {

View File

@@ -4,175 +4,183 @@
package index package index
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"io" "io"
"io/fs" "io/fs"
"path/filepath" "path/filepath"
"testing" "testing"
"testing/fstest" "testing/fstest"
"time" "time"
) )
func TestRolodexLoadsFilesCorrectly_NoErrors(t *testing.T) { func TestRolodexLoadsFilesCorrectly_NoErrors(t *testing.T) {
t.Parallel() t.Parallel()
testFS := fstest.MapFS{ testFS := fstest.MapFS{
"spec.yaml": {Data: []byte("hip"), ModTime: time.Now()}, "spec.yaml": {Data: []byte("hip"), ModTime: time.Now()},
"spock.yaml": {Data: []byte("hip: : hello: :\n:hw"), ModTime: time.Now()}, "spock.yaml": {Data: []byte("hip: : hello: :\n:hw"), ModTime: time.Now()},
"subfolder/spec1.json": {Data: []byte("hop"), ModTime: time.Now()}, "subfolder/spec1.json": {Data: []byte("hop"), ModTime: time.Now()},
"subfolder2/spec2.yaml": {Data: []byte("chop"), ModTime: time.Now()}, "subfolder2/spec2.yaml": {Data: []byte("chop"), ModTime: time.Now()},
"subfolder2/hello.jpg": {Data: []byte("shop"), ModTime: time.Now()}, "subfolder2/hello.jpg": {Data: []byte("shop"), ModTime: time.Now()},
} }
fileFS, err := NewLocalFS(".", testFS) fileFS, err := NewLocalFS(".", testFS)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
files := fileFS.GetFiles() files := fileFS.GetFiles()
assert.Len(t, files, 4) assert.Len(t, files, 4)
assert.Len(t, fileFS.GetErrors(), 0) assert.Len(t, fileFS.GetErrors(), 0)
key, _ := filepath.Abs(filepath.Join(fileFS.baseDirectory, "spec.yaml")) key, _ := filepath.Abs(filepath.Join(fileFS.baseDirectory, "spec.yaml"))
localFile := files[key] localFile := files[key]
assert.NotNil(t, localFile) assert.NotNil(t, localFile)
assert.Nil(t, localFile.GetIndex()) assert.Nil(t, localFile.GetIndex())
lf := localFile.(*LocalFile) lf := localFile.(*LocalFile)
idx, ierr := lf.Index(CreateOpenAPIIndexConfig()) idx, ierr := lf.Index(CreateOpenAPIIndexConfig())
assert.NoError(t, ierr) assert.NoError(t, ierr)
assert.NotNil(t, idx) assert.NotNil(t, idx)
assert.NotNil(t, localFile.GetContent()) assert.NotNil(t, localFile.GetContent())
d, e := localFile.GetContentAsYAMLNode() d, e := localFile.GetContentAsYAMLNode()
assert.NoError(t, e) assert.NoError(t, e)
assert.NotNil(t, d) assert.NotNil(t, d)
assert.NotNil(t, localFile.GetIndex()) assert.NotNil(t, localFile.GetIndex())
assert.Equal(t, YAML, localFile.GetFileExtension()) assert.Equal(t, YAML, localFile.GetFileExtension())
assert.Equal(t, key, localFile.GetFullPath()) assert.Equal(t, key, localFile.GetFullPath())
assert.Equal(t, "spec.yaml", lf.Name()) assert.Equal(t, "spec.yaml", lf.Name())
assert.Equal(t, int64(3), lf.Size()) assert.Equal(t, int64(3), lf.Size())
assert.Equal(t, fs.FileMode(0), lf.Mode()) assert.Equal(t, fs.FileMode(0), lf.Mode())
assert.False(t, lf.IsDir()) assert.False(t, lf.IsDir())
assert.Equal(t, time.Now().Unix(), lf.ModTime().Unix()) assert.Equal(t, time.Now().Unix(), lf.ModTime().Unix())
assert.Nil(t, lf.Sys()) assert.Nil(t, lf.Sys())
assert.Nil(t, lf.Close()) assert.Nil(t, lf.Close())
q, w := lf.Stat() q, w := lf.Stat()
assert.NotNil(t, q) assert.NotNil(t, q)
assert.NoError(t, w) assert.NoError(t, w)
b, x := io.ReadAll(lf) b, x := io.ReadAll(lf)
assert.Len(t, b, 3) assert.Len(t, b, 3)
assert.NoError(t, x) assert.NoError(t, x)
assert.Equal(t, key, lf.FullPath()) assert.Equal(t, key, lf.FullPath())
assert.Len(t, localFile.GetErrors(), 0) assert.Len(t, localFile.GetErrors(), 0)
// try and reindex // try and reindex
idx, ierr = lf.Index(CreateOpenAPIIndexConfig()) idx, ierr = lf.Index(CreateOpenAPIIndexConfig())
assert.NoError(t, ierr) assert.NoError(t, ierr)
assert.NotNil(t, idx) assert.NotNil(t, idx)
key, _ = filepath.Abs(filepath.Join(fileFS.baseDirectory, "spock.yaml")) key, _ = filepath.Abs(filepath.Join(fileFS.baseDirectory, "spock.yaml"))
localFile = files[key] localFile = files[key]
assert.NotNil(t, localFile) assert.NotNil(t, localFile)
assert.Nil(t, localFile.GetIndex()) assert.Nil(t, localFile.GetIndex())
lf = localFile.(*LocalFile) lf = localFile.(*LocalFile)
idx, ierr = lf.Index(CreateOpenAPIIndexConfig()) idx, ierr = lf.Index(CreateOpenAPIIndexConfig())
assert.Error(t, ierr) assert.Error(t, ierr)
assert.Nil(t, idx) assert.Nil(t, idx)
assert.NotNil(t, localFile.GetContent()) assert.NotNil(t, localFile.GetContent())
assert.Nil(t, localFile.GetIndex()) assert.Nil(t, localFile.GetIndex())
} }
func TestRolodexLocalFS_NoConfig(t *testing.T) { func TestRolodexLocalFS_NoConfig(t *testing.T) {
lfs := &LocalFS{} lfs := &LocalFS{}
f, e := lfs.Open("test.yaml") f, e := lfs.Open("test.yaml")
assert.Nil(t, f) assert.Nil(t, f)
assert.Error(t, e) assert.Error(t, e)
} }
func TestRolodexLocalFS_NoLookup(t *testing.T) { func TestRolodexLocalFS_NoLookup(t *testing.T) {
cf := CreateClosedAPIIndexConfig() cf := CreateClosedAPIIndexConfig()
lfs := &LocalFS{indexConfig: cf} lfs := &LocalFS{indexConfig: cf}
f, e := lfs.Open("test.yaml") f, e := lfs.Open("test.yaml")
assert.Nil(t, f) assert.Nil(t, f)
assert.Error(t, e) assert.Error(t, e)
} }
func TestRolodexLocalFS_BadAbsFile(t *testing.T) { func TestRolodexLocalFS_BadAbsFile(t *testing.T) {
cf := CreateOpenAPIIndexConfig() cf := CreateOpenAPIIndexConfig()
lfs := &LocalFS{indexConfig: cf} lfs := &LocalFS{indexConfig: cf}
f, e := lfs.Open("/test.yaml") f, e := lfs.Open("/test.yaml")
assert.Nil(t, f) assert.Nil(t, f)
assert.Error(t, e) assert.Error(t, e)
} }
func TestRolodexLocalFile_BadParse(t *testing.T) { func TestRolodexLocalFile_BadParse(t *testing.T) {
lf := &LocalFile{} lf := &LocalFile{}
n, e := lf.GetContentAsYAMLNode() n, e := lf.GetContentAsYAMLNode()
assert.Nil(t, n) assert.Nil(t, n)
assert.Error(t, e) assert.Error(t, e)
assert.Equal(t, "no data to parse for file: ", e.Error()) assert.Equal(t, "no data to parse for file: ", e.Error())
} }
func TestRolodexLocalFile_NoIndexRoot(t *testing.T) { func TestRolodexLocalFile_NoIndexRoot(t *testing.T) {
lf := &LocalFile{data: []byte("burders"), index: &SpecIndex{}} lf := &LocalFile{data: []byte("burders"), index: &SpecIndex{}}
n, e := lf.GetContentAsYAMLNode() n, e := lf.GetContentAsYAMLNode()
assert.NotNil(t, n) assert.NotNil(t, n)
assert.NoError(t, e) assert.NoError(t, e)
} }
func TestRolodexLocalFile_IndexSingleFile(t *testing.T) { func TestRolodexLocalFile_IndexSingleFile(t *testing.T) {
testFS := fstest.MapFS{ testFS := fstest.MapFS{
"spec.yaml": {Data: []byte("hip"), ModTime: time.Now()}, "spec.yaml": {Data: []byte("hip"), ModTime: time.Now()},
"spock.yaml": {Data: []byte("hop"), ModTime: time.Now()}, "spock.yaml": {Data: []byte("hop"), ModTime: time.Now()},
"i-am-a-dir": {Mode: fs.FileMode(fs.ModeDir), ModTime: time.Now()}, "i-am-a-dir": {Mode: fs.FileMode(fs.ModeDir), ModTime: time.Now()},
} }
fileFS, _ := NewLocalFS("spec.yaml", testFS) fileFS, _ := NewLocalFS("spec.yaml", testFS)
files := fileFS.GetFiles() files := fileFS.GetFiles()
assert.Len(t, files, 1) assert.Len(t, files, 1)
} }
func TestRolodexLocalFile_TestFilters(t *testing.T) { func TestRolodexLocalFile_TestFilters(t *testing.T) {
testFS := fstest.MapFS{ testFS := fstest.MapFS{
"spec.yaml": {Data: []byte("hip"), ModTime: time.Now()}, "spec.yaml": {Data: []byte("hip"), ModTime: time.Now()},
"spock.yaml": {Data: []byte("pip"), ModTime: time.Now()}, "spock.yaml": {Data: []byte("pip"), ModTime: time.Now()},
"jam.jpg": {Data: []byte("sip"), ModTime: time.Now()}, "jam.jpg": {Data: []byte("sip"), ModTime: time.Now()},
} }
fileFS, _ := NewLocalFSWithConfig(&LocalFSConfig{ fileFS, _ := NewLocalFSWithConfig(&LocalFSConfig{
BaseDirectory: ".", BaseDirectory: ".",
FileFilters: []string{"spec.yaml", "spock.yaml", "jam.jpg"}, FileFilters: []string{"spec.yaml", "spock.yaml", "jam.jpg"},
DirFS: testFS, DirFS: testFS,
}) })
files := fileFS.GetFiles() files := fileFS.GetFiles()
assert.Len(t, files, 2) assert.Len(t, files, 2)
} }
func TestRolodexLocalFile_TestBasFS(t *testing.T) { func TestRolodexLocalFile_TestBadFS(t *testing.T) {
testFS := test_badfs{} testFS := test_badfs{}
fileFS, err := NewLocalFSWithConfig(&LocalFSConfig{ fileFS, err := NewLocalFSWithConfig(&LocalFSConfig{
BaseDirectory: ".", BaseDirectory: ".",
DirFS: &testFS, DirFS: &testFS,
}) })
assert.Error(t, err) assert.Error(t, err)
assert.Nil(t, fileFS) assert.Nil(t, fileFS)
} }
func TestNewRolodexLocalFile_BadOffset(t *testing.T) {
lf := &LocalFile{offset: -1}
z, y := io.ReadAll(lf)
assert.Len(t, z, 0)
assert.Error(t, y)
}

View File

@@ -4,6 +4,7 @@
package index package index
import ( import (
"context"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"os" "os"
@@ -21,3 +22,15 @@ func TestSpecIndex_SearchIndexForReference(t *testing.T) {
ref, _ := idx.SearchIndexForReference("#/components/schemas/Pet") ref, _ := idx.SearchIndexForReference("#/components/schemas/Pet")
assert.NotNil(t, ref) assert.NotNil(t, ref)
} }
func TestSpecIndex_SearchIndexForReferenceWithContext(t *testing.T) {
petstore, _ := os.ReadFile("../test_specs/petstorev3.json")
var rootNode yaml.Node
_ = yaml.Unmarshal(petstore, &rootNode)
c := CreateOpenAPIIndexConfig()
idx := NewSpecIndexWithConfig(&rootNode, c)
ref, _, _ := idx.SearchIndexForReferenceWithContext(context.Background(), "#/components/schemas/Pet")
assert.NotNil(t, ref)
}