Skip to content

Commit a1b3bb6

Browse files
authored
test(sidekick): add test for codec helpers (#1587)
`language.Generate()` and `language.WalkTemplatesDir` are helpers for the language codecs. They need unit tests, even though the integration tests already use the code and indirectly test it.
1 parent 9247ab1 commit a1b3bb6

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package language
16+
17+
import (
18+
"embed"
19+
"os"
20+
"path"
21+
"testing"
22+
23+
"github.com/googleapis/librarian/internal/sidekick/internal/api"
24+
)
25+
26+
//go:embed all:testTemplates
27+
var templates embed.FS
28+
29+
func TestGenerate(t *testing.T) {
30+
model := api.NewTestAPI([]*api.Message{}, []*api.Enum{}, []*api.Service{})
31+
outDir := t.TempDir()
32+
33+
provider := func(name string) (string, error) {
34+
contents, err := templates.ReadFile(name)
35+
if err != nil {
36+
return "", err
37+
}
38+
return string(contents), nil
39+
}
40+
// The list of files to generate, just load them from the embedded templates.
41+
generatedFiles := WalkTemplatesDir(templates, "testTemplates")
42+
err := GenerateFromModel(outDir, model, provider, generatedFiles)
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
for _, expected := range []string{"README.md", "test001.txt"} {
47+
filename := path.Join(outDir, expected)
48+
stat, err := os.Stat(filename)
49+
if os.IsNotExist(err) {
50+
t.Errorf("missing %s: %s", filename, err)
51+
}
52+
if stat.Mode().Perm()|0666 != 0666 {
53+
t.Errorf("generated files should not be executable %s: %o", filename, stat.Mode())
54+
}
55+
}
56+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{!
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
}}
16+
# Test Only - {{{Title}}}
17+
18+
<!-- Code generated by sidekick. DO NOT EDIT. -->
19+
{{#Description}}
20+
21+
{{{.}}}
22+
{{/Description}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{!
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
}}
16+
17+
{{! Verify partials are not included in Walk}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{!
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
}}
16+
{{>/testTemplates/common/partial}}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package language
16+
17+
import (
18+
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
21+
)
22+
23+
func TestWalkDir(t *testing.T) {
24+
// It should get the `*.md.mustache` files and skip `partial.mustache`
25+
got := WalkTemplatesDir(templates, "testTemplates")
26+
want := []GeneratedFile{
27+
{
28+
TemplatePath: "testTemplates/README.md.mustache",
29+
OutputPath: "/README.md",
30+
},
31+
{
32+
TemplatePath: "testTemplates/test001.txt.mustache",
33+
OutputPath: "/test001.txt",
34+
},
35+
}
36+
if diff := cmp.Diff(want, got); len(diff) != 0 {
37+
t.Errorf("mismatched config from LoadConfig (-want, +got):\n%s", diff)
38+
}
39+
}

0 commit comments

Comments
 (0)