1212
1313
1414def brace_delta (line ):
15+ """Return the net brace balance contributed by a single Bruno line."""
1516 return line .count ("{" ) - line .count ("}" )
1617
1718
1819def read_text (path ):
20+ """Read a Bruno file while tolerating invalid byte sequences."""
1921 return path .read_text (encoding = "utf-8" , errors = "replace" )
2022
2123
2224def collect_blocks (text ):
25+ """Collect top-level Bruno blocks keyed by block name."""
2326 lines = text .splitlines ()
2427 blocks = {}
2528 index = 0
@@ -52,6 +55,7 @@ def collect_blocks(text):
5255
5356
5457def parse_fields (block ):
58+ """Parse simple Bruno key-value fields from a block body."""
5559 fields = {}
5660 for line in block .splitlines ():
5761 match = re .match (r"^\s*(~?[^:]+):\s*(.*)$" , line )
@@ -63,6 +67,7 @@ def parse_fields(block):
6367
6468
6569def parse_meta (blocks ):
70+ """Extract display metadata and sort order from parsed Bruno blocks."""
6671 meta = parse_fields (blocks .get ("meta" , ["" ])[0 ])
6772 seq = meta .get ("seq" , "999999" )
6873 try :
@@ -76,6 +81,7 @@ def parse_meta(blocks):
7681
7782
7883def parse_vars (blocks ):
84+ """Extract pre-request variables defined in Bruno request blocks."""
7985 variables = {}
8086 for block in blocks .get ("vars:pre-request" , []):
8187 for key , value in parse_fields (block ).items ():
@@ -84,10 +90,12 @@ def parse_vars(blocks):
8490
8591
8692def clean_body_json (block ):
93+ """Return the JSON body text exactly as k6 should send it."""
8794 return block .strip ()
8895
8996
9097def parse_multipart (block ):
98+ """Parse Bruno multipart form fields into k6-friendly metadata."""
9199 fields = []
92100 for line in block .splitlines ():
93101 match = re .match (r"^\s*(~?[^:]+):\s*(.*?)\s*$" , line )
@@ -133,6 +141,7 @@ def parse_multipart(block):
133141
134142
135143def parse_request (path , collection_dir ):
144+ """Parse one Bruno request file into the intermediate request model."""
136145 text = read_text (path )
137146 blocks = collect_blocks (text )
138147 method = next ((name for name in HTTP_METHODS if name in blocks ), None )
@@ -171,6 +180,7 @@ def parse_request(path, collection_dir):
171180
172181
173182def order_key (request ):
183+ """Sort auth-ending requests after regular requests and login first."""
174184 url = request ["url" ]
175185 if LOGIN_PATH in url :
176186 return (0 , request ["relativePath" ], request ["seq" ])
@@ -180,6 +190,7 @@ def order_key(request):
180190
181191
182192def load_requests (collection_dir , include_external , include_destructive ):
193+ """Load and filter HTTP requests from a Bruno collection directory."""
183194 requests = []
184195 for path in sorted (collection_dir .rglob ("*.bru" )):
185196 if path .name in ("collection.bru" , "folder.bru" ):
@@ -201,6 +212,7 @@ def load_requests(collection_dir, include_external, include_destructive):
201212
202213
203214def render_k6 (requests ):
215+ """Render parsed Bruno requests as a self-contained k6 JavaScript file."""
204216 regular_requests = [request for request in requests if LOGIN_PATH not in request ["url" ]]
205217 payload = json .dumps (regular_requests , ensure_ascii = True , indent = 2 )
206218
@@ -400,6 +412,7 @@ def render_k6(requests):
400412
401413
402414def main ():
415+ """Parse CLI arguments and write the generated k6 script."""
403416 parser = argparse .ArgumentParser (description = "Generate a k6 script from a Bruno collection." )
404417 parser .add_argument ("--collection-dir" , required = True , type = Path )
405418 parser .add_argument ("--output" , required = True , type = Path )
0 commit comments