Skip to content

Commit 0c0b82b

Browse files
authored
feat(internal/librarian/golang): clean generated go code (#4104)
Updates clean logic for Go library. Only clean a file in the library output directory if it matches one of the patterns. Files in the keep list will still be skipped. Inspired by [this](https://github.com/googleapis/google-cloud-go/pull/13276/changes#diff-7e62796112c00634cc622408013ed91eb2ab2cf3cc7ce59fe4f152731f66419dL236) PR. Should be merged after #4084 For #3617
1 parent 4df7577 commit 0c0b82b

2 files changed

Lines changed: 61 additions & 14 deletions

File tree

internal/librarian/golang/clean.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,27 @@ import (
2020
"io/fs"
2121
"os"
2222
"path/filepath"
23+
"regexp"
2324

2425
"github.com/googleapis/librarian/internal/config"
2526
)
2627

28+
var (
29+
generatedRegex = []*regexp.Regexp{
30+
regexp.MustCompile(`.*/apiv(\d+).*/\.repo-metadata\.json$`),
31+
regexp.MustCompile(`.*/apiv(\d+).*/auxiliary\.go$`),
32+
regexp.MustCompile(`.*/apiv(\d+).*/auxiliary_go123\.go$`),
33+
regexp.MustCompile(`.*/apiv(\d+).*/doc\.go$`),
34+
regexp.MustCompile(`.*/apiv(\d+).*/.*_client\.go$`),
35+
regexp.MustCompile(`.*/apiv(\d+).*/.*_client_example_go123_test\.go$`),
36+
regexp.MustCompile(`.*/apiv(\d+).*/.*_client_example_test\.go$`),
37+
regexp.MustCompile(`.*/apiv(\d+).*/gapic_metadata\.json$`),
38+
regexp.MustCompile(`.*/apiv(\d+).*/helpers\.go$`),
39+
regexp.MustCompile(`.*pb/.*\.pb\.go$`),
40+
regexp.MustCompile(`.*/internal/generated/snippets/.*$`),
41+
}
42+
)
43+
2744
// Clean cleans up a Go library and its associated snippets.
2845
func Clean(library *config.Library) error {
2946
libraryDir := filepath.Join(library.Output, library.Name)
@@ -95,7 +112,14 @@ func clean(dir, nestedModule string, keepSet map[string]bool) error {
95112
if err != nil {
96113
return err
97114
}
98-
if keepSet[rel] {
115+
isGenerated := false
116+
for _, re := range generatedRegex {
117+
if re.MatchString(path) {
118+
isGenerated = true
119+
break
120+
}
121+
}
122+
if keepSet[rel] || !isGenerated {
99123
return nil
100124
}
101125
return os.Remove(path)

internal/librarian/golang/clean_test.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
func TestClean_Success(t *testing.T) {
28-
root := t.TempDir()
28+
t.Parallel()
2929
libraryName := "testlib"
3030
for _, test := range []struct {
3131
name string
@@ -37,30 +37,53 @@ func TestClean_Success(t *testing.T) {
3737
wantSnippets []string
3838
}{
3939
{
40-
name: "removes all except keep list",
41-
outputFiles: []string{"file1.go", "file2.go", "go.mod"},
40+
name: "removes all generated files",
41+
outputFiles: []string{
42+
"apiv1/.repo-metadata.json",
43+
"apiv1/auxiliary.go",
44+
"apiv1/auxiliary_go123.go",
45+
"apiv1/doc.go",
46+
"apiv1/library_client.go",
47+
"apiv1/library_client_example_go123_test.go",
48+
"apiv1/library_client_example_test.go",
49+
"apiv1/gapic_metadata.json",
50+
"apiv1/helpers.go",
51+
"apiv1/librarypb/content.pb.go",
52+
"apiv1/non-generated.go",
53+
"doc.go",
54+
},
4255
snippetFiles: []string{"snippet1.go", "snippet2.go", "README.md"},
43-
keep: []string{"file1.go"},
44-
wantOutput: []string{"file1.go"},
56+
keep: []string{},
57+
wantOutput: []string{
58+
"apiv1/non-generated.go",
59+
// skipped because the file doesn't live in a versioned directory.
60+
"doc.go",
61+
},
4562
},
4663
{
47-
name: "remove all files",
48-
outputFiles: []string{"file1.go", "file2.go"},
64+
name: "remove all generated files except keep list",
65+
outputFiles: []string{
66+
"apiv1/auxiliary.go",
67+
"apiv1/auxiliary_go123.go",
68+
"nested/apiv1/doc.go",
69+
},
4970
snippetFiles: []string{"snippet1.go"},
50-
keep: []string{},
51-
wantOutput: []string{},
71+
keep: []string{"apiv1/auxiliary_go123.go"},
72+
wantOutput: []string{"apiv1/auxiliary_go123.go"},
5273
},
5374
{
5475
name: "nested module",
55-
outputFiles: []string{"file1.go", "file2.go", "nested/nested_file.go"},
76+
outputFiles: []string{"nested/apiv1beta1/doc.go"},
5677
snippetFiles: []string{"nested/snippet.go"},
5778
keep: []string{},
5879
nestedModule: "nested",
59-
wantOutput: []string{"nested/nested_file.go"},
80+
wantOutput: []string{"nested/apiv1beta1/doc.go"},
6081
wantSnippets: []string{"internal/generated/snippets/testlib/nested/snippet.go"},
6182
},
6283
} {
6384
t.Run(test.name, func(t *testing.T) {
85+
t.Parallel()
86+
root := t.TempDir()
6487
outputPath := filepath.Join(root, libraryName)
6588
snippetPath := filepath.Join(root, "internal", "generated", "snippets", libraryName)
6689
lib := &config.Library{
@@ -98,8 +121,7 @@ func TestClean_Success(t *testing.T) {
98121
}
99122
}
100123

101-
func TestCleanGo_Error(t *testing.T) {
102-
root := t.TempDir()
124+
func TestClean_Error(t *testing.T) {
103125
libraryName := "testlib"
104126
for _, test := range []struct {
105127
name string
@@ -117,6 +139,7 @@ func TestCleanGo_Error(t *testing.T) {
117139
},
118140
} {
119141
t.Run(test.name, func(t *testing.T) {
142+
root := t.TempDir()
120143
outputPath := filepath.Join(root, libraryName)
121144
snippetPath := filepath.Join(root, "internal", "generated", "snippets", libraryName)
122145
lib := &config.Library{

0 commit comments

Comments
 (0)