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

Commit 1b03b43

Browse files
mikekapsdwilsh
authored andcommitted
Add some support for running go tests.
Summary: This isn't as complete as the `go test` command, mostly because `go test` "magically" recompiles the package under test. The closest you can get to `go test`-like functionality is via duplication, as mentioned in the docs. It might be nice to make this easier via param or different test rule (`go_internal_test`) that just does the src/dep copying for you. We may be able to make this even more amazing via some sort of auto-discovery (we can easily tell if the test is external or internal at test main generation time) and create two different libs. But I digress; this is a v1. This also fixes support for custom package names (was previously somewhat broken). Closes #449
1 parent 1a6880e commit 1b03b43

33 files changed

+1734
-70
lines changed

docs/__common.soy

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ src="https://www.facebook.com/tr?id=1637165926500152&ev=PageView&noscript=1"
198198
'go': [
199199
'go_binary',
200200
'go_library',
201+
'go_test',
201202
],
202203
'halide': [
203204
'halide_library',

docs/__go_common.soy

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Note: Buck is currently tested with (and therefore supports) version 1.5 of Go.
3535
{param default : 'path relative to the buck root' /}
3636
{param desc}
3737
Sets the full name of the package being compiled. This defaults to the path from the buck root.
38-
(e.g. given a ./.buckconfig, a rule in ./a/b/BUCK defaults to package "a/b"
38+
(e.g. given a ./.buckconfig, a rule in ./a/b/BUCK defaults to package "a/b")
3939
{/param}
4040
{/call}
4141
{/template}

docs/rule/go_binary.soy

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
{call go_common.linker_flags_arg /}
4141

42+
{call buck.visibility_arg /}
43+
4244
{/param} // close args
4345

4446
{param examples}

docs/rule/go_library.soy

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939

4040
{call go_common.compiler_flags_arg /}
4141

42+
{call buck.tests_arg /}
43+
44+
{call buck.visibility_arg /}
45+
4246
{/param} // close args
4347

4448
{param examples}

docs/rule/go_test.soy

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{namespace go_test}
2+
3+
/***/
4+
{template .soyweb}
5+
{call buck.page}
6+
{param title: 'go_test()' /}
7+
{param prettify: true /}
8+
{param description}
9+
A go_test() rule builds a Go library with the sources and runs them as a
10+
"testing" package test.
11+
{/param}
12+
{param content}
13+
14+
{call buck.rule}
15+
{param status: 'UNFROZEN' /}
16+
{param overview}
17+
<p>
18+
A go_test() rule builds a native binary from the supplied set of Go source files
19+
and a generated main file. It's similar to the <i>go test</i> command.
20+
</p>
21+
<p>
22+
{call go_common.supported_language_version /}
23+
</p>
24+
{/param}
25+
26+
{param args}
27+
28+
{call buck.arg}
29+
{param name: 'name' /}
30+
{param desc}
31+
The name of the rule.
32+
{/param}
33+
{/call}
34+
35+
{call go_common.srcs_arg /}
36+
37+
{call buck.arg}
38+
{param name : 'package_name' /}
39+
{param default : 'path relative to the buck root + "_test"' /}
40+
{param desc}
41+
Sets the full name of the test package being compiled. This defaults to the path from the buck
42+
root with "_test" appended. (e.g. given a ./.buckconfig, a rule in ./a/b/BUCK defaults to package "a/b_test")
43+
44+
<p>Note: in order to test packages internally (i.e. same package name), you will need to re-declare
45+
all the sources/dependencies on this test and set package_name appropriately.</p>
46+
{/param}
47+
{/call}
48+
49+
{call go_common.deps_arg /}
50+
51+
{call go_common.compiler_flags_arg /}
52+
53+
{call go_common.linker_flags_arg /}
54+
55+
{call buck.test_label_arg /}
56+
57+
{call buck.run_test_separately_arg /}
58+
59+
{/param} // close args
60+
61+
{param examples}
62+
63+
{call go_common.more_examples /}
64+
65+
{literal}<pre class="prettyprint lang-py">
66+
go_library(
67+
name='greeting',
68+
srcs=[
69+
'greeting.go',
70+
],
71+
deps=[
72+
':join',
73+
],
74+
)
75+
76+
go_test(
77+
name='greeting-test',
78+
srcs=[
79+
'greeting_ext_test.go',
80+
],
81+
deps=[
82+
':greeting'
83+
],
84+
)
85+
86+
go_test(
87+
name='greeting-internal-test',
88+
package_name='greeting',
89+
srcs=[
90+
'greeting.go',
91+
'greeting_test.go',
92+
],
93+
deps=[
94+
':join',
95+
],
96+
)
97+
</pre>{/literal}
98+
{/param}
99+
100+
{/call} // close buck.rule
101+
102+
{/param}
103+
{/call}
104+
{/template}

scripts/travisci_run.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export TERM=dumb
1515
# Go tests in Travis
1616
./bin/buck test \
1717
--num-threads=3 \
18-
--test-selectors '!GoBinaryIntegrationTest'
18+
--test-selectors '!GoBinaryIntegrationTest|GoTestIntegrationTest'
1919

2020
# Run all the other checks with ant.
2121
ant travis

src/com/facebook/buck/go/BUCK

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ java_library(
1111
'GoLinkable.java',
1212
'GoLinkStep.java',
1313
'GoSymlinkTree.java',
14+
'GoTest.java',
15+
'GoTestDescription.java',
16+
'GoTestMain.java',
17+
'GoTestMainStep.java',
18+
'GoTestStep.java',
1419
],
1520
deps = [
1621
'//src/com/facebook/buck/cli:config',
@@ -23,15 +28,22 @@ java_library(
2328
'//src/com/facebook/buck/shell:steps',
2429
'//src/com/facebook/buck/step:step',
2530
'//src/com/facebook/buck/step/fs:fs',
31+
'//src/com/facebook/buck/test/result/type:type',
32+
'//src/com/facebook/buck/test:test',
2633
'//src/com/facebook/buck/util:escaper',
2734
'//src/com/facebook/buck/util:exceptions',
2835
'//src/com/facebook/buck/util:io',
2936
'//third-party/java/guava:guava',
3037
'//third-party/java/infer-annotations:infer-annotations',
38+
'//third-party/java/jackson:jackson',
3139
'//third-party/java/jsr:jsr305',
3240
],
3341
visibility = [
3442
'//src/com/facebook/buck/rules:types',
3543
'//test/com/facebook/buck/go/...',
3644
],
45+
resources = [
46+
'testmaingen.go',
47+
'LICENSE',
48+
],
3749
)

src/com/facebook/buck/go/GoBinaryDescription.java

+5-37
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,19 @@
1818

1919
import com.facebook.buck.cxx.CxxPlatform;
2020
import com.facebook.buck.model.BuildTarget;
21-
import com.facebook.buck.model.ImmutableFlavor;
2221
import com.facebook.buck.rules.BuildRule;
2322
import com.facebook.buck.rules.BuildRuleParams;
2423
import com.facebook.buck.rules.BuildRuleResolver;
2524
import com.facebook.buck.rules.BuildRuleType;
2625
import com.facebook.buck.rules.Description;
2726
import com.facebook.buck.rules.SourcePath;
28-
import com.facebook.buck.rules.SourcePathResolver;
2927
import com.facebook.buck.rules.TargetGraph;
3028
import com.facebook.infer.annotation.SuppressFieldNotInitialized;
3129
import com.google.common.base.Optional;
32-
import com.google.common.base.Suppliers;
3330
import com.google.common.collect.ImmutableList;
3431
import com.google.common.collect.ImmutableSet;
3532
import com.google.common.collect.ImmutableSortedSet;
3633

37-
import java.nio.file.Paths;
3834
import java.util.List;
3935

4036
public class GoBinaryDescription implements Description<GoBinaryDescription.Arg> {
@@ -66,42 +62,14 @@ public <A extends Arg> BuildRule createBuildRule(
6662
BuildRuleParams params,
6763
BuildRuleResolver resolver,
6864
A args) {
69-
BuildTarget libraryTarget =
70-
BuildTarget.builder(params.getBuildTarget())
71-
.addFlavors(ImmutableFlavor.of("compile"))
72-
.build();
73-
GoLibrary library = GoDescriptors.createGoLibraryRule(
74-
params.copyWithBuildTarget(libraryTarget),
65+
return GoDescriptors.createGoBinaryRule(
66+
params,
7567
resolver,
7668
goBuckConfig,
77-
Paths.get("main"),
69+
cxxPlatform,
7870
args.srcs,
79-
args.compilerFlags.or(ImmutableList.<String>of())
80-
);
81-
resolver.addToIndex(library);
82-
83-
BuildRuleParams binaryParams = params.copyWithDeps(
84-
Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>of(library)),
85-
Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>of())
86-
);
87-
88-
GoSymlinkTree symlinkTree = GoDescriptors.requireTransitiveSymlinkTreeRule(
89-
binaryParams,
90-
resolver);
91-
92-
return new GoBinary(
93-
params.copyWithDeps(
94-
Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>of(symlinkTree, library)),
95-
Suppliers.ofInstance(ImmutableSortedSet.<BuildRule>of())),
96-
new SourcePathResolver(resolver),
97-
cxxPlatform.getLd(),
98-
symlinkTree,
99-
library,
100-
goBuckConfig.getGoLinker().get(),
101-
ImmutableList.<String>builder()
102-
.addAll(goBuckConfig.getLinkerFlags())
103-
.addAll(args.linkerFlags.or(ImmutableList.<String>of()))
104-
.build());
71+
args.compilerFlags.or(ImmutableList.<String>of()),
72+
args.linkerFlags.or(ImmutableList.<String>of()));
10573
}
10674

10775
@SuppressFieldNotInitialized

src/com/facebook/buck/go/GoBuckConfig.java

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.facebook.buck.go;
1818

1919
import com.facebook.buck.cli.BuckConfig;
20+
import com.facebook.buck.rules.BuildRuleResolver;
2021
import com.facebook.buck.rules.HashedFileTool;
2122
import com.facebook.buck.rules.Tool;
2223
import com.facebook.buck.io.ExecutableFinder;
@@ -86,6 +87,10 @@ Supplier<Tool> getGoLinker() {
8687
return getGoTool("linker", "link");
8788
}
8889

90+
Optional<Tool> getGoTestMainGenerator(BuildRuleResolver resolver) {
91+
return delegate.getTool("go", "test_main_gen", resolver);
92+
}
93+
8994
ImmutableList<String> getCompilerFlags() {
9095
return ImmutableList.copyOf(
9196
Splitter.on(" ").omitEmptyStrings().split(

0 commit comments

Comments
 (0)