|  | 
| 2 | 2 | 
 | 
| 3 | 3 | import sys | 
| 4 | 4 | import unittest | 
| 5 |  | - | 
|  | 5 | +import os | 
| 6 | 6 | # rip off all interesting stuff from test_profile | 
| 7 | 7 | import cProfile | 
| 8 | 8 | import tempfile | 
| @@ -151,23 +151,111 @@ def test_bad_descriptor(self): | 
| 151 | 151 | 
 | 
| 152 | 152 | 
 | 
| 153 | 153 | class TestCommandLine(unittest.TestCase): | 
|  | 154 | +    def setUp(self): | 
|  | 155 | +        self.test_script = tempfile.NamedTemporaryFile("w+", suffix=".py", delete=False) | 
|  | 156 | +        self.test_script.write(textwrap.dedent(""" | 
|  | 157 | +            def simple_function(): | 
|  | 158 | +                sum(range(1000)) | 
|  | 159 | +                 | 
|  | 160 | +            if __name__ == "__main__": | 
|  | 161 | +                simple_function() | 
|  | 162 | +        """)) | 
|  | 163 | +        self.test_script.close() | 
|  | 164 | +         | 
|  | 165 | +    def tearDown(self): | 
|  | 166 | +        os.unlink(self.test_script.name) | 
|  | 167 | +     | 
| 154 | 168 |     def test_sort(self): | 
| 155 | 169 |         rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo') | 
| 156 | 170 |         self.assertGreater(rc, 0) | 
| 157 | 171 |         self.assertIn(b"option -s: invalid choice: 'demo'", err) | 
| 158 |  | - | 
|  | 172 | +     | 
|  | 173 | +    def test_valid_sort_options(self): | 
|  | 174 | +        for sort_opt in ['calls', 'cumulative', 'cumtime', 'filename', | 
|  | 175 | +                         'ncalls', 'pcalls', 'line', 'name', 'nfl', | 
|  | 176 | +                         'stdname', 'time', 'tottime']: | 
|  | 177 | +            rc, out, err = assert_python_ok('-m', 'cProfile', '-s', sort_opt, self.test_script.name) | 
|  | 178 | +            self.assertEqual(rc, 0) | 
|  | 179 | +            self.assertIn(b"function calls", out) | 
|  | 180 | +     | 
|  | 181 | +    def test_outfile(self): | 
|  | 182 | +        with tempfile.NamedTemporaryFile(suffix='.prof', delete=False) as outfile: | 
|  | 183 | +            outfile_name = outfile.name | 
|  | 184 | +         | 
|  | 185 | +        try: | 
|  | 186 | +            rc, out, err = assert_python_ok('-m', 'cProfile', '-o', outfile_name, self.test_script.name) | 
|  | 187 | +            self.assertEqual(rc, 0) | 
|  | 188 | +            self.assertTrue(os.path.exists(outfile_name)) | 
|  | 189 | +            self.assertGreater(os.path.getsize(outfile_name), 0) | 
|  | 190 | +        finally: | 
|  | 191 | +            if os.path.exists(outfile_name): | 
|  | 192 | +                os.unlink(outfile_name) | 
|  | 193 | +     | 
|  | 194 | +    def test_no_arguments(self): | 
|  | 195 | +        rc, out, err = assert_python_failure('-m', 'cProfile') | 
|  | 196 | +        self.assertGreater(rc, 0) | 
|  | 197 | +     | 
|  | 198 | +    def test_help_option(self): | 
|  | 199 | +        rc, out, err = assert_python_ok('-m', 'cProfile', '--help') | 
|  | 200 | +        self.assertEqual(rc, 0) | 
|  | 201 | +        self.assertIn(b"Usage:", out) | 
|  | 202 | +     | 
|  | 203 | +    def test_version_output(self): | 
|  | 204 | +        rc, out, err = assert_python_ok('-m', 'cProfile', self.test_script.name) | 
|  | 205 | +        self.assertEqual(rc, 0) | 
|  | 206 | +        import os | 
|  | 207 | +        self.assertIn(os.path.basename(self.test_script.name).encode(), out) | 
|  | 208 | +     | 
|  | 209 | +    def test_run_command_line_module(self): | 
|  | 210 | +        rc, out, err = assert_python_ok('-m', 'cProfile', '-m', 'timeit', '-n', '1', 'pass') | 
|  | 211 | +        self.assertEqual(rc, 0) | 
|  | 212 | +        self.assertIn(b"function calls", out) | 
|  | 213 | +     | 
| 159 | 214 |     def test_profile_script_importing_main(self): | 
| 160 |  | -        """Check that scripts that reference __main__ see their own namespace | 
| 161 |  | -        when being profiled.""" | 
| 162 |  | -        with tempfile.NamedTemporaryFile("w+", delete_on_close=False) as f: | 
|  | 215 | +        with tempfile.NamedTemporaryFile("w+", suffix='.py', delete=False) as f: | 
| 163 | 216 |             f.write(textwrap.dedent("""\ | 
| 164 |  | -                class Foo: | 
| 165 |  | -                    pass | 
| 166 |  | -                import __main__ | 
| 167 |  | -                assert Foo == __main__.Foo | 
|  | 217 | +                def test_func(): | 
|  | 218 | +                    x = 1 + 1 | 
|  | 219 | +                    return x | 
|  | 220 | +                 | 
|  | 221 | +                if __name__ == "__main__": | 
|  | 222 | +                    test_func() | 
| 168 | 223 |                 """)) | 
| 169 | 224 |             f.close() | 
| 170 |  | -            assert_python_ok('-m', "cProfile", f.name) | 
|  | 225 | +            try: | 
|  | 226 | +                rc, out, err = assert_python_ok('-m', "cProfile", f.name) | 
|  | 227 | +                self.assertEqual(rc, 0) | 
|  | 228 | +                self.assertIn(b"function calls", out) | 
|  | 229 | +                self.assertIn(b"test_func", out) | 
|  | 230 | +            finally: | 
|  | 231 | +                os.unlink(f.name) | 
|  | 232 | +     | 
|  | 233 | +    def test_output_format(self): | 
|  | 234 | +        rc, out, err = assert_python_ok('-m', 'cProfile', self.test_script.name) | 
|  | 235 | +        self.assertEqual(rc, 0) | 
|  | 236 | +         | 
|  | 237 | +        output = out.decode('utf-8') | 
|  | 238 | +         | 
|  | 239 | +        self.assertRegex(output, r'\d+ function calls in \d+\.\d+ seconds') | 
|  | 240 | +         | 
|  | 241 | +        self.assertIn('Ordered by:', output) | 
|  | 242 | +         | 
|  | 243 | +        self.assertIn('ncalls', output) | 
|  | 244 | +        self.assertIn('tottime', output) | 
|  | 245 | +        self.assertIn('percall', output) | 
|  | 246 | +        self.assertIn('cumtime', output) | 
|  | 247 | +        self.assertIn('filename:lineno(function)', output) | 
|  | 248 | +         | 
|  | 249 | +        self.assertIn('simple_function', output) | 
|  | 250 | +         | 
|  | 251 | +    def test_different_sort_outputs(self): | 
|  | 252 | +        rc1, out1, _ = assert_python_ok('-m', 'cProfile', '-s', 'time', self.test_script.name) | 
|  | 253 | +        rc2, out2, _ = assert_python_ok('-m', 'cProfile', '-s', 'cumulative', self.test_script.name) | 
|  | 254 | +         | 
|  | 255 | +        self.assertNotEqual(out1, out2) | 
|  | 256 | +         | 
|  | 257 | +        self.assertIn(b'simple_function', out1) | 
|  | 258 | +        self.assertIn(b'simple_function', out2) | 
| 171 | 259 | 
 | 
| 172 | 260 | 
 | 
| 173 | 261 | def main(): | 
|  | 
0 commit comments