OLD | NEW |
| (Empty) |
1 # Copyright (C) 2011 Google Inc. All rights reserved. | |
2 # | |
3 # Redistribution and use in source and binary forms, with or without | |
4 # modification, are permitted provided that the following conditions are | |
5 # met: | |
6 # | |
7 # * Redistributions of source code must retain the above copyright | |
8 # notice, this list of conditions and the following disclaimer. | |
9 # * Redistributions in binary form must reproduce the above | |
10 # copyright notice, this list of conditions and the following disclaimer | |
11 # in the documentation and/or other materials provided with the | |
12 # distribution. | |
13 # * Neither the name of Google Inc. nor the names of its | |
14 # contributors may be used to endorse or promote products derived from | |
15 # this software without specific prior written permission. | |
16 # | |
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 import platform | |
30 import sys | |
31 import unittest | |
32 | |
33 from webkitpy.common.system.executive import Executive | |
34 from webkitpy.common.system.executive_mock import MockExecutive2 | |
35 from webkitpy.common.system.filesystem import FileSystem | |
36 from webkitpy.common.system.filesystem_mock import MockFileSystem | |
37 from webkitpy.common.system.platforminfo import PlatformInfo | |
38 | |
39 | |
40 def fake_sys(platform_str='darwin', windows_version_tuple=None): | |
41 | |
42 class FakeSysModule(object): | |
43 platform = platform_str | |
44 if windows_version_tuple: | |
45 getwindowsversion = lambda x: windows_version_tuple | |
46 | |
47 return FakeSysModule() | |
48 | |
49 | |
50 def fake_platform(mac_version_string='10.6.3', release_string='bar', linux_versi
on='trusty'): | |
51 | |
52 class FakePlatformModule(object): | |
53 | |
54 def mac_ver(self): | |
55 return tuple([mac_version_string, tuple(['', '', '']), 'i386']) | |
56 | |
57 def linux_distribution(self): | |
58 return tuple([None, None, linux_version]) | |
59 | |
60 def platform(self): | |
61 return 'foo' | |
62 | |
63 def release(self): | |
64 return release_string | |
65 | |
66 return FakePlatformModule() | |
67 | |
68 | |
69 def fake_executive(output=None): | |
70 if output: | |
71 return MockExecutive2(output=output) | |
72 return MockExecutive2(exception=SystemError) | |
73 | |
74 | |
75 class TestPlatformInfo(unittest.TestCase): | |
76 | |
77 def make_info(self, sys_module=None, platform_module=None, filesystem_module
=None, executive=None): | |
78 return PlatformInfo(sys_module or fake_sys(), platform_module or fake_pl
atform(), | |
79 filesystem_module or MockFileSystem(), executive or
fake_executive()) | |
80 | |
81 def test_real_code(self): | |
82 # This test makes sure the real (unmocked) code actually works. | |
83 info = PlatformInfo(sys, platform, FileSystem(), Executive()) | |
84 self.assertNotEquals(info.os_name, '') | |
85 self.assertNotEquals(info.os_version, '') | |
86 self.assertNotEquals(info.display_name(), '') | |
87 self.assertTrue(info.is_mac() or info.is_win() or info.is_linux() or inf
o.is_freebsd()) | |
88 self.assertIsNotNone(info.terminal_width()) | |
89 | |
90 if info.is_linux(): | |
91 self.assertIsNotNone(info.linux_distribution()) | |
92 | |
93 if info.is_mac(): | |
94 self.assertTrue(info.total_bytes_memory() > 0) | |
95 else: | |
96 self.assertIsNone(info.total_bytes_memory()) | |
97 | |
98 def test_os_name_and_wrappers(self): | |
99 info = self.make_info(fake_sys('linux2')) | |
100 self.assertTrue(info.is_linux()) | |
101 self.assertFalse(info.is_mac()) | |
102 self.assertFalse(info.is_win()) | |
103 self.assertFalse(info.is_freebsd()) | |
104 | |
105 info = self.make_info(fake_sys('linux3')) | |
106 self.assertTrue(info.is_linux()) | |
107 self.assertFalse(info.is_mac()) | |
108 self.assertFalse(info.is_win()) | |
109 self.assertFalse(info.is_freebsd()) | |
110 | |
111 info = self.make_info(fake_sys('darwin'), fake_platform('10.6.3')) | |
112 self.assertEqual(info.os_name, 'mac') | |
113 self.assertFalse(info.is_linux()) | |
114 self.assertTrue(info.is_mac()) | |
115 self.assertFalse(info.is_win()) | |
116 self.assertFalse(info.is_freebsd()) | |
117 | |
118 info = self.make_info(fake_sys('win32', tuple([6, 1, 7600]))) | |
119 self.assertEqual(info.os_name, 'win') | |
120 self.assertFalse(info.is_linux()) | |
121 self.assertFalse(info.is_mac()) | |
122 self.assertTrue(info.is_win()) | |
123 self.assertFalse(info.is_freebsd()) | |
124 | |
125 info = self.make_info(fake_sys('cygwin'), executive=fake_executive('6.1.
7600')) | |
126 self.assertEqual(info.os_name, 'win') | |
127 self.assertFalse(info.is_linux()) | |
128 self.assertFalse(info.is_mac()) | |
129 self.assertTrue(info.is_win()) | |
130 self.assertFalse(info.is_freebsd()) | |
131 | |
132 info = self.make_info(fake_sys('freebsd8')) | |
133 self.assertEqual(info.os_name, 'freebsd') | |
134 self.assertFalse(info.is_linux()) | |
135 self.assertFalse(info.is_mac()) | |
136 self.assertFalse(info.is_win()) | |
137 self.assertTrue(info.is_freebsd()) | |
138 | |
139 self.assertRaises(AssertionError, self.make_info, fake_sys('vms')) | |
140 | |
141 def test_os_version(self): | |
142 self.assertRaises(AssertionError, self.make_info, fake_sys('darwin'), fa
ke_platform('10.4.3')) | |
143 self.assertEqual(self.make_info(fake_sys('darwin'), fake_platform('10.9.
0')).os_version, 'mac10.9') | |
144 self.assertEqual(self.make_info(fake_sys('darwin'), fake_platform('10.10
.0')).os_version, 'mac10.10') | |
145 self.assertEqual(self.make_info(fake_sys('darwin'), fake_platform('10.11
.0')).os_version, 'mac10.11') | |
146 self.assertEqual(self.make_info(fake_sys('darwin'), fake_platform('10.12
.0')).os_version, 'future') | |
147 | |
148 self.assertEqual(self.make_info(fake_sys('linux2')).os_version, 'trusty'
) | |
149 info = self.make_info(fake_sys('linux2'), fake_platform(linux_version='u
topic')) | |
150 self.assertEqual(info.os_version, 'trusty') | |
151 | |
152 self.assertEqual(self.make_info(fake_sys('freebsd8'), fake_platform('',
'8.3-PRERELEASE')).os_version, '8.3-PRERELEASE') | |
153 self.assertEqual(self.make_info(fake_sys('freebsd9'), fake_platform('',
'9.0-RELEASE')).os_version, '9.0-RELEASE') | |
154 | |
155 self.assertRaises(AssertionError, self.make_info, fake_sys('win32', tupl
e([5, 0, 1234]))) | |
156 self.assertRaises(AssertionError, self.make_info, fake_sys('win32', tupl
e([6, 1, 1234]))) | |
157 self.assertEqual(self.make_info(fake_sys('win32', tuple([10, 1, 1234])))
.os_version, 'future') | |
158 self.assertEqual(self.make_info(fake_sys('win32', tuple([10, 0, 1234])))
.os_version, '10') | |
159 self.assertEqual(self.make_info(fake_sys('win32', tuple([6, 3, 1234]))).
os_version, '8.1') | |
160 self.assertEqual(self.make_info(fake_sys('win32', tuple([6, 2, 1234]))).
os_version, '8') | |
161 self.assertEqual(self.make_info(fake_sys('win32', tuple([6, 1, 7601]))).
os_version, '7sp1') | |
162 self.assertEqual(self.make_info(fake_sys('win32', tuple([6, 1, 7600]))).
os_version, '7sp0') | |
163 self.assertEqual(self.make_info(fake_sys('win32', tuple([6, 0, 1234]))).
os_version, 'vista') | |
164 self.assertEqual(self.make_info(fake_sys('win32', tuple([5, 1, 1234]))).
os_version, 'xp') | |
165 | |
166 self.assertRaises(AssertionError, self.make_info, fake_sys('win32'), | |
167 executive=fake_executive('5.0.1234')) | |
168 self.assertRaises(AssertionError, self.make_info, fake_sys('win32'), | |
169 executive=fake_executive('6.1.1234')) | |
170 self.assertEqual(self.make_info(fake_sys('cygwin'), executive=fake_execu
tive('10.1.1234')).os_version, 'future') | |
171 self.assertEqual(self.make_info(fake_sys('cygwin'), executive=fake_execu
tive('10.0.1234')).os_version, '10') | |
172 self.assertEqual(self.make_info(fake_sys('cygwin'), executive=fake_execu
tive('6.3.1234')).os_version, '8.1') | |
173 self.assertEqual(self.make_info(fake_sys('cygwin'), executive=fake_execu
tive('6.2.1234')).os_version, '8') | |
174 self.assertEqual(self.make_info(fake_sys('cygwin'), executive=fake_execu
tive('6.1.7601')).os_version, '7sp1') | |
175 self.assertEqual(self.make_info(fake_sys('cygwin'), executive=fake_execu
tive('6.1.7600')).os_version, '7sp0') | |
176 self.assertEqual(self.make_info(fake_sys('cygwin'), executive=fake_execu
tive('6.0.1234')).os_version, 'vista') | |
177 self.assertEqual(self.make_info(fake_sys('cygwin'), executive=fake_execu
tive('5.1.1234')).os_version, 'xp') | |
178 | |
179 def _assert_file_implies_linux_distribution(self, file, distribution): | |
180 info = self.make_info(sys_module=fake_sys('linux2'), filesystem_module=M
ockFileSystem({file: ''})) | |
181 self.assertEqual(info.linux_distribution(), distribution) | |
182 | |
183 def test_linux_distro_detection(self): | |
184 self._assert_file_implies_linux_distribution('/etc/arch-release', 'arch'
) | |
185 self._assert_file_implies_linux_distribution('/etc/debian_version', 'deb
ian') | |
186 self._assert_file_implies_linux_distribution('/etc/redhat-release', 'red
hat') | |
187 self._assert_file_implies_linux_distribution('/etc/mock-release', 'unkno
wn') | |
188 | |
189 info = self.make_info(fake_sys('cygwin'), executive=fake_executive('6.1.
7600')) | |
190 self.assertIsNone(info.linux_distribution()) | |
191 | |
192 def test_display_name(self): | |
193 info = self.make_info(fake_sys('darwin')) | |
194 self.assertNotEquals(info.display_name(), '') | |
195 | |
196 info = self.make_info(fake_sys('win32', tuple([6, 1, 7600]))) | |
197 self.assertNotEquals(info.display_name(), '') | |
198 | |
199 info = self.make_info(fake_sys('linux2')) | |
200 self.assertNotEquals(info.display_name(), '') | |
201 | |
202 info = self.make_info(fake_sys('freebsd9')) | |
203 self.assertNotEquals(info.display_name(), '') | |
204 | |
205 def test_total_bytes_memory(self): | |
206 info = self.make_info(fake_sys('darwin'), fake_platform('10.6.3'), execu
tive=fake_executive('1234')) | |
207 self.assertEqual(info.total_bytes_memory(), 1234) | |
208 | |
209 info = self.make_info(fake_sys('win32', tuple([6, 1, 7600]))) | |
210 self.assertIsNone(info.total_bytes_memory()) | |
211 | |
212 info = self.make_info(fake_sys('linux2')) | |
213 self.assertIsNone(info.total_bytes_memory()) | |
214 | |
215 info = self.make_info(fake_sys('freebsd9')) | |
216 self.assertIsNone(info.total_bytes_memory()) | |
OLD | NEW |