2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
12 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
13 TOOLS_DIR
= os
.path
.dirname(SCRIPT_DIR
)
14 CHROME_SRC
= os
.path
.dirname(os
.path
.dirname(os
.path
.dirname(TOOLS_DIR
)))
15 MOCK_DIR
= os
.path
.join(CHROME_SRC
, 'third_party', 'pymock')
17 # For the mock library
18 sys
.path
.append(MOCK_DIR
)
21 # For getos, the module under test
22 sys
.path
.append(TOOLS_DIR
)
27 class TestCaseExtended(unittest
.TestCase
):
28 """Monkey patch some 2.7-only TestCase features."""
29 # TODO(sbc): remove this once we switch to python2.7 everywhere
30 def assertIn(self
, expr1
, expr2
, msg
=None):
31 if hasattr(super(TestCaseExtended
, self
), 'assertIn'):
32 return super(TestCaseExtended
, self
).assertIn(expr1
, expr2
, msg
)
33 if expr1
not in expr2
:
34 self
.fail(msg
or '%r not in %r' % (expr1
, expr2
))
37 class TestGetos(TestCaseExtended
):
39 self
.patch1
= mock
.patch
.dict('os.environ',
40 {'NACL_SDK_ROOT': os
.path
.dirname(TOOLS_DIR
)})
42 self
.patch2
= mock
.patch
.object(oshelpers
, 'FindExeInPath',
43 return_value
='/bin/ls')
50 def testGetSDKPath(self
):
51 """honors environment variable."""
52 with mock
.patch
.dict('os.environ', {'NACL_SDK_ROOT': 'dummy'}):
53 self
.assertEqual(getos
.GetSDKPath(), 'dummy')
55 def testGetSDKPathDefault(self
):
56 """defaults to relative path."""
57 del os
.environ
['NACL_SDK_ROOT']
58 self
.assertEqual(getos
.GetSDKPath(), os
.path
.dirname(TOOLS_DIR
))
60 def testGetPlatform(self
):
61 """returns a valid platform."""
62 platform
= getos
.GetPlatform()
63 self
.assertIn(platform
, ('mac', 'linux', 'win'))
65 def testGetSystemArch(self
):
66 """returns a valid architecture."""
67 arch
= getos
.GetSystemArch(getos
.GetPlatform())
68 self
.assertIn(arch
, ('x86_64', 'x86_32', 'arm'))
70 def testGetChromePathEnv(self
):
71 """honors CHROME_PATH environment."""
72 with mock
.patch
.dict('os.environ', {'CHROME_PATH': '/dummy/file'}):
73 expect
= "Invalid CHROME_PATH.*/dummy/file"
74 platform
= getos
.GetPlatform()
75 if hasattr(self
, 'assertRaisesRegexp'):
76 with self
.assertRaisesRegexp(getos
.Error
, expect
):
77 getos
.GetChromePath(platform
)
79 # TODO(sbc): remove this path once we switch to python2.7 everywhere
80 self
.assertRaises(getos
.Error
, getos
.GetChromePath
, platform
)
82 def testGetChromePathCheckExists(self
):
83 """checks that existence of explicitly CHROME_PATH is checked."""
84 mock_location
= '/bin/ls'
85 platform
= getos
.GetPlatform()
87 mock_location
= 'c:\\nowhere'
88 with mock
.patch
.dict('os.environ', {'CHROME_PATH': mock_location
}):
89 with mock
.patch('os.path.exists') as mock_exists
:
90 chrome
= getos
.GetChromePath(platform
)
91 self
.assertEqual(chrome
, mock_location
)
92 mock_exists
.assert_called_with(chrome
)
94 def testGetNaClArch(self
):
95 """returns a valid architecture."""
96 platform
= getos
.GetPlatform()
97 # Since the unix implementation of GetNaClArch will run objdump on the
98 # chrome binary, and we want to be able to run this test without chrome
99 # installed we mock the GetChromePath call to return a known system binary,
100 # which objdump will work with.
101 with mock
.patch('getos.GetChromePath') as mock_chrome_path
:
102 mock_chrome_path
.return_value
= '/bin/ls'
103 arch
= getos
.GetNaClArch(platform
)
104 self
.assertIn(arch
, ('x86_64', 'x86_32', 'arm'))
106 def testMainInvalidArgs(self
):
107 with self
.assertRaises(SystemExit):
108 with mock
.patch('sys.stderr'):
111 @mock.patch('sys.stdout', mock
.Mock())
112 @mock.patch('getos.GetPlatform')
113 def testMainNoArgs(self
, mock_get_platform
):
114 mock_get_platform
.return_value
= 'platform'
117 @mock.patch('sys.stdout', mock
.Mock())
118 @mock.patch('getos.GetSystemArch')
119 def testMainArgsParsing(self
, mock_system_arch
):
120 mock_system_arch
.return_value
= 'dummy'
121 getos
.main(['--arch'])
122 mock_system_arch
.assert_called()
125 class TestGetosWithTempdir(TestCaseExtended
):
127 self
.tempdir
= tempfile
.mkdtemp("_sdktest")
128 self
.patch
= mock
.patch
.dict('os.environ',
129 {'NACL_SDK_ROOT': self
.tempdir
})
133 shutil
.rmtree(self
.tempdir
)
136 def testGetSDKVersion(self
):
137 """correctly parses README to find SDK version."""
138 expected_version
= (16, 100, 'f00baacabba6e-refs/heads/master@{#100}')
139 with
open(os
.path
.join(self
.tempdir
, 'README'), 'w') as out
:
140 out
.write('Version: %d\n' % expected_version
[0])
141 out
.write('Chrome Revision: %d\n' % expected_version
[1])
142 out
.write('Chrome Commit Position: %s\n' % expected_version
[2])
144 version
= getos
.GetSDKVersion()
145 self
.assertEqual(version
, expected_version
)
147 def testParseVersion(self
):
148 """correctly parses a version given to --check-version."""
149 check_version_string
= '15.100'
150 self
.assertEquals((15, 100), getos
.ParseVersion(check_version_string
))
152 def testCheckVersion(self
):
153 """correctly rejects SDK versions earlier than the required one."""
154 actual_version
= (16, 100, 'f00baacabba6e-refs/heads/master@{#100}')
155 with
open(os
.path
.join(self
.tempdir
, 'README'), 'w') as out
:
156 out
.write('Version: %d\n' % actual_version
[0])
157 out
.write('Chrome Revision: %d\n' % actual_version
[1])
158 out
.write('Chrome Commit Position: %s\n' % actual_version
[2])
160 required_version
= (15, 150)
161 getos
.CheckVersion(required_version
)
163 required_version
= (16, 99)
164 getos
.CheckVersion(required_version
)
166 required_version
= (16, 100)
167 getos
.CheckVersion(required_version
)
169 required_version
= (16, 101)
170 self
.assertRaisesRegexp(
172 r
'SDK version too old \(current: 16.100, required: 16.101\)',
176 required_version
= (17, 50)
177 self
.assertRaisesRegexp(
179 r
'SDK version too old \(current: 16.100, required: 17.50\)',
184 if __name__
== '__main__':