Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support :all syntax correctly when embedding #161

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/embed/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ subinclude("//build_defs:go")
go_library(
name = "embed",
srcs = ["embed.go"],
resources = ["hello.txt"],
resources = ["hello.txt", "subdir"],
)

go_test(
name = "embed_test",
srcs = ["embed_test.go"],
resources = ["hello.txt"],
resources = ["hello.txt", "subdir"],
deps = [
":embed",
"//third_party/go:testify",
Expand Down
8 changes: 7 additions & 1 deletion test/embed/embed.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package embed

import _ "embed"
import "embed"

//go:embed hello.txt
var hello string

//go:embed subdir
var subdir embed.FS

//go:embed all:subdir
var subdirAll embed.FS
11 changes: 11 additions & 0 deletions test/embed/embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ import (
func TestLibEmbed(t *testing.T) {
assert.Equal(t, "hello", strings.TrimSpace(hello))
}

func TestEmbedDir(t *testing.T) {
_, err := subdir.ReadFile("subdir/_test.txt")
assert.Error(t, err)
}

func TestEmbedDirAll(t *testing.T) {
b, err := subdirAll.ReadFile("subdir/_test.txt")
assert.NoError(t, err)
assert.Equal(t, "hello", strings.TrimSpace(string(b)))
}
1 change: 1 addition & 0 deletions test/embed/subdir/_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
13 changes: 12 additions & 1 deletion tools/please_go/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/build"
"io"
"io/fs"
"log"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -54,6 +55,7 @@ func Parse(gofiles []string) (*Cfg, error) {
// AddPackage parses a go package and adds any embed patterns to the configuration
func (cfg *Cfg) AddPackage(pkg *build.Package) error {
for _, pattern := range append(append(pkg.EmbedPatterns, pkg.TestEmbedPatterns...), pkg.XTestEmbedPatterns...) {
log.Printf("here %s", pattern)
paths, err := relglob(pkg.Dir, pattern)
if err != nil {
return err
Expand All @@ -79,6 +81,13 @@ func dirs(files []string) []string {
}

func relglob(dir, pattern string) ([]string, error) {
// Go allows prefixing the pattern with all: which picks up files prefixed with . or _ (by default these should be ignored)
includeHidden := false
if strings.HasPrefix(pattern, "all:") {
pattern = strings.TrimPrefix(pattern, "all:")
includeHidden = true
}

paths, err := filepath.Glob(path.Join(dir, pattern))
if err == nil && len(paths) == 0 {
return nil, fmt.Errorf("pattern %s: no matching paths found", pattern)
Expand All @@ -89,7 +98,9 @@ func relglob(dir, pattern string) ([]string, error) {
if err != nil {
return err
} else if !d.IsDir() {
ret = append(ret, strings.TrimLeft(strings.TrimPrefix(path, dir), string(filepath.Separator)))
if hidden := strings.HasPrefix(d.Name(), ".") || strings.HasPrefix(d.Name(), "_"); !hidden || includeHidden {
ret = append(ret, strings.TrimLeft(strings.TrimPrefix(path, dir), string(filepath.Separator)))
}
}
return nil
}); err != nil {
Expand Down
Loading