Skip to content

Commit 4c664df

Browse files
authoredDec 14, 2021
Upgrade to Pants 2.9 and demonstrate embedding and testdata (#4)
Major change is that you now explicitly declare one `go_package` target per directory. This also demonstrates the new features of `testdata` folder and resource embedding.
1 parent fe2932c commit 4c664df

File tree

10 files changed

+54
-1
lines changed

10 files changed

+54
-1
lines changed
 

‎cmd/greeter_en/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2021 Pants project contributors.
22
# Licensed under the Apache License, Version 2.0 (see LICENSE).
33

4+
go_package()
5+
46
# This target allows us to use `./pants run` and `./pants package` on this `main` Go package.
57
#
68
# You can optionally set the field `output_path="greeter_en"`, for example, for the binary's name

‎cmd/greeter_es/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2021 Pants project contributors.
22
# Licensed under the Apache License, Version 2.0 (see LICENSE).
33

4+
go_package()
5+
46
# This target allows us to use `./pants run` and `./pants package` on this `main` Go package.
57
#
68
# You can optionally set the field `output_path="greeter_es"`, for example, for the binary's name

‎pants.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the Apache License, Version 2.0 (see LICENSE).
33

44
[GLOBAL]
5-
pants_version = "2.8.0"
5+
pants_version = "2.9.0.dev3"
66
backend_packages = ["pants.backend.experimental.go"]
77

88
[anonymous-telemetry]

‎pkg/embed_example/BUILD

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2021 Pants project contributors.
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
go_package(dependencies=[":embeds", ":testdata"])
5+
6+
resources(name="embeds", sources=["hello.txt"])
7+
files(name="testdata", sources=["testdata/*"])

‎pkg/embed_example/hello.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I'm an embedded resource!

‎pkg/embed_example/lib.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2021 Pants project contributors.
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
package embed_example
5+
6+
// TODO: enable after fixing Pants when lib code has embeds but tests don't.
7+
// import _ "embed"
8+
// //go:embed hello.txt
9+
// var embeddedHello string

‎pkg/embed_example/lib_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2021 Pants project contributors.
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
package embed_example
5+
6+
import (
7+
"os"
8+
"testing"
9+
)
10+
11+
// TODO: enable after fixing Pants when lib code has embeds but tests don't.
12+
// func TestResourceEmbedding(t *testing.T) {
13+
// if embeddedHello != "I'm an embedded resource!" {
14+
// t.Fatalf("message mismatch: want=%s; got=%s", "I'm an embedded resource!", embeddedHello)
15+
// }
16+
// }
17+
18+
func TestTestDataFolder(t *testing.T) {
19+
_, err := os.Stat("testdata/f.txt")
20+
if err != nil {
21+
t.Fatalf("Could not stat pkg/embed_example/testdata/f.txt: %v", err)
22+
}
23+
}

‎pkg/embed_example/testdata/f.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Some test data!

‎pkg/greeter/BUILD

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2021 Pants project contributors.
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
go_package()

‎pkg/uuid/BUILD

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2021 Pants project contributors.
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
go_package()

0 commit comments

Comments
 (0)