-
Notifications
You must be signed in to change notification settings - Fork 767
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix code generator bug and add tests
- Loading branch information
1 parent
a055960
commit 05029f3
Showing
8 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
syntax = "proto3"; | ||
|
||
message MessageOuter { | ||
message MessageInner { | ||
int32 value = 1; | ||
} | ||
|
||
repeated MessageInner someProp = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
syntax = "proto3"; | ||
import "test03.proto"; | ||
|
||
service MyService { | ||
rpc addOne(Integer) returns (Integer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
syntax = "proto3"; | ||
|
||
message Integer { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as grpcWeb from 'grpc-web'; | ||
|
||
import {MessageOuter} from './generated/test01_pb'; | ||
|
||
let inner1 = new MessageOuter.MessageInner(); | ||
inner1.setValue(123); | ||
let msgOuter = new MessageOuter(); | ||
msgOuter.setSomepropList([inner1]); | ||
|
||
export {msgOuter} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as grpcWeb from 'grpc-web'; | ||
|
||
import {Integer} from './generated/test03_pb'; | ||
import {MyServiceClient} from './generated/Test02ServiceClientPb'; | ||
|
||
const service = new MyServiceClient('http://mydummy.com', null, null); | ||
const req = new Integer(); | ||
|
||
service.addOne(req, {}, (err: grpcWeb.Error, resp: Integer) => { | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/** | ||
* | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
const assert = require('assert'); | ||
const execSync = require('child_process').execSync; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const removeDirectory = require('./common.js').removeDirectory; | ||
const mockXmlHttpRequest = require('mock-xmlhttprequest'); | ||
|
||
var MockXMLHttpRequest; | ||
|
||
function relativePath(relPath) { | ||
return path.resolve(__dirname, relPath); | ||
} | ||
function cleanup() { | ||
removeDirectory(relativePath('./tsc-tests/dist')); | ||
removeDirectory(relativePath('./tsc-tests/generated')); | ||
} | ||
function createGeneratedCodeDir() { | ||
fs.mkdirSync(relativePath('./tsc-tests/generated')); | ||
} | ||
function assertFileExists(relPath) { | ||
assert.equal(true, fs.existsSync(relativePath(relPath))); | ||
} | ||
const outputDir = './test/tsc-tests/generated'; | ||
|
||
describe('tsc test01: nested messages', function() { | ||
before(function() { | ||
cleanup(); | ||
createGeneratedCodeDir(); | ||
execSync(`protoc -I=./test/protos test01.proto \ | ||
--js_out=import_style=commonjs:${outputDir} \ | ||
--grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:${outputDir}`); | ||
}); | ||
|
||
after(function() { | ||
cleanup(); | ||
}); | ||
|
||
it('generated code should exist', function() { | ||
assertFileExists('./tsc-tests/generated/test01_pb.js'); | ||
assertFileExists('./tsc-tests/generated/test01_pb.d.ts'); | ||
}); | ||
|
||
it('tsc should run and export', function() { | ||
execSync(`tsc client01.ts generated/test01_pb.d.ts generated/test01_pb.js \ | ||
--allowJs --outDir ./dist`, { | ||
cwd: relativePath('./tsc-tests') | ||
}); | ||
|
||
// check for the tsc output | ||
assertFileExists('./tsc-tests/dist/client01.js'); | ||
assertFileExists('./tsc-tests/dist/generated/test01_pb.js'); | ||
|
||
// load the compiled js files and do some tests | ||
const {msgOuter} = require(relativePath('./tsc-tests/dist/client01.js')); | ||
assert.equal(123, msgOuter.getSomepropList()[0].getValue()); | ||
}); | ||
}); | ||
|
||
describe('tsc test02: simple rpc, messages in separate proto', function() { | ||
before(function() { | ||
cleanup(); | ||
createGeneratedCodeDir(); | ||
MockXMLHttpRequest = mockXmlHttpRequest.newMockXhr(); | ||
global.XMLHttpRequest = MockXMLHttpRequest; | ||
execSync(`protoc -I=./test/protos test02.proto test03.proto \ | ||
--js_out=import_style=commonjs:${outputDir} \ | ||
--grpc-web_out=import_style=typescript,mode=grpcwebtext:${outputDir}`); | ||
}); | ||
|
||
after(function() { | ||
cleanup(); | ||
}); | ||
|
||
it('generated code should exist', function() { | ||
assertFileExists('./tsc-tests/generated/Test02ServiceClientPb.ts'); | ||
assertFileExists('./tsc-tests/generated/test02_pb.js'); | ||
assertFileExists('./tsc-tests/generated/test02_pb.d.ts'); | ||
assertFileExists('./tsc-tests/generated/test03_pb.js'); | ||
assertFileExists('./tsc-tests/generated/test03_pb.d.ts'); | ||
}); | ||
|
||
it('tsc should run and export', function(done) { | ||
execSync(`tsc client02.ts generated/Test02ServiceClientPb.ts \ | ||
generated/test02_pb.d.ts generated/test02_pb.js \ | ||
generated/test03_pb.d.ts generated/test03_pb.js \ | ||
--allowJs --outDir ./dist`, { | ||
cwd: relativePath('./tsc-tests') | ||
}); | ||
|
||
// check for the tsc output | ||
assertFileExists('./tsc-tests/dist/client02.js'); | ||
assertFileExists('./tsc-tests/dist/generated/Test02ServiceClientPb.js'); | ||
assertFileExists('./tsc-tests/dist/generated/test02_pb.js'); | ||
assertFileExists('./tsc-tests/dist/generated/test03_pb.js'); | ||
|
||
// load the compiled js files and do some tests | ||
MockXMLHttpRequest.onSend = function(xhr) { | ||
assert.equal('http://mydummy.com/MyService/addOne', xhr.url); | ||
assert.equal('AAAAAAA=', xhr.body); | ||
done(); | ||
}; | ||
require(relativePath('./tsc-tests/dist/client02.js')); | ||
}); | ||
}); |