Skip to content
This repository has been archived by the owner on Jun 21, 2020. It is now read-only.

Commit

Permalink
Adds client generation to bxpb-protoc-plugin.
Browse files Browse the repository at this point in the history
Refs #1.

This calls the newly implemented client generation code from the plugin. Tests simply assert that the function is called correctly as it's implementation tests it pretty thoroughly already.
  • Loading branch information
dgp1130 committed Jun 1, 2020
1 parent 7c483ef commit 129f898
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/bxpb-protoc-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CodeGeneratorRequest, CodeGeneratorResponse } from 'google-protobuf/goo
import { FileDescriptorProto } from 'google-protobuf/google/protobuf/descriptor_pb';
import * as descriptorGenerator from './generators/descriptors';
import * as serviceGenerator from './generators/services';
import * as clientGenerator from './generators/clients';

/**
* Executes the plugin by reading a serialized {@link CodeGeneratorRequest} from stdin and writing a
Expand Down Expand Up @@ -57,11 +58,10 @@ function* generateProto(file: string, fileDescriptor: FileDescriptorProto):
// If there are no services, there is nothing to generate.
if (fileDescriptor.getServiceList().length === 0) return [];

// Generate descriptors.
// Generate proto code.
yield* descriptorGenerator.generateDescriptorFiles(file, fileDescriptor);
yield* serviceGenerator.generateServiceFiles(file, fileDescriptor);

// TODO: Generate client.
yield* clientGenerator.generateClientFiles(file, fileDescriptor);
}

/**
Expand Down
73 changes: 69 additions & 4 deletions packages/bxpb-protoc-plugin/src/plugin_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { execute } from './plugin';
import { dummyFileDescriptor, dummyCodegenRequest, dummyCodegenResponseFile } from './testing/dummies';
import * as descriptorGenerator from './generators/descriptors';
import * as serviceGenerator from './generators/services';
import * as clientGenerator from './generators/clients';

/** Helper function to invoke the plugin's {@link execute} method in a simple fashion. */
async function invokePlugin(req: CodeGeneratorRequest): Promise<CodeGeneratorResponse> {
Expand Down Expand Up @@ -57,14 +58,28 @@ describe('plugin', () => {
}),
]);

spyOn(clientGenerator, 'generateClientFiles').and.returnValue([
dummyCodegenResponseFile({
name: 'foo_clients.js',
content: 'export const clients = [ /* ... */ ];',
}),
dummyCodegenResponseFile({
name: 'foo_clients.d.ts',
content: 'export const clients: Client[];',
}),
]);

const res = await invokePlugin(req);

expect(descriptorGenerator.generateDescriptorFiles).toHaveBeenCalledWith(
'foo.proto', dummyFileDescriptor());
expect(serviceGenerator.generateServiceFiles).toHaveBeenCalledWith(
'foo.proto', dummyFileDescriptor());
expect(clientGenerator.generateClientFiles).toHaveBeenCalledWith(
'foo.proto', dummyFileDescriptor());

const [ descriptorJs, descriptorDts, serviceJs, serviceDts ] = res.getFileList();
const [ descriptorJs, descriptorDts, serviceJs, serviceDts, clientJs, clientDts ] =
res.getFileList();

expect(descriptorJs.getName()).toBe('foo_descriptors.js');
expect(descriptorJs.getContent()).toBe('export const descriptors = [ /* ... */ ];');
Expand All @@ -77,6 +92,12 @@ describe('plugin', () => {

expect(serviceDts.getName()).toBe('foo_services.d.ts');
expect(serviceDts.getContent()).toBe('export const services: Service[];');

expect(clientJs.getName()).toBe('foo_clients.js');
expect(clientJs.getContent()).toBe('export const clients = [ /* ... */ ];');

expect(clientDts.getName()).toBe('foo_clients.d.ts');
expect(clientDts.getContent()).toBe('export const clients: Client[];');
});

it('generates from multiple proto source files', async () => {
Expand Down Expand Up @@ -124,6 +145,20 @@ describe('plugin', () => {
],
);

spyOn(clientGenerator, 'generateClientFiles').and.returnValues(
// Call to generate foo.proto.
[
dummyCodegenResponseFile({ name: 'foo_clients.js' }),
dummyCodegenResponseFile({ name: 'foo_clients.d.ts' }),
],

// Call to generate bar.proto.
[
dummyCodegenResponseFile({ name: 'bar_clients.js' }),
dummyCodegenResponseFile({ name: 'bar_clients.d.ts' }),
],
);

const res = await invokePlugin(req);

expect(descriptorGenerator.generateDescriptorFiles).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -162,18 +197,46 @@ describe('plugin', () => {
}),
);

const [ fooDescriptorJs, fooDescriptorDts, fooServiceJs, fooServiceDts,
barDescriptorJs, barDescriptorDts, barServiceJs, barServiceDts ] =
res.getFileList();
expect(clientGenerator.generateClientFiles).toHaveBeenCalledTimes(2);
expect(clientGenerator.generateClientFiles).toHaveBeenCalledWith(
'foo.proto',
dummyFileDescriptor({
services: [
{ name: 'Foo' },
],
}),
);
expect(clientGenerator.generateClientFiles).toHaveBeenCalledWith(
'bar.proto',
dummyFileDescriptor({
services: [
{ name: 'Bar' },
],
}),
);

const [
fooDescriptorJs, fooDescriptorDts,
fooServiceJs, fooServiceDts,
fooClientJs, fooClientDts,

barDescriptorJs, barDescriptorDts,
barServiceJs, barServiceDts,
barClientJs, barClientDts,
] = res.getFileList();

expect(fooDescriptorJs.getName()).toBe('foo_descriptors.js');
expect(fooDescriptorDts.getName()).toBe('foo_descriptors.d.ts');
expect(fooServiceJs.getName()).toBe('foo_services.js');
expect(fooServiceDts.getName()).toBe('foo_services.d.ts');
expect(fooClientJs.getName()).toBe('foo_clients.js');
expect(fooClientDts.getName()).toBe('foo_clients.d.ts');
expect(barDescriptorJs.getName()).toBe('bar_descriptors.js');
expect(barDescriptorDts.getName()).toBe('bar_descriptors.d.ts');
expect(barServiceJs.getName()).toBe('bar_services.js');
expect(barServiceDts.getName()).toBe('bar_services.d.ts');
expect(barClientJs.getName()).toBe('bar_clients.js');
expect(barClientDts.getName()).toBe('bar_clients.d.ts');
});

it('generates from source file with no services', async () => {
Expand All @@ -188,11 +251,13 @@ describe('plugin', () => {

spyOn(descriptorGenerator, 'generateDescriptorFiles');
spyOn(serviceGenerator, 'generateServiceFiles');
spyOn(clientGenerator, 'generateClientFiles');

const res = await invokePlugin(req);

expect(descriptorGenerator.generateDescriptorFiles).not.toHaveBeenCalled();
expect(serviceGenerator.generateServiceFiles).not.toHaveBeenCalled();
expect(clientGenerator.generateClientFiles).not.toHaveBeenCalled();

expect(res.getFileList()).toEqual([]);
});
Expand Down

0 comments on commit 129f898

Please sign in to comment.