bump coverage

removed dead code after abs path change on lookup

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2024-01-15 14:07:45 -05:00
parent 46dc0d4630
commit 55a2065f8d
3 changed files with 127 additions and 149 deletions

View File

@@ -609,19 +609,6 @@ func (index *SpecIndex) ExtractComponentsFromRefs(refs []*Reference) []*Referenc
var refsToCheck []*Reference var refsToCheck []*Reference
for _, ref := range refs { for _, ref := range refs {
// check reference for backslashes (hah yeah seen this too!)
if strings.Contains(ref.Definition, "\\\\") {
_, path := utils.ConvertComponentIdIntoFriendlyPathSearch(ref.Definition)
indexError := &IndexingError{
Err: fmt.Errorf("component '%s' contains a backslash '\\'. It's not valid", ref.Definition),
Node: ref.Node,
Path: path,
}
index.refErrors = append(index.refErrors, indexError)
continue
}
refsToCheck = append(refsToCheck, ref) refsToCheck = append(refsToCheck, ref)
} }
mappedRefsInSequence := make([]*ReferenceMapped, len(refsToCheck)) mappedRefsInSequence := make([]*ReferenceMapped, len(refsToCheck))

View File

@@ -514,32 +514,30 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
if strings.HasPrefix(exp[0], "http") { if strings.HasPrefix(exp[0], "http") {
fullDef = value fullDef = value
} else { } else {
if filepath.IsAbs(exp[0]) {
fullDef = value if strings.HasPrefix(ref.FullDefinition, "http") {
// split the http URI into parts
httpExp := strings.Split(ref.FullDefinition, "#/")
u, _ := url.Parse(httpExp[0])
abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), exp[0], string(filepath.Separator)))
u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs)
u.Fragment = ""
fullDef = fmt.Sprintf("%s#/%s", u.String(), exp[1])
} else { } else {
if strings.HasPrefix(ref.FullDefinition, "http") {
// split the http URI into parts // split the referring ref full def into parts
httpExp := strings.Split(ref.FullDefinition, "#/") fileDef := strings.Split(ref.FullDefinition, "#/")
u, _ := url.Parse(httpExp[0]) // extract the location of the ref and build a full def path.
abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), exp[0], string(filepath.Separator))) abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(fileDef[0]), exp[0], string(filepath.Separator)))
u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) //abs = utils.ReplaceWindowsDriveWithLinuxPath(abs)
u.Fragment = "" fullDef = fmt.Sprintf("%s#/%s", abs, exp[1])
fullDef = fmt.Sprintf("%s#/%s", u.String(), exp[1])
} else {
// split the referring ref full def into parts
fileDef := strings.Split(ref.FullDefinition, "#/")
// extract the location of the ref and build a full def path.
abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(fileDef[0]), exp[0], string(filepath.Separator)))
//abs = utils.ReplaceWindowsDriveWithLinuxPath(abs)
fullDef = fmt.Sprintf("%s#/%s", abs, exp[1])
}
} }
} }
} else { } else {
// local component, full def is based on passed in ref // local component, full def is based on passed in ref
@@ -568,24 +566,21 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
if strings.HasPrefix(value, "http") { if strings.HasPrefix(value, "http") {
fullDef = value fullDef = value
} else { } else {
if filepath.IsAbs(value) {
fullDef = value // split the full def into parts
fileDef := strings.Split(ref.FullDefinition, "#/")
// is the file def a http link?
if strings.HasPrefix(fileDef[0], "http") {
u, _ := url.Parse(fileDef[0])
path, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), exp[0], string(filepath.Separator)))
u.Path = utils.ReplaceWindowsDriveWithLinuxPath(path)
fullDef = u.String()
} else { } else {
fullDef, _ = filepath.Abs(utils.CheckPathOverlap(filepath.Dir(fileDef[0]), exp[0], string(filepath.Separator)))
// split the full def into parts
fileDef := strings.Split(ref.FullDefinition, "#/")
// is the file def a http link?
if strings.HasPrefix(fileDef[0], "http") {
u, _ := url.Parse(fileDef[0])
path, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), exp[0], string(filepath.Separator)))
u.Path = utils.ReplaceWindowsDriveWithLinuxPath(path)
fullDef = u.String()
} else {
fullDef, _ = filepath.Abs(utils.CheckPathOverlap(filepath.Dir(fileDef[0]), exp[0], string(filepath.Separator)))
}
} }
} }
} }
@@ -691,24 +686,22 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
if strings.HasPrefix(l, "http") { if strings.HasPrefix(l, "http") {
def = l def = l
} else { } else {
if filepath.IsAbs(l) {
def = l
} else {
// check if were dealing with a remote file
if strings.HasPrefix(ref.FullDefinition, "http") {
// split the url. // check if were dealing with a remote file
u, _ := url.Parse(ref.FullDefinition) if strings.HasPrefix(ref.FullDefinition, "http") {
abs, _ := filepath.Abs(filepath.Join(filepath.Dir(u.Path), l))
u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) // split the url.
u.Fragment = "" u, _ := url.Parse(ref.FullDefinition)
def = u.String() abs, _ := filepath.Abs(filepath.Join(filepath.Dir(u.Path), l))
} else { u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs)
lookupRef := strings.Split(ref.FullDefinition, "#/") u.Fragment = ""
abs, _ := filepath.Abs(filepath.Join(filepath.Dir(lookupRef[0]), l)) def = u.String()
def = abs } else {
} lookupRef := strings.Split(ref.FullDefinition, "#/")
abs, _ := filepath.Abs(filepath.Join(filepath.Dir(lookupRef[0]), l))
def = abs
} }
} }
} }
@@ -814,24 +807,22 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
if strings.HasPrefix(l, "http") { if strings.HasPrefix(l, "http") {
def = l def = l
} else { } else {
if filepath.IsAbs(l) {
def = l
} else {
// check if were dealing with a remote file
if strings.HasPrefix(ref.FullDefinition, "http") {
// split the url. // check if were dealing with a remote file
u, _ := url.Parse(ref.FullDefinition) if strings.HasPrefix(ref.FullDefinition, "http") {
abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), l, string(filepath.Separator)))
u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs) // split the url.
u.Fragment = "" u, _ := url.Parse(ref.FullDefinition)
def = u.String() abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(u.Path), l, string(filepath.Separator)))
} else { u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs)
lookupRef := strings.Split(ref.FullDefinition, "#/") u.Fragment = ""
abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(lookupRef[0]), l, string(filepath.Separator))) def = u.String()
def = abs } else {
} lookupRef := strings.Split(ref.FullDefinition, "#/")
abs, _ := filepath.Abs(utils.CheckPathOverlap(filepath.Dir(lookupRef[0]), l, string(filepath.Separator)))
def = abs
} }
} }
} }

View File

@@ -1,88 +1,88 @@
package utils package utils
import ( import (
"os" "os"
"runtime" "runtime"
"testing" "testing"
) )
func TestReplaceWindowsDriveWithLinuxPath(t *testing.T) { func TestReplaceWindowsDriveWithLinuxPath(t *testing.T) {
path := `C:\Users\pb33f\go\src\github.com\pb33f\libopenapi\utils\windows_drive_test.go` path := `C:\Users\pb33f\go\src\github.com\pb33f\libopenapi\utils\windows_drive_test.go`
expected := `/Users/pb33f/go/src/github.com/pb33f/libopenapi/utils/windows_drive_test.go` expected := `/Users/pb33f/go/src/github.com/pb33f/libopenapi/utils/windows_drive_test.go`
result := ReplaceWindowsDriveWithLinuxPath(path) result := ReplaceWindowsDriveWithLinuxPath(path)
if result != expected { if result != expected {
t.Errorf("Expected %s, got %s", expected, result) t.Errorf("Expected %s, got %s", expected, result)
} }
path = `/do/not/replace/this/path` path = `/do/not/replace/this/path`
expected = `/do/not/replace/this/path` expected = `/do/not/replace/this/path`
result = ReplaceWindowsDriveWithLinuxPath(path) result = ReplaceWindowsDriveWithLinuxPath(path)
if result != expected { if result != expected {
t.Errorf("Expected %s, got %s", expected, result) t.Errorf("Expected %s, got %s", expected, result)
} }
} }
func TestCheckPathOverlap(t *testing.T) { func TestCheckPathOverlap(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
pathA := `C:\Users\pb33f` pathA := `C:\Users\pb33f`
pathB := `pb33f\files\thing.yaml` pathB := `pb33f\files\thing.yaml`
expected := `C:\Users\pb33f\files\thing.yaml` expected := `C:\Users\pb33f\files\thing.yaml`
result := CheckPathOverlap(pathA, pathB, string(os.PathSeparator)) result := CheckPathOverlap(pathA, pathB, string(os.PathSeparator))
if result != expected { if result != expected {
t.Errorf("Expected %s, got %s", expected, result) t.Errorf("Expected %s, got %s", expected, result)
} }
} else { } else {
pathA := `/Users/pb33f` pathA := `/Users/pb33f`
pathB := `pb33f/files/thing.yaml` pathB := `pb33f/files/thing.yaml`
expected := `/Users/pb33f/files/thing.yaml` expected := `/Users/pb33f/files/thing.yaml`
result := CheckPathOverlap(pathA, pathB, string(os.PathSeparator)) result := CheckPathOverlap(pathA, pathB, string(os.PathSeparator))
if result != expected { if result != expected {
t.Errorf("Expected %s, got %s", expected, result) t.Errorf("Expected %s, got %s", expected, result)
} }
} }
} }
func TestCheckPathOverlap_CheckSlash(t *testing.T) { func TestCheckPathOverlap_CheckSlash(t *testing.T) {
pathA := `/Users/pb33f` pathA := `/Users/pb33f`
pathB := `Users/pb33f\files\thing.yaml` pathB := `Users/pb33f\files\thing.yaml`
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
expected := `/Users/pb33f/files\thing.yaml` expected := `/Users/pb33f/files\thing.yaml`
result := CheckPathOverlap(pathA, pathB, `\`) result := CheckPathOverlap(pathA, pathB, `\`)
if result != expected { if result != expected {
t.Errorf("Expected %s, got %s", expected, result) t.Errorf("Expected %s, got %s", expected, result)
} }
} else { } else {
expected := `\Users\pb33f\files\thing.yaml` expected := `\Users\pb33f\files\thing.yaml`
result := CheckPathOverlap(pathA, pathB, `\`) result := CheckPathOverlap(pathA, pathB, `\`)
if result != expected { if result != expected {
t.Errorf("Expected %s, got %s", expected, result) t.Errorf("Expected %s, got %s", expected, result)
} }
} }
} }
func TestCheckPathOverlap_VariationA(t *testing.T) { func TestCheckPathOverlap_VariationA(t *testing.T) {
pathA := `/Users/pb33f` pathA := `/Users/pb33f`
pathB := `pb33f/files/thing.yaml` pathB := `pb33f/files/thing.yaml`
expected := `/Users/pb33f/files/thing.yaml` expected := `/Users/pb33f/files/thing.yaml`
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
expected = `\Users\pb33f\files\thing.yaml` expected = `\Users\pb33f\files\thing.yaml`
} }
result := CheckPathOverlap(pathA, pathB, `/`) result := CheckPathOverlap(pathA, pathB, `/`)
if result != expected { if result != expected {
t.Errorf("Expected %s, got %s", expected, result) t.Errorf("Expected %s, got %s", expected, result)
} }
} }
func TestCheckPathOverlap_VariationB(t *testing.T) { func TestCheckPathOverlap_VariationB(t *testing.T) {
pathA := `somewhere/pb33f` pathA := `somewhere/pb33f`
pathB := `pb33f/files/thing.yaml` pathB := `pb33f/files/thing.yaml`
expected := `somewhere/pb33f/files/thing.yaml` expected := `somewhere/pb33f/files/thing.yaml`
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
expected = `somewhere\pb33f\files\thing.yaml` expected = `somewhere\pb33f\files\thing.yaml`
} }
result := CheckPathOverlap(pathA, pathB, `/`) result := CheckPathOverlap(pathA, pathB, `/`)
if result != expected { if result != expected {
t.Errorf("Expected %s, got %s", expected, result) t.Errorf("Expected %s, got %s", expected, result)
} }
} }