diff options
author | nickreid <nickreid@google.com> | 2023-01-31 09:24:40 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-01-31 09:25:14 -0800 |
commit | 11b0d6a326ba40315a8952af843d7ac7fae4df74 (patch) | |
tree | a434439d2a241af4752349f0b6d5f2a1a411a2a0 | |
parent | 436c1d9a3d2ae40dbca72ba7e372fc844ed349c9 (diff) | |
download | bazelbuild-kotlin-rules-11b0d6a326ba40315a8952af843d7ac7fae4df74.tar.gz |
Be stricter about separating files and treeartifacts that end in ".kt".
PiperOrigin-RevId: 506024992
-rw-r--r-- | kotlin/common.bzl | 11 | ||||
-rw-r--r-- | tests/analysis/jvm_library/treeartifacts_srcs/BUILD | 104 | ||||
-rw-r--r-- | tests/analysis/jvm_library/treeartifacts_srcs/Input.java | 16 | ||||
-rw-r--r-- | tests/analysis/jvm_library/treeartifacts_srcs/Input.kt | 16 | ||||
-rw-r--r-- | tests/analysis/util.bzl | 33 |
5 files changed, 158 insertions, 22 deletions
diff --git a/kotlin/common.bzl b/kotlin/common.bzl index 73fde72..b3be72b 100644 --- a/kotlin/common.bzl +++ b/kotlin/common.bzl @@ -80,6 +80,9 @@ _RULE_FAMILY = struct( def _is_dir(file, basename): return file.is_directory and file.basename == basename +def _is_file(file, extension): + return (not file.is_directory) and file.path.endswith(extension) + def _is_kt_src(src): """Decides if `src` Kotlin code. @@ -88,7 +91,7 @@ def _is_kt_src(src): - a tree-artifact expected to contain only Kotlin source files """ - return src.path.endswith(_EXT.KT) or _is_dir(src, "kotlin") + return _is_file(src, _EXT.KT) or _is_dir(src, "kotlin") # Compute module name based on target (b/139403883), similar to Swift def _derive_module_name(ctx): @@ -536,7 +539,7 @@ def _run_import_deps_checker( ) def _offline_instrument_jar(ctx, toolchain, jar, srcs = []): - if not jar.basename.endswith(".jar"): + if not _is_file(jar, _EXT.JAR): fail("Expect JAR input but got %s" % jar) file_factory = FileFactory(ctx, jar) @@ -838,10 +841,10 @@ def _kt_jvm_library( # Split sources, as java requires a separate compile step. kt_srcs = [s for s in srcs if _is_kt_src(s)] - java_srcs = [s for s in srcs if s.path.endswith(_EXT.JAVA)] + java_srcs = [s for s in srcs if _is_file(s, _EXT.JAVA)] java_syncer = _DirSrcjarSyncer(ctx, kt_toolchain, file_factory) java_syncer.add_dirs([s for s in srcs if _is_dir(s, "java")]) - java_syncer.add_srcjars([s for s in srcs if s.path.endswith(_EXT.SRCJAR)]) + java_syncer.add_srcjars([s for s in srcs if _is_file(s, _EXT.SRCJAR)]) expected_srcs = sets.make(kt_srcs + java_srcs + java_syncer.dirs + java_syncer.srcjars) unexpected_srcs = sets.difference(sets.make(srcs), expected_srcs) diff --git a/tests/analysis/jvm_library/treeartifacts_srcs/BUILD b/tests/analysis/jvm_library/treeartifacts_srcs/BUILD new file mode 100644 index 0000000..3d724c4 --- /dev/null +++ b/tests/analysis/jvm_library/treeartifacts_srcs/BUILD @@ -0,0 +1,104 @@ +# Copyright 2022 Google LLC. 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. + +load("//tests/analysis:for_test.bzl", "rules_for_test") +load("//tests/analysis:assert_failure_test.bzl", "assert_failure_test") +load("//tests/analysis:jvm_library_test.bzl", "jvm_library_test") +load("//tests/analysis:util.bzl", "create_dir") + +package(default_testonly = True) + +licenses(["notice"]) + +jvm_library_test( + name = "treeartifact_basename_kotlin_test", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_basename_kotlin", + srcs = [ + create_dir( + name = "treeartifact_extension/kotlin", + srcs = [ + "Input.java", # TODO: Reject this source + ], + ), + ], + ), +) + +jvm_library_test( + name = "treeartifact_basename_java_test", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_basename_java", + srcs = [ + create_dir( + name = "treeartifact_extension/java", + srcs = [ + "Input.kt", # TODO: Reject this source + ], + ), + ], + ), +) + +assert_failure_test( + name = "treeartifact_extension_kt_test", + msg_contains = "/treeartifact_extension.kt", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_extension_kt", + srcs = [ + create_dir( + name = "treeartifact_extension.kt", + ), + ], + ), +) + +assert_failure_test( + name = "treeartifact_extension_java_test", + msg_contains = "/treeartifact_extension.java", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_extension_java", + srcs = [ + create_dir( + name = "treeartifact_extension.java", + ), + ], + ), +) + +assert_failure_test( + name = "treeartifact_extension_srcjar_test", + msg_contains = "/treeartifact_extension.srcjar", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_extension_srcjar", + srcs = [ + create_dir( + name = "treeartifact_extension.srcjar", + ), + ], + ), +) + +assert_failure_test( + name = "treeartifact_no_extension_test", + msg_contains = "/treeartifact_no_extension", + target_under_test = rules_for_test.kt_jvm_library( + name = "treeartifact_no_extension", + srcs = [ + create_dir( + name = "treeartifact_no_extension_dir", + ), + ], + ), +) diff --git a/tests/analysis/jvm_library/treeartifacts_srcs/Input.java b/tests/analysis/jvm_library/treeartifacts_srcs/Input.java new file mode 100644 index 0000000..e675bc1 --- /dev/null +++ b/tests/analysis/jvm_library/treeartifacts_srcs/Input.java @@ -0,0 +1,16 @@ +/* + * * Copyright 2022 Google LLC. 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. + */ + diff --git a/tests/analysis/jvm_library/treeartifacts_srcs/Input.kt b/tests/analysis/jvm_library/treeartifacts_srcs/Input.kt new file mode 100644 index 0000000..e675bc1 --- /dev/null +++ b/tests/analysis/jvm_library/treeartifacts_srcs/Input.kt @@ -0,0 +1,16 @@ +/* + * * Copyright 2022 Google LLC. 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. + */ + diff --git a/tests/analysis/util.bzl b/tests/analysis/util.bzl index cec0cc3..28a31f7 100644 --- a/tests/analysis/util.bzl +++ b/tests/analysis/util.bzl @@ -35,23 +35,17 @@ EOF def _create_dir_impl(ctx): dir = ctx.actions.declare_directory(ctx.attr.name) - if ctx.files.srcs: - ctx.actions.run_shell( - command = "mkdir -p {0} && cp {1} {0}".format( - dir.path + "/" + ctx.attr.subdir, - " ".join([s.path for s in ctx.files.srcs]), - ), - inputs = ctx.files.srcs, - outputs = [dir], - ) - else: - ctx.actions.run_shell( - command = "mkdir -p {0}".format( - dir.path + "/" + ctx.attr.subdir, - ), - inputs = ctx.files.srcs, - outputs = [dir], - ) + + command = "mkdir -p {0} " + ("&& cp {1} {0}" if ctx.files.srcs else "# {1}") + ctx.actions.run_shell( + command = command.format( + dir.path + "/" + ctx.attr.subdir, + " ".join([s.path for s in ctx.files.srcs]), + ), + inputs = ctx.files.srcs, + outputs = [dir], + ) + return [DefaultInfo(files = depset([dir]))] _create_dir = rule( @@ -62,7 +56,10 @@ _create_dir = rule( ), ) -def create_dir(name, subdir, srcs): +def create_dir( + name, + subdir = None, + srcs = None): _create_dir( name = name, subdir = subdir, |