Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(912)

Side by Side Diff: tools/telemetry/third_party/rope/ropetest/runmodtest.py

Issue 1132103009: Example of refactoring using rope library. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 import os
2 import unittest
3
4 from rope.base import exceptions
5 from ropetest import testutils
6
7
8 class PythonFileRunnerTest(unittest.TestCase):
9
10 def setUp(self):
11 super(PythonFileRunnerTest, self).setUp()
12 self.project = testutils.sample_project()
13 self.pycore = self.project.pycore
14
15 def tearDown(self):
16 testutils.remove_project(self.project)
17 super(PythonFileRunnerTest, self).tearDown()
18
19 def make_sample_python_file(self, file_path,
20 get_text_function_source=None):
21 self.project.root.create_file(file_path)
22 file = self.project.get_resource(file_path)
23 if not get_text_function_source:
24 get_text_function_source = "def get_text():\n return 'run'\n\n"
25 file_content = get_text_function_source + \
26 "output = open('output.txt', 'w')\n" \
27 "output.write(get_text())\noutput.close()\n"
28 file.write(file_content)
29
30 def get_output_file_content(self, file_path):
31 try:
32 output_path = ''
33 last_slash = file_path.rfind('/')
34 if last_slash != -1:
35 output_path = file_path[0:last_slash + 1]
36 file = self.project.get_resource(output_path + 'output.txt')
37 return file.read()
38 except exceptions.ResourceNotFoundError:
39 return ''
40
41 def test_making_runner(self):
42 file_path = 'sample.py'
43 self.make_sample_python_file(file_path)
44 file_resource = self.project.get_resource(file_path)
45 runner = self.pycore.run_module(file_resource)
46 runner.wait_process()
47 self.assertEquals('run', self.get_output_file_content(file_path))
48
49 def test_passing_arguments(self):
50 file_path = 'sample.py'
51 function_source = 'import sys\ndef get_text():' \
52 '\n return str(sys.argv[1:])\n'
53 self.make_sample_python_file(file_path, function_source)
54 file_resource = self.project.get_resource(file_path)
55 runner = self.pycore.run_module(file_resource, args=['hello', 'world'])
56 runner.wait_process()
57 self.assertTrue(self.get_output_file_content(
58 file_path).endswith("['hello', 'world']"))
59
60 def test_passing_arguments_with_spaces(self):
61 file_path = 'sample.py'
62 function_source = 'import sys\ndef get_text():' \
63 '\n return str(sys.argv[1:])\n'
64 self.make_sample_python_file(file_path, function_source)
65 file_resource = self.project.get_resource(file_path)
66 runner = self.pycore.run_module(file_resource, args=['hello world'])
67 runner.wait_process()
68 self.assertTrue(self.get_output_file_content(
69 file_path).endswith("['hello world']"))
70
71 def test_killing_runner(self):
72 file_path = 'sample.py'
73 self.make_sample_python_file(file_path,
74 'def get_text():'
75 '\n import time'
76 '\n time.sleep(1)'
77 "\n return 'run'\n")
78 file_resource = self.project.get_resource(file_path)
79 runner = self.pycore.run_module(file_resource)
80 runner.kill_process()
81 self.assertEquals('', self.get_output_file_content(file_path))
82
83 def test_running_nested_files(self):
84 self.project.root.create_folder('src')
85 file_path = 'src/sample.py'
86 self.make_sample_python_file(file_path)
87 file_resource = self.project.get_resource(file_path)
88 runner = self.pycore.run_module(file_resource)
89 runner.wait_process()
90 self.assertEquals('run', self.get_output_file_content(file_path))
91
92 def test_setting_process_input(self):
93 file_path = 'sample.py'
94 self.make_sample_python_file(file_path,
95 "def get_text():" +
96 "\n import sys"
97 "\n return sys.stdin.readline()\n")
98 temp_file_name = 'processtest.tmp'
99 try:
100 temp_file = open(temp_file_name, 'w')
101 temp_file.write('input text\n')
102 temp_file.close()
103 file_resource = self.project.get_resource(file_path)
104 stdin = open(temp_file_name)
105 runner = self.pycore.run_module(file_resource, stdin=stdin)
106 runner.wait_process()
107 stdin.close()
108 self.assertEquals('input text\n',
109 self.get_output_file_content(file_path))
110 finally:
111 os.remove(temp_file_name)
112
113 def test_setting_process_output(self):
114 file_path = 'sample.py'
115 self.make_sample_python_file(file_path,
116 "def get_text():" +
117 "\n print 'output text'"
118 "\n return 'run'\n")
119 temp_file_name = 'processtest.tmp'
120 try:
121 file_resource = self.project.get_resource(file_path)
122 stdout = open(temp_file_name, 'w')
123 runner = self.pycore.run_module(file_resource, stdout=stdout)
124 runner.wait_process()
125 stdout.close()
126 temp_file = open(temp_file_name, 'r')
127 self.assertEquals('output text\n', temp_file.read())
128 temp_file.close()
129 finally:
130 os.remove(temp_file_name)
131
132 def test_setting_pythonpath(self):
133 src = self.project.root.create_folder('src')
134 src.create_file('sample.py')
135 src.get_child('sample.py').write('def f():\n pass\n')
136 self.project.root.create_folder('test')
137 file_path = 'test/test.py'
138 self.make_sample_python_file(file_path,
139 "def get_text():\n"
140 " import sample"
141 "\n sample.f()\n return'run'\n")
142 file_resource = self.project.get_resource(file_path)
143 runner = self.pycore.run_module(file_resource)
144 runner.wait_process()
145 self.assertEquals('run', self.get_output_file_content(file_path))
146
147 def test_making_runner_when_doi_is_disabled(self):
148 self.project.set('enable_doi', False)
149 file_path = 'sample.py'
150 self.make_sample_python_file(file_path)
151 file_resource = self.project.get_resource(file_path)
152 runner = self.pycore.run_module(file_resource)
153 runner.wait_process()
154 self.assertEquals('run', self.get_output_file_content(file_path))
155
156
157 def suite():
158 result = unittest.TestSuite()
159 result.addTests(unittest.makeSuite(PythonFileRunnerTest))
160 return result
161
162 if __name__ == '__main__':
163 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698