Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
pre-commit/tests/prefixed_command_runner_test.py at master · tomixu/pre-commit · 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 }}
tomixu
/
pre-commit
Public
forked from
pre-commit/pre-commit
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
pre-commit
/
tests
/
prefixed_command_runner_test.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
164 lines (129 loc) · 4.63 KB
master
Breadcrumbs
pre-commit
/
tests
/
prefixed_command_runner_test.py
Copy path
Top
File metadata and controls
Code
Blame
164 lines (129 loc) · 4.63 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
from
__future__
import
unicode_literals
import
os
import
subprocess
import
mock
import
pytest
from
pre_commit
import
five
from
pre_commit
.
prefixed_command_runner
import
PrefixedCommandRunner
from
pre_commit
.
util
import
CalledProcessError
def
norm_slash
(
input_tup
):
return
tuple
(
x
.
replace
(
'/'
,
os
.
sep
)
for
x
in
input_tup
)
def
test_CalledProcessError_str
():
error
=
CalledProcessError
(
1
,
[
five
.
n
(
'git'
),
five
.
n
(
'status'
)],
0
,
(
five
.
n
(
'stdout'
),
five
.
n
(
'stderr'
)),
)
assert
str
(
error
)
==
(
"Command: ['git', 'status']
\n
"
"Return code: 1
\n
"
"Expected return code: 0
\n
"
"Output:
\n
"
" stdout
\n
"
"Errors:
\n
"
" stderr
\n
"
)
def
test_CalledProcessError_str_nooutput
():
error
=
CalledProcessError
(
1
, [
five
.
n
(
'git'
),
five
.
n
(
'status'
)],
0
, (
five
.
n
(
''
),
five
.
n
(
''
))
)
assert
str
(
error
)
==
(
"Command: ['git', 'status']
\n
"
"Return code: 1
\n
"
"Expected return code: 0
\n
"
"Output: (none)
\n
"
"Errors: (none)
\n
"
)
@
pytest
.
fixture
def
popen_mock
():
popen
=
mock
.
Mock
(
spec
=
subprocess
.
Popen
)
popen
.
return_value
.
communicate
.
return_value
=
(
b'stdout'
,
b'stderr'
)
return
popen
@
pytest
.
fixture
def
makedirs_mock
():
return
mock
.
Mock
(
spec
=
os
.
makedirs
)
@
pytest
.
mark
.
parametrize
((
'input'
,
'expected_prefix'
), (
norm_slash
((
'.'
,
'./'
)),
norm_slash
((
'foo'
,
'foo/'
)),
norm_slash
((
'bar/'
,
'bar/'
)),
norm_slash
((
'foo/bar'
,
'foo/bar/'
)),
norm_slash
((
'foo/bar/'
,
'foo/bar/'
)),
))
def
test_init_normalizes_path_endings
(
input
,
expected_prefix
):
input
=
input
.
replace
(
'/'
,
os
.
sep
)
expected_prefix
=
expected_prefix
.
replace
(
'/'
,
os
.
sep
)
instance
=
PrefixedCommandRunner
(
input
)
assert
instance
.
prefix_dir
==
expected_prefix
def
test_run_substitutes_prefix
(
popen_mock
,
makedirs_mock
):
instance
=
PrefixedCommandRunner
(
'prefix'
,
popen
=
popen_mock
,
makedirs
=
makedirs_mock
,
)
ret
=
instance
.
run
([
'{prefix}bar'
,
'baz'
],
retcode
=
None
)
popen_mock
.
assert_called_once_with
(
[
five
.
n
(
os
.
path
.
join
(
'prefix'
,
'bar'
)),
five
.
n
(
'baz'
)],
env
=
None
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
)
assert
ret
==
(
popen_mock
.
return_value
.
returncode
,
'stdout'
,
'stderr'
)
PATH_TESTS
=
(
norm_slash
((
'foo'
,
''
,
'foo'
)),
norm_slash
((
'foo'
,
'bar'
,
'foo/bar'
)),
norm_slash
((
'foo/bar'
,
'../baz'
,
'foo/baz'
)),
norm_slash
((
'./'
,
'bar'
,
'bar'
)),
norm_slash
((
'./'
,
''
,
'.'
)),
norm_slash
((
'/tmp/foo'
,
'/tmp/bar'
,
'/tmp/bar'
)),
)
@
pytest
.
mark
.
parametrize
((
'prefix'
,
'path_end'
,
'expected_output'
),
PATH_TESTS
)
def
test_path
(
prefix
,
path_end
,
expected_output
):
instance
=
PrefixedCommandRunner
(
prefix
)
ret
=
instance
.
path
(
path_end
)
assert
ret
==
expected_output
def
test_path_multiple_args
():
instance
=
PrefixedCommandRunner
(
'foo'
)
ret
=
instance
.
path
(
'bar'
,
'baz'
)
assert
ret
==
os
.
path
.
join
(
'foo'
,
'bar'
,
'baz'
)
@
pytest
.
mark
.
parametrize
(
(
'prefix'
,
'path_end'
,
'expected_output'
),
tuple
(
(
prefix
,
path_end
,
expected_output
+
os
.
sep
)
for
prefix
,
path_end
,
expected_output
in
PATH_TESTS
),
)
def
test_from_command_runner
(
prefix
,
path_end
,
expected_output
):
first
=
PrefixedCommandRunner
(
prefix
)
second
=
PrefixedCommandRunner
.
from_command_runner
(
first
,
path_end
)
assert
second
.
prefix_dir
==
expected_output
def
test_from_command_runner_preserves_popen
(
popen_mock
,
makedirs_mock
):
first
=
PrefixedCommandRunner
(
'foo'
,
popen
=
popen_mock
,
makedirs
=
makedirs_mock
,
)
second
=
PrefixedCommandRunner
.
from_command_runner
(
first
,
'bar'
)
second
.
run
([
'foo/bar/baz'
],
retcode
=
None
)
popen_mock
.
assert_called_once_with
(
[
five
.
n
(
'foo/bar/baz'
)],
env
=
None
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
)
makedirs_mock
.
assert_called_once_with
(
os
.
path
.
join
(
'foo'
,
'bar'
)
+
os
.
sep
)
def
test_create_path_if_not_exists
(
in_tmpdir
):
instance
=
PrefixedCommandRunner
(
'foo'
)
assert
not
os
.
path
.
exists
(
'foo'
)
instance
.
_create_path_if_not_exists
()
assert
os
.
path
.
exists
(
'foo'
)
def
test_exists_does_not_exist
(
in_tmpdir
):
assert
not
PrefixedCommandRunner
(
'.'
).
exists
(
'foo'
)
def
test_exists_does_exist
(
in_tmpdir
):
os
.
mkdir
(
'foo'
)
assert
PrefixedCommandRunner
(
'.'
).
exists
(
'foo'
)
def
test_raises_on_error
(
popen_mock
,
makedirs_mock
):
popen_mock
.
return_value
.
returncode
=
1
with
pytest
.
raises
(
CalledProcessError
):
instance
=
PrefixedCommandRunner
(
'.'
,
popen
=
popen_mock
,
makedirs
=
makedirs_mock
,
)
instance
.
run
([
'foo'
])
You can’t perform that action at this time.