mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 04:20:17 +00:00
@@ -522,11 +522,9 @@ func (r *Rolodex) Open(location string) (RolodexFile, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check if this is a native rolodex FS, then the work is done.
|
// check if this is a native rolodex FS, then the work is done.
|
||||||
if lrf, ok := interface{}(f).(*localRolodexFile); ok {
|
if lf, ko := interface{}(f).(*LocalFile); ko {
|
||||||
if lf, ko := interface{}(lrf.f).(*LocalFile); ko {
|
localFile = lf
|
||||||
localFile = lf
|
break
|
||||||
break
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// not a native FS, so we need to read the file and create a local file.
|
// not a native FS, so we need to read the file and create a local file.
|
||||||
bytes, rErr := io.ReadAll(f)
|
bytes, rErr := io.ReadAll(f)
|
||||||
|
|||||||
@@ -4,310 +4,277 @@
|
|||||||
package index
|
package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pb33f/libopenapi/datamodel"
|
"github.com/pb33f/libopenapi/datamodel"
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LocalFS struct {
|
type LocalFS struct {
|
||||||
indexConfig *SpecIndexConfig
|
indexConfig *SpecIndexConfig
|
||||||
entryPointDirectory string
|
entryPointDirectory string
|
||||||
baseDirectory string
|
baseDirectory string
|
||||||
Files map[string]RolodexFile
|
Files map[string]RolodexFile
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
readingErrors []error
|
readingErrors []error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFS) GetFiles() map[string]RolodexFile {
|
func (l *LocalFS) GetFiles() map[string]RolodexFile {
|
||||||
return l.Files
|
return l.Files
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFS) GetErrors() []error {
|
func (l *LocalFS) GetErrors() []error {
|
||||||
return l.readingErrors
|
return l.readingErrors
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFS) Open(name string) (fs.File, error) {
|
func (l *LocalFS) Open(name string) (fs.File, error) {
|
||||||
|
|
||||||
if l.indexConfig != nil && !l.indexConfig.AllowFileLookup {
|
if l.indexConfig != nil && !l.indexConfig.AllowFileLookup {
|
||||||
return nil, &fs.PathError{Op: "open", Path: name,
|
return nil, &fs.PathError{Op: "open", Path: name,
|
||||||
Err: fmt.Errorf("file lookup for '%s' not allowed, set the index configuration "+
|
Err: fmt.Errorf("file lookup for '%s' not allowed, set the index configuration "+
|
||||||
"to AllowFileLookup to be true", name)}
|
"to AllowFileLookup to be true", name)}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !filepath.IsAbs(name) {
|
if !filepath.IsAbs(name) {
|
||||||
var absErr error
|
name, _ = filepath.Abs(filepath.Join(l.baseDirectory, name))
|
||||||
name, absErr = filepath.Abs(filepath.Join(l.baseDirectory, name))
|
}
|
||||||
if absErr != nil {
|
|
||||||
return nil, absErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if f, ok := l.Files[name]; ok {
|
if f, ok := l.Files[name]; ok {
|
||||||
return &localRolodexFile{f: f}, nil
|
return f.(*LocalFile), nil
|
||||||
} else {
|
} else {
|
||||||
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
|
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type LocalFile struct {
|
type LocalFile struct {
|
||||||
filename string
|
filename string
|
||||||
name string
|
name string
|
||||||
extension FileExtension
|
extension FileExtension
|
||||||
data []byte
|
data []byte
|
||||||
fullPath string
|
fullPath string
|
||||||
lastModified time.Time
|
lastModified time.Time
|
||||||
readingErrors []error
|
readingErrors []error
|
||||||
index *SpecIndex
|
index *SpecIndex
|
||||||
parsed *yaml.Node
|
parsed *yaml.Node
|
||||||
|
offset int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) GetIndex() *SpecIndex {
|
func (l *LocalFile) GetIndex() *SpecIndex {
|
||||||
return l.index
|
return l.index
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) Index(config *SpecIndexConfig) (*SpecIndex, error) {
|
func (l *LocalFile) Index(config *SpecIndexConfig) (*SpecIndex, error) {
|
||||||
if l.index != nil {
|
if l.index != nil {
|
||||||
return l.index, nil
|
return l.index, nil
|
||||||
}
|
}
|
||||||
content := l.data
|
content := l.data
|
||||||
|
|
||||||
// first, we must parse the content of the file
|
// first, we must parse the content of the file
|
||||||
info, err := datamodel.ExtractSpecInfoWithDocumentCheck(content, true)
|
info, err := datamodel.ExtractSpecInfoWithDocumentCheck(content, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
index := NewSpecIndexWithConfig(info.RootNode, config)
|
index := NewSpecIndexWithConfig(info.RootNode, config)
|
||||||
index.specAbsolutePath = l.fullPath
|
index.specAbsolutePath = l.fullPath
|
||||||
|
|
||||||
l.index = index
|
l.index = index
|
||||||
return index, nil
|
return index, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) GetContent() string {
|
func (l *LocalFile) GetContent() string {
|
||||||
return string(l.data)
|
return string(l.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) GetContentAsYAMLNode() (*yaml.Node, error) {
|
func (l *LocalFile) GetContentAsYAMLNode() (*yaml.Node, error) {
|
||||||
if l.parsed != nil {
|
if l.parsed != nil {
|
||||||
return l.parsed, nil
|
return l.parsed, nil
|
||||||
}
|
}
|
||||||
if l.index != nil && l.index.root != nil {
|
if l.index != nil && l.index.root != nil {
|
||||||
return l.index.root, nil
|
return l.index.root, nil
|
||||||
}
|
}
|
||||||
if l.data == nil {
|
if l.data == nil {
|
||||||
return nil, fmt.Errorf("no data to parse for file: %s", l.fullPath)
|
return nil, fmt.Errorf("no data to parse for file: %s", l.fullPath)
|
||||||
}
|
}
|
||||||
var root yaml.Node
|
var root yaml.Node
|
||||||
err := yaml.Unmarshal(l.data, &root)
|
err := yaml.Unmarshal(l.data, &root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if l.index != nil && l.index.root == nil {
|
if l.index != nil && l.index.root == nil {
|
||||||
l.index.root = &root
|
l.index.root = &root
|
||||||
}
|
}
|
||||||
l.parsed = &root
|
l.parsed = &root
|
||||||
return &root, nil
|
return &root, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) GetFileExtension() FileExtension {
|
func (l *LocalFile) GetFileExtension() FileExtension {
|
||||||
return l.extension
|
return l.extension
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) GetFullPath() string {
|
func (l *LocalFile) GetFullPath() string {
|
||||||
return l.fullPath
|
return l.fullPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) GetErrors() []error {
|
func (l *LocalFile) GetErrors() []error {
|
||||||
return l.readingErrors
|
return l.readingErrors
|
||||||
}
|
}
|
||||||
|
|
||||||
type LocalFSConfig struct {
|
type LocalFSConfig struct {
|
||||||
// the base directory to index
|
// the base directory to index
|
||||||
BaseDirectory string
|
BaseDirectory string
|
||||||
Logger *slog.Logger
|
Logger *slog.Logger
|
||||||
FileFilters []string
|
FileFilters []string
|
||||||
DirFS fs.FS
|
DirFS fs.FS
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalFSWithConfig(config *LocalFSConfig) (*LocalFS, error) {
|
func NewLocalFSWithConfig(config *LocalFSConfig) (*LocalFS, error) {
|
||||||
localFiles := make(map[string]RolodexFile)
|
localFiles := make(map[string]RolodexFile)
|
||||||
var allErrors []error
|
var allErrors []error
|
||||||
|
|
||||||
log := config.Logger
|
log := config.Logger
|
||||||
if log == nil {
|
if log == nil {
|
||||||
log = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
log = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
Level: slog.LevelError,
|
Level: slog.LevelError,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the basedir is an absolute file, we're just going to index that file.
|
// if the basedir is an absolute file, we're just going to index that file.
|
||||||
ext := filepath.Ext(config.BaseDirectory)
|
ext := filepath.Ext(config.BaseDirectory)
|
||||||
file := filepath.Base(config.BaseDirectory)
|
file := filepath.Base(config.BaseDirectory)
|
||||||
|
|
||||||
var absBaseDir string
|
var absBaseDir string
|
||||||
var absBaseErr error
|
absBaseDir, _ = filepath.Abs(config.BaseDirectory)
|
||||||
|
|
||||||
absBaseDir, absBaseErr = filepath.Abs(config.BaseDirectory)
|
walkErr := fs.WalkDir(config.DirFS, ".", func(p string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if absBaseErr != nil {
|
// we don't care about directories, or errors, just read everything we can.
|
||||||
return nil, absBaseErr
|
if d.IsDir() {
|
||||||
}
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
walkErr := fs.WalkDir(config.DirFS, ".", func(p string, d fs.DirEntry, err error) error {
|
if len(ext) > 2 && p != file {
|
||||||
if err != nil {
|
return nil
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// we don't care about directories, or errors, just read everything we can.
|
if strings.HasPrefix(p, ".") {
|
||||||
if d == nil || d.IsDir() {
|
return nil
|
||||||
return nil
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if len(ext) > 2 && p != file {
|
if len(config.FileFilters) > 0 {
|
||||||
return nil
|
if !slices.Contains(config.FileFilters, p) {
|
||||||
}
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(p, ".") {
|
extension := ExtractFileType(p)
|
||||||
return nil
|
var readingErrors []error
|
||||||
}
|
abs, _ := filepath.Abs(filepath.Join(config.BaseDirectory, p))
|
||||||
|
|
||||||
if len(config.FileFilters) > 0 {
|
var fileData []byte
|
||||||
if !slices.Contains(config.FileFilters, p) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension := ExtractFileType(p)
|
switch extension {
|
||||||
var readingErrors []error
|
case YAML, JSON:
|
||||||
abs, absErr := filepath.Abs(filepath.Join(config.BaseDirectory, p))
|
|
||||||
if absErr != nil {
|
|
||||||
readingErrors = append(readingErrors, absErr)
|
|
||||||
log.Error("cannot create absolute path for file: ", "file", p, "error", absErr.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
var fileData []byte
|
dirFile, _ := config.DirFS.Open(p)
|
||||||
|
modTime := time.Now()
|
||||||
|
stat, _ := dirFile.Stat()
|
||||||
|
if stat != nil {
|
||||||
|
modTime = stat.ModTime()
|
||||||
|
}
|
||||||
|
fileData, _ = io.ReadAll(dirFile)
|
||||||
|
log.Debug("collecting JSON/YAML file", "file", abs)
|
||||||
|
localFiles[abs] = &LocalFile{
|
||||||
|
filename: p,
|
||||||
|
name: filepath.Base(p),
|
||||||
|
extension: ExtractFileType(p),
|
||||||
|
data: fileData,
|
||||||
|
fullPath: abs,
|
||||||
|
lastModified: modTime,
|
||||||
|
readingErrors: readingErrors,
|
||||||
|
}
|
||||||
|
case UNSUPPORTED:
|
||||||
|
log.Debug("skipping non JSON/YAML file", "file", abs)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
switch extension {
|
if walkErr != nil {
|
||||||
case YAML, JSON:
|
return nil, walkErr
|
||||||
|
}
|
||||||
|
|
||||||
dirFile, readErr := config.DirFS.Open(p)
|
return &LocalFS{
|
||||||
modTime := time.Now()
|
Files: localFiles,
|
||||||
if readErr != nil {
|
logger: log,
|
||||||
allErrors = append(allErrors, readErr)
|
baseDirectory: absBaseDir,
|
||||||
log.Error("[rolodex] cannot open file: ", "file", abs, "error", readErr.Error())
|
entryPointDirectory: config.BaseDirectory,
|
||||||
return nil
|
readingErrors: allErrors,
|
||||||
}
|
}, nil
|
||||||
stat, statErr := dirFile.Stat()
|
|
||||||
if statErr != nil {
|
|
||||||
allErrors = append(allErrors, statErr)
|
|
||||||
log.Error("[rolodex] cannot stat file: ", "file", abs, "error", statErr.Error())
|
|
||||||
}
|
|
||||||
if stat != nil {
|
|
||||||
modTime = stat.ModTime()
|
|
||||||
}
|
|
||||||
fileData, readErr = io.ReadAll(dirFile)
|
|
||||||
if readErr != nil {
|
|
||||||
allErrors = append(allErrors, readErr)
|
|
||||||
log.Error("cannot read file data: ", "file", abs, "error", readErr.Error())
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debug("collecting JSON/YAML file", "file", abs)
|
|
||||||
localFiles[abs] = &LocalFile{
|
|
||||||
filename: p,
|
|
||||||
name: filepath.Base(p),
|
|
||||||
extension: ExtractFileType(p),
|
|
||||||
data: fileData,
|
|
||||||
fullPath: abs,
|
|
||||||
lastModified: modTime,
|
|
||||||
readingErrors: readingErrors,
|
|
||||||
}
|
|
||||||
case UNSUPPORTED:
|
|
||||||
log.Debug("skipping non JSON/YAML file", "file", abs)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if walkErr != nil {
|
|
||||||
return nil, walkErr
|
|
||||||
}
|
|
||||||
|
|
||||||
return &LocalFS{
|
|
||||||
Files: localFiles,
|
|
||||||
logger: log,
|
|
||||||
baseDirectory: absBaseDir,
|
|
||||||
entryPointDirectory: config.BaseDirectory,
|
|
||||||
readingErrors: allErrors,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalFS(baseDir string, dirFS fs.FS) (*LocalFS, error) {
|
func NewLocalFS(baseDir string, dirFS fs.FS) (*LocalFS, error) {
|
||||||
config := &LocalFSConfig{
|
config := &LocalFSConfig{
|
||||||
BaseDirectory: baseDir,
|
BaseDirectory: baseDir,
|
||||||
DirFS: dirFS,
|
DirFS: dirFS,
|
||||||
}
|
}
|
||||||
return NewLocalFSWithConfig(config)
|
return NewLocalFSWithConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) FullPath() string {
|
func (l *LocalFile) FullPath() string {
|
||||||
return l.fullPath
|
return l.fullPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) Name() string {
|
func (l *LocalFile) Name() string {
|
||||||
return l.name
|
return l.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) Size() int64 {
|
func (l *LocalFile) Size() int64 {
|
||||||
return int64(len(l.data))
|
return int64(len(l.data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) Mode() fs.FileMode {
|
func (l *LocalFile) Mode() fs.FileMode {
|
||||||
return fs.FileMode(0)
|
return fs.FileMode(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) ModTime() time.Time {
|
func (l *LocalFile) ModTime() time.Time {
|
||||||
return l.lastModified
|
return l.lastModified
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) IsDir() bool {
|
func (l *LocalFile) IsDir() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalFile) Sys() interface{} {
|
func (l *LocalFile) Sys() interface{} {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type localRolodexFile struct {
|
func (l *LocalFile) Close() error {
|
||||||
f RolodexFile
|
return nil
|
||||||
offset int64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *localRolodexFile) Close() error {
|
func (l *LocalFile) Stat() (fs.FileInfo, error) {
|
||||||
return nil
|
return l, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *localRolodexFile) Stat() (fs.FileInfo, error) {
|
func (l *LocalFile) Read(b []byte) (int, error) {
|
||||||
return r.f, nil
|
if l.offset >= int64(len(l.GetContent())) {
|
||||||
}
|
return 0, io.EOF
|
||||||
|
}
|
||||||
func (r *localRolodexFile) Read(b []byte) (int, error) {
|
if l.offset < 0 {
|
||||||
if r.offset >= int64(len(r.f.GetContent())) {
|
return 0, &fs.PathError{Op: "read", Path: l.GetFullPath(), Err: fs.ErrInvalid}
|
||||||
return 0, io.EOF
|
}
|
||||||
}
|
n := copy(b, l.GetContent()[l.offset:])
|
||||||
if r.offset < 0 {
|
l.offset += int64(n)
|
||||||
return 0, &fs.PathError{Op: "read", Path: r.f.GetFullPath(), Err: fs.ErrInvalid}
|
return n, nil
|
||||||
}
|
|
||||||
n := copy(b, r.f.GetContent()[r.offset:])
|
|
||||||
r.offset += int64(n)
|
|
||||||
return n, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,26 +4,175 @@
|
|||||||
package index
|
package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"io"
|
||||||
"testing/fstest"
|
"io/fs"
|
||||||
"time"
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"testing/fstest"
|
||||||
|
"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()},
|
||||||
"subfolder/spec1.json": {Data: []byte("hop"), ModTime: time.Now()},
|
"spock.yaml": {Data: []byte("hip: : hello: :\n:hw"), ModTime: time.Now()},
|
||||||
"subfolder2/spec2.yaml": {Data: []byte("chop"), ModTime: time.Now()},
|
"subfolder/spec1.json": {Data: []byte("hop"), ModTime: time.Now()},
|
||||||
"subfolder2/hello.jpg": {Data: []byte("shop"), ModTime: time.Now()},
|
"subfolder2/spec2.yaml": {Data: []byte("chop"), 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()
|
||||||
|
assert.Len(t, files, 4)
|
||||||
|
assert.Len(t, fileFS.GetErrors(), 0)
|
||||||
|
|
||||||
|
key, _ := filepath.Abs(filepath.Join(fileFS.baseDirectory, "spec.yaml"))
|
||||||
|
|
||||||
|
localFile := files[key]
|
||||||
|
assert.NotNil(t, localFile)
|
||||||
|
assert.Nil(t, localFile.GetIndex())
|
||||||
|
|
||||||
|
lf := localFile.(*LocalFile)
|
||||||
|
idx, ierr := lf.Index(CreateOpenAPIIndexConfig())
|
||||||
|
assert.NoError(t, ierr)
|
||||||
|
assert.NotNil(t, idx)
|
||||||
|
assert.NotNil(t, localFile.GetContent())
|
||||||
|
|
||||||
|
d, e := localFile.GetContentAsYAMLNode()
|
||||||
|
assert.NoError(t, e)
|
||||||
|
assert.NotNil(t, d)
|
||||||
|
assert.NotNil(t, localFile.GetIndex())
|
||||||
|
assert.Equal(t, YAML, localFile.GetFileExtension())
|
||||||
|
assert.Equal(t, key, localFile.GetFullPath())
|
||||||
|
assert.Equal(t, "spec.yaml", lf.Name())
|
||||||
|
assert.Equal(t, int64(3), lf.Size())
|
||||||
|
assert.Equal(t, fs.FileMode(0), lf.Mode())
|
||||||
|
assert.False(t, lf.IsDir())
|
||||||
|
assert.Equal(t, time.Now().Unix(), lf.ModTime().Unix())
|
||||||
|
assert.Nil(t, lf.Sys())
|
||||||
|
assert.Nil(t, lf.Close())
|
||||||
|
q, w := lf.Stat()
|
||||||
|
assert.NotNil(t, q)
|
||||||
|
assert.NoError(t, w)
|
||||||
|
|
||||||
|
b, x := io.ReadAll(lf)
|
||||||
|
assert.Len(t, b, 3)
|
||||||
|
assert.NoError(t, x)
|
||||||
|
|
||||||
|
assert.Equal(t, key, lf.FullPath())
|
||||||
|
assert.Len(t, localFile.GetErrors(), 0)
|
||||||
|
|
||||||
|
// try and reindex
|
||||||
|
idx, ierr = lf.Index(CreateOpenAPIIndexConfig())
|
||||||
|
assert.NoError(t, ierr)
|
||||||
|
assert.NotNil(t, idx)
|
||||||
|
|
||||||
|
key, _ = filepath.Abs(filepath.Join(fileFS.baseDirectory, "spock.yaml"))
|
||||||
|
|
||||||
|
localFile = files[key]
|
||||||
|
assert.NotNil(t, localFile)
|
||||||
|
assert.Nil(t, localFile.GetIndex())
|
||||||
|
|
||||||
|
lf = localFile.(*LocalFile)
|
||||||
|
idx, ierr = lf.Index(CreateOpenAPIIndexConfig())
|
||||||
|
assert.Error(t, ierr)
|
||||||
|
assert.Nil(t, idx)
|
||||||
|
assert.NotNil(t, localFile.GetContent())
|
||||||
|
assert.Nil(t, localFile.GetIndex())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFS_NoConfig(t *testing.T) {
|
||||||
|
|
||||||
|
lfs := &LocalFS{}
|
||||||
|
f, e := lfs.Open("test.yaml")
|
||||||
|
assert.Nil(t, f)
|
||||||
|
assert.Error(t, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFS_NoLookup(t *testing.T) {
|
||||||
|
|
||||||
|
cf := CreateClosedAPIIndexConfig()
|
||||||
|
lfs := &LocalFS{indexConfig: cf}
|
||||||
|
f, e := lfs.Open("test.yaml")
|
||||||
|
assert.Nil(t, f)
|
||||||
|
assert.Error(t, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFS_BadAbsFile(t *testing.T) {
|
||||||
|
|
||||||
|
cf := CreateOpenAPIIndexConfig()
|
||||||
|
lfs := &LocalFS{indexConfig: cf}
|
||||||
|
f, e := lfs.Open("/test.yaml")
|
||||||
|
assert.Nil(t, f)
|
||||||
|
assert.Error(t, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFile_BadParse(t *testing.T) {
|
||||||
|
|
||||||
|
lf := &LocalFile{}
|
||||||
|
n, e := lf.GetContentAsYAMLNode()
|
||||||
|
assert.Nil(t, n)
|
||||||
|
assert.Error(t, e)
|
||||||
|
assert.Equal(t, "no data to parse for file: ", e.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFile_NoIndexRoot(t *testing.T) {
|
||||||
|
|
||||||
|
lf := &LocalFile{data: []byte("burders"), index: &SpecIndex{}}
|
||||||
|
n, e := lf.GetContentAsYAMLNode()
|
||||||
|
assert.NotNil(t, n)
|
||||||
|
assert.NoError(t, e)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFile_IndexSingleFile(t *testing.T) {
|
||||||
|
|
||||||
|
testFS := fstest.MapFS{
|
||||||
|
"spec.yaml": {Data: []byte("hip"), ModTime: time.Now()},
|
||||||
|
"spock.yaml": {Data: []byte("hop"), ModTime: time.Now()},
|
||||||
|
"i-am-a-dir": {Mode: fs.FileMode(fs.ModeDir), ModTime: time.Now()},
|
||||||
|
}
|
||||||
|
|
||||||
|
fileFS, _ := NewLocalFS("spec.yaml", testFS)
|
||||||
|
files := fileFS.GetFiles()
|
||||||
|
assert.Len(t, files, 1)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFile_TestFilters(t *testing.T) {
|
||||||
|
|
||||||
|
testFS := fstest.MapFS{
|
||||||
|
"spec.yaml": {Data: []byte("hip"), ModTime: time.Now()},
|
||||||
|
"spock.yaml": {Data: []byte("pip"), ModTime: time.Now()},
|
||||||
|
"jam.jpg": {Data: []byte("sip"), ModTime: time.Now()},
|
||||||
|
}
|
||||||
|
|
||||||
|
fileFS, _ := NewLocalFSWithConfig(&LocalFSConfig{
|
||||||
|
BaseDirectory: ".",
|
||||||
|
FileFilters: []string{"spec.yaml", "spock.yaml", "jam.jpg"},
|
||||||
|
DirFS: testFS,
|
||||||
|
})
|
||||||
|
files := fileFS.GetFiles()
|
||||||
|
assert.Len(t, files, 2)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRolodexLocalFile_TestBasFS(t *testing.T) {
|
||||||
|
|
||||||
|
testFS := test_badfs{}
|
||||||
|
|
||||||
|
fileFS, err := NewLocalFSWithConfig(&LocalFSConfig{
|
||||||
|
BaseDirectory: ".",
|
||||||
|
DirFS: &testFS,
|
||||||
|
})
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, fileFS)
|
||||||
|
|
||||||
assert.Len(t, fileFS.Files, 3)
|
|
||||||
assert.Len(t, fileFS.readingErrors, 0)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,10 +96,10 @@ type test_badfs struct {
|
|||||||
|
|
||||||
func (t *test_badfs) Open(v string) (fs.File, error) {
|
func (t *test_badfs) Open(v string) (fs.File, error) {
|
||||||
ok := false
|
ok := false
|
||||||
if v != "/" && v != "http://localhost/test.yaml" {
|
if v != "/" && v != "." && v != "http://localhost/test.yaml" {
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
if v == "http://localhost/goodstat.yaml" || v == "goodstat.yaml" {
|
if v == "http://localhost/goodstat.yaml" || strings.HasSuffix(v, "goodstat.yaml") {
|
||||||
ok = true
|
ok = true
|
||||||
t.goodstat = true
|
t.goodstat = true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user