1# Copyright 2014 Dirk Pranke. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import sys
16
17from typ.tests import host_test
18from typ.fakes.host_fake import FakeHost, FakeResponse
19
20is_python3 = bool(sys.version_info.major == 3)
21
22if is_python3:  # pragma: python3
23    # redefining built-in 'unicode' pylint: disable=W0622
24    unicode = str
25
26class TestFakeHost(host_test.TestHost):
27
28    def host(self):
29        return FakeHost()
30
31    def test_add_to_path(self):
32        # TODO: FakeHost uses the real sys.path, and then gets
33        # confused becayse host.abspath() doesn't work right for
34        # windows-style paths.
35        if sys.platform != 'win32':
36            super(TestFakeHost, self).test_add_to_path()
37
38    def test_call(self):
39        h = self.host()
40        ret, out, err = h.call(['echo', 'hello, world'])
41        self.assertEqual(ret, 0)
42        self.assertEqual(out, '')
43        self.assertEqual(err, '')
44        self.assertEqual(h.cmds, [['echo', 'hello, world']])
45
46    def test_call_inline(self):
47        h = self.host()
48        ret = h.call_inline(['echo', 'hello, world'])
49        self.assertEqual(ret, 0)
50
51    def test_capture_output(self):
52        h = self.host()
53        self.host = lambda: h
54        super(TestFakeHost, self).test_capture_output()
55
56        # This tests that the super-method only tested the
57        # divert=True case, and things were diverted properly.
58        self.assertEqual(h.stdout.getvalue(), '')
59        self.assertEqual(h.stderr.getvalue(), '')
60
61        h.capture_output(divert=False)
62        h.print_('on stdout')
63        h.print_('on stderr', stream=h.stderr)
64        out, err = h.restore_output()
65        self.assertEqual(out, 'on stdout\n')
66        self.assertEqual(err, 'on stderr\n')
67        self.assertEqual(h.stdout.getvalue(), 'on stdout\n')
68        self.assertEqual(h.stderr.getvalue(), 'on stderr\n')
69
70    def test_for_mp(self):
71        h = self.host()
72        self.assertNotEqual(h.for_mp(), None)
73
74    def test_fetch(self):
75        h = self.host()
76        url = 'http://localhost/test'
77        resp = FakeResponse(unicode('foo'), url)
78        h.fetch_responses[url] = resp
79        actual_resp = h.fetch(url)
80        self.assertEqual(actual_resp.geturl(), url)
81        self.assertEqual(actual_resp.getcode(), 200)
82        self.assertEqual(resp, actual_resp)
83        self.assertEqual(h.fetches, [(url, None, None, actual_resp)])
84