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
|
package antest
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestLongTestEnabled(t *testing.T) {
t.Run("should be false by default", func(t *testing.T) {
require.False(t, LongTestsEnabled())
})
}
func TestLoadSpecErrorCases(t *testing.T) {
t.Run("should not load invalid path", func(t *testing.T) {
_, err := LoadSpec("nowhere.json")
require.Error(t, err)
})
t.Run("should not load invalid YAML", func(t *testing.T) {
invalidYAMLFile, clean := prepareBadDoc(t, "yaml", true)
t.Cleanup(clean)
_, err := LoadSpec(invalidYAMLFile)
require.Error(t, err)
})
t.Run("should not load invalid JSON", func(t *testing.T) {
invalidJSONFile, clean := prepareBadDoc(t, "json", true)
t.Cleanup(clean)
_, err := LoadSpec(invalidJSONFile)
require.Error(t, err)
})
t.Run("should not load invalid spec", func(t *testing.T) {
invalidJSONFile, clean := prepareBadDoc(t, "json", false)
t.Cleanup(clean)
_, err := LoadSpec(invalidJSONFile)
require.Error(t, err)
})
}
func prepareBadDoc(t testing.TB, kind string, invalidFormat bool) (string, func()) {
t.Helper()
var (
file string
data []byte
)
switch kind {
case "yaml", "yml":
f, err := os.CreateTemp("", "*.yaml")
require.NoError(t, err)
file = f.Name()
if invalidFormat {
data = []byte(`--
zig:
zag 3, 4
`)
} else {
data = []byte(`--
swagger: 2
info:
title: true
`)
}
case "json":
f, err := os.CreateTemp("", "*.json")
require.NoError(t, err)
file = f.Name()
if invalidFormat {
data = []byte(`{
"zig": {
"zag"
}`)
} else {
data = []byte(`{
"swagger": 2
"info": {
"title": true
}
}`)
}
default:
panic("supports only yaml or json")
}
require.NoError(t,
os.WriteFile(file, data, 0600),
)
return file, func() {
_ = os.RemoveAll(file)
}
}
|