Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit e28cde0

Browse files
Aaron Brucknerfacebook-github-bot
authored andcommitted
deps_query Support for java_test Rules
Summary: Adding `deps_query` argument to all `java_test` rules. This feature already exists on `android_library` rules but was missing from other rule types. With this feature, unit tests can cleanly import the direct dependencies of the source target they test, making our buck files smaller and allowing devs to write unit tests faster. Without this, devs go through a loop with each new test target, adding the missing deps they may require from the source target's direct dependencies. Related Posts: https://fb.workplace.com/groups/1827686307449366/permalink/6459970044051556/ https://fb.workplace.com/groups/askbuck/permalink/6452151958166698/ Reviewed By: IanChilds fbshipit-source-id: d291c1d96bcac308c9005623ec9ab1c54c164090
1 parent 2f9f332 commit e28cde0

11 files changed

Lines changed: 159 additions & 5 deletions

File tree

src/com/facebook/buck/android/RobolectricTestDescription.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import com.facebook.buck.rules.args.Arg;
6464
import com.facebook.buck.rules.coercer.ManifestEntries;
6565
import com.facebook.buck.rules.macros.StringWithMacrosConverter;
66+
import com.facebook.buck.rules.query.Query;
6667
import com.facebook.buck.test.config.TestBuckConfig;
6768
import com.google.common.base.Preconditions;
6869
import com.google.common.cache.CacheBuilder;
@@ -528,5 +529,13 @@ default RobolectricTestDescriptionArg withSrcs(Iterable<SourcePath> srcs) {
528529
}
529530
return RobolectricTestDescriptionArg.builder().from(this).setSrcs(srcs).build();
530531
}
532+
533+
@Override
534+
default RobolectricTestDescriptionArg withDepsQuery(Query query) {
535+
if (getDepsQuery().equals(Optional.of(query))) {
536+
return (RobolectricTestDescriptionArg) this;
537+
}
538+
return RobolectricTestDescriptionArg.builder().from(this).setDepsQuery(query).build();
539+
}
531540
}
532541
}

src/com/facebook/buck/core/description/arg/HasDepsQuery.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import com.facebook.buck.rules.query.Query;
2020
import java.util.Optional;
2121

22+
/**
23+
* `deps_query` allows you to add additional dependencies to a target via executing a buck query.
24+
*/
2225
public interface HasDepsQuery extends HasDeclaredDeps {
2326

2427
// TODO: Remove this and fix descriptions to work with implicit deps.

src/com/facebook/buck/jvm/groovy/GroovyTestDescription.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.facebook.buck.jvm.java.toolchain.JavaOptionsProvider;
4646
import com.facebook.buck.jvm.java.toolchain.JavacOptionsProvider;
4747
import com.facebook.buck.rules.macros.StringWithMacrosConverter;
48+
import com.facebook.buck.rules.query.Query;
4849
import com.facebook.buck.test.config.TestBuckConfig;
4950
import com.google.common.collect.ImmutableCollection;
5051
import com.google.common.collect.ImmutableMap;
@@ -200,5 +201,13 @@ public void findDepsForTargetFromConstructorArgs(
200201

201202
@RuleArg
202203
interface AbstractGroovyTestDescriptionArg
203-
extends GroovyLibraryDescription.CoreArg, JavaTestDescription.CoreArg {}
204+
extends GroovyLibraryDescription.CoreArg, JavaTestDescription.CoreArg {
205+
@Override
206+
default GroovyTestDescriptionArg withDepsQuery(Query query) {
207+
if (getDepsQuery().equals(Optional.of(query))) {
208+
return (GroovyTestDescriptionArg) this;
209+
}
210+
return GroovyTestDescriptionArg.builder().from(this).setDepsQuery(query).build();
211+
}
212+
}
204213
}

src/com/facebook/buck/jvm/java/JavaTestDescription.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.facebook.buck.core.cell.CellPathResolver;
2020
import com.facebook.buck.core.cell.nameresolver.CellNameResolver;
21+
import com.facebook.buck.core.description.arg.HasDepsQuery;
2122
import com.facebook.buck.core.description.arg.HasTestTimeout;
2223
import com.facebook.buck.core.description.attr.ImplicitDepsInferringDescription;
2324
import com.facebook.buck.core.exceptions.HumanReadableException;
@@ -55,6 +56,7 @@
5556
import com.facebook.buck.rules.macros.MacroExpander;
5657
import com.facebook.buck.rules.macros.StringWithMacros;
5758
import com.facebook.buck.rules.macros.StringWithMacrosConverter;
59+
import com.facebook.buck.rules.query.Query;
5860
import com.facebook.buck.test.config.TestBuckConfig;
5961
import com.facebook.buck.util.stream.RichStream;
6062
import com.facebook.buck.versions.VersionRoot;
@@ -312,7 +314,7 @@ public void findDepsForTargetFromConstructorArgs(
312314
targetGraphOnlyDepsBuilder, constructorArg, buildTarget.getTargetConfiguration());
313315
}
314316

315-
public interface CoreArg extends HasTestTimeout, JavaLibraryDescription.CoreArg {
317+
public interface CoreArg extends HasTestTimeout, HasDepsQuery, JavaLibraryDescription.CoreArg {
316318
ImmutableList<StringWithMacros> getVmArgs();
317319

318320
Optional<TestType> getTestType();
@@ -345,7 +347,15 @@ default ForkMode getForkMode() {
345347
}
346348

347349
@RuleArg
348-
interface AbstractJavaTestDescriptionArg extends CoreArg, HasTestRunner {}
350+
interface AbstractJavaTestDescriptionArg extends CoreArg, HasTestRunner {
351+
@Override
352+
default JavaTestDescriptionArg withDepsQuery(Query query) {
353+
if (getDepsQuery().equals(Optional.of(query))) {
354+
return (JavaTestDescriptionArg) this;
355+
}
356+
return JavaTestDescriptionArg.builder().from(this).setDepsQuery(query).build();
357+
}
358+
}
349359

350360
public static class CxxLibraryEnhancement {
351361
public final BuildRuleParams updatedParams;

src/com/facebook/buck/jvm/kotlin/KotlinTestDescription.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.facebook.buck.jvm.java.toolchain.JavaOptionsProvider;
4545
import com.facebook.buck.jvm.java.toolchain.JavacOptionsProvider;
4646
import com.facebook.buck.rules.macros.StringWithMacrosConverter;
47+
import com.facebook.buck.rules.query.Query;
4748
import com.facebook.buck.test.config.TestBuckConfig;
4849
import com.google.common.cache.CacheBuilder;
4950
import com.google.common.cache.CacheLoader;
@@ -214,5 +215,13 @@ public void findDepsForTargetFromConstructorArgs(
214215

215216
@RuleArg
216217
interface AbstractKotlinTestDescriptionArg
217-
extends KotlinLibraryDescription.CoreArg, JavaTestDescription.CoreArg {}
218+
extends KotlinLibraryDescription.CoreArg, JavaTestDescription.CoreArg {
219+
@Override
220+
default KotlinTestDescriptionArg withDepsQuery(Query query) {
221+
if (getDepsQuery().equals(Optional.of(query))) {
222+
return (KotlinTestDescriptionArg) this;
223+
}
224+
return KotlinTestDescriptionArg.builder().from(this).setDepsQuery(query).build();
225+
}
226+
}
218227
}

src/com/facebook/buck/jvm/scala/ScalaTestDescription.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.facebook.buck.jvm.java.toolchain.JavaOptionsProvider;
4949
import com.facebook.buck.jvm.java.toolchain.JavacOptionsProvider;
5050
import com.facebook.buck.rules.macros.StringWithMacrosConverter;
51+
import com.facebook.buck.rules.query.Query;
5152
import com.facebook.buck.test.config.TestBuckConfig;
5253
import com.google.common.collect.ImmutableCollection;
5354
import com.google.common.collect.ImmutableMap;
@@ -227,5 +228,13 @@ public void findDepsForTargetFromConstructorArgs(
227228

228229
@RuleArg
229230
interface AbstractScalaTestDescriptionArg
230-
extends ScalaLibraryDescription.CoreArg, JavaTestDescription.CoreArg {}
231+
extends ScalaLibraryDescription.CoreArg, JavaTestDescription.CoreArg {
232+
@Override
233+
default ScalaTestDescriptionArg withDepsQuery(Query query) {
234+
if (getDepsQuery().equals(Optional.of(query))) {
235+
return (ScalaTestDescriptionArg) this;
236+
}
237+
return ScalaTestDescriptionArg.builder().from(this).setDepsQuery(query).build();
238+
}
239+
}
231240
}

test/com/facebook/buck/jvm/java/JavaTestIntegrationTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,14 @@ public void testSimpleWorkingJunitTest() throws IOException {
636636
workspace.runBuckCommand("test", "//:java_test_working").assertSuccess();
637637
}
638638

639+
@Test
640+
public void testSimpleWorkingJunitTestWithDepsQuery() throws IOException {
641+
ProjectWorkspace workspace =
642+
TestDataHelper.createProjectWorkspaceForScenario(this, "java_test_deps_query", temp);
643+
workspace.setUp();
644+
workspace.runBuckCommand("test", "//:java_test_deps_query").assertSuccess();
645+
}
646+
639647
@Test
640648
public void testSimpleWorkingJunitTestWithDependencyOrderClasspath() throws IOException {
641649
ProjectWorkspace workspace =
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
java_test(
2+
name = "java_test_deps_query",
3+
srcs = [
4+
"SimpleTest.java",
5+
],
6+
deps_query = "deps(':class_a', 1)",
7+
deps = [
8+
":class_a",
9+
"buck//third-party/java/junit:junit",
10+
],
11+
)
12+
13+
java_library(
14+
name = "class_a",
15+
srcs = [
16+
"ClassA.java",
17+
],
18+
deps = [
19+
":class_b",
20+
],
21+
)
22+
23+
java_library(
24+
name = "class_b",
25+
srcs = [
26+
"ClassB.java",
27+
],
28+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2014-present Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
public class ClassA {
18+
public void example() {
19+
// No Op
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2014-present Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
public class ClassB {
18+
public void example() {
19+
// No Op
20+
}
21+
}

0 commit comments

Comments
 (0)