Debian Codegraph @

internal/librariangen/postprocessor/postprocessor_test.go

raw
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 // http://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 postprocessor
16
17 import (
18 "context"
19 "errors"
20 "os"
21 "path/filepath"
22 "strings"
23 "testing"
24
25 "cloud.google.com/go/internal/postprocessor/librarian/librariangen/config"
26 "cloud.google.com/go/internal/postprocessor/librarian/librariangen/request"
27 )
28
29 func TestPostProcess(t *testing.T) {
30 tests := []struct {
31 name string
32 mockexecvRun func(ctx context.Context, args []string, dir string) error
33 wantErr bool
34 noVersion bool
35 }{
36 {
37 name: "success",
38 mockexecvRun: func(ctx context.Context, args []string, dir string) error {
39 return nil
40 },
41 wantErr: false,
42 },
43 {
44 name: "goimports fails (fatal)",
45 mockexecvRun: func(ctx context.Context, args []string, dir string) error {
46 if args[0] == "goimports" {
47 return errors.New("goimports failed")
48 }
49 return nil
50 },
51 wantErr: true,
52 },
53 {
54 name: "fail without version",
55 noVersion: true,
56 wantErr: true,
57 },
58 }
59
60 for _, tt := range tests {
61 t.Run(tt.name, func(t *testing.T) {
62 outputDir := t.TempDir()
63 moduleDir := filepath.Join(outputDir, "chronicle")
64 if err := os.MkdirAll(moduleDir, 0755); err != nil {
65 t.Fatalf("failed to create directory %s: %v", moduleDir, err)
66 return
67 }
68
69 snippetsDir := filepath.Join(outputDir, "internal", "generated", "snippets", "chronicle")
70
71 if err := createDirectories(t, moduleDir, snippetsDir, filepath.Join(snippetsDir, "sublevel1/sublevel2")); err != nil {
72 // Specific failure will have been logged already.
73 return
74 }
75
76 snippetMetadataFiles := []string{
77 "apiv1/snippet_metadata.google.cloud.chronicle.v1.json",
78 "apiv2/snippet_metadata.google.cloud.chronicle.v2.json",
79 "sublevel1/sublevel2/apiv1/snippet_metadata.google.cloud.chronicle.sublevel1.sublevel2.v1.json",
80 // This is *not* part of the request, so won't be modified.
81 "apiv3/snippet_metadata.google.cloud.chronicle.v3.json",
82 }
83 for _, snippetMetadataFile := range snippetMetadataFiles {
84 fullPath := filepath.Join(snippetsDir, snippetMetadataFile)
85 specificSnippetDir := filepath.Dir(fullPath)
86 if err := os.MkdirAll(specificSnippetDir, 0755); err != nil {
87 t.Fatalf("failed to create directory %s: %v", moduleDir, err)
88 return
89 }
90 content := "x\ny\nversion: $VERSION\na\nb\n"
91 if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
92 t.Fatalf("failed to write file %s: %v", fullPath, err)
93 return
94 }
95 }
96
97 if err := os.MkdirAll(moduleDir, 0755); err != nil {
98 t.Fatalf("failed to create moduleDir %v", err)
99 return
100 }
101
102 execvRun = tt.mockexecvRun
103
104 req := &request.Library{
105 ID: "chronicle",
106 APIs: []request.API{
107 {Path: "google/cloud/chronicle/v1"},
108 {Path: "google/cloud/chronicle/v2"},
109 {Path: "google/cloud/chronicle/sublevel1/sublevel2/v1"},
110 },
111 Version: "1.0.0",
112 }
113
114 if tt.noVersion {
115 req.Version = ""
116 }
117
118 moduleConfig := &config.ModuleConfig{
119 Name: "chronicle",
120 }
121 if err := PostProcess(context.Background(), req, outputDir, moduleDir, moduleConfig); (err != nil) != tt.wantErr {
122 t.Fatalf("PostProcess() error = %v, wantErr %v", err, tt.wantErr)
123 }
124
125 if tt.wantErr {
126 return
127 }
128
129 for _, snippetMetadataFile := range snippetMetadataFiles {
130 path := filepath.Join(snippetsDir, snippetMetadataFile)
131 read, err := os.ReadFile(path)
132 if err != nil {
133 t.Errorf("Couldn't read snippet metadata file %s: %v", snippetMetadataFile, err)
134 }
135 wantModified := !strings.Contains(snippetMetadataFile, "v3")
136 gotModified := strings.Contains(string(read), req.Version)
137 if wantModified != gotModified {
138 t.Errorf("incorrect snippet metadata modification for %s; got = %v; want = %v", snippetMetadataFile, gotModified, wantModified)
139 }
140 }
141 })
142 }
143 }
144
145 func createDirectories(t *testing.T, directories ...string) error {
146 for _, directory := range directories {
147 if err := os.MkdirAll(directory, 0755); err != nil {
148 t.Fatalf("failed to create directory %s: %v", directory, err)
149 return err
150 }
151 }
152 return nil
153 }