Skip to content

Commit e5a3d82

Browse files
authored
feat(go): generated wiremock tests (#9314)
* initial commit * Use only first endpoint example so as to avoid name and file collisions * add support for as is files * include all dist outputs in go-v2 generator * mvp fixes * do not include error case for now * remove unnecessary safe escape code * rename tests to avoid collisions * final fixes for now * add illustrative wire test seed fixture * remove uneeded initializer flag * use consistent property declaration * fix biome * fix test snapshots to use new aliasing behavior * fully fix snapshots * fix dynamic snippet snapshot tests also * small tweak to main_test * ignore new fixture for now
1 parent 831ca13 commit e5a3d82

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+5316
-174
lines changed

generators/go-v2/ast/src/ast/__test__/__snapshots__/Snippets.test.ts.snap

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ exports[`Snippets > 'enum (optional)' 1`] = `
5454
"package example
5555
5656
import (
57-
acme "github.com/acme/acme-go"
57+
acmego "github.com/acme/acme-go"
5858
)
5959
60-
var value = acme.StatusDeactivated.Ptr()"
60+
var value = acmego.StatusDeactivated.Ptr()"
6161
`;
6262

6363
exports[`Snippets > 'enum (required)' 1`] = `
6464
"package example
6565
6666
import (
67-
acme "github.com/acme/acme-go"
67+
acmego "github.com/acme/acme-go"
6868
)
6969
70-
var value = acme.StatusActivated"
70+
var value = acmego.StatusActivated"
7171
`;
7272

7373
exports[`Snippets > 'map (empty)' 1`] = `
@@ -80,14 +80,14 @@ exports[`Snippets > 'map (nested)' 1`] = `
8080
"package example
8181
8282
import (
83-
acme "github.com/acme/acme-go"
83+
acmego "github.com/acme/acme-go"
8484
)
8585
86-
var value = map[string]acme.User{
87-
"john": acme.User{
86+
var value = map[string]acmego.User{
87+
"john": acmego.User{
8888
Name: "John Doe",
8989
},
90-
"jane": acme.User{
90+
"jane": acmego.User{
9191
Name: "Jane Doe",
9292
},
9393
}"
@@ -151,30 +151,30 @@ exports[`Snippets > 'struct (empty)' 1`] = `
151151
"package example
152152
153153
import (
154-
acme "github.com/acme/acme-go"
154+
acmego "github.com/acme/acme-go"
155155
)
156156
157-
var value = acme.User{}"
157+
var value = acmego.User{}"
158158
`;
159159

160160
exports[`Snippets > 'struct (nested)' 1`] = `
161161
"package example
162162
163163
import (
164-
acme "github.com/acme/acme-go"
164+
acmego "github.com/acme/acme-go"
165165
billing "github.com/acme/acme-go/billing"
166166
uuid "github.com/google/uuid"
167167
)
168168
169-
var value = acme.User{
169+
var value = acmego.User{
170170
Name: "John Doe",
171171
Address: billing.Address{
172172
ID: uuid.MustParse(
173173
"123e4567-e89b-12d3-a456-426614174000",
174174
),
175175
Street: "123 Main St.",
176-
CreatedAt: acme.Time(
177-
acme.MustParseDateTime(
176+
CreatedAt: acmego.Time(
177+
acmego.MustParseDateTime(
178178
"1994-01-01T00:00:00Z",
179179
),
180180
),
@@ -186,13 +186,13 @@ exports[`Snippets > 'struct (primitives)' 1`] = `
186186
"package example
187187
188188
import (
189-
acme "github.com/acme/acme-go"
189+
acmego "github.com/acme/acme-go"
190190
)
191191
192-
var value = acme.User{
192+
var value = acmego.User{
193193
Name: "John Doe",
194194
Age: 42,
195-
Active: acme.Bool(
195+
Active: acmego.Bool(
196196
true,
197197
),
198198
}"
@@ -202,10 +202,10 @@ exports[`Snippets > 'struct (w/ nop)' 1`] = `
202202
"package example
203203
204204
import (
205-
acme "github.com/acme/acme-go"
205+
acmego "github.com/acme/acme-go"
206206
)
207207
208-
var value = acme.User{
208+
var value = acmego.User{
209209
Name: "John Doe",
210210
Age: 42,
211211
}"

generators/go-v2/ast/src/ast/core/Writer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ export class Writer extends AbstractWriter {
9494
if (split[0] == null) {
9595
return s;
9696
}
97-
return split[0].replace(INVALID_GO_IDENTIFIER_TOKEN, "");
97+
return split.map((part) => part.replace(INVALID_GO_IDENTIFIER_TOKEN, "")).join("");
9898
}
9999
}

generators/go-v2/ast/src/custom-config/BaseGoCustomConfigSchema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export const baseGoCustomConfigSchema = z.object({
2020
union: z.enum(["v0", "v1"]).optional(),
2121
useReaderForBytesRequest: z.boolean().optional(),
2222
useDefaultRequestParameterValues: z.boolean().optional(),
23-
gettersPassByValue: z.boolean().optional()
23+
gettersPassByValue: z.boolean().optional(),
24+
enableWireTests: z.boolean().optional()
2425
});
2526

2627
export type BaseGoCustomConfigSchema = z.infer<typeof baseGoCustomConfigSchema>;

generators/go-v2/base/src/AsIs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export enum AsIsFiles {
33
ExtraProperties = "internal/extra_properties.go_",
44
ExtraPropertiesTest = "internal/extra_properties_test.go_",
55
Stringer = "internal/stringer.go_",
6-
Time = "internal/time.go_"
6+
Time = "internal/time.go_",
7+
MainTest = "test/main_test.go_"
78
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package wiremock
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"testing"
8+
9+
gowiremock "github.com/wiremock/go-wiremock"
10+
wiremocktestcontainersgo "github.com/wiremock/wiremock-testcontainers-go"
11+
)
12+
13+
// Global test fixtures
14+
var (
15+
WireMockContainer *wiremocktestcontainersgo.WireMockContainer
16+
WireMockBaseURL string
17+
WireMockClient *gowiremock.Client
18+
)
19+
20+
// TestMain sets up shared test fixtures for all tests in this package
21+
func TestMain(m *testing.M) {
22+
// Setup shared WireMock container
23+
ctx := context.Background()
24+
container, err := wiremocktestcontainersgo.RunContainerAndStopOnCleanup(
25+
ctx,
26+
&testing.T{},
27+
wiremocktestcontainersgo.WithImage("docker.io/wiremock/wiremock:3.9.1"),
28+
)
29+
if err != nil {
30+
fmt.Printf("Failed to start WireMock container: %v\n", err)
31+
os.Exit(1)
32+
}
33+
34+
// Store global references
35+
WireMockContainer = container
36+
WireMockClient = container.Client
37+
38+
// Get the base URL
39+
baseURL, err := container.Endpoint(ctx, "")
40+
if err != nil {
41+
fmt.Printf("Failed to get WireMock container endpoint: %v\n", err)
42+
os.Exit(1)
43+
}
44+
WireMockBaseURL = "http://" + baseURL
45+
46+
// Run all tests
47+
code := m.Run()
48+
49+
// Cleanup
50+
if WireMockContainer != nil {
51+
WireMockContainer.Terminate(ctx)
52+
}
53+
54+
// Exit with the same code as the tests
55+
os.Exit(code)
56+
}

generators/go-v2/base/src/context/AbstractGoGeneratorContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,4 +667,6 @@ export abstract class AbstractGoGeneratorContext<
667667
}
668668

669669
public abstract getInternalAsIsFiles(): string[];
670+
671+
public abstract getTestAsIsFiles(): string[];
670672
}

generators/go-v2/base/src/project/GoProject.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ export class GoProject extends AbstractProject<AbstractGoGeneratorContext<BaseGo
8989
});
9090
}
9191

92+
public async writeSharedTestFiles(): Promise<AbsoluteFilePath> {
93+
const sharedTestFiles = await Promise.all(
94+
this.context.getTestAsIsFiles().map(async (filename) => await this.createAsIsFile({ filename }))
95+
);
96+
97+
return await this.createGoDirectory({
98+
absolutePathToDirectory: join(this.absolutePathToOutputDirectory),
99+
files: sharedTestFiles
100+
});
101+
}
102+
92103
private async createGoDirectory({
93104
absolutePathToDirectory,
94105
files

generators/go-v2/dynamic-snippets/src/DynamicSnippetsGenerator.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {
22
AbstractDynamicSnippetsGenerator,
33
AbstractFormatter,
4-
FernGeneratorExec
4+
FernGeneratorExec,
5+
Options
56
} from "@fern-api/browser-compatible-base-generator";
67
import { FernIr } from "@fern-api/dynamic-ir-sdk";
78
import { DynamicSnippetsGeneratorContext } from "./context/DynamicSnippetsGeneratorContext";
@@ -27,13 +28,17 @@ export class DynamicSnippetsGenerator extends AbstractDynamicSnippetsGenerator<
2728
}
2829

2930
public async generate(
30-
request: FernIr.dynamic.EndpointSnippetRequest
31+
request: FernIr.dynamic.EndpointSnippetRequest,
32+
options?: Options
3133
): Promise<FernIr.dynamic.EndpointSnippetResponse> {
32-
return super.generate(request);
34+
return super.generate(request, options);
3335
}
3436

35-
public generateSync(request: FernIr.dynamic.EndpointSnippetRequest): FernIr.dynamic.EndpointSnippetResponse {
36-
return super.generateSync(request);
37+
public generateSync(
38+
request: FernIr.dynamic.EndpointSnippetRequest,
39+
options?: Options
40+
): FernIr.dynamic.EndpointSnippetResponse {
41+
return super.generateSync(request, options);
3742
}
3843

3944
protected createSnippetGenerator(context: DynamicSnippetsGeneratorContext): EndpointSnippetGenerator {

0 commit comments

Comments
 (0)