From c6eecb56f90e04d0565f99053d0ba85b8e91ac71 Mon Sep 17 00:00:00 2001 From: Nick George Date: Wed, 20 May 2020 14:49:07 -0700 Subject: [PATCH] Makes GeneratedProtoTest read core dependencies from bazel rather than assuming local, which enables GeneratedProtoTest to work both within the core lib and externally. PiperOrigin-RevId: 312559760 --- bazel/protogen.bzl | 2 ++ cc/google/fhir/BUILD | 1 - .../protogen/ProtoGeneratorTestUtils.java | 28 +++++++++++-------- .../fhir/protogen/GeneratedProtoTest.java | 8 +++++- .../fhir/protogen/ProtoGeneratorTest.java | 5 +++- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/bazel/protogen.bzl b/bazel/protogen.bzl index 96abe9fcd..3f61e4ebe 100644 --- a/bazel/protogen.bzl +++ b/bazel/protogen.bzl @@ -143,6 +143,8 @@ def gen_fhir_protos( "-Drule_name=" + name, "-Ddependencies=" + deps_test_flag, "-Dimports=" + additional_import_test_flag, + "-Dstu3_core_dep=$(location %s)" % _get_zip_for_pkg(STU3_PACKAGE_DEP), + "-Dr4_core_dep=$(location %s)" % _get_zip_for_pkg(R4_PACKAGE_DEP), ] native.java_test( diff --git a/cc/google/fhir/BUILD b/cc/google/fhir/BUILD index 539db2815..8829a4012 100644 --- a/cc/google/fhir/BUILD +++ b/cc/google/fhir/BUILD @@ -121,7 +121,6 @@ cc_library( "//proto/r4/core:datatypes_cc_proto", "//proto/stu3:datatypes_cc_proto", "@com_google_absl//absl/status", - "@org_tensorflow//tensorflow/core:lib", "@com_googlesource_code_re2//:re2", ], ) diff --git a/java/src/main/java/com/google/fhir/protogen/ProtoGeneratorTestUtils.java b/java/src/main/java/com/google/fhir/protogen/ProtoGeneratorTestUtils.java index d6600ae76..3fdef5dba 100644 --- a/java/src/main/java/com/google/fhir/protogen/ProtoGeneratorTestUtils.java +++ b/java/src/main/java/com/google/fhir/protogen/ProtoGeneratorTestUtils.java @@ -18,10 +18,11 @@ import static com.google.common.truth.Truth.assertWithMessage; import static java.util.stream.Collectors.toList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import com.google.fhir.common.FhirVersion; import com.google.fhir.common.ProtoUtils; import com.google.fhir.proto.Annotations; +import com.google.fhir.proto.PackageInfo; import com.google.fhir.proto.PackageInfo.FileSplittingBehavior; import com.google.fhir.proto.ProtoGeneratorAnnotations; import com.google.fhir.r4.core.StructureDefinition; @@ -51,29 +52,34 @@ public static void initializeRegistry(ExtensionRegistry registry) throws IOExcep } public static ProtoGenerator makeProtoGenerator( - String packageLocation, ImmutableSet dependencyLocations) throws IOException { + String packageLocation, + ImmutableMap coreDepMap, + ImmutableSet dependencyLocations) + throws IOException { FhirPackage packageToGenerate = FhirPackage.load(packageLocation); + PackageInfo packageInfo = packageToGenerate.packageInfo; Set packages = new HashSet<>(); packages.add(packageToGenerate); for (String location : dependencyLocations) { packages.add(FhirPackage.load(location)); } - // Add core dependency - packages.add( - FhirPackage.load( - FhirVersion.fromAnnotation(packageToGenerate.packageInfo.getFhirVersion()) - .coreFhirPackageZip)); + + String coreDep = coreDepMap.get(packageInfo.getFhirVersion().toString()); + if (coreDep == null) { + throw new IllegalArgumentException( + "Unable to load core dep for fhir version: " + packageInfo.getFhirVersion()); + } + packages.add(FhirPackage.load(coreDep)); return new ProtoGenerator( - packageToGenerate.packageInfo, - ImmutableSet.copyOf(packages), - new ValueSetGenerator(packageToGenerate.packageInfo, packages)); + packageInfo, ImmutableSet.copyOf(packages), new ValueSetGenerator(packageInfo, packages)); } public static void testGeneratedProto( String packageLocation, String ruleName, + ImmutableMap coreDepMap, ImmutableSet dependencyLocations, ImmutableSet additionalImports) throws Exception { @@ -85,7 +91,7 @@ public static void testGeneratedProto( return; } - ProtoGenerator generator = makeProtoGenerator(packageLocation, dependencyLocations); + ProtoGenerator generator = makeProtoGenerator(packageLocation, coreDepMap, dependencyLocations); // TODO: Also test generated extension files when split. boolean hasSplitExtensionFile = diff --git a/java/src/test/java/com/google/fhir/protogen/GeneratedProtoTest.java b/java/src/test/java/com/google/fhir/protogen/GeneratedProtoTest.java index 5f0668c49..8afde79b9 100644 --- a/java/src/test/java/com/google/fhir/protogen/GeneratedProtoTest.java +++ b/java/src/test/java/com/google/fhir/protogen/GeneratedProtoTest.java @@ -15,6 +15,7 @@ package com.google.fhir.protogen; import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import org.junit.Test; import org.junit.runner.RunWith; @@ -22,7 +23,9 @@ /** * Generic test case for ensuring generated protos are up to date. This rule is automatically added - * to every profile set by the generation rules. + * to every profile set by the generation rules. Note: this file should be kept clear of + * dependencies on the core fhir package other than ProtoGeneratorTestUtils, so that it works both + * inside and outside of the core lib. */ @RunWith(JUnit4.class) public final class GeneratedProtoTest { @@ -34,6 +37,9 @@ public void testGeneratedProto() throws Exception { ProtoGeneratorTestUtils.testGeneratedProto( System.getProperty("fhir_package"), System.getProperty("rule_name"), + ImmutableMap.of( + "STU3", System.getProperty("stu3_core_dep"), + "R4", System.getProperty("r4_core_dep")), ImmutableSet.copyOf(splitter.splitToList(System.getProperty("dependencies"))), ImmutableSet.copyOf(splitter.splitToList(System.getProperty("imports")))); } diff --git a/java/src/test/java/com/google/fhir/protogen/ProtoGeneratorTest.java b/java/src/test/java/com/google/fhir/protogen/ProtoGeneratorTest.java index 359a9c963..9e12d7e5a 100644 --- a/java/src/test/java/com/google/fhir/protogen/ProtoGeneratorTest.java +++ b/java/src/test/java/com/google/fhir/protogen/ProtoGeneratorTest.java @@ -18,6 +18,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.fail; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.io.Files; import com.google.devtools.build.runfiles.Runfiles; @@ -1429,7 +1430,9 @@ private void testGeneratedR4Proto(ProtoGenerator protoGenerator, String resource public void generateR4() throws Exception { ProtoGenerator protoGenerator = ProtoGeneratorTestUtils.makeProtoGenerator( - "spec/fhir_r4_package.zip", ImmutableSet.of() /* no dependencies */); + "spec/fhir_r4_package.zip", + ImmutableMap.of("R4", "spec/fhir_r4_package.zip"), + ImmutableSet.of() /* no dependencies */); String suffix = ".descriptor.prototxt"; int fileCount = 0; for (File file :