FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cecli/tests/basic/test_editor.py at main · cecli-dev/cecli · GitHub
cecli-dev
cecli
Repository navigation
Code
Issues
12
(12)
Pull requests
7
(7)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cecli
/
tests
/
basic
/
test_editor.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
159 lines (126 loc) · 4.98 KB
Breadcrumbs
cecli
/
tests
/
basic
/
test_editor.py
Copy path
File metadata and controls
159 lines (126 loc) · 4.98 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
import
os
from
unittest
.
mock
import
MagicMock
,
patch
from
cecli
.
editor
import
(
DEFAULT_EDITOR_NIX
,
DEFAULT_EDITOR_OS_X
,
DEFAULT_EDITOR_WINDOWS
,
discover_editor
,
get_environment_editor
,
pipe_editor
,
print_status_message
,
write_temp_file
,
)
def
test_get_environment_editor
():
# Test with no environment variables set
with
patch
.
dict
(
os
.
environ
, {},
clear
=
True
):
assert
get_environment_editor
(
"default"
)
==
"default"
# Test EDITOR precedence
with
patch
.
dict
(
os
.
environ
, {
"EDITOR"
:
"vim"
},
clear
=
True
):
assert
get_environment_editor
()
==
"vim"
# Test VISUAL overrides EDITOR
with
patch
.
dict
(
os
.
environ
, {
"EDITOR"
:
"vim"
,
"VISUAL"
:
"code"
}):
assert
get_environment_editor
()
==
"code"
def
test_discover_editor_defaults
():
with
patch
(
"platform.system"
)
as
mock_system
:
# Test Windows default
mock_system
.
return_value
=
"Windows"
with
patch
.
dict
(
os
.
environ
, {},
clear
=
True
):
assert
discover_editor
()
==
DEFAULT_EDITOR_WINDOWS
# Test macOS default
mock_system
.
return_value
=
"Darwin"
with
patch
.
dict
(
os
.
environ
, {},
clear
=
True
):
assert
discover_editor
()
==
DEFAULT_EDITOR_OS_X
# Test Linux default
mock_system
.
return_value
=
"Linux"
with
patch
.
dict
(
os
.
environ
, {},
clear
=
True
):
assert
discover_editor
()
==
DEFAULT_EDITOR_NIX
def
test_write_temp_file
():
# Test basic file creation
content
=
"test content"
filepath
=
write_temp_file
(
content
)
assert
os
.
path
.
exists
(
filepath
)
with
open
(
filepath
,
"r"
)
as
f
:
assert
f
.
read
()
==
content
os
.
remove
(
filepath
)
# Test with suffix
filepath
=
write_temp_file
(
"content"
,
suffix
=
"txt"
)
assert
filepath
.
endswith
(
".txt"
)
os
.
remove
(
filepath
)
# Test with prefix
filepath
=
write_temp_file
(
"content"
,
prefix
=
"test_"
)
assert
os
.
path
.
basename
(
filepath
).
startswith
(
"test_"
)
os
.
remove
(
filepath
)
def
test_print_status_message
(
capsys
):
# Test success message
print_status_message
(
True
,
"Success!"
)
captured
=
capsys
.
readouterr
()
assert
"Success!"
in
captured
.
out
# Test failure message
print_status_message
(
False
,
"Failed!"
)
captured
=
capsys
.
readouterr
()
assert
"Failed!"
in
captured
.
out
def
test_discover_editor_override
():
# Test editor override
assert
discover_editor
(
"code"
)
==
"code"
assert
discover_editor
(
'vim -c "set noswapfile"'
)
==
'vim -c "set noswapfile"'
def
test_pipe_editor_with_fake_editor
():
# Create a temporary Python script that logs its arguments
import
sys
import
tempfile
with
tempfile
.
NamedTemporaryFile
(
mode
=
"w"
,
suffix
=
".log"
,
delete
=
False
)
as
log_f
:
log_path
=
log_f
.
name
# Convert to raw string path to avoid escape issues on Windows
log_path_escaped
=
log_path
.
replace
(
"
\\
"
,
"
\\
\\
"
)
with
tempfile
.
NamedTemporaryFile
(
mode
=
"w"
,
suffix
=
".py"
,
delete
=
False
)
as
f
:
f
.
write
(
f"""import sys
with open(r"
{
log_path_escaped
}
", "w") as f:
f.write(" ".join(sys.argv))
"""
)
script_path
=
f
.
name
try
:
# Use the Python script as editor and verify it's called with .md file
python_exe
=
sys
.
executable
editor_cmd
=
f"
{
python_exe
}
{
script_path
}
"
pipe_editor
(
"test content"
,
suffix
=
"md"
,
editor
=
editor_cmd
)
# Read the log file to see what arguments were passed
with
open
(
log_path
)
as
f
:
called_args
=
f
.
read
().
strip
()
# Verify the editor was called with a .md file
assert
called_args
.
endswith
(
".md"
),
f"Called args:
{
called_args
!r
}
"
finally
:
# Clean up
os
.
unlink
(
script_path
)
os
.
unlink
(
log_path
)
def
test_pipe_editor
():
# Test with default editor
test_content
=
"Initial content"
modified_content
=
"Modified content"
# Mock the file operations and editor call
with
(
patch
(
"cecli.editor.write_temp_file"
)
as
mock_write
,
patch
(
"builtins.open"
)
as
mock_open
,
patch
(
"os.remove"
)
as
mock_remove
,
patch
(
"subprocess.call"
)
as
mock_subprocess
,
):
# Setup mocks
mock_write
.
return_value
=
"temp.txt"
mock_file
=
MagicMock
()
mock_file
.
__enter__
.
return_value
.
read
.
return_value
=
modified_content
mock_open
.
return_value
=
mock_file
# Test with default editor
result
=
pipe_editor
(
test_content
)
assert
result
==
modified_content
mock_write
.
assert_called_with
(
test_content
,
None
)
mock_subprocess
.
assert_called
()
# Test with custom editor
result
=
pipe_editor
(
test_content
,
editor
=
"code"
)
assert
result
==
modified_content
mock_subprocess
.
assert_called
()
# Test with suffix
result
=
pipe_editor
(
test_content
,
suffix
=
"md"
)
assert
result
==
modified_content
mock_write
.
assert_called_with
(
test_content
,
"md"
)
# Test cleanup on permission error
mock_remove
.
side_effect
=
PermissionError
result
=
pipe_editor
(
test_content
)
assert
result
==
modified_content
Back
|
FazBrowse Home
|
New Git URL