Skip to content

Commit

Permalink
chore: re-enable tests which weren't running
Browse files Browse the repository at this point in the history
* fix more error sets and add dummy http test to verify
* disable logging in non-tests
  • Loading branch information
travisstaloch committed May 8, 2023
1 parent 83e16ba commit eb1b532
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 110 deletions.
18 changes: 9 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ pub fn build(b: *std.build.Builder) !void {
build_options.addOption(GenFormat, "output_format", gen_format);

// for capturing output of system installed protoc. just echoes out whatever protoc sends
const protocgen_echo = b.addExecutable(.{
const protoc_echo = b.addExecutable(.{
.name = "protoc-echo-to-stderr",
.root_source_file = .{ .path = "src/protoc-echo-to-stderr.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(protocgen_echo);
protocgen_echo.addOptions("build_options", build_options);
b.installArtifact(protoc_echo);
protoc_echo.addOptions("build_options", build_options);

const protoc_zig = b.addExecutable(.{
const protoc_gen_zig = b.addExecutable(.{
.name = "protoc-gen-zig",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(protoc_zig);
protoc_zig.addOptions("build_options", build_options);
protoc_zig.addModule("protobuf", protobuf_mod);
b.installArtifact(protoc_gen_zig);
protoc_gen_zig.addOptions("build_options", build_options);
protoc_gen_zig.addModule("protobuf", protobuf_mod);

const run_protoc_cmd = b.addSystemCommand(&.{
"protoc",
Expand All @@ -69,7 +69,7 @@ pub fn build(b: *std.build.Builder) !void {
run_step.dependOn(&run_protoc_cmd.step);

// generate files that need to be avaliable in tests
var gen_step = try sdk.GenStep.create(b, protoc_zig, &.{
var gen_step = try sdk.GenStep.create(b, protoc_gen_zig, &.{
"examples/all_types.proto",
"examples/only_enum.proto",
"examples/person.proto",
Expand Down Expand Up @@ -100,7 +100,7 @@ pub fn build(b: *std.build.Builder) !void {
main_tests.filter = test_filter;

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
test_step.dependOn(&b.addRunArtifact(main_tests).step);

const conformance_exe = b.addExecutable(.{
.name = "conformance",
Expand Down
17 changes: 17 additions & 0 deletions src/common.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
const std = @import("std");
const mem = std.mem;

pub const log = if (@import("builtin").is_test)
std.log.scoped(.@"protobuf-zig")
else
struct {
pub const debug = dummy_log;
pub const info = dummy_log;
pub const warn = dummy_log;
pub const err = std.log.err;
fn dummy_log(
comptime format: []const u8,
args: anytype,
) void {
_ = args;
_ = format;
}
};

pub const GenFormat = enum { zig, c };
pub const panicf = std.debug.panic;
pub fn ptrAlign(comptime Ptr: type) comptime_int {
Expand Down
5 changes: 3 additions & 2 deletions src/gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const String = extern_types.String;
const top_level = @This();
const genc = @import("gen-c.zig");
const genzig = @import("gen-zig.zig");
const log = common.log;

const output_format = @import("build_options").output_format;

Expand All @@ -36,7 +37,7 @@ pub const GenError = error{
};

pub fn genErr(comptime fmt: []const u8, args: anytype, err: anyerror) anyerror {
std.log.err(fmt, args);
log.err(fmt, args);
return err;
}

Expand Down Expand Up @@ -324,7 +325,7 @@ pub fn gen(ctx: *Context) !CodeGeneratorResponse {
res.set(.supported_features, @intCast(u64, @enumToInt(CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL)));

if (output_format == .c) {
std.log.err("TODO support output_format == .c", .{});
log.err("TODO support output_format == .c", .{});
return error.Todo;
}

Expand Down
3 changes: 2 additions & 1 deletion src/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const common = pb.common;
const ptrAlignCast = common.ptrAlignCast;
const flagsContain = types.flagsContain;
const Error = pb.protobuf.Error;
const log = common.log;

fn b64Encode(s: String, writer: anytype) !void {
var encbuf: [0x100]u8 = undefined;
Expand All @@ -27,7 +28,7 @@ fn b64Encode(s: String, writer: anytype) !void {
}

fn serializeErr(comptime fmt: []const u8, args: anytype, err: Error) Error {
std.log.err("json serialization error: " ++ fmt, args);
log.err("json serialization error: " ++ fmt, args);
return err;
}

Expand Down
31 changes: 0 additions & 31 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,6 @@ const pb = @import("protobuf");
pub const CodeGeneratorRequest = pb.plugin.CodeGeneratorRequest;
pub const gen = @import("gen.zig");

pub const std_options = struct {
pub const log_level = std.meta.stringToEnum(std.log.Level, @tagName(@import("build_options").log_level)).?;
pub const logFn = log;
};

pub fn log(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
if (!std.log.logEnabled(level, scope)) return;

std.debug.getStderrMutex().lock();
defer std.debug.getStderrMutex().unlock();
const stderr = std.io.getStdErr().writer();
nosuspend stderr.print(format ++ "\n", args) catch return;
}

fn getArg(args: *[]const []const u8, comptime startswith: []const u8) !?[]const u8 {
if (std.mem.startsWith(u8, args.*[0], startswith ++ "=")) {
return args.*[0][startswith.len + 1 ..];
}
if (std.mem.startsWith(u8, args.*[0], startswith)) {
args.* = args.*[1..];
if (args.len == 0) return error.Args;
return args.*[0];
}
return null;
}

/// A simple protoc plugin implementation. Similar to
/// https://github.com/protocolbuffers/protobuf-go/blob/master/cmd/protoc-gen-go/main.go.
/// Reads a CodeGeneratorRequest from stdin and writes a CodeGeneratorResponse to stdout.
Expand Down
35 changes: 18 additions & 17 deletions src/protobuf-types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Label = FieldDescriptorProto.Label;
const Type = FieldDescriptorProto.Type;
const FieldFlag = FieldDescriptor.FieldFlag;
const common = pb.common;
const log = common.log;
const top_level = @This();

pub const SERVICE_DESCRIPTOR_MAGIC = 0x14159bc3;
Expand Down Expand Up @@ -69,7 +70,7 @@ fn SetPresent(comptime T: type) fn (*T, comptime types.FieldEnum(T)) void {
compileErr("{s}.setPresent() field_enum == .base", .{name});
const field_idx = types.fieldIndex(T, tagname) orelse
compileErr("{s}.setPresent() invalid field name {s}", .{ name, tagname });
std.log.debug("setPresent() {s}.{s}:{}", .{ name, @tagName(field_enum), field_idx });
log.debug("setPresent() {s}.{s}:{}", .{ name, @tagName(field_enum), field_idx });
self.base.setPresentFieldIndex(field_idx - 1);
}
}.setPresent;
Expand All @@ -85,7 +86,7 @@ fn Has(comptime T: type) fn (T, comptime types.FieldEnum(T)) bool {
const field_idx = types.fieldIndex(T, tagname) orelse
compileErr("{s}.Has() invalid field name {s}", .{ name, tagname });
const field_id = T.field_ids[field_idx - 1];
std.log.debug(
log.debug(
"Has() {s}.{s}:{} field_id {} hasFieldId()={}",
.{ name, @tagName(field_enum), field_idx, field_id, self.base.hasFieldId(field_id) },
);
Expand Down Expand Up @@ -134,7 +135,7 @@ pub fn setFieldHelp(
) void {
self.setPresent(field_enum);
const tagname = @tagName(field_enum);
std.log.debug("setFieldHelp() .{s}={}", .{ tagname, value });
log.debug("setFieldHelp() .{s}={}", .{ tagname, value });
const field = types.fields(T)[@enumToInt(field_enum)];
const F = field.ty();
if (field == .union_field) {
Expand All @@ -152,7 +153,7 @@ pub fn getFieldHelp(
comptime field_enum: types.FieldEnum(T),
) types.fieldInfo(T, field_enum).ty() {
const tagname = @tagName(field_enum);
std.log.debug("getFieldHelp() .{s}", .{tagname});
log.debug("getFieldHelp() .{s}", .{tagname});
if (comptime mem.indexOf(u8, tagname, "__")) |i| {
const u = @field(self, tagname[0..i]);
return @field(u, tagname[i + 2 ..]);
Expand Down Expand Up @@ -551,10 +552,10 @@ pub const Message = extern struct {
pub fn setPresent(m: *Message, field_id: c_uint) void {
const desc = m.descriptor orelse
@panic("called setPresent() on a message with no descriptor.");
std.log.debug("setPresent({})", .{field_id});
log.debug("setPresent({})", .{field_id});
const opt_field_idx = desc.optionalFieldIndex(field_id) orelse return;
m.optional_fields_present |= @as(u64, 1) << @intCast(u6, opt_field_idx);
std.log.debug("setPresent 2 m.optional_fields_present {b:0>64}", .{m.optional_fields_present});
log.debug("setPresent 2 m.optional_fields_present {b:0>64}", .{m.optional_fields_present});
// TODO if oneof field, remove other fields w/ same oneof_index
}

Expand All @@ -563,13 +564,13 @@ pub const Message = extern struct {
pub fn setPresentValue(m: *Message, field_id: c_uint, value: bool) void {
const desc = m.descriptor orelse
@panic("called setPresentValue() on a message with no descriptor.");
std.log.debug("setPresentValue({}, {})", .{ field_id, value });
log.debug("setPresentValue({}, {})", .{ field_id, value });
const opt_field_idx = desc.optionalFieldIndex(field_id) orelse return;
if (value)
m.optional_fields_present |= @as(u64, 1) << @intCast(u6, opt_field_idx)
else
m.optional_fields_present &= ~(@as(u64, 1) << @intCast(u6, opt_field_idx));
std.log.debug("setPresentValue 2 m.optional_fields_present {b:0>64}", .{m.optional_fields_present});
log.debug("setPresentValue 2 m.optional_fields_present {b:0>64}", .{m.optional_fields_present});
// TODO if oneof field, remove other fields w/ same oneof_index
}

Expand All @@ -578,14 +579,14 @@ pub const Message = extern struct {
pub fn setPresentFieldIndex(m: *Message, field_index: usize) void {
const desc = m.descriptor orelse
@panic("called setPresentFieldIndex() on a message with no descriptor.");
std.log.info("setPresentFieldIndex() field_index {}", .{field_index});
log.info("setPresentFieldIndex() field_index {}", .{field_index});
m.setPresent(desc.field_ids.items[field_index]);
}

/// ptr cast to T. verifies that m.descriptor.name ends with @typeName(T)
pub fn as(m: *Message, comptime T: type) !*T {
if (!mem.endsWith(u8, @typeName(T), m.descriptor.?.name.slice())) {
std.log.err("expected '{s}' to contain '{s}'", .{ @typeName(T), m.descriptor.?.name });
log.err("expected '{s}' to contain '{s}'", .{ @typeName(T), m.descriptor.?.name });
return error.TypeMismatch;
}
return @ptrCast(*T, m);
Expand Down Expand Up @@ -745,7 +746,7 @@ pub const Message = extern struct {
const desc = m.descriptor orelse
panicf("can't deinit a message with no descriptor.", .{});

std.log.debug(
log.debug(
"\ndeinit message {s}{}-{} size={}",
.{ desc.name, ptrfmt(m), ptrfmt(bytes + desc.sizeof_message), desc.sizeof_message },
);
Expand All @@ -761,7 +762,7 @@ pub const Message = extern struct {
const L = ListMutScalar(String);
var list = ptrAlignCast(*L, bytes + field.offset);
if (list.len != 0) {
std.log.debug(
log.debug(
"deinit {s}.{s} repeated string field len {}",
.{ desc.name, field.name, list.len },
);
Expand All @@ -772,7 +773,7 @@ pub const Message = extern struct {
const L = ListMut(*Message);
var list = ptrAlignCast(*L, bytes + field.offset);
if (list.len != 0) {
std.log.debug(
log.debug(
"deinit {s}.{s} repeated message field len/cap {}/{}",
.{ desc.name, field.name, list.len, list.cap },
);
Expand All @@ -785,7 +786,7 @@ pub const Message = extern struct {
const L = ListMutScalar(u8);
var list = ptrAlignCast(*L, bytes + field.offset);
if (list.len != 0) {
std.log.debug(
log.debug(
"deinit {s}.{s} repeated field {s} len {} size {} bytelen {}",
.{
desc.name,
Expand Down Expand Up @@ -820,15 +821,15 @@ pub const Message = extern struct {
allocator.free(ptr[0 .. size * list.cap]);
},
else => {
std.log.err("TODO: support type={s} size={}", .{ @tagName(field.type), size });
log.err("TODO: support type={s} size={}", .{ @tagName(field.type), size });
unreachable;
},
}
}
}
} else if (field.type == .TYPE_MESSAGE or field.type == .TYPE_GROUP) {
if (m.hasFieldId(field.id)) {
std.log.debug(
log.debug(
"deinit {s}.{s} single message field",
.{ desc.name, field.name },
);
Expand All @@ -842,7 +843,7 @@ pub const Message = extern struct {
} else if (field.type == .TYPE_STRING or field.type == .TYPE_BYTES) {
var s = ptrAlignCast(*String, bytes + field.offset);
if (s.len != 0 and s.items != String.empty.items) {
std.log.debug(
log.debug(
"deinit {s}.{s} single string field {} offset {}",
.{
desc.name,
Expand Down
Loading

0 comments on commit eb1b532

Please sign in to comment.