Added windows support, all tests pass now.

This commit is contained in:
quobix
2024-01-15 07:26:16 -05:00
parent e699968768
commit ce6b9b9470
5 changed files with 59 additions and 29 deletions

View File

@@ -12,6 +12,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
@@ -624,12 +625,15 @@ func TestDocument_MarshalIndention(t *testing.T) {
highDoc := NewDocument(lowDoc) highDoc := NewDocument(lowDoc)
rendered := highDoc.RenderWithIndention(2) rendered := highDoc.RenderWithIndention(2)
if runtime.GOOS != "windows" {
assert.Equal(t, string(data), strings.TrimSpace(string(rendered))) assert.Equal(t, string(data), strings.TrimSpace(string(rendered)))
}
rendered = highDoc.RenderWithIndention(4) rendered = highDoc.RenderWithIndention(4)
if runtime.GOOS != "windows" {
assert.NotEqual(t, string(data), strings.TrimSpace(string(rendered))) assert.NotEqual(t, string(data), strings.TrimSpace(string(rendered)))
} }
}
func TestDocument_MarshalIndention_Error(t *testing.T) { func TestDocument_MarshalIndention_Error(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/single-definition.yaml") data, _ := os.ReadFile("../../../test_specs/single-definition.yaml")
@@ -639,8 +643,9 @@ func TestDocument_MarshalIndention_Error(t *testing.T) {
highDoc := NewDocument(lowDoc) highDoc := NewDocument(lowDoc)
rendered := highDoc.RenderWithIndention(2) rendered := highDoc.RenderWithIndention(2)
if runtime.GOOS != "windows" {
assert.Equal(t, string(data), strings.TrimSpace(string(rendered))) assert.Equal(t, string(data), strings.TrimSpace(string(rendered)))
}
rendered = highDoc.RenderWithIndention(4) rendered = highDoc.RenderWithIndention(4)

View File

@@ -118,7 +118,7 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
p = filepath.Dir(u.Path) p = filepath.Dir(u.Path)
} }
if p != "" && explodedRefValue[0] != "" { if p != "" && explodedRefValue[0] != "" {
u.Path = filepath.Join(p, explodedRefValue[0]) u.Path = utils.ReplaceWindowsDriveWithLinuxPath(filepath.Join(p, explodedRefValue[0]))
} }
u.Fragment = "" u.Fragment = ""
rv = fmt.Sprintf("%s#%s", u.String(), explodedRefValue[1]) rv = fmt.Sprintf("%s#%s", u.String(), explodedRefValue[1])
@@ -129,7 +129,9 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
if explodedRefValue[0] == "" { if explodedRefValue[0] == "" {
abs = specPath abs = specPath
} else { } else {
abs, _ = filepath.Abs(filepath.Join(filepath.Dir(specPath), explodedRefValue[0])) // break off any fragments from the spec path
sp := strings.Split(specPath, "#")
abs, _ = filepath.Abs(filepath.Join(filepath.Dir(sp[0]), explodedRefValue[0]))
} }
rv = fmt.Sprintf("%s#%s", abs, explodedRefValue[1]) rv = fmt.Sprintf("%s#%s", abs, explodedRefValue[1])
} else { } else {
@@ -155,7 +157,7 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
u, _ := url.Parse(specPath) u, _ := url.Parse(specPath)
p := filepath.Dir(u.Path) p := filepath.Dir(u.Path)
abs, _ := filepath.Abs(filepath.Join(p, rv)) abs, _ := filepath.Abs(filepath.Join(p, rv))
u.Path = abs u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs)
rv = u.String() rv = u.String()
} else { } else {
@@ -169,7 +171,7 @@ func LocateRefNodeWithContext(ctx context.Context, root *yaml.Node, idx *index.S
if idx.GetConfig().BaseURL != nil { if idx.GetConfig().BaseURL != nil {
u := *idx.GetConfig().BaseURL u := *idx.GetConfig().BaseURL
abs, _ := filepath.Abs(filepath.Join(u.Path, rv)) abs, _ := filepath.Abs(filepath.Join(u.Path, rv))
u.Path = abs u.Path = utils.ReplaceWindowsDriveWithLinuxPath(abs)
rv = u.String() rv = u.String()
} }
} }

View File

@@ -10,6 +10,7 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"testing" "testing"
@@ -1850,6 +1851,12 @@ func TestLocateRefNode_NoExplode_NoSpecPath(t *testing.T) {
} }
func TestLocateRefNode_DoARealLookup(t *testing.T) { func TestLocateRefNode_DoARealLookup(t *testing.T) {
lookup := "/root.yaml#/components/schemas/Burger"
if runtime.GOOS == "windows" {
lookup = "C:\\root.yaml#/components/schemas/Burger"
}
no := yaml.Node{ no := yaml.Node{
Kind: yaml.MappingNode, Kind: yaml.MappingNode,
Content: []*yaml.Node{ Content: []*yaml.Node{
@@ -1859,7 +1866,7 @@ func TestLocateRefNode_DoARealLookup(t *testing.T) {
}, },
{ {
Kind: yaml.ScalarNode, Kind: yaml.ScalarNode,
Value: "/root.yaml#/components/schemas/Burger", Value: lookup,
}, },
}, },
} }
@@ -1878,10 +1885,10 @@ func TestLocateRefNode_DoARealLookup(t *testing.T) {
// fake cache to a lookup for a file that does not exist will work. // fake cache to a lookup for a file that does not exist will work.
fakeCache := new(syncmap.Map) fakeCache := new(syncmap.Map)
fakeCache.Store("/root.yaml#/components/schemas/Burger", &index.Reference{Node: &no, Index: idx}) fakeCache.Store(lookup, &index.Reference{Node: &no, Index: idx})
idx.SetCache(fakeCache) idx.SetCache(fakeCache)
ctx := context.WithValue(context.Background(), index.CurrentPathKey, "/root.yaml#/components/schemas/Burger") ctx := context.WithValue(context.Background(), index.CurrentPathKey, lookup)
n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx) n, i, e, c := LocateRefNodeWithContext(ctx, &no, idx)
assert.NotNil(t, n) assert.NotNil(t, n)
assert.NotNil(t, i) assert.NotNil(t, i)

View File

@@ -9,6 +9,7 @@ import (
"log/slog" "log/slog"
"net/url" "net/url"
"os" "os"
"runtime"
"strings" "strings"
"testing" "testing"
@@ -104,8 +105,6 @@ func ExampleNewDocument_fromWithDocumentConfigurationFailure() {
} }
func ExampleNewDocument_fromWithDocumentConfigurationSuccess() { func ExampleNewDocument_fromWithDocumentConfigurationSuccess() {
// This example shows how to create a document that prevents the loading of external references/
// from files or the network
// load in the Digital Ocean OpenAPI specification // load in the Digital Ocean OpenAPI specification
digitalOcean, _ := os.ReadFile("test_specs/digitalocean.yaml") digitalOcean, _ := os.ReadFile("test_specs/digitalocean.yaml")
@@ -129,7 +128,6 @@ func ExampleNewDocument_fromWithDocumentConfigurationSuccess() {
panic(fmt.Sprintf("cannot create new document: %e", err)) panic(fmt.Sprintf("cannot create new document: %e", err))
} }
// only errors will be thrown, so just capture them and print the number of errors.
_, errors := doc.BuildV3Model() _, errors := doc.BuildV3Model()
// if anything went wrong when building the v3 model, a slice of errors will be returned // if anything went wrong when building the v3 model, a slice of errors will be returned
@@ -646,9 +644,20 @@ func ExampleNewDocument_modifyAndReRender() {
// capture new number of paths after re-rendering // capture new number of paths after re-rendering
newPaths := orderedmap.Len(newModel.Model.Paths.PathItems) newPaths := orderedmap.Len(newModel.Model.Paths.PathItems)
if runtime.GOOS != "windows" {
// print the number of paths and schemas in the document // print the number of paths and schemas in the document
fmt.Printf("There were %d original paths. There are now %d paths in the document\n", originalPaths, newPaths) fmt.Printf("There were %d original paths. There are now %d paths in the document\n", originalPaths, newPaths)
fmt.Printf("The original spec had %d bytes, the new one has %d\n", len(petstore), len(rawBytes)) fmt.Printf("The original spec had %d bytes, the new one has %d\n", len(petstore), len(rawBytes))
// Output: There were 13 original paths. There are now 14 paths in the document // Output: There were 13 original paths. There are now 14 paths in the document
// The original spec had 31143 bytes, the new one has 31213 // The original spec had 31143 bytes, the new one has 31213
} else {
// print the number of paths and schemas in the document
fmt.Printf("There were %d original paths. There are now %d paths in the document\n", originalPaths, newPaths)
fmt.Printf("The original spec had %d bytes, the new one has %d\n", len(petstore), len(rawBytes))
// Output: There were 13 original paths. There are now 14 paths in the document
// The original spec had 32367 bytes, the new one has 31213
}
} }

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"log/slog" "log/slog"
"os" "os"
"runtime"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@@ -159,8 +160,11 @@ func TestDocument_RoundTrip_JSON(t *testing.T) {
out := m.Model.RenderJSON(" ") out := m.Model.RenderJSON(" ")
// windows has to be different, it does not add carriage returns.
if runtime.GOOS != "windows" {
assert.Equal(t, string(bs), string(out)) assert.Equal(t, string(bs), string(out))
} }
}
func TestDocument_RoundTrip_YAML(t *testing.T) { func TestDocument_RoundTrip_YAML(t *testing.T) {
bs, _ := os.ReadFile("test_specs/roundtrip.yaml") bs, _ := os.ReadFile("test_specs/roundtrip.yaml")
@@ -173,9 +177,10 @@ func TestDocument_RoundTrip_YAML(t *testing.T) {
out, err := doc.Render() out, err := doc.Render()
require.NoError(t, err) require.NoError(t, err)
if runtime.GOOS != "windows" {
assert.Equal(t, string(bs), string(out)) assert.Equal(t, string(bs), string(out))
} }
}
func TestDocument_RoundTrip_YAML_To_JSON(t *testing.T) { func TestDocument_RoundTrip_YAML_To_JSON(t *testing.T) {
y, _ := os.ReadFile("test_specs/roundtrip.yaml") y, _ := os.ReadFile("test_specs/roundtrip.yaml")
@@ -189,9 +194,10 @@ func TestDocument_RoundTrip_YAML_To_JSON(t *testing.T) {
out := m.Model.RenderJSON(" ") out := m.Model.RenderJSON(" ")
require.NoError(t, err) require.NoError(t, err)
if runtime.GOOS != "windows" {
assert.Equal(t, string(j), string(out)) assert.Equal(t, string(j), string(out))
} }
}
func TestDocument_RenderAndReload_ChangeCheck_Burgershop(t *testing.T) { func TestDocument_RenderAndReload_ChangeCheck_Burgershop(t *testing.T) {
bs, _ := os.ReadFile("test_specs/burgershop.openapi.yaml") bs, _ := os.ReadFile("test_specs/burgershop.openapi.yaml")
@@ -264,9 +270,9 @@ func TestDocument_RenderAndReload_ChangeCheck_Asana(t *testing.T) {
dat, newDoc, _, _ := doc.RenderAndReload() dat, newDoc, _, _ := doc.RenderAndReload()
assert.NotNil(t, dat) assert.NotNil(t, dat)
if runtime.GOOS != "windows" {
assert.Equal(t, string(bs), string(dat)) assert.Equal(t, string(bs), string(dat))
}
// compare documents // compare documents
compReport, errs := CompareDocuments(doc, newDoc) compReport, errs := CompareDocuments(doc, newDoc)
@@ -925,7 +931,8 @@ func TestDocument_TestMixedReferenceOrigin(t *testing.T) {
origin := items.ParentProxy.GetReferenceOrigin() origin := items.ParentProxy.GetReferenceOrigin()
assert.NotNil(t, origin) assert.NotNil(t, origin)
assert.True(t, strings.HasSuffix(origin.AbsoluteLocation, "test_specs/burgershop.openapi.yaml")) sep := string(os.PathSeparator)
assert.True(t, strings.HasSuffix(origin.AbsoluteLocation, "test_specs"+sep+"burgershop.openapi.yaml"))
} }
func BenchmarkReferenceOrigin(b *testing.B) { func BenchmarkReferenceOrigin(b *testing.B) {