-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa576dc
commit 95f4320
Showing
1 changed file
with
75 additions
and
0 deletions.
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,75 @@ | ||
import logging | ||
import os | ||
import sys | ||
import subprocess | ||
|
||
class PytestTool: | ||
def __init__(self, root): | ||
self.root = os.path.expanduser(root) | ||
|
||
def definition_v0(self): | ||
return { | ||
"type": "function", | ||
"function": { | ||
"name": "run_pytest", | ||
"description": "Runs pytest on specified test files or directories.", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"targets": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"description": "A list of test files or directories to run pytest on." | ||
}, | ||
"options": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"description": "Optional pytest command line options." | ||
} | ||
}, | ||
"required": ["targets"], | ||
}, | ||
}, | ||
} | ||
|
||
def definition(self): | ||
return { | ||
"name": "run_pytest", | ||
"description": "Runs pytest on specified test files or directories.", | ||
"input_schema": { | ||
"type": "object", | ||
"properties": { | ||
"targets": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"description": "A list of test files or directories to run pytest on." | ||
}, | ||
"options": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"description": "Optional pytest command line options." | ||
} | ||
}, | ||
"required": ["targets"] | ||
} | ||
} | ||
|
||
def run(self, tool_use_args): | ||
logging.debug(f'running run_pytest({tool_use_args})') | ||
targets = [os.path.join(self.root, t) for t in tool_use_args['targets']] | ||
options = tool_use_args.get('options', []) | ||
cmd = ['pytest'] + options + targets | ||
result = subprocess.run(cmd, capture_output=True, text=True) | ||
return f"Pytest output:\n{result.stdout}\n\nErrors (if any):\n{result.stderr}" | ||
|
||
if __name__ == '__main__': | ||
tool = PytestTool(sys.argv[1]) | ||
print(tool.run({'targets': sys.argv[2:]})) |