1#!/usr/bin/env python
2# Copyright 2015 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.
5
6# pylint: disable=protected-access
7
8import logging
9import sys
10import unittest
11
12from devil import devil_env
13
14_sys_path_before = list(sys.path)
15with devil_env.SysPath(devil_env.PYMOCK_PATH):
16  _sys_path_with_pymock = list(sys.path)
17  import mock  # pylint: disable=import-error
18_sys_path_after = list(sys.path)
19
20
21class DevilEnvTest(unittest.TestCase):
22
23  def testSysPath(self):
24    self.assertEquals(_sys_path_before, _sys_path_after)
25    self.assertEquals(
26        _sys_path_before + [devil_env.PYMOCK_PATH],
27        _sys_path_with_pymock)
28
29  def testGetEnvironmentVariableConfig_configType(self):
30    with mock.patch('os.environ.get',
31                    mock.Mock(side_effect=lambda _env_var: None)):
32      env_config = devil_env._GetEnvironmentVariableConfig()
33    self.assertEquals('BaseConfig', env_config.get('config_type'))
34
35  def testGetEnvironmentVariableConfig_noEnv(self):
36    with mock.patch('os.environ.get',
37                    mock.Mock(side_effect=lambda _env_var: None)):
38      env_config = devil_env._GetEnvironmentVariableConfig()
39    self.assertEquals({}, env_config.get('dependencies'))
40
41  def testGetEnvironmentVariableConfig_adbPath(self):
42    def mock_environment(env_var):
43      return '/my/fake/adb/path' if env_var == 'ADB_PATH' else None
44
45    with mock.patch('os.environ.get',
46                    mock.Mock(side_effect=mock_environment)):
47      env_config = devil_env._GetEnvironmentVariableConfig()
48    self.assertEquals(
49        {
50          'adb': {
51            'file_info': {
52              'linux2_x86_64': {
53                'local_paths': ['/my/fake/adb/path'],
54              },
55            },
56          },
57        },
58        env_config.get('dependencies'))
59
60
61if __name__ == '__main__':
62  logging.getLogger().setLevel(logging.DEBUG)
63  unittest.main(verbosity=2)
64