summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--editor.py17
-rw-r--r--subcmds/upload.py5
-rw-r--r--tests/test_editor.py60
3 files changed, 70 insertions, 12 deletions
diff --git a/editor.py b/editor.py
index 19b96c37..fcf16386 100644
--- a/editor.py
+++ b/editor.py
@@ -68,11 +68,14 @@ least one of these before using this command.""", file=sys.stderr)
68 def EditString(cls, data): 68 def EditString(cls, data):
69 """Opens an editor to edit the given content. 69 """Opens an editor to edit the given content.
70 70
71 Args: 71 Args:
72 data : the text to edit 72 data: The text to edit.
73 73
74 Returns: 74 Returns:
75 new value of edited text; None if editing did not succeed 75 New value of edited text.
76
77 Raises:
78 EditorError: The editor failed to run.
76 """ 79 """
77 editor = cls._GetEditor() 80 editor = cls._GetEditor()
78 if editor == ':': 81 if editor == ':':
@@ -80,7 +83,7 @@ least one of these before using this command.""", file=sys.stderr)
80 83
81 fd, path = tempfile.mkstemp() 84 fd, path = tempfile.mkstemp()
82 try: 85 try:
83 os.write(fd, data) 86 os.write(fd, data.encode('utf-8'))
84 os.close(fd) 87 os.close(fd)
85 fd = None 88 fd = None
86 89
@@ -106,8 +109,8 @@ least one of these before using this command.""", file=sys.stderr)
106 raise EditorError('editor failed with exit status %d: %s %s' 109 raise EditorError('editor failed with exit status %d: %s %s'
107 % (rc, editor, path)) 110 % (rc, editor, path))
108 111
109 with open(path) as fd2: 112 with open(path, mode='rb') as fd2:
110 return fd2.read() 113 return fd2.read().decode('utf-8')
111 finally: 114 finally:
112 if fd: 115 if fd:
113 os.close(fd) 116 os.close(fd)
diff --git a/subcmds/upload.py b/subcmds/upload.py
index d0dd3837..5c12aaee 100644
--- a/subcmds/upload.py
+++ b/subcmds/upload.py
@@ -271,11 +271,6 @@ Gerrit Code Review: https://www.gerritcodereview.com/
271 branches[project.name] = b 271 branches[project.name] = b
272 script.append('') 272 script.append('')
273 273
274 script = [ x.encode('utf-8')
275 if issubclass(type(x), unicode)
276 else x
277 for x in script ]
278
279 script = Editor.EditString("\n".join(script)).split("\n") 274 script = Editor.EditString("\n".join(script)).split("\n")
280 275
281 project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$') 276 project_re = re.compile(r'^#?\s*project\s*([^\s]+)/:$')
diff --git a/tests/test_editor.py b/tests/test_editor.py
new file mode 100644
index 00000000..fbcfcdbd
--- /dev/null
+++ b/tests/test_editor.py
@@ -0,0 +1,60 @@
1# -*- coding:utf-8 -*-
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Unittests for the editor.py module."""
18
19from __future__ import print_function
20
21import unittest
22
23from editor import Editor
24
25
26class EditorTestCase(unittest.TestCase):
27 """Take care of resetting Editor state across tests."""
28
29 def setUp(self):
30 self.setEditor(None)
31
32 def tearDown(self):
33 self.setEditor(None)
34
35 @staticmethod
36 def setEditor(editor):
37 Editor._editor = editor
38
39
40class GetEditor(EditorTestCase):
41 """Check GetEditor behavior."""
42
43 def test_basic(self):
44 """Basic checking of _GetEditor."""
45 self.setEditor(':')
46 self.assertEqual(':', Editor._GetEditor())
47
48
49class EditString(EditorTestCase):
50 """Check EditString behavior."""
51
52 def test_no_editor(self):
53 """Check behavior when no editor is available."""
54 self.setEditor(':')
55 self.assertEqual('foo', Editor.EditString('foo'))
56
57 def test_cat_editor(self):
58 """Check behavior when editor is `cat`."""
59 self.setEditor('cat')
60 self.assertEqual('foo', Editor.EditString('foo'))