Skip to content

Commit

Permalink
Playbook: Fixed bug where we reused the same buffer for both playbook…
Browse files Browse the repository at this point in the history
…s and tests
  • Loading branch information
michaelo committed Oct 11, 2021
1 parent e9d9eba commit 14f3e5a
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,16 @@ fn getNumOfSegmentType(segments: []const parser.PlaybookSegment, segment_type: p

fn processPlaybook(playbook_path: []const u8, args: AppArguments, input_vars:*kvstore.KvStore, extracted_vars: *kvstore.KvStore) !void {
// Load playbook
var buf = initBoundedArray(u8, 1024*1024);
var buf_playbook = initBoundedArray(u8, 1024*1024); // TODO: this must currently be kept as it is used to look up data from for the segments
var buf_test = initBoundedArray(u8, 1024*1024);

io.readFile(u8, buf.buffer.len, playbook_path, &buf) catch {
io.readFile(u8, buf_playbook.buffer.len, playbook_path, &buf_playbook) catch {
debug("ERROR: Could not read playbook file: {s}\n", .{playbook_path});
return error.ParseError;
};

var segments = initBoundedArray(parser.PlaybookSegment, 128);
try segments.resize(parser.parsePlaybook(buf.constSlice(), segments.unusedCapacitySlice()));
try segments.resize(parser.parsePlaybook(buf_playbook.constSlice(), segments.unusedCapacitySlice()));


// Iterate over playbook and act according to each type
Expand All @@ -490,7 +491,7 @@ fn processPlaybook(playbook_path: []const u8, args: AppArguments, input_vars:*kv
const time_start = std.time.milliTimestamp();
// Pass through each item and process according to type
for(segments.constSlice()) |segment| {
try buf.resize(0);
try buf_test.resize(0);

switch(segment.segment_type) {
.Unknown => { unreachable; },
Expand All @@ -505,9 +506,9 @@ fn processPlaybook(playbook_path: []const u8, args: AppArguments, input_vars:*kv
// TODO: how to limit max length? will 128<s pad the result?
name_slice = std.fmt.bufPrint(&name_buf, "{s}", .{segment.slice}) catch { unreachable; };
repeats = segment.meta.TestInclude.repeats;
// debug("Processing: {s}\n", .{segment.slice});
debug("Processing: {s}\n", .{segment.slice});
// Load from file and parse
io.readFile(u8, buf.buffer.len, segment.slice, &buf) catch {
io.readFile(u8, buf_test.buffer.len, segment.slice, &buf_test) catch {
debug("ERROR: Could not read file: {s}\n", .{segment.slice});
num_failed += 1;
continue;
Expand All @@ -516,13 +517,13 @@ fn processPlaybook(playbook_path: []const u8, args: AppArguments, input_vars:*kv
// debug("Processing: {s}\n", .{"inline test"});
// Test raw
name_slice = std.fmt.bufPrint(&name_buf, "Inline segment at line: {d}", .{segment.line_start}) catch { unreachable; };
try buf.appendSlice(segment.slice);
try buf_test.appendSlice(segment.slice);

}
parser.expandVariablesAndFunctions(buf.buffer.len, &buf, extracted_vars) catch {};
parser.expandVariablesAndFunctions(buf.buffer.len, &buf, input_vars) catch {};
parser.expandVariablesAndFunctions(buf_test.buffer.len, &buf_test, extracted_vars) catch {};
parser.expandVariablesAndFunctions(buf_test.buffer.len, &buf_test, input_vars) catch {};

if(processAndEvaluateEntryFromBuf(num_processed, total_num_tests, name_slice, buf.constSlice(), args, input_vars, extracted_vars, repeats)) {
if(processAndEvaluateEntryFromBuf(num_processed, total_num_tests, name_slice, buf_test.constSlice(), args, input_vars, extracted_vars, repeats)) {
// OK
} else |_| {
// debug("Got error: {s}\n", .{err});
Expand Down

0 comments on commit 14f3e5a

Please sign in to comment.