|
5 | 5 | import itertools
|
6 | 6 | from json import loads
|
7 | 7 | from ddt import ddt, data
|
| 8 | +from mock import patch, Mock |
8 | 9 | import unittest2 as unittest
|
9 | 10 |
|
10 | 11 | if sys.version_info < (2, 7):
|
|
17 | 18 | import codecov
|
18 | 19 |
|
19 | 20 |
|
20 |
| -jacoco_xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> |
21 |
| -<!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN" "report.dtd"> |
22 |
| -<report name="JaCoCo Maven plug-in example for Java project"> |
23 |
| - <sessioninfo id="Steves-MBP.local-b048b758" start="1411925087600" dump="1411925088117" /> |
24 |
| - <package name="org/jacoco/examples/maven/java"> |
25 |
| - <class name="org/jacoco/examples/maven/java/HelloWorld"> |
26 |
| - <method name="<init>" desc="()V" line="3"> |
27 |
| - <counter type="INSTRUCTION" missed="0" covered="3" /> |
28 |
| - <counter type="LINE" missed="0" covered="1" /> |
29 |
| - <counter type="COMPLEXITY" missed="0" covered="1" /> |
30 |
| - <counter type="METHOD" missed="0" covered="1" /> |
31 |
| - </method> |
32 |
| - <method name="getMessage" desc="(Z)Ljava/lang/String;" line="6"> |
33 |
| - <counter type="INSTRUCTION" missed="2" covered="4" /> |
34 |
| - <counter type="BRANCH" missed="1" covered="1" /> |
35 |
| - <counter type="LINE" missed="1" covered="2" /> |
36 |
| - <counter type="COMPLEXITY" missed="1" covered="1" /> |
37 |
| - <counter type="METHOD" missed="0" covered="1" /> |
38 |
| - </method> |
39 |
| - <counter type="INSTRUCTION" missed="2" covered="7" /> |
40 |
| - <counter type="BRANCH" missed="1" covered="1" /> |
41 |
| - <counter type="LINE" missed="1" covered="3" /> |
42 |
| - <counter type="COMPLEXITY" missed="1" covered="2" /> |
43 |
| - <counter type="METHOD" missed="0" covered="2" /> |
44 |
| - <counter type="CLASS" missed="0" covered="1" /> |
45 |
| - </class> |
46 |
| - <sourcefile name="HelloWorld.java"> |
47 |
| - <line nr="3" mi="0" ci="3" mb="0" cb="0" /> |
48 |
| - <line nr="6" mi="0" ci="2" mb="1" cb="1" /> |
49 |
| - <line nr="7" mi="2" ci="0" mb="0" cb="0" /> |
50 |
| - <line nr="9" mi="0" ci="2" mb="0" cb="0" /> |
51 |
| - <line nr="10" mi="0" ci="2" mb="0" cb="2" /> |
52 |
| - <counter type="INSTRUCTION" missed="2" covered="7" /> |
53 |
| - <counter type="BRANCH" missed="1" covered="1" /> |
54 |
| - <counter type="LINE" missed="1" covered="3" /> |
55 |
| - <counter type="COMPLEXITY" missed="1" covered="2" /> |
56 |
| - <counter type="METHOD" missed="0" covered="2" /> |
57 |
| - <counter type="CLASS" missed="0" covered="1" /> |
58 |
| - </sourcefile> |
59 |
| - <sourcefile name="HelloWorld.java"> |
60 |
| - <counter type="INSTRUCTION" missed="2" covered="7" /> |
61 |
| - <counter type="BRANCH" missed="1" covered="1" /> |
62 |
| - <counter type="LINE" missed="1" covered="3" /> |
63 |
| - <counter type="COMPLEXITY" missed="1" covered="2" /> |
64 |
| - <counter type="METHOD" missed="0" covered="2" /> |
65 |
| - <counter type="CLASS" missed="0" covered="1" /> |
66 |
| - </sourcefile> |
67 |
| - <counter type="INSTRUCTION" missed="2" covered="7" /> |
68 |
| - <counter type="BRANCH" missed="1" covered="1" /> |
69 |
| - <counter type="LINE" missed="1" covered="3" /> |
70 |
| - <counter type="COMPLEXITY" missed="1" covered="2" /> |
71 |
| - <counter type="METHOD" missed="0" covered="2" /> |
72 |
| - <counter type="CLASS" missed="0" covered="1" /> |
73 |
| - </package> |
74 |
| - <counter type="INSTRUCTION" missed="2" covered="7" /> |
75 |
| - <counter type="BRANCH" missed="1" covered="1" /> |
76 |
| - <counter type="LINE" missed="1" covered="3" /> |
77 |
| - <counter type="COMPLEXITY" missed="1" covered="2" /> |
78 |
| - <counter type="METHOD" missed="0" covered="2" /> |
79 |
| - <counter type="CLASS" missed="0" covered="1" /> |
80 |
| -</report> |
81 |
| -""" |
82 |
| - |
83 |
| - |
84 | 21 | @ddt
|
85 | 22 | class TestUploader(unittest.TestCase):
|
86 | 23 | maxDiff = None
|
@@ -191,28 +128,43 @@ def test_exits_1(self):
|
191 | 128 | raise Exception("did not exit")
|
192 | 129 |
|
193 | 130 | def test_returns_none(self):
|
194 |
| - with open(self.filepath, 'w+') as f: |
195 |
| - f.write('coverage data') |
196 |
| - sys.argv = ['', '--commit=8ed84d96bc225deff66605486180cd555366806b', |
197 |
| - '--branch=master', |
198 |
| - '--token=473c8c5b-10ee-4d83-86c6-bfd72a185a27'] |
199 |
| - self.assertEqual(codecov.main(), None) |
| 131 | + with patch('requests.post') as post, patch('requests.put') as put: |
| 132 | + post.return_value = Mock(status_code=200, text='target\ns3') |
| 133 | + put.return_value = Mock(status_code=200) |
| 134 | + with open(self.filepath, 'w+') as f: |
| 135 | + f.write('coverage data') |
| 136 | + sys.argv = ['', '--commit=8ed84d96bc225deff66605486180cd555366806b', |
| 137 | + '--branch=master', |
| 138 | + '--token=473c8c5b-10ee-4d83-86c6-bfd72a185a27'] |
| 139 | + self.assertEqual(codecov.main(), None) |
| 140 | + assert post.called and put.called |
200 | 141 |
|
201 | 142 | def test_send(self):
|
202 |
| - with open(self.filepath, 'w+') as f: |
203 |
| - f.write('coverage data') |
204 |
| - res = self.run_cli(False, commit='a'*40, branch='master', token='473c8c5b-10ee-4d83-86c6-bfd72a185a27') |
205 |
| - self.assertEqual(res['result'].strip(), 'http://codecov.io/github/codecov/ci-repo?ref=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') |
| 143 | + with patch('requests.post') as post, patch('requests.put') as put: |
| 144 | + post.return_value = Mock(status_code=200, text='target\ns3') |
| 145 | + put.return_value = Mock(status_code=200) |
| 146 | + with open(self.filepath, 'w+') as f: |
| 147 | + f.write('coverage data') |
| 148 | + res = self.run_cli(False, commit='a'*40, branch='master', token='<token>') |
| 149 | + self.assertEqual(res['result'].strip(), 'target') |
| 150 | + print post.call_args |
| 151 | + post.assert_called_with('https://codecov.io/upload/v3?commit=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&token=%3Ctoken%3E&branch=master&package=py1.6.5', headers={'Accept': 'text/plain'}, verify=None) |
| 152 | + print put.call_args |
| 153 | + put.assert_called_with('s3', |
| 154 | + data='\n.coveragerc\n.gitignore\n.token\n.travis.yml\nCHANGELOG.md\nMakefile\nREADME.md\nappveyor.yml\ncircle.yml\ncodecov/__init__.py\nnose.cfg\nsetup.cfg\nsetup.py\ntests/__init__.py\ntests/requirements.txt\ntests/test.py\ntox.ini\n<<<<<< network\n# path=/Users/peak/Documents/codecov/codecov-python/tests/coverage.xml\ncoverage data\n<<<<<< EOF\n# path=fixes\n\n\n\n\n\n\n\n\n<<<<<< EOF', |
| 155 | + headers={'Content-Type': 'plain/text', 'x-amz-acl': 'public-read'}) |
206 | 156 |
|
207 | 157 | def test_send_error(self):
|
208 |
| - with open(self.filepath, 'w+') as f: |
209 |
| - f.write('coverage data') |
210 |
| - try: |
211 |
| - self.run_cli(False, token='not-a-token', commit='a'*40, branch='master') |
212 |
| - except Exception: |
213 |
| - pass |
214 |
| - else: |
215 |
| - raise Exception('400 never raised') |
| 158 | + with patch('requests.post') as post: |
| 159 | + post.return_value = Mock(status_code=400, text='error') |
| 160 | + with open(self.filepath, 'w+') as f: |
| 161 | + f.write('coverage data') |
| 162 | + try: |
| 163 | + self.run_cli(False, token='not-a-token', commit='a'*40, branch='master') |
| 164 | + except Exception: |
| 165 | + pass |
| 166 | + else: |
| 167 | + raise Exception('400 never raised') |
216 | 168 |
|
217 | 169 | @data((dict(commit='sha'), 'Missing repository upload token'), )
|
218 | 170 | def test_require_branch(self, dd):
|
@@ -309,22 +261,22 @@ def test_bowerrc_none(self):
|
309 | 261 |
|
310 | 262 | def test_discovers(self):
|
311 | 263 | with open(self.jacoco, 'w+') as f:
|
312 |
| - f.write(jacoco_xml) |
| 264 | + f.write('<jacoco></jacoco>') |
313 | 265 | with open(self.filepath, 'w+') as f:
|
314 | 266 | f.write('coverage data')
|
315 | 267 | res = self.run_cli(**self.defaults)
|
316 | 268 | self.assertIn('coverage.xml', res['reports'])
|
317 | 269 | self.assertIn('coverage data', res['reports'])
|
318 | 270 | self.assertIn('jacoco.xml', res['reports'])
|
319 |
| - self.assertIn('org/jacoco/examples/maven/java/HelloWorld.java', res['reports']) |
| 271 | + self.assertIn('<jacoco></jacoco>', res['reports']) |
320 | 272 |
|
321 | 273 | def test_jacoco(self):
|
322 | 274 | with open(self.jacoco, 'w+') as f:
|
323 |
| - f.write(jacoco_xml) |
| 275 | + f.write('<jacoco></jacoco>') |
324 | 276 | res = self.run_cli(file='jacoco.xml', **self.defaults)
|
325 | 277 | report = res['reports'].split('<<<<<< network\n')[1].splitlines()
|
326 | 278 | self.assertEqual(report[0], '# path=jacoco.xml')
|
327 |
| - self.assertEqual(loads(report[1]), {"coverage": {"org/jacoco/examples/maven/java/HelloWorld.java": {"3": 3, "9": 2, "7": 0, "6": "1/2", "10": "2/2"}}}) |
| 279 | + self.assertEqual(report[1], '<jacoco></jacoco>') |
328 | 280 |
|
329 | 281 | def test_not_jacoco(self):
|
330 | 282 | with open(self.filepath, 'w+') as f:
|
@@ -457,6 +409,22 @@ def test_ci_circleci(self):
|
457 | 409 | self.assertEqual(res['query']['slug'], 'owner/repo')
|
458 | 410 | self.assertEqual(res['query']['branch'], 'master')
|
459 | 411 |
|
| 412 | + def test_ci_buildkite(self): |
| 413 | + self.set_env(CI='true', |
| 414 | + BUILDKITE='true', |
| 415 | + BUILDKITE_BUILD_NUMBER='57', |
| 416 | + BUILDKITE_JOB_ID='1', |
| 417 | + BUILDKITE_BRANCH='master', |
| 418 | + BUILDKITE_PROJECT_SLUG='owner/repo', |
| 419 | + BUILDKITE_COMMIT='d653b934ed59c1a785cc1cc79d08c9aaa4eba73b') |
| 420 | + self.fake_report() |
| 421 | + res = self.run_cli() |
| 422 | + self.assertEqual(res['query']['service'], 'buildkite') |
| 423 | + self.assertEqual(res['query']['commit'], 'd653b934ed59c1a785cc1cc79d08c9aaa4eba73b') |
| 424 | + self.assertEqual(res['query']['build'], '57.1') |
| 425 | + self.assertEqual(res['query']['slug'], 'owner/repo') |
| 426 | + self.assertEqual(res['query']['branch'], 'master') |
| 427 | + |
460 | 428 | def test_ci_semaphore(self):
|
461 | 429 | self.set_env(SEMAPHORE='true',
|
462 | 430 | BRANCH_NAME='master',
|
@@ -576,6 +544,8 @@ def test_ci_gitlab(self):
|
576 | 544 | CI_BUILD_REPO='https://gitlab.com/owner/repo.git',
|
577 | 545 | CI_SERVER_NAME='GitLab CI',
|
578 | 546 | CI_BUILD_REF='d653b934ed59c1a785cc1cc79d08c9aaa4eba73b',
|
| 547 | + HOME='/', |
| 548 | + CI_PROJECT_DIR=os.getcwd().strip('/'), |
579 | 549 | CODECOV_TOKEN='token')
|
580 | 550 | self.fake_report()
|
581 | 551 | res = self.run_cli()
|
|
0 commit comments