forked from devchat-ai/devchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_command_parser.py
121 lines (110 loc) · 4.56 KB
/
test_command_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import json
import os
import tempfile
import pytest
from devchat.engine import Command, parse_command
from devchat.engine import CommandParser, Namespace
def test_parse_command():
# Test with a valid configuration file with most fields filled
with tempfile.NamedTemporaryFile('w', delete=False) as config_file:
config_file.write("""
description: Get the current weather in a given location
parameters:
location:
type: string
description: The city and state, e.g. San Francisco, CA
unit:
type: string
enum: [celsius, fahrenheit]
default: celsius
steps:
- run:
./get_weather --location=$location --unit=$unit
""")
config_file.seek(0)
command = parse_command(config_file.name)
assert isinstance(command, Command)
assert command.description == 'Get the current weather in a given location'
assert 'location' in command.parameters
assert command.parameters['unit'].default == 'celsius'
assert command.steps[0]['run'] == './get_weather --location=$location --unit=$unit'
# Test with a valid configuration file with missing optional fields
with tempfile.NamedTemporaryFile('w', delete=False) as config_file:
config_file.write("""
description: Prompt for /code
parameters:
""")
config_file.seek(0)
command = parse_command(config_file.name)
assert command.parameters is None
assert command.steps is None
# Test with an invalid configuration file
with tempfile.NamedTemporaryFile('w', delete=False) as config_file:
config_file.write("""
description:
parameters:
location:
type: string
""")
config_file.seek(0)
with pytest.raises(Exception):
parse_command(config_file.name)
# Test with a non-existent file
with pytest.raises(FileNotFoundError):
parse_command('path/to/non_existent_file.yml')
def test_command_parser(tmp_path):
# Create a Namespace instance with the temporary directory as the root path
namespace = Namespace(tmp_path)
command_parser = CommandParser(namespace)
# Test with a valid configuration file with most fields filled
os.makedirs(os.path.join(tmp_path, 'usr', 'a', 'b', 'c'), exist_ok=True)
command_file_path = os.path.join(tmp_path, 'usr', 'a', 'b', 'c', 'command.yml')
with open(command_file_path, 'w', encoding='utf-8') as file:
file.write("""
description: Get the current weather in a given location
parameters:
location:
type: string
description: The city and state, e.g. San Francisco, CA
unit:
type: string
enum: [celsius, fahrenheit]
default: celsius
steps:
- run:
./get_weather --location=$location --unit=$unit
""")
command_json = command_parser.parse_json('a.b.c')
command = json.loads(command_json)
assert command['description'] == 'Get the current weather in a given location'
assert 'location' in command['parameters']
assert command['parameters']['unit']['default'] == 'celsius'
assert command['steps'][0]['run'] == './get_weather --location=$location --unit=$unit'
# Test with a valid configuration file with missing optional fields
os.makedirs(os.path.join(tmp_path, 'usr', 'd', 'e', 'f'), exist_ok=True)
command_file_path = os.path.join(tmp_path, 'usr', 'd', 'e', 'f', 'command.yml')
with open(command_file_path, 'w', encoding='utf-8') as file:
file.write("""
description: Prompt for /code
parameters:
""")
command_json = command_parser.parse_json('d.e.f')
command = json.loads(command_json)
assert command['description'] == 'Prompt for /code'
assert command['parameters'] is None
assert command['steps'] is None
# Test with an invalid configuration file
os.makedirs(os.path.join(tmp_path, 'usr', 'g', 'h', 'i'), exist_ok=True)
command_file_path = os.path.join(tmp_path, 'usr', 'g', 'h', 'i', 'command.yml')
with open(command_file_path, 'w', encoding='utf-8') as file:
file.write("""
description:
parameters:
location:
type: string
""")
with pytest.raises(Exception):
command_parser.parse_json('g.h.i')
# Test with a non-existent command
command_json = command_parser.parse_json('j.k.l')
assert command_json is None