15
14
from utility import temp_dir
17
class FakePopen(object):
19
def __init__(self, code):
22
def communicate(self):
23
self.returncode = self._code
18
27
class gotesttarfileTestCase(TestCase):
20
29
def test_run_success(self):
22
with patch('subprocess.check_output', return_value='pass') as mock:
23
returncode, output = run('go', 'test', './...', env=env)
31
with patch('subprocess.Popen', autospec=True,
32
return_value=FakePopen(0)) as mock:
33
returncode = run(['go', 'test', './...'], env=env)
24
34
self.assertEqual(0, returncode)
25
self.assertEqual('pass', output)
26
args, kwargs = mock.call_args
27
self.assertEqual((('go', 'test', './...'), ), args)
28
self.assertIs(env, kwargs['env'])
29
self.assertIs(subprocess.STDOUT, kwargs['stderr'])
35
mock.assert_called_once_with(['go', 'test', './...'], env=env)
31
37
def test_run_fail(self):
33
e = subprocess.CalledProcessError(1, ['foo'], output='fail')
34
with patch('subprocess.check_output', side_effect=e):
35
returncode, output = run('go', 'test', './...', env=env)
39
with patch('subprocess.Popen', autospec=True,
40
return_value=FakePopen(1)) as mock:
41
returncode = run(['go', 'test', './...'], env=env)
36
42
self.assertEqual(1, returncode)
37
self.assertEqual('fail', output)
43
mock.assert_called_once_with(['go', 'test', './...'], env=env)
39
45
def test_untar_gopath(self):
40
46
with temp_dir() as base_dir:
56
62
package_path = os.path.join(
57
63
gopath, 'src', 'github.com', 'juju', 'juju')
58
64
os.makedirs(package_path)
59
with patch('gotesttarfile.run', return_value=[0, 'success'],
65
with patch('gotesttarfile.run', return_value=0,
60
66
autospec=True) as run_mock:
61
67
devnull = open(os.devnull, 'w')
62
68
with patch('sys.stdout', devnull):
63
69
returncode = go_test_package(
64
70
'github.com/juju/juju', 'go', gopath)
65
71
self.assertEqual(0, returncode)
66
args, kwargs = run_mock.call_args
67
self.assertEqual(('go', 'test', './...'), args)
72
self.assertEqual(run_mock.call_count, 2)
73
args, kwargs = run_mock.call_args_list[0]
74
self.assertEqual((['go', 'test', './...'],), args)
68
75
self.assertEqual('amd64', kwargs['env'].get('GOARCH'))
69
76
self.assertEqual(gopath, kwargs['env'].get('GOPATH'))
77
run_mock.assert_called_with(['sudo', 'killall', '-SIGABRT', 'mongod'])
71
79
def test_go_test_package_win32(self):
72
80
with temp_dir() as gopath:
73
81
package_path = os.path.join(
74
82
gopath, 'src', 'github.com', 'juju', 'juju')
75
83
os.makedirs(package_path)
76
with patch('gotesttarfile.run', return_value=[0, 'success'],
84
with patch('gotesttarfile.run', return_value=0,
77
85
autospec=True) as run_mock:
78
86
devnull = open(os.devnull, 'w')
79
87
with patch('sys.stdout', devnull):
83
91
returncode = go_test_package(
84
92
'github.com/juju/juju', 'go', gopath)
85
93
self.assertEqual(0, returncode)
86
args, kwargs = run_mock.call_args
94
args, kwargs = run_mock.call_args_list[0]
95
self.assertEqual(run_mock.call_count, 2)
88
('powershell.exe', '-Command', 'go', 'test', './...'),
97
(['powershell.exe', '-Command', 'go', 'test', './...'], ),
90
99
self.assertEqual(r'C:\foo;C:\baz', kwargs['env'].get('PATH'))
100
self.assertEqual(kwargs['env'].get('Path'), kwargs['env'].get('PATH'))
91
101
self.assertEqual(gopath, os.path.dirname(kwargs['env'].get('TMP')))
92
102
self.assertIn("tmp-juju-", os.path.basename(kwargs['env'].get('TMP')))
93
103
self.assertEqual(kwargs['env'].get('TEMP'), kwargs['env'].get('TMP'))
104
run_mock.assert_called_with(
105
['taskkill.exe', '/F', '/FI', 'imagename eq mongod.exe'])
95
107
def test_parse_args(self):
96
108
args = parse_args(