From 48379aa03ddfd0aaa00dd270e81c97aabf495177 Mon Sep 17 00:00:00 2001 From: Peter Ebden Date: Sun, 1 Oct 2023 10:12:27 +0100 Subject: [PATCH 1/2] Add test --- test/embed/embed.go | 8 +++++++- test/embed/embed_test.go | 11 +++++++++++ test/embed/subdir/_test.txt | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/embed/subdir/_test.txt diff --git a/test/embed/embed.go b/test/embed/embed.go index 04dc295e..3a9755b3 100644 --- a/test/embed/embed.go +++ b/test/embed/embed.go @@ -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 diff --git a/test/embed/embed_test.go b/test/embed/embed_test.go index 48c57eef..5d82a15a 100644 --- a/test/embed/embed_test.go +++ b/test/embed/embed_test.go @@ -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 := subdir.ReadFile("subdir/_test.txt") + assert.NoError(t, err) + assert.Equal(t, "hello", strings.TrimSpace(string(b))) +} diff --git a/test/embed/subdir/_test.txt b/test/embed/subdir/_test.txt new file mode 100644 index 00000000..ce013625 --- /dev/null +++ b/test/embed/subdir/_test.txt @@ -0,0 +1 @@ +hello From fe087185b793e2c03e09df87599bb69e77eea4ac Mon Sep 17 00:00:00 2001 From: Peter Ebden Date: Sun, 1 Oct 2023 10:17:04 +0100 Subject: [PATCH 2/2] Implement --- test/embed/BUILD | 4 ++-- test/embed/embed_test.go | 2 +- tools/please_go/embed/embed.go | 13 ++++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/test/embed/BUILD b/test/embed/BUILD index af34d8d8..0509edd1 100644 --- a/test/embed/BUILD +++ b/test/embed/BUILD @@ -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", diff --git a/test/embed/embed_test.go b/test/embed/embed_test.go index 5d82a15a..7b57dc0a 100644 --- a/test/embed/embed_test.go +++ b/test/embed/embed_test.go @@ -17,7 +17,7 @@ func TestEmbedDir(t *testing.T) { } func TestEmbedDirAll(t *testing.T) { - b, err := subdir.ReadFile("subdir/_test.txt") + b, err := subdirAll.ReadFile("subdir/_test.txt") assert.NoError(t, err) assert.Equal(t, "hello", strings.TrimSpace(string(b))) } diff --git a/tools/please_go/embed/embed.go b/tools/please_go/embed/embed.go index 5c2a4f8c..679433af 100644 --- a/tools/please_go/embed/embed.go +++ b/tools/please_go/embed/embed.go @@ -7,6 +7,7 @@ import ( "go/build" "io" "io/fs" + "log" "path" "path/filepath" "strings" @@ -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 @@ -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) @@ -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 {