Skip to content

Commit c557959

Browse files
suyashkumarnickgeorge
authored andcommitted
Update integration_test.go so that it can function without Bazel, move integration_test and perf_test into folders named integration and perf respectively to fit what go modules expects.
PiperOrigin-RevId: 345077990
1 parent 9df72b6 commit c557959

File tree

5 files changed

+76
-59
lines changed

5 files changed

+76
-59
lines changed

Diff for: go/jsonformat/test/BUILD

-44
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,7 @@ package(default_visibility = ["//visibility:public"])
22

33
licenses(["notice"])
44

5-
load("@io_bazel_rules_go//go:def.bzl", "go_test")
6-
7-
go_test(
8-
name = "integration_test",
9-
size = "large",
10-
srcs = ["integration_test.go"],
11-
data = [
12-
"//spec:r4_examples",
13-
"//spec:stu3",
14-
"//testdata/r4:bigquery",
15-
"//testdata/r4:examples",
16-
"//testdata/stu3:bigquery",
17-
"//testdata/stu3:examples",
18-
],
19-
deps = [
20-
"//go/jsonformat",
21-
"//go/jsonformat/internal/jsonpbhelper",
22-
"//go/jsonformat/internal/protopath",
23-
"//proto/google/fhir/proto/r4/core/resources:bundle_and_contained_resource_go_proto",
24-
"//proto/google/fhir/proto/r4/core/resources:composition_go_proto",
25-
"//proto/google/fhir/proto/r4/core/resources:observation_go_proto",
26-
"//proto/google/fhir/proto/r4/core/resources:patient_go_proto",
27-
"//proto/google/fhir/proto/stu3:resources_go_proto",
28-
"@com_bitbucket_creachadair_stringset//:go_default_library",
29-
"@com_github_golang_protobuf//proto:go_default_library",
30-
"@com_github_google_go_cmp//cmp:go_default_library",
31-
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
32-
"@org_golang_google_protobuf//testing/protocmp:go_default_library",
33-
],
34-
)
35-
365
filegroup(
376
name = "testdata",
387
srcs = glob(["testdata/*.json"]),
398
)
40-
41-
go_test(
42-
name = "perf_test",
43-
size = "small",
44-
srcs = ["perf_test.go"],
45-
data = [
46-
":testdata",
47-
],
48-
deps = [
49-
"//go/jsonformat",
50-
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
51-
],
52-
)

Diff for: go/jsonformat/test/integration/BUILD

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
licenses(["notice"])
4+
5+
load("@io_bazel_rules_go//go:def.bzl", "go_test")
6+
7+
go_test(
8+
name = "integration_test",
9+
size = "large",
10+
srcs = ["integration_test.go"],
11+
data = [
12+
"//spec:r4_examples",
13+
"//spec:stu3",
14+
"//testdata/r4:bigquery",
15+
"//testdata/r4:examples",
16+
"//testdata/stu3:bigquery",
17+
"//testdata/stu3:examples",
18+
],
19+
deps = [
20+
"//go/jsonformat",
21+
"//go/jsonformat/internal/jsonpbhelper",
22+
"//go/jsonformat/internal/protopath",
23+
"//proto/google/fhir/proto/r4/core/resources:bundle_and_contained_resource_go_proto",
24+
"//proto/google/fhir/proto/r4/core/resources:composition_go_proto",
25+
"//proto/google/fhir/proto/r4/core/resources:observation_go_proto",
26+
"//proto/google/fhir/proto/r4/core/resources:patient_go_proto",
27+
"//proto/google/fhir/proto/stu3:resources_go_proto",
28+
"@com_bitbucket_creachadair_stringset//:go_default_library",
29+
"@com_github_golang_protobuf//proto:go_default_library",
30+
"@com_github_google_go_cmp//cmp:go_default_library",
31+
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
32+
"@org_golang_google_protobuf//testing/protocmp:go_default_library",
33+
],
34+
)

Diff for: go/jsonformat/test/integration_test.go renamed to go/jsonformat/test/integration/integration_test.go

+24-15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"strings"
2828
"testing"
2929

30+
"runtime"
31+
3032
"github.com/bazelbuild/rules_go/go/tools/bazel"
3133
"github.com/google/fhir/go/jsonformat/internal/jsonpbhelper"
3234
"github.com/google/fhir/go/jsonformat/internal/protopath"
@@ -532,18 +534,33 @@ func newContainedResource(ver jsonformat.Version) (proto.Message, error) {
532534
}
533535
}
534536

537+
var (
538+
_, b, _, _ = runtime.Caller(0)
539+
callerRoot = filepath.Dir(b)
540+
)
541+
// getRootPath returns the root bazel runfiles path if running in a bazel
542+
// environment, otherwise it will return the root path of the FHIR proto
543+
// repository. Typically this is used to access files such as testdata.
544+
// As usual, the caller should check to see if the expected file(s)
545+
// exist, and if not, report an error.
546+
func getRootPath() string {
547+
var root string
548+
root, err := bazel.RunfilesPath()
549+
if err != nil {
550+
// Fall back to the non-bazel way to get to the root directory.
551+
root = callerRoot + "/../../../../"
552+
}
553+
return root
554+
}
555+
535556
// readTestCases reads the JSON example files and returns the ones that have a
536557
// matching prototxt file, to be used as a test case.
537558
func readTestCases(t *testing.T, ver jsonformat.Version) []string {
538559
t.Helper()
539560
fileNames := readTestCaseFileNames(t, versionToJSONPath[ver])
540561
var withGolden []string
541562
for _, fileName := range fileNames {
542-
root, err := bazel.RunfilesPath()
543-
if err != nil {
544-
t.Fatalf("failed to read runfiles root: %v", err)
545-
}
546-
fullPath := path.Join(root, versionToProtoPath[ver], fmt.Sprintf("%s.%s", fileName, txtprotoExt))
563+
fullPath := path.Join(getRootPath(), versionToProtoPath[ver], fmt.Sprintf("%s.%s", fileName, txtprotoExt))
547564
if _, err := os.Stat(fullPath); err != nil {
548565
if os.IsNotExist(err) {
549566
continue
@@ -571,11 +588,7 @@ func readTestCaseFile(ver jsonformat.Version, name string) ([]byte, []byte, erro
571588

572589
func readTestCaseFileNames(t *testing.T, basePath string) []string {
573590
t.Helper()
574-
rp, err := bazel.RunfilesPath()
575-
if err != nil {
576-
t.Fatalf("Failed to get runfiles tree path: %v", err)
577-
}
578-
files, err := filepath.Glob(path.Join(rp, fmt.Sprintf("%s*.json", basePath)))
591+
files, err := filepath.Glob(path.Join(getRootPath(), fmt.Sprintf("%s*.json", basePath)))
579592
if err != nil {
580593
t.Fatalf("Failed to read test cases with err: %v", err)
581594
}
@@ -591,11 +604,7 @@ func readTestCaseFileNames(t *testing.T, basePath string) []string {
591604
}
592605

593606
func readFile(basePath, fileName, ext string) ([]byte, error) {
594-
root, err := bazel.RunfilesPath()
595-
if err != nil {
596-
return nil, err
597-
}
598-
filePath := path.Join(root, basePath, fmt.Sprintf("%s.%s", fileName, ext))
607+
filePath := path.Join(getRootPath(), basePath, fmt.Sprintf("%s.%s", fileName, ext))
599608
return ioutil.ReadFile(filePath)
600609
}
601610

Diff for: go/jsonformat/test/perf/BUILD

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
licenses(["notice"])
4+
5+
load("@io_bazel_rules_go//go:def.bzl", "go_test")
6+
7+
go_test(
8+
name = "perf_test",
9+
size = "small",
10+
srcs = ["perf_test.go"],
11+
data = [
12+
"//go/jsonformat/test:testdata",
13+
],
14+
deps = [
15+
"//go/jsonformat",
16+
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
17+
],
18+
)
File renamed without changes.

0 commit comments

Comments
 (0)