-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Honor experimental editions in the preamble generated by @bufbuild/protoplugin #644
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
// Copyright 2021-2023 Buf Technologies, Inc. | ||
// | ||
// 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 | ||
// | ||
// http://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. | ||
|
||
import { describe, expect, test } from "@jest/globals"; | ||
import { UpstreamProtobuf } from "upstream-protobuf"; | ||
import { CodeGeneratorRequest } from "@bufbuild/protobuf"; | ||
import type { Schema } from "@bufbuild/protoplugin"; | ||
import { createEcmaScriptPlugin } from "@bufbuild/protoplugin"; | ||
|
||
describe("file preamble", () => { | ||
test("contains plugin name and version", async () => { | ||
const lines = await testGenerate( | ||
{ | ||
"x.proto": `syntax="proto3";`, | ||
}, | ||
{ | ||
pluginName: "pi-plugin", | ||
pluginVersion: "v3.14159", | ||
}, | ||
); | ||
expect(lines).toContain("// @generated by pi-plugin v3.14159"); | ||
}); | ||
|
||
test("contains plugin options", async () => { | ||
const lines = await testGenerate( | ||
{ | ||
"x.proto": `syntax="proto3";`, | ||
}, | ||
{ | ||
parameter: "foo=bar,baz", | ||
}, | ||
); | ||
expect(lines).toContain( | ||
`// @generated by test v1 with parameter "foo=bar,baz"`, | ||
); | ||
}); | ||
|
||
test("elides rewrite_imports plugin option", async () => { | ||
const lines = await testGenerate( | ||
{ | ||
"x.proto": `syntax="proto3";`, | ||
}, | ||
{ | ||
parameter: | ||
"foo,rewrite_imports=./test/*_pb.js:@buf/test,rewrite_imports=./test/*_web.js:@buf/web,bar", | ||
}, | ||
); | ||
expect(lines).toContain( | ||
`// @generated by test v1 with parameter "foo,bar"`, | ||
); | ||
}); | ||
|
||
test("contains source file info for proto3", async () => { | ||
const lines = await testGenerate({ | ||
"x.proto": `syntax="proto3";`, | ||
}); | ||
expect(lines).toContain("// @generated from file x.proto (syntax proto3)"); | ||
}); | ||
|
||
test("contains eslint-disable annotation", async () => { | ||
const lines = await testGenerate({ | ||
"x.proto": `syntax="proto3";`, | ||
}); | ||
expect(lines).toContain("/* eslint-disable */"); | ||
}); | ||
|
||
test("contains ts-nocheck annotation by default", async () => { | ||
const lines = await testGenerate({ | ||
"x.proto": `syntax="proto3";`, | ||
}); | ||
expect(lines).toContain("// @ts-nocheck"); | ||
}); | ||
|
||
test("does not contain ts-nocheck annotation when turned off", async () => { | ||
const lines = await testGenerate( | ||
{ | ||
"x.proto": `syntax="proto3";`, | ||
}, | ||
{ | ||
parameter: "ts_nocheck=false", | ||
}, | ||
); | ||
expect(lines).not.toContain("// @ts-nocheck"); | ||
}); | ||
|
||
test("contains source file info for proto2", async () => { | ||
const lines = await testGenerate({ | ||
"x.proto": `syntax="proto2";`, | ||
}); | ||
expect(lines).toContain("// @generated from file x.proto (syntax proto2)"); | ||
}); | ||
|
||
test("contains source file info for edition 2023", async () => { | ||
const lines = await testGenerate({ | ||
"x.proto": `edition="2023";`, | ||
}); | ||
expect(lines).toContain("// @generated from file x.proto (edition 2023)"); | ||
}); | ||
|
||
test("contains syntax comments", async () => { | ||
const lines = await testGenerate({ | ||
"x.proto": ` | ||
// comment above... | ||
// ... the syntax declaration | ||
syntax="proto3"; | ||
`, | ||
}); | ||
const firstLines = lines.slice( | ||
0, | ||
lines.indexOf("// @generated by test v1"), | ||
); | ||
expect(firstLines).toStrictEqual([ | ||
"// comment above...", | ||
"// ... the syntax declaration", | ||
"", | ||
]); | ||
}); | ||
|
||
test("contains syntax comments with edition 2023", async () => { | ||
const lines = await testGenerate({ | ||
"x.proto": ` | ||
// comment above... | ||
// ... the syntax declaration | ||
edition="2023"; | ||
`, | ||
}); | ||
const firstLines = lines.slice( | ||
0, | ||
lines.indexOf("// @generated by test v1"), | ||
); | ||
expect(firstLines).toStrictEqual([ | ||
"// comment above...", | ||
"// ... the syntax declaration", | ||
"", | ||
]); | ||
}); | ||
|
||
test("contains package comments", async () => { | ||
const lines = await testGenerate({ | ||
"x.proto": ` | ||
syntax="proto3"; | ||
|
||
// comment above... | ||
// ... the package declaration | ||
package foo; | ||
`, | ||
}); | ||
const lastLines = lines.slice(lines.indexOf("// @ts-nocheck")); | ||
expect(lastLines).toStrictEqual([ | ||
"// @ts-nocheck", | ||
"", | ||
"// comment above...", | ||
"// ... the package declaration", | ||
"", | ||
"const placeholder = 1; // ensure file is not considered empty", | ||
]); | ||
}); | ||
|
||
// test helper to generate just a file with a preamble for each input proto file | ||
async function testGenerate( | ||
protoFiles: Record<string, string>, | ||
opt?: { parameter?: string; pluginName?: string; pluginVersion?: string }, | ||
) { | ||
const plugin = createEcmaScriptPlugin({ | ||
name: opt?.pluginName ?? "test", | ||
version: opt?.pluginVersion ?? "v1", | ||
generateTs: generateFileWithPreamble, | ||
generateJs: generateFileWithPreamble, | ||
generateDts: generateFileWithPreamble, | ||
parseOption: () => { | ||
// accept all options | ||
}, | ||
}); | ||
|
||
function generateFileWithPreamble( | ||
schema: Schema, | ||
target: "js" | "ts" | "dts", | ||
) { | ||
for (const file of schema.files) { | ||
const f = schema.generateFile(`${file.name}.${target}`); | ||
f.preamble(file); | ||
f.print( | ||
"const placeholder = 1; // ensure file is not considered empty", | ||
); | ||
} | ||
} | ||
|
||
const upstream = new UpstreamProtobuf(); | ||
const req = CodeGeneratorRequest.fromBinary( | ||
await upstream.createCodeGeneratorRequest(protoFiles, { | ||
parameter: opt?.parameter, | ||
}), | ||
); | ||
expect(req.protoFile.length).toBe(1); | ||
const res = plugin.run(req); | ||
expect(res.file.length).toBeGreaterThanOrEqual(1); | ||
const content = res.file[0]?.content ?? ""; | ||
return content.trim().split("\n"); | ||
} | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why those tests where here. Moved to a different file and increased coverage.