Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
RustPython/extra_tests/test_snippets.py at main · rrupy/RustPython · 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 }}
rrupy
/
RustPython
Public
forked from
RustPython/RustPython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
RustPython
/
extra_tests
/
test_snippets.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
133 lines (100 loc) · 3.67 KB
main
Breadcrumbs
RustPython
/
extra_tests
/
test_snippets.py
Copy path
Top
File metadata and controls
Code
Blame
133 lines (100 loc) · 3.67 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
# This is a python unittest class automatically populating with all tests
# in the snippets folder.
import
sys
import
os
import
unittest
import
glob
import
logging
import
subprocess
import
contextlib
import
enum
from
pathlib
import
Path
class
_TestType
(
enum
.
Enum
):
functional
=
1
logger
=
logging
.
getLogger
(
"tests"
)
ROOT_DIR
=
".."
TEST_ROOT
=
os
.
path
.
abspath
(
os
.
path
.
join
(
ROOT_DIR
,
"extra_tests"
))
TEST_DIRS
=
{
_TestType
.
functional
:
os
.
path
.
join
(
TEST_ROOT
,
"snippets"
)}
@
contextlib
.
contextmanager
def
pushd
(
path
):
old_dir
=
os
.
getcwd
()
os
.
chdir
(
path
)
yield
os
.
chdir
(
old_dir
)
def
perform_test
(
filename
,
method
,
test_type
):
logger
.
info
(
"Running %s via %s"
,
filename
,
method
)
if
method
==
"cpython"
:
run_via_cpython
(
filename
)
elif
method
==
"rustpython"
:
run_via_rustpython
(
filename
,
test_type
)
else
:
raise
NotImplementedError
(
method
)
def
run_via_cpython
(
filename
):
""" Simply invoke python itself on the script """
env
=
os
.
environ
.
copy
()
subprocess
.
check_call
([
sys
.
executable
,
filename
],
env
=
env
)
RUSTPYTHON_BINARY
=
os
.
environ
.
get
(
"RUSTPYTHON"
)
or
os
.
path
.
join
(
ROOT_DIR
,
"target/release/rustpython"
)
RUSTPYTHON_BINARY
=
os
.
path
.
abspath
(
RUSTPYTHON_BINARY
)
def
run_via_rustpython
(
filename
,
test_type
):
env
=
os
.
environ
.
copy
()
env
[
'RUST_LOG'
]
=
'info,cargo=error,jobserver=error'
env
[
'RUST_BACKTRACE'
]
=
'1'
subprocess
.
check_call
([
RUSTPYTHON_BINARY
,
filename
],
env
=
env
)
def
create_test_function
(
cls
,
filename
,
method
,
test_type
):
""" Create a test function for a single snippet """
core_test_directory
,
snippet_filename
=
os
.
path
.
split
(
filename
)
test_function_name
=
"test_{}_"
.
format
(
method
)
+
os
.
path
.
splitext
(
snippet_filename
)[
0
].
replace
(
"."
,
"_"
).
replace
(
"-"
,
"_"
)
def
test_function
(
self
):
perform_test
(
filename
,
method
,
test_type
)
if
hasattr
(
cls
,
test_function_name
):
raise
ValueError
(
"Duplicate test case {}"
.
format
(
test_function_name
))
setattr
(
cls
,
test_function_name
,
test_function
)
def
populate
(
method
):
def
wrapper
(
cls
):
""" Decorator function which can populate a unittest.TestCase class """
for
test_type
,
filename
in
get_test_files
():
create_test_function
(
cls
,
filename
,
method
,
test_type
)
return
cls
return
wrapper
def
get_test_files
():
""" Retrieve test files """
for
test_type
,
test_dir
in
TEST_DIRS
.
items
():
for
filepath
in
sorted
(
glob
.
iglob
(
os
.
path
.
join
(
test_dir
,
"*.py"
))):
filename
=
os
.
path
.
split
(
filepath
)[
1
]
if
filename
.
startswith
(
"xfail_"
):
continue
yield
test_type
,
os
.
path
.
abspath
(
filepath
)
def
generate_slices
(
path
):
# loop used to build slices_res.py with cpython
ll
=
[
0
,
1
,
2
,
3
]
start
=
list
(
range
(
-
7
,
7
))
end
=
list
(
range
(
-
7
,
7
))
step
=
list
(
range
(
-
5
,
5
))
step
.
pop
(
step
.
index
(
0
))
for
i
in
[
start
,
end
,
step
]:
i
.
append
(
None
)
slices_res
=
[]
for
s
in
start
:
for
e
in
end
:
for
t
in
step
:
slices_res
.
append
(
ll
[
s
:
e
:
t
])
path
.
write_text
(
"SLICES_RES={}
\n
START= {}
\n
END= {}
\n
STEP= {}
\n
LL={}
\n
"
.
format
(
slices_res
,
start
,
end
,
step
,
ll
)
)
@
populate
(
"cpython"
)
@
populate
(
"rustpython"
)
class
SampleTestCase
(
unittest
.
TestCase
):
@
classmethod
def
setUpClass
(
cls
):
# Here add resource files
cls
.
slices_resource_path
=
Path
(
TEST_DIRS
[
_TestType
.
functional
])
/
"cpython_generated_slices.py"
if
cls
.
slices_resource_path
.
exists
():
cls
.
slices_resource_path
.
unlink
()
generate_slices
(
cls
.
slices_resource_path
)
@
classmethod
def
tearDownClass
(
cls
):
cls
.
slices_resource_path
.
unlink
()
You can’t perform that action at this time.