Skip to content

Commit

Permalink
Add test coverage for packed and packedByDefault properties of DescFi…
Browse files Browse the repository at this point in the history
…eld (#625)
  • Loading branch information
timostamm authored Nov 21, 2023
1 parent 1f3faa6 commit 52a8e58
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 deletions.
45 changes: 45 additions & 0 deletions packages/protobuf-test/src/descriptor-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
TestAllExtensions,
TestNestedExtension,
} from "./gen/ts/google/protobuf/unittest_pb.js";
import { UpstreamProtobuf } from "upstream-protobuf";

const fdsBytes = readFileSync("./descriptorset.bin");

Expand All @@ -54,6 +55,50 @@ describe("DescriptorSet", () => {
expect(descFile?.syntax).toBe("editions");
expect(descFile?.edition).toBe(Edition.EDITION_2023);
});
describe("repeated field packing", () => {
test("proto2 is unpacked by default", async () => {
const bin = await new UpstreamProtobuf().compileToDescriptorSet(`
syntax="proto2";
message M {
optional int32 not_packable = 1;
repeated bytes also_not_packable = 2;
repeated int32 default = 3;
repeated int32 explicitly_packed = 4 [packed = true];
repeated int32 explicitly_expanded = 5 [packed = false];
}
`);
const fields = createDescriptorSet(bin).messages.get("M")?.fields;
expect(fields?.[0].packedByDefault).toBe(false);
expect(fields?.[1].packedByDefault).toBe(false);
expect(fields?.[2].packedByDefault).toBe(false);
expect(fields?.[0].packed).toBe(false);
expect(fields?.[1].packed).toBe(false);
expect(fields?.[2].packed).toBe(false);
expect(fields?.[3].packed).toBe(true);
expect(fields?.[4].packed).toBe(false);
});
test("proto3 is packed by default", async () => {
const bin = await new UpstreamProtobuf().compileToDescriptorSet(`
syntax="proto3";
message M {
int32 not_packable = 1;
repeated bytes also_not_packable = 2;
repeated int32 default = 3;
repeated int32 explicitly_packed = 4 [packed = true];
repeated int32 explicitly_expanded = 5 [packed = false];
}
`);
const fields = createDescriptorSet(bin).messages.get("M")?.fields;
expect(fields?.[0].packedByDefault).toBe(true);
expect(fields?.[1].packedByDefault).toBe(false);
expect(fields?.[2].packedByDefault).toBe(true);
expect(fields?.[0].packed).toBe(true);
expect(fields?.[1].packed).toBe(false);
expect(fields?.[2].packed).toBe(true);
expect(fields?.[3].packed).toBe(true);
expect(fields?.[4].packed).toBe(false);
});
});
test("knows extension", () => {
const ext = set.extensions.get(
"protobuf_unittest.optional_int32_extension",
Expand Down
13 changes: 12 additions & 1 deletion packages/upstream-protobuf/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,16 @@ export declare class UpstreamProtobuf {
maximumEdition?: string,
): Promise<Uint8Array>;

compileToDescriptorSet(files: Record<string, string>): Promise<Uint8Array>;
compileToDescriptorSet(
fileContent: string,
opt?: CompileToDescriptorSetOptions,
): Promise<Uint8Array>;
compileToDescriptorSet(
files: Record<string, string>,
opt?: CompileToDescriptorSetOptions,
): Promise<Uint8Array>;
}

interface CompileToDescriptorSetOptions {
includeSourceInfo?: boolean;
}
41 changes: 25 additions & 16 deletions packages/upstream-protobuf/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,40 @@ export class UpstreamProtobuf {
}

/**
* @param {Record<string, string>} files
* @typedef CompileToDescriptorSetOptions
* @property {boolean} [includeSourceInfo]
*/
/**
* @param {Record<string, string>|string} filesOrFileContent
* @param {CompileToDescriptorSetOptions} [opt]
* @return {Promise<Buffer>}
*/
async compileToDescriptorSet(files) {
async compileToDescriptorSet(filesOrFileContent, opt) {
const protocPath = await this.getProtocPath();
const tempDir = mkdtempSync(
joinPath(this.#temp, "compile-descriptor-set-"),
);
const files =
typeof filesOrFileContent == "string"
? { "input.proto": filesOrFileContent }
: filesOrFileContent;
try {
writeTree(Object.entries(files), tempDir);
const outPath = joinPath(tempDir, "desc.bin");
execFileSync(
protocPath,
[
"--descriptor_set_out",
outPath,
"--proto_path",
tempDir,
...Object.keys(files),
],
{
shell: false,
stdio: "ignore",
},
);
const args = [
"--experimental_editions",
"--descriptor_set_out",
outPath,
"--proto_path",
tempDir,
...Object.keys(files),
];
if (opt?.includeSourceInfo) {
args.unshift("--include_source_info");
}
execFileSync(protocPath, args, {
shell: false,
});
return readFileSync(outPath);
} finally {
rmSync(tempDir, { recursive: true });
Expand Down

0 comments on commit 52a8e58

Please sign in to comment.