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'))
107 class TestGetosWithTempdir(TestCaseExtended
):
109 self
.tempdir
= tempfile
.mkdtemp("_sdktest")
110 self
.patch
= mock
.patch
.dict('os.environ',
111 {'NACL_SDK_ROOT': self
.tempdir
})
115 shutil
.rmtree(self
.tempdir
)
118 def testGetSDKVersion(self
):
119 """correctly parses README to find SDK version."""
120 expected_version
= (16, 196)
121 with
open(os
.path
.join(self
.tempdir
, 'README'), 'w') as out
:
122 out
.write('Version: %s\n' % expected_version
[0])
123 out
.write('Chrome Revision: %s\n' % expected_version
[1])
125 version
= getos
.GetSDKVersion()
126 self
.assertEqual(version
, expected_version
)
129 if __name__
== '__main__':