Skip to content

Commit be8a2ec

Browse files
authored
Hardening for tests (#15677)
1 parent 7f6dc92 commit be8a2ec

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

mmv1/api/resource_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"path/filepath"
66
"reflect"
7+
"runtime"
78
"slices"
89
"strings"
910
"testing"
@@ -325,11 +326,12 @@ func TestLeafProperties(t *testing.T) {
325326
// to files relative to this location will remain valid even if the repository structure
326327
// changes or the source is downloaded without git metadata.
327328
func TestMagicianLocation(t *testing.T) {
328-
// Get the current working directory of the test
329-
pwd, err := os.Getwd()
330-
if err != nil {
331-
t.Fatalf("Failed to get working directory: %v", err)
329+
// Get the path where this test file is located
330+
_, testFilePath, _, ok := runtime.Caller(0)
331+
if !ok {
332+
t.Fatal("Failed to get current test file path")
332333
}
334+
pwd := filepath.Dir(testFilePath)
333335

334336
// Walk up directories until we either:
335337
// 1. Find the mmv1 directory

mmv1/openapi_generate/parser_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package openapi_generate
22

33
import (
4-
"context"
5-
"github.com/getkin/kin-openapi/openapi3"
4+
_ "embed"
65
"testing"
6+
7+
"github.com/getkin/kin-openapi/openapi3"
78
)
89

10+
//go:embed test_data/test_api.yaml
11+
var testData []byte
12+
913
func TestMapType(t *testing.T) {
1014
_ = NewOpenapiParser("/fake", "/fake")
11-
ctx := context.Background()
15+
ctx := t.Context()
1216
loader := &openapi3.Loader{Context: ctx, IsExternalRefsAllowed: true}
13-
doc, _ := loader.LoadFromFile("./test_data/test_api.yaml")
17+
doc, _ := loader.LoadFromData(testData)
1418
_ = doc.Validate(ctx)
1519

1620
petSchema := doc.Paths.Map()["/pets"].Post.Parameters[0].Value.Schema

0 commit comments

Comments
 (0)