diff options
author | 2016-05-17 14:52:17 +0000 | |
---|---|---|
committer | 2016-05-17 16:17:51 +0000 | |
commit | 3b0a52de342894160f16ec95e147f1ada1bfb172 (patch) | |
tree | aacb1a8a147750e0ede87a22508dbbd34f01a5ed /src/test | |
parent | cbd8789f46258d3d900bc69ed6cd36c1c46c51bb (diff) |
Add Skylark tests for intellij_info.bzl
This add a "testing framework" and reimplements first two tests from AndroidStudioInfoAspectTest.
--
MOS_MIGRATED_REVID=122523803
Diffstat (limited to 'src/test')
6 files changed, 247 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/intellij_info.bzl b/src/test/java/com/google/devtools/build/lib/ideinfo/intellij_info.bzl index 2e6aadf3a0..6d6eab6409 100644 --- a/src/test/java/com/google/devtools/build/lib/ideinfo/intellij_info.bzl +++ b/src/test/java/com/google/devtools/build/lib/ideinfo/intellij_info.bzl @@ -353,12 +353,14 @@ def _aspect_impl(target, ctx): ide_info_text = set() ide_resolve_files = set() + ide_infos = [] for attr_name in ALL_DEPS.label_list: if hasattr(rule_attrs, attr_name): for dep in getattr(rule_attrs, attr_name): ide_info_text = ide_info_text | dep.intellij_info_files.ide_info_text ide_resolve_files = ide_resolve_files | dep.intellij_info_files.ide_resolve_files + ide_infos += dep.ide_infos for attr_name in ALL_DEPS.label: if hasattr(rule_attrs, attr_name): @@ -414,6 +416,7 @@ def _aspect_impl(target, ctx): output = ctx.new_file(target.label.name + ".aswb-build.txt") ctx.file_action(output, info.to_proto()) ide_info_text += set([output]) + ide_infos += [info] # Return providers. return struct( @@ -426,6 +429,7 @@ def _aspect_impl(target, ctx): ide_resolve_files = ide_resolve_files, ), export_deps = export_deps, + ide_infos = ide_infos, ) intellij_info_aspect = aspect( diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/intellij_info_tests.bzl b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/intellij_info_tests.bzl new file mode 100644 index 0000000000..2722efdccd --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/intellij_info_tests.bzl @@ -0,0 +1,137 @@ +# Copyright 2016 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. + +# Tests for intellij_info.bzl + +load(":skylarktests/testing.bzl", + "start_test", + "end_test", + "fail_test", + "assert_equals", + "assert_true") + + +load(":intellij_info.bzl", "intellij_info_aspect") + +def test(impl): + return rule(impl, + attrs = { + 'targets' : attr.label_list(aspects = [intellij_info_aspect]), + }, + test = True, + ) + +def infos_to_dict(infos): + return { i.label : i for i in infos } + +def _source_paths(env, artifact_locations): + for f in artifact_locations: + assert_true(env, f.is_source, '%s is not source' % f) + return [f.relative_path for f in artifact_locations] + +def _jar_string(library_artifact, name): + if hasattr(library_artifact, name): + return "<%s:%s>" % (name, getattr(library_artifact, name).relative_path) + else: + return "" + +def _library_artifact_string(env, library_artifact): + return _jar_string(library_artifact, 'jar') + \ + _jar_string(library_artifact, 'interface_jar') + \ + _jar_string(library_artifact, 'source_jar') + +def _jar_expected_string(base, jar, interface_jar, source_jar): + s = "" + if jar: + s += "<jar:%s>" % (base + "/" + jar) + if interface_jar: + s += "<interface_jar:%s>" % (base + "/" + interface_jar) + if source_jar: + s += "<source_jar:%s>" % (base + "/" + source_jar) + return s + +################################################ + +def _test_simple_java_library(ctx): + env = start_test(ctx) + infos = infos_to_dict(ctx.attr.targets[0].ide_infos) + info = infos.get(str(ctx.label.relative(":simple1"))) + if not info: + fail_test(env, "info not found") + end_test(ctx, env) + return + assert_equals(env, + ctx.label.package + "/BUILD", + info.build_file_artifact_location.relative_path) + + assert_equals(env, + True, + info.build_file_artifact_location.is_source) + + assert_equals(env, "java_library", info.kind_string) + + assert_equals(env, [], info.dependencies) + + assert_equals(env, + [ctx.label.package + "/skylarktests/testfiles/Simple1.java"], + _source_paths(env, info.java_rule_ide_info.sources)) + + assert_equals(env, + [_jar_expected_string(ctx.label.package, + "libsimple1.jar", "libsimple1-ijar.jar", "libsimple1-src.jar")], + [_library_artifact_string(env, a) for a in info.java_rule_ide_info.jars]) + + assert_equals(env, + ctx.label.package + "/libsimple1.jdeps", + info.java_rule_ide_info.jdeps.relative_path) + + end_test(env) + +test_simple_java_library_rule_test = test(_test_simple_java_library) + +def test_simple_java_library(): + native.java_library(name = "simple1", srcs = ["skylarktests/testfiles/Simple1.java"]) + test_simple_java_library_rule_test(name = "test_simple_java_library", + targets = [":simple1"] + ) + +################################################ +def _test_java_library_with_dependencies(ctx): + env = start_test(ctx) + infos = infos_to_dict(ctx.attr.targets[0].ide_infos) + info_simple = infos.get(str(ctx.label.relative(":simple2"))) + info_complex = infos.get(str(ctx.label.relative(":complex2"))) + assert_equals(env, + [ctx.label.package + "/skylarktests/testfiles/Complex2.java"], + _source_paths(env, info_complex.java_rule_ide_info.sources)) + assert_equals(env, + [str(ctx.label.relative(":simple2"))], + info_complex.dependencies) + end_test(env) + +test_java_library_with_dependencies_rule_test = test(_test_java_library_with_dependencies) + +def test_java_library_with_dependencies(): + native.java_library(name = "simple2", srcs = ["skylarktests/testfiles/Simple2.java"]) + native.java_library(name = "complex2", + srcs = ["skylarktests/testfiles/Complex2.java"], + deps = [":simple2"]) + test_java_library_with_dependencies_rule_test(name = "test_java_library_with_dependencies", + targets = [":complex2"] + ) + +def skylark_tests(): + test_simple_java_library() + test_java_library_with_dependencies() + diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Complex2.java b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Complex2.java new file mode 100644 index 0000000000..5a0f6e2e40 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Complex2.java @@ -0,0 +1,21 @@ +// Copyright 2016 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.ideinfo.skylarktests.testfiles; + +/** + * Test class. + */ +public class Complex2 { + +} diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple1.java b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple1.java new file mode 100644 index 0000000000..84d7729666 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple1.java @@ -0,0 +1,21 @@ +// Copyright 2016 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.ideinfo.skylarktests.testfiles; + +/** + * Test class. + */ +public class Simple1 { + +} diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple2.java b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple2.java new file mode 100644 index 0000000000..3b28249ff7 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testfiles/Simple2.java @@ -0,0 +1,21 @@ +// Copyright 2016 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.ideinfo.skylarktests.testfiles; + +/** + * Test class. + */ +public class Simple2 { + +} diff --git a/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testing.bzl b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testing.bzl new file mode 100644 index 0000000000..05f3e83320 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/ideinfo/skylarktests/testing.bzl @@ -0,0 +1,43 @@ +# Copyright 2016 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. + +# Small testing framework for Skylark rules and aspects. + +def start_test(ctx): + return struct(errors = [], ctx = ctx) + +def end_test(env): + cmd = "\n".join([ + "cat << EOF", + "\n".join(reversed(env.errors)), + "EOF", + "exit %d" % (1 if env.errors else 0)]) + env.ctx.file_action( + output = env.ctx.outputs.executable, + content = cmd, + executable = True, + ) + +def fail_test(env, msg): + print(msg) + env.errors.append(msg) + + +def assert_equals(env, expected, actual): + if not expected == actual: + fail_test(env, "'%s' != '%s'" % (expected, actual)) + +def assert_true(env, condition, message): + if not condition: + fail_test(env, message) |