Skip to content

Commit

Permalink
Update tests for @bufbuild/protoplugin (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm committed Dec 12, 2023
1 parent 62f9ac4 commit db30763
Show file tree
Hide file tree
Showing 32 changed files with 1,242 additions and 1,913 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ $(GEN)/protobuf-test: $(BUILD)/upstream-protobuf $(BUILD)/protoc-gen-es $(shell

$(GEN)/protoplugin-test: $(BUILD)/protoc-gen-es $(shell find packages/protoplugin-test/proto -name '*.proto')
@rm -rf packages/protoplugin-test/src/gen/* packages/protoplugin-test/descriptorset.bin
@npm run -w packages/protoplugin-test buf:build
@npm run -w packages/protoplugin-test generate
@mkdir -p $(@D)
@touch $(@)

$(GEN)/protobuf-conformance: $(BUILD)/upstream-protobuf $(BUILD)/protoc-gen-es
npm run -w packages/protobuf-conformance generate
Expand Down
14 changes: 5 additions & 9 deletions packages/protoplugin-example/src/protoc-gen-twirp-es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
import { createEcmaScriptPlugin, runNodeJs } from "@bufbuild/protoplugin";
import { version } from "../package.json";
import type { Schema } from "@bufbuild/protoplugin/ecmascript";
import {
literalString,
localName,
makeJsDoc,
} from "@bufbuild/protoplugin/ecmascript";
import { localName } from "@bufbuild/protoplugin/ecmascript";
import { MethodKind } from "@bufbuild/protobuf";

const protocGenTwirpEs = createEcmaScriptPlugin({
Expand All @@ -40,7 +36,7 @@ function generateTs(schema: Schema) {
JsonValue
} = schema.runtime;
for (const service of file.services) {
f.print(makeJsDoc(service));
f.print(f.jsDoc(service));
f.print(f.exportDecl("class", localName(service) + "Client"), " {");
f.print(" private baseUrl: string = '';");
f.print();
Expand Down Expand Up @@ -75,11 +71,11 @@ function generateTs(schema: Schema) {
for (const method of service.methods) {
if (method.methodKind === MethodKind.Unary) {
f.print();
f.print(makeJsDoc(method, " "));
f.print(f.jsDoc(method, " "));
f.print(" async ", localName(method), "(request: ", method.input, "): Promise<", method.output, "> {");
f.print(" const promise = this.request(");
f.print(" ", literalString(service.typeName), ",");
f.print(" ", literalString(method.name), ",");
f.print(" ", f.string(service.typeName), ",");
f.print(" ", f.string(method.name), ",");
f.print(' "application/json",');
f.print(" request");
f.print(" );");
Expand Down
1 change: 0 additions & 1 deletion packages/protoplugin-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"clean": "rm -rf ./dist/cjs/* ./dist/esm/* ./dist/types/*",
"build": "npm run build:esm+types",
"build:esm+types": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --verbatimModuleSyntax --outDir ./dist/esm --declaration --declarationDir ./dist/types",
"buf:build": "buf build -o descriptorset.bin",
"generate": "buf generate",
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest"
},
Expand Down
62 changes: 0 additions & 62 deletions packages/protoplugin-test/proto/custom_options.proto

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// limitations under the License.

syntax = "proto3";
package example;

import "proto/person.proto";
package test;

// Our address book file is just one of these.
message AddressBook {
repeated Person people = 1;
// Used in custom-options.test.ts
enum OptionEnum {
OPTION_ENUM_UNSPECIFIED = 0;
OPTION_ENUM_A = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,12 @@
// limitations under the License.

syntax = "proto3";
package example;

import "google/protobuf/timestamp.proto";
package test;

message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;

enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}

message PhoneNumber {
string number = 1;
PhoneType type = 2;
}

repeated PhoneNumber phones = 4;

google.protobuf.Timestamp last_updated = 5;
// Used in custom-options.test.ts
message OptionMessage {
int32 foo = 1;
string bar = 2;
repeated string many = 4;
}

63 changes: 0 additions & 63 deletions packages/protoplugin-test/proto/service.proto

This file was deleted.

34 changes: 12 additions & 22 deletions packages/protoplugin-test/src/byo-transpile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@
// limitations under the License.

import { describe, expect, test } from "@jest/globals";
import { CodeGeneratorRequest } from "@bufbuild/protobuf";
import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";
import type { FileInfo } from "@bufbuild/protoplugin/ecmascript";
import { createTestPluginAndRun } from "./helpers";

describe("bring your own transpile", () => {
test("does not transpile target=ts", () => {
const lines = testGenerate("target=ts");
test("does not transpile target=ts", async () => {
const lines = await testGenerate("target=ts");
expect(lines).toStrictEqual(["fake typescript source"]);
});

test("transpiles to target js", () => {
const lines = testGenerate("target=js");
test("transpiles to target js", async () => {
const lines = await testGenerate("target=js");
expect(lines).toStrictEqual(["fake transpiled to js"]);
});

test("transpiles to target dts", () => {
const lines = testGenerate("target=dts");
test("transpiles to target dts", async () => {
const lines = await testGenerate("target=dts");
expect(lines).toStrictEqual(["fake transpiled to dts"]);
});

function testGenerate(parameter: string): string[] {
const plugin = createEcmaScriptPlugin({
name: "test",
version: "v1",
async function testGenerate(parameter: string) {
return await createTestPluginAndRun({
returnLinesOfFirstFile: true,
proto: `syntax="proto3";`,
parameter,
generateTs: (schema) => {
const f = schema.generateFile("test.ts");
f.print("fake typescript source");
Expand Down Expand Up @@ -84,15 +84,5 @@ describe("bring your own transpile", () => {
return out;
},
});
const req = new CodeGeneratorRequest({
parameter,
});
const res = plugin.run(req);
expect(res.file.length).toBeGreaterThanOrEqual(1);
let content = res.file[0]?.content ?? "";
if (content.endsWith("\n")) {
content = content.slice(0, -1); // trim final newline so we don't return an extra line
}
return content.split("\n");
}
});

0 comments on commit db30763

Please sign in to comment.