Skip to content

Commit

Permalink
Makes GeneratedProtoTest read core dependencies from bazel rather tha…
Browse files Browse the repository at this point in the history
…n assuming local, which enables GeneratedProtoTest to work both within the core lib and externally.

PiperOrigin-RevId: 312559760
  • Loading branch information
nickgeorge committed May 21, 2020
1 parent d2de420 commit c6eecb5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
2 changes: 2 additions & 0 deletions bazel/protogen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion cc/google/fhir/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,29 +52,34 @@ public static void initializeRegistry(ExtensionRegistry registry) throws IOExcep
}

public static ProtoGenerator makeProtoGenerator(
String packageLocation, ImmutableSet<String> dependencyLocations) throws IOException {
String packageLocation,
ImmutableMap<String, String> coreDepMap,
ImmutableSet<String> dependencyLocations)
throws IOException {
FhirPackage packageToGenerate = FhirPackage.load(packageLocation);
PackageInfo packageInfo = packageToGenerate.packageInfo;

Set<FhirPackage> 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<String, String> coreDepMap,
ImmutableSet<String> dependencyLocations,
ImmutableSet<String> additionalImports)
throws Exception {
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
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;
import org.junit.runners.JUnit4;

/**
* 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 {
Expand All @@ -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"))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 :
Expand Down

0 comments on commit c6eecb5

Please sign in to comment.