Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import gzip | |
| 6 import os | |
| 7 import re | |
| 8 import shutil | |
| 9 import subprocess | |
| 10 import tempfile | |
| 11 import unittest | |
| 12 | |
| 13 LOADING_DIR = os.path.dirname(__file__) | |
| 14 TEST_DATA_DIR = os.path.join(LOADING_DIR, 'testdata') | |
| 15 LOADING_TRACE_ANALYZER = os.path.join(LOADING_DIR, 'loading_trace_analyzer.py') | |
|
mattcary
2016/02/26 09:42:29
Generally it would be better to call loading_trace
gabadie
2016/02/29 18:58:45
Done. But I am a not fan of this. It has just move
| |
| 16 | |
| 17 | |
| 18 class LoadingTraceAnalyzerTest(unittest.TestCase): | |
| 19 _ROLLING_STONE = os.path.join(TEST_DATA_DIR, 'rollingstone.trace.gz') | |
| 20 | |
| 21 def setUp(self): | |
| 22 self._temp_dir = tempfile.mkdtemp() | |
| 23 self.trace_path = self._TmpPath('trace.json') | |
| 24 with gzip.GzipFile(self._ROLLING_STONE) as f: | |
| 25 with open(self.trace_path, 'w') as g: | |
| 26 g.write(f.read()) | |
| 27 | |
| 28 def tearDown(self): | |
| 29 shutil.rmtree(self._temp_dir) | |
| 30 | |
| 31 def _Cmd(self, args): | |
| 32 cmd = ['python', LOADING_TRACE_ANALYZER] | |
| 33 cmd.extend(args) | |
| 34 print cmd | |
| 35 process = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
| 36 process.stdout, _ = process.communicate() | |
| 37 process.stdout = process.stdout.split('\n')[:-1] | |
| 38 return process | |
| 39 | |
| 40 def _TmpPath(self, name): | |
| 41 return os.path.join(self._temp_dir, name) | |
| 42 | |
| 43 def test_RequestsCmd(self): | |
|
pasko
2016/02/26 16:50:53
It seems that naming like testRequestsCmd (without
gabadie
2016/02/29 18:58:45
Done.
| |
| 44 process = self._Cmd(['requests', self.trace_path]) | |
| 45 self.assertEquals(0, process.returncode) | |
| 46 self.assertNotEqual(0, len(process.stdout)) | |
| 47 | |
| 48 process = self._Cmd(['requests', | |
| 49 '--output', self._TmpPath('output0'), | |
| 50 self.trace_path]) | |
| 51 self.assertEquals(0, process.returncode) | |
| 52 self.assertEqual(0, len(process.stdout)) | |
| 53 with open(self._TmpPath('output0')) as f: | |
| 54 self.assertNotEqual(0, len(f.readlines())) | |
| 55 | |
| 56 process = self._Cmd(['requests', | |
| 57 '--output-format', 'hello {protocol} world {url}', | |
| 58 self.trace_path]) | |
| 59 self.assertEquals(0, process.returncode) | |
| 60 self.assertNotEqual(0, len(process.stdout)) | |
| 61 for line in process.stdout: | |
| 62 self.assertTrue(re.match(r'^hello \S+ world \S+$', line)) | |
| 63 | |
| 64 process = self._Cmd(['requests', | |
| 65 '--output-format', '{url}', | |
| 66 '--where', '{url}', r'^http://.*$', | |
| 67 self.trace_path]) | |
| 68 self.assertEqual(0, process.returncode) | |
| 69 self.assertNotEqual(0, len(process.stdout)) | |
| 70 for line in process.stdout: | |
| 71 self.assertTrue(line.startswith('http://')) | |
| 72 | |
| 73 process = self._Cmd(['requests', | |
| 74 '--output-format', '{url}', | |
| 75 '--where', '{url}', r'^https://.*$', | |
| 76 self.trace_path]) | |
| 77 self.assertEqual(0, process.returncode) | |
| 78 self.assertNotEqual(0, len(process.stdout)) | |
| 79 for line in process.stdout: | |
| 80 self.assertTrue(line.startswith('https://')) | |
| 81 | |
| 82 | |
| 83 if __name__ == '__main__': | |
| 84 unittest.main() | |
| OLD | NEW |