aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test
diff options
context:
space:
mode:
authorGravatar cparsons <cparsons@google.com>2017-12-21 12:31:43 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-12-21 12:33:41 -0800
commit46356dfa59428b665aa280ba4cbca6e449f73c5b (patch)
tree06732215a6bf9a0e0e6daca2a36c68a13fb8c219 /src/test
parent2d1406c9fed6bd97ff5e17680aa1968fe28661a2 (diff)
Use an annotation preprocessor to validate SkylarkConfigurationField.
RELNOTES: None. PiperOrigin-RevId: 179845261
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/BUILD29
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/SkylarkConfigurationFieldProcessorTest.java103
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/GoldenConfigurationField.java45
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/HasMethodParameters.java45
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/MethodIsPrivate.java46
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/MethodThrowsException.java45
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/NonConfigurationFragment.java44
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/NonExposedConfigurationFragment.java38
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/ReturnsOtherType.java44
9 files changed, 439 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/BUILD b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/BUILD
new file mode 100644
index 0000000000..a583cfad24
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/BUILD
@@ -0,0 +1,29 @@
+licenses(["notice"]) # Apache 2.0
+
+filegroup(
+ name = "srcs",
+ srcs = glob(
+ ["**"],
+ ),
+ visibility = ["//src/main/java/com/google/devtools/build/lib:__pkg__"],
+)
+
+java_test(
+ name = "SkylarkConfigurationFieldProcessorTest",
+ srcs = ["SkylarkConfigurationFieldProcessorTest.java"],
+ resources = [":ProcessorTestFiles"],
+ deps = [
+ "//src/main/java/com/google/devtools/build/lib:build-base",
+ "//src/main/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor:annotation_preprocessor",
+ "//src/main/java/com/google/devtools/build/lib/cmdline",
+ "//third_party:compile_testing",
+ "//third_party:guava",
+ "//third_party:junit4",
+ "//third_party:truth",
+ ],
+)
+
+filegroup(
+ name = "ProcessorTestFiles",
+ srcs = glob(["optiontestsources/*.java"]),
+)
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/SkylarkConfigurationFieldProcessorTest.java b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/SkylarkConfigurationFieldProcessorTest.java
new file mode 100644
index 0000000000..3edeb437eb
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/SkylarkConfigurationFieldProcessorTest.java
@@ -0,0 +1,103 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.analysis.skylark.annotations.processor;
+
+import static com.google.common.truth.Truth.assertAbout;
+import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
+
+import com.google.common.io.Resources;
+import com.google.testing.compile.JavaFileObjects;
+import javax.tools.JavaFileObject;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Unit tests for SkylarkConfigurationFieldProcessor.
+ */
+@RunWith(JUnit4.class)
+public final class SkylarkConfigurationFieldProcessorTest {
+
+ private static JavaFileObject getFile(String pathToFile) {
+ return JavaFileObjects.forResource(Resources.getResource(
+ "com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/"
+ + pathToFile));
+ }
+
+ @Test
+ public void testGoldenConfigurationField() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("GoldenConfigurationField.java"))
+ .processedWith(new SkylarkConfigurationFieldProcessor())
+ .compilesWithoutError();
+ }
+
+ @Test
+ public void testHasMethodParameters() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("HasMethodParameters.java"))
+ .processedWith(new SkylarkConfigurationFieldProcessor())
+ .failsToCompile()
+ .withErrorContaining(
+ "@SkylarkConfigurationField annotated methods must have zero arguments.");
+ }
+
+ @Test
+ public void testMethodIsPrivate() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("MethodIsPrivate.java"))
+ .processedWith(new SkylarkConfigurationFieldProcessor())
+ .failsToCompile()
+ .withErrorContaining("@SkylarkConfigurationField annotated methods must be public.");
+ }
+
+ @Test
+ public void testMethodThrowsException() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("MethodThrowsException.java"))
+ .processedWith(new SkylarkConfigurationFieldProcessor())
+ .failsToCompile()
+ .withErrorContaining("@SkylarkConfigurationField annotated must not throw exceptions.");
+ }
+
+ @Test
+ public void testNonConfigurationFragment() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("NonConfigurationFragment.java"))
+ .processedWith(new SkylarkConfigurationFieldProcessor())
+ .failsToCompile()
+ .withErrorContaining("@SkylarkConfigurationField annotated methods must be methods of "
+ + "configuration fragments with the @SkylarkModule annotation.");
+ }
+
+ @Test
+ public void testNonExposedConfigurationFragment() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("NonExposedConfigurationFragment.java"))
+ .processedWith(new SkylarkConfigurationFieldProcessor())
+ .failsToCompile()
+ .withErrorContaining("@SkylarkConfigurationField annotated methods must be methods of "
+ + "configuration fragments with the @SkylarkModule annotation.");
+ }
+
+ @Test
+ public void testReturnsOtherType() throws Exception {
+ assertAbout(javaSource())
+ .that(getFile("ReturnsOtherType.java"))
+ .processedWith(new SkylarkConfigurationFieldProcessor())
+ .failsToCompile()
+ .withErrorContaining("@SkylarkConfigurationField annotated methods must return Label.");
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/GoldenConfigurationField.java b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/GoldenConfigurationField.java
new file mode 100644
index 0000000000..47fd152b48
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/GoldenConfigurationField.java
@@ -0,0 +1,45 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.analysis.skylark.annotations.processor.optiontestsources;
+
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.analysis.skylark.annotations.SkylarkConfigurationField;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+
+/**
+ * A test case of SkylarkConfigurationFieldProcessorTest.
+ */
+@SkylarkModule(
+ name = "module_name",
+ doc = "A fake configuration fragment for a test.",
+ category = SkylarkModuleCategory.CONFIGURATION_FRAGMENT
+)
+public class GoldenConfigurationField extends BuildConfiguration.Fragment {
+
+ /**
+ * Returns the label of the xcode_config rule to use for resolving the host system xcode version.
+ */
+ @SkylarkConfigurationField(
+ name = "some_field",
+ doc = "Documentation ",
+ defaultLabel = "defaultLabel",
+ defaultInToolRepository = true
+ )
+ public Label getXcodeConfigLabel() {
+ return null;
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/HasMethodParameters.java b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/HasMethodParameters.java
new file mode 100644
index 0000000000..f75519c1cc
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/HasMethodParameters.java
@@ -0,0 +1,45 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.analysis.skylark.annotations.processor.optiontestsources;
+
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.analysis.skylark.annotations.SkylarkConfigurationField;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+
+/**
+ * A test case of SkylarkConfigurationFieldProcessorTest.
+ */
+@SkylarkModule(
+ name = "module_name",
+ doc = "A fake configuration fragment for a test.",
+ category = SkylarkModuleCategory.CONFIGURATION_FRAGMENT
+)
+public class HasMethodParameters extends BuildConfiguration.Fragment {
+
+ /**
+ * Returns the label of the xcode_config rule to use for resolving the host system xcode version.
+ */
+ @SkylarkConfigurationField(
+ name = "some_field",
+ doc = "Documentation ",
+ defaultLabel = "defaultLabel",
+ defaultInToolRepository = true
+ )
+ public Label getXcodeConfigLabel(int x) {
+ return null;
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/MethodIsPrivate.java b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/MethodIsPrivate.java
new file mode 100644
index 0000000000..3569917255
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/MethodIsPrivate.java
@@ -0,0 +1,46 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.analysis.skylark.annotations.processor.optiontestsources;
+
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.analysis.skylark.annotations.SkylarkConfigurationField;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+
+/**
+ * A test case of SkylarkConfigurationFieldProcessorTest.
+ */
+@SkylarkModule(
+ name = "module_name",
+ doc = "A fake configuration fragment for a test.",
+ category = SkylarkModuleCategory.CONFIGURATION_FRAGMENT
+)
+public class MethodIsPrivate extends BuildConfiguration.Fragment {
+
+ /**
+ * Returns the label of the xcode_config rule to use for resolving the host system xcode version.
+ */
+ @SkylarkConfigurationField(
+ name = "some_field",
+ doc = "Documentation ",
+ defaultLabel = "defaultLabel",
+ defaultInToolRepository = true
+ )
+ private Label getXcodeConfigLabel() {
+ return null;
+ }
+}
+
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/MethodThrowsException.java b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/MethodThrowsException.java
new file mode 100644
index 0000000000..43fdc46139
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/MethodThrowsException.java
@@ -0,0 +1,45 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.analysis.skylark.annotations.processor.optiontestsources;
+
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.analysis.skylark.annotations.SkylarkConfigurationField;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+
+/**
+ * A test case of SkylarkConfigurationFieldProcessorTest.
+ */
+@SkylarkModule(
+ name = "module_name",
+ doc = "A fake configuration fragment for a test.",
+ category = SkylarkModuleCategory.CONFIGURATION_FRAGMENT
+)
+public class MethodThrowsException extends BuildConfiguration.Fragment {
+
+ /**
+ * Returns the label of the xcode_config rule to use for resolving the host system xcode version.
+ */
+ @SkylarkConfigurationField(
+ name = "some_field",
+ doc = "Documentation ",
+ defaultLabel = "defaultLabel",
+ defaultInToolRepository = true
+ )
+ public Label getXcodeConfigLabel() throws IllegalArgumentException {
+ return null;
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/NonConfigurationFragment.java b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/NonConfigurationFragment.java
new file mode 100644
index 0000000000..221493775f
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/NonConfigurationFragment.java
@@ -0,0 +1,44 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.analysis.skylark.annotations.processor.optiontestsources;
+
+import com.google.devtools.build.lib.analysis.skylark.annotations.SkylarkConfigurationField;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+
+/**
+ * A test case of SkylarkConfigurationFieldProcessorTest.
+ */
+@SkylarkModule(
+ name = "module_name",
+ doc = "A fake configuration fragment for a test.",
+ category = SkylarkModuleCategory.CONFIGURATION_FRAGMENT
+)
+public class NonConfigurationFragment {
+
+ /**
+ * Returns the label of the xcode_config rule to use for resolving the host system xcode version.
+ */
+ @SkylarkConfigurationField(
+ name = "some_field",
+ doc = "Documentation ",
+ defaultLabel = "defaultLabel",
+ defaultInToolRepository = true
+ )
+ public Label getXcodeConfigLabel() {
+ return null;
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/NonExposedConfigurationFragment.java b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/NonExposedConfigurationFragment.java
new file mode 100644
index 0000000000..6806c14c2a
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/NonExposedConfigurationFragment.java
@@ -0,0 +1,38 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.analysis.skylark.annotations.processor.optiontestsources;
+
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.analysis.skylark.annotations.SkylarkConfigurationField;
+import com.google.devtools.build.lib.cmdline.Label;
+
+/**
+ * A test case of SkylarkConfigurationFieldProcessorTest.
+ */
+public class NonExposedConfigurationFragment extends BuildConfiguration.Fragment {
+
+ /**
+ * Returns the label of the xcode_config rule to use for resolving the host system xcode version.
+ */
+ @SkylarkConfigurationField(
+ name = "some_field",
+ doc = "Documentation ",
+ defaultLabel = "defaultLabel",
+ defaultInToolRepository = true
+ )
+ public Label getXcodeConfigLabel() {
+ return null;
+ }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/ReturnsOtherType.java b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/ReturnsOtherType.java
new file mode 100644
index 0000000000..2f167c33b5
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/analysis/skylark/annotations/processor/optiontestsources/ReturnsOtherType.java
@@ -0,0 +1,44 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.analysis.skylark.annotations.processor.optiontestsources;
+
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.analysis.skylark.annotations.SkylarkConfigurationField;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+
+/**
+ * A test case of SkylarkConfigurationFieldProcessorTest.
+ */
+@SkylarkModule(
+ name = "module_name",
+ doc = "A fake configuration fragment for a test.",
+ category = SkylarkModuleCategory.CONFIGURATION_FRAGMENT
+)
+public class ReturnsOtherType extends BuildConfiguration.Fragment {
+
+ /**
+ * Returns the label of the xcode_config rule to use for resolving the host system xcode version.
+ */
+ @SkylarkConfigurationField(
+ name = "some_field",
+ doc = "Documentation ",
+ defaultLabel = "defaultLabel",
+ defaultInToolRepository = true
+ )
+ public String getXcodeConfigLabel() {
+ return null;
+ }
+}