Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
codeflash/tests/code_utils/test_code_utils.py at main · codeflash-ai/codeflash · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
codeflash-ai
/
codeflash
Public
Notifications
You must be signed in to change notification settings
Fork
26
Star
247
Code
Issues
18
Pull requests
52
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
codeflash
/
tests
/
code_utils
/
test_code_utils.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
186 lines (145 loc) · 7.47 KB
main
Breadcrumbs
codeflash
/
tests
/
code_utils
/
test_code_utils.py
Copy path
Top
File metadata and controls
Code
Blame
186 lines (145 loc) · 7.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from
__future__
import
annotations
import
configparser
import
os
from
pathlib
import
Path
import
pytest
import
tomlkit
from
codeflash
.
code_utils
.
code_utils
import
custom_addopts
def
test_custom_addopts_modifies_and_restores_dotini_file
(
tmp_path
:
Path
)
->
None
:
"""Verify that custom_addopts correctly modifies and then restores a pytest.ini file."""
# Create a dummy pytest.ini file
config_file
=
tmp_path
/
".pytest.ini"
original_content
=
"[pytest]
\n
addopts = -v --cov=./src -n auto
\n
"
config_file
.
write_text
(
original_content
)
# Use patch to mock get_all_closest_config_files
os
.
chdir
(
tmp_path
)
with
custom_addopts
():
# Check that the file is modified inside the context
modified_content
=
config_file
.
read_text
()
config
=
configparser
.
ConfigParser
()
config
.
read_string
(
modified_content
)
modified_addopts
=
config
.
get
(
"pytest"
,
"addopts"
,
fallback
=
""
)
assert
modified_addopts
==
"-v"
# Check that the file is restored after exiting the context
restored_content
=
config_file
.
read_text
()
assert
restored_content
.
strip
()
==
original_content
.
strip
()
def
test_custom_addopts_modifies_and_restores_ini_file
(
tmp_path
:
Path
)
->
None
:
"""Verify that custom_addopts correctly modifies and then restores a pytest.ini file."""
# Create a dummy pytest.ini file
config_file
=
tmp_path
/
"pytest.ini"
original_content
=
"[pytest]
\n
addopts = -v --cov=./src -n auto
\n
"
config_file
.
write_text
(
original_content
)
# Use patch to mock get_all_closest_config_files
os
.
chdir
(
tmp_path
)
with
custom_addopts
():
# Check that the file is modified inside the context
modified_content
=
config_file
.
read_text
()
config
=
configparser
.
ConfigParser
()
config
.
read_string
(
modified_content
)
modified_addopts
=
config
.
get
(
"pytest"
,
"addopts"
,
fallback
=
""
)
assert
modified_addopts
==
"-v"
# Check that the file is restored after exiting the context
restored_content
=
config_file
.
read_text
()
assert
restored_content
.
strip
()
==
original_content
.
strip
()
def
test_custom_addopts_modifies_and_restores_toml_file
(
tmp_path
:
Path
)
->
None
:
"""Verify that custom_addopts correctly modifies and then restores a pyproject.toml file."""
# Create a dummy pyproject.toml file
config_file
=
tmp_path
/
"pyproject.toml"
os
.
chdir
(
tmp_path
)
original_addopts
=
"-v --cov=./src --junitxml=report.xml"
original_content_dict
=
{
"tool"
: {
"pytest"
: {
"ini_options"
: {
"addopts"
:
original_addopts
}}}}
original_content
=
tomlkit
.
dumps
(
original_content_dict
)
config_file
.
write_text
(
original_content
)
# Use patch to mock get_all_closest_config_files
os
.
chdir
(
tmp_path
)
with
custom_addopts
():
# Check that the file is modified inside the context
modified_content
=
config_file
.
read_text
()
modified_data
=
tomlkit
.
parse
(
modified_content
)
modified_addopts
=
modified_data
.
get
(
"tool"
, {}).
get
(
"pytest"
, {}).
get
(
"ini_options"
, {}).
get
(
"addopts"
,
""
)
assert
modified_addopts
==
"-v"
# Check that the file is restored after exiting the context
restored_content
=
config_file
.
read_text
()
assert
restored_content
.
strip
()
==
original_content
.
strip
()
def
test_custom_addopts_handles_no_addopts
(
tmp_path
:
Path
)
->
None
:
"""Ensure custom_addopts doesn't fail when a config file has no addopts."""
# Create a dummy pytest.ini file without addopts
config_file
=
tmp_path
/
"pytest.ini"
original_content
=
"[pytest]
\n
# no addopts here
\n
"
config_file
.
write_text
(
original_content
)
os
.
chdir
(
tmp_path
)
with
custom_addopts
():
# The file should not be modified
content_inside_context
=
config_file
.
read_text
()
assert
content_inside_context
==
original_content
# The file should remain unchanged
content_after_context
=
config_file
.
read_text
()
assert
content_after_context
==
original_content
def
test_custom_addopts_handles_no_relevant_files
(
tmp_path
:
Path
)
->
None
:
"""Ensure custom_addopts runs without error when no config files are found."""
# No config files created in tmp_path
os
.
chdir
(
tmp_path
)
# This should execute without raising any exceptions
with
custom_addopts
():
pass
# No assertions needed, the test passes if no exceptions were raised
def
test_custom_addopts_toml_without_pytest_section
(
tmp_path
:
Path
)
->
None
:
"""Verify custom_addopts doesn't fail with a toml file missing a [tool.pytest] section."""
config_file
=
tmp_path
/
"pyproject.toml"
original_content_dict
=
{
"tool"
: {
"other_tool"
: {
"key"
:
"value"
}}}
original_content
=
tomlkit
.
dumps
(
original_content_dict
)
config_file
.
write_text
(
original_content
)
os
.
chdir
(
tmp_path
)
with
custom_addopts
():
content_inside_context
=
config_file
.
read_text
()
assert
content_inside_context
==
original_content
content_after_context
=
config_file
.
read_text
()
assert
content_after_context
==
original_content
def
test_custom_addopts_ini_without_pytest_section
(
tmp_path
:
Path
)
->
None
:
"""Verify custom_addopts doesn't fail with an ini file missing a [pytest] section."""
config_file
=
tmp_path
/
"pytest.ini"
original_content
=
"[other_section]
\n
key = value
\n
"
config_file
.
write_text
(
original_content
)
os
.
chdir
(
tmp_path
)
with
custom_addopts
():
content_inside_context
=
config_file
.
read_text
()
assert
content_inside_context
==
original_content
content_after_context
=
config_file
.
read_text
()
assert
content_after_context
==
original_content
def
test_custom_addopts_with_multiple_config_files
(
tmp_path
:
Path
)
->
None
:
"""Verify custom_addopts modifies and restores all found config files."""
os
.
chdir
(
tmp_path
)
# Create pytest.ini
ini_file
=
tmp_path
/
"pytest.ini"
ini_original_content
=
"[pytest]
\n
addopts = -v --cov
\n
"
ini_file
.
write_text
(
ini_original_content
)
# Create pyproject.toml
toml_file
=
tmp_path
/
"pyproject.toml"
toml_original_addopts
=
"-s -n auto"
toml_original_content_dict
=
{
"tool"
: {
"pytest"
: {
"ini_options"
: {
"addopts"
:
toml_original_addopts
}}}}
toml_original_content
=
tomlkit
.
dumps
(
toml_original_content_dict
)
toml_file
.
write_text
(
toml_original_content
)
with
custom_addopts
():
# Check INI file modification
ini_modified_content
=
ini_file
.
read_text
()
config
=
configparser
.
ConfigParser
()
config
.
read_string
(
ini_modified_content
)
assert
config
.
get
(
"pytest"
,
"addopts"
,
fallback
=
""
)
==
"-v"
# Check TOML file modification
toml_modified_content
=
toml_file
.
read_text
()
modified_data
=
tomlkit
.
parse
(
toml_modified_content
)
modified_addopts
=
modified_data
.
get
(
"tool"
, {}).
get
(
"pytest"
, {}).
get
(
"ini_options"
, {}).
get
(
"addopts"
,
""
)
assert
modified_addopts
==
"-s"
# Check that both files are restored
assert
ini_file
.
read_text
().
strip
()
==
ini_original_content
.
strip
()
assert
toml_file
.
read_text
().
strip
()
==
toml_original_content
.
strip
()
def
test_custom_addopts_restores_on_exception
(
tmp_path
:
Path
)
->
None
:
"""Ensure config file is restored even if an exception occurs inside the context."""
config_file
=
tmp_path
/
"pytest.ini"
original_content
=
"[pytest]
\n
addopts = -v --cov
\n
"
config_file
.
write_text
(
original_content
)
os
.
chdir
(
tmp_path
)
with
pytest
.
raises
(
ValueError
,
match
=
"Test exception"
),
custom_addopts
():
raise
ValueError
(
"Test exception"
)
restored_content
=
config_file
.
read_text
()
assert
restored_content
.
strip
()
==
original_content
.
strip
()
You can’t perform that action at this time.