File: options_test.go

package info (click to toggle)
golang-github-go-openapi-loads 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 1,208 kB
  • sloc: makefile: 4
file content (118 lines) | stat: -rw-r--r-- 3,200 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package loads

import (
	"encoding/json"
	"errors"
	"os"
	"path/filepath"
	"testing"

	"github.com/go-openapi/swag"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

const optionFixture = "fixtures/json/resources/pathLoaderIssue.json"

func TestOptionsWithDocLoader(t *testing.T) {
	document, err := Spec(optionFixture, WithDocLoader(func(pth string) (json.RawMessage, error) {
		buf, err := os.ReadFile(pth)
		return json.RawMessage(buf), err
	}))
	require.NoError(t, err)
	require.NotNil(t, document)
	require.NotNil(t, document.pathLoader)

	b, err := document.pathLoader.Load(optionFixture)
	require.NoError(t, err)

	trimmed, err := trimData(b)
	require.NoError(t, err)

	assert.EqualValues(t, trimmed, document.Raw())

	// a nil loader is a no op
	_, err = Spec(optionFixture, WithDocLoader(nil))
	require.NoError(t, err)
}

func TestOptionsLoaderFromOptions(t *testing.T) {
	var called int

	// not chaining here, just replacing with the last one
	l := loaderFromOptions([]LoaderOption{
		WithDocLoader(func(pth string) (json.RawMessage, error) {
			called = 1
			buf, err := os.ReadFile(pth)
			return json.RawMessage(buf), err
		}),
		WithDocLoader(func(pth string) (json.RawMessage, error) {
			called = 2
			buf, err := os.ReadFile(pth)
			return json.RawMessage(buf), err
		}),
	})
	require.NotNil(t, l)

	b, err := l.Load(optionFixture)
	require.NoError(t, err)
	require.NotNil(t, b)

	require.Equal(t, 2, called)
}

func TestOptionsWithDocLoaderMatches(t *testing.T) {
	jsonLoader := NewDocLoaderWithMatch(
		func(pth string) (json.RawMessage, error) {
			buf, err := os.ReadFile(pth)
			return json.RawMessage(buf), err
		},
		func(pth string) bool {
			return filepath.Ext(pth) == ".json"
		},
	)

	document, err := Spec(optionFixture, WithDocLoaderMatches(jsonLoader))
	require.NoError(t, err)
	require.NotNil(t, document)
	require.NotNil(t, document.pathLoader)

	yamlLoader := NewDocLoaderWithMatch(
		swag.YAMLDoc,
		func(pth string) bool {
			return filepath.Ext(pth) == ".yaml"
		},
	)

	document, err = Spec(optionFixture, WithDocLoaderMatches(yamlLoader))
	require.Error(t, err)
	require.Nil(t, document)

	// chained loaders, with different ordering
	document, err = Spec(optionFixture, WithDocLoaderMatches(yamlLoader, jsonLoader))
	require.NoError(t, err)
	require.NotNil(t, document)

	document, err = Spec(optionFixture, WithDocLoaderMatches(jsonLoader, yamlLoader))
	require.NoError(t, err)
	require.NotNil(t, document)

	// the nil loader is a no op
	nilLoader := NewDocLoaderWithMatch(nil, nil)
	document, err = Spec(optionFixture, WithDocLoaderMatches(nilLoader, jsonLoader, yamlLoader))
	require.NoError(t, err)
	require.NotNil(t, document)

	// the nil matcher always matches
	nilMatcher := NewDocLoaderWithMatch(func(_ string) (json.RawMessage, error) {
		return nil, errors.New("test")
	}, nil)
	_, err = Spec(optionFixture, WithDocLoaderMatches(nilMatcher))
	require.Error(t, err)
	require.Equal(t, "test", err.Error())

	// when a matcher returns an errors, the next one is tried
	document, err = Spec(optionFixture, WithDocLoaderMatches(nilMatcher, jsonLoader, yamlLoader))
	require.NoError(t, err)
	require.NotNil(t, document)
}