|
@@ -5,7 +5,9 @@
|
|
|
|
|
|
import logging
|
|
|
import os
|
|
|
+import shutil
|
|
|
import sys
|
|
|
+import tempfile
|
|
|
import unittest
|
|
|
from unittest import mock
|
|
|
|
|
@@ -41,6 +43,77 @@ class GitCacheTest(unittest.TestCase):
|
|
|
self.assertEqual(version, 'unknown')
|
|
|
|
|
|
|
|
|
+class ConfigDirTest(unittest.TestCase):
|
|
|
+
|
|
|
+ @mock.patch('sys.platform', 'win')
|
|
|
+ def testWin(self):
|
|
|
+ self.assertEqual(DEPOT_TOOLS_ROOT, utils.depot_tools_config_dir())
|
|
|
+
|
|
|
+ @mock.patch('sys.platform', 'darwin')
|
|
|
+ def testMac(self):
|
|
|
+ self.assertEqual(DEPOT_TOOLS_ROOT, utils.depot_tools_config_dir())
|
|
|
+
|
|
|
+ @mock.patch('sys.platform', 'foo')
|
|
|
+ def testOther(self):
|
|
|
+ self.assertEqual(DEPOT_TOOLS_ROOT, utils.depot_tools_config_dir())
|
|
|
+
|
|
|
+ @mock.patch('sys.platform', 'linux')
|
|
|
+ @mock.patch.dict('os.environ', {})
|
|
|
+ def testLinuxDefault(self):
|
|
|
+ self.assertEqual(
|
|
|
+ os.path.join(os.path.expanduser('~/.config'), 'depot_tools'),
|
|
|
+ utils.depot_tools_config_dir())
|
|
|
+
|
|
|
+ @mock.patch('sys.platform', 'linux')
|
|
|
+ @mock.patch.dict('os.environ', {'XDG_CONFIG_HOME': '/my/home'})
|
|
|
+ def testLinuxCustom(self):
|
|
|
+ self.assertEqual(os.path.join('/my/home', 'depot_tools'),
|
|
|
+ utils.depot_tools_config_dir())
|
|
|
+
|
|
|
+
|
|
|
+class ConfigPathTest(unittest.TestCase):
|
|
|
+
|
|
|
+ def setUp(self):
|
|
|
+ self.temp_dir = tempfile.mkdtemp(prefix='utils_test')
|
|
|
+ self.config_dir = os.path.join(self.temp_dir, 'test_files')
|
|
|
+
|
|
|
+ self.isfile = mock.Mock()
|
|
|
+ self.move = mock.Mock()
|
|
|
+
|
|
|
+ mock.patch('os.path.isfile', self.isfile).start()
|
|
|
+ mock.patch('shutil.move', self.move).start()
|
|
|
+ mock.patch('utils.depot_tools_config_dir',
|
|
|
+ lambda: self.config_dir).start()
|
|
|
+
|
|
|
+ self.addCleanup(mock.patch.stopall)
|
|
|
+ self.addCleanup(shutil.rmtree, self.temp_dir)
|
|
|
+
|
|
|
+ def testCreatesConfigDir(self):
|
|
|
+ # Ensure "legacy path" doesn't exist so that nothing gets moved.
|
|
|
+ def EnsureLegacyPathNotExists(path):
|
|
|
+ return path != os.path.join(DEPOT_TOOLS_ROOT, 'metrics.cfg')
|
|
|
+
|
|
|
+ self.isfile.side_effect = EnsureLegacyPathNotExists
|
|
|
+
|
|
|
+ self.assertEqual(os.path.join(self.config_dir, 'metrics.cfg'),
|
|
|
+ utils.depot_tools_config_path('metrics.cfg'))
|
|
|
+ self.assertTrue(os.path.exists(self.config_dir))
|
|
|
+ self.move.assert_not_called()
|
|
|
+
|
|
|
+ def testMovesLegacy(self):
|
|
|
+ # Ensure "legacy path" exists so that it gets moved.
|
|
|
+ def EnsureLegacyPathExists(path):
|
|
|
+ return path == os.path.join(DEPOT_TOOLS_ROOT, 'metrics.cfg')
|
|
|
+
|
|
|
+ self.isfile.side_effect = EnsureLegacyPathExists
|
|
|
+
|
|
|
+ self.assertEqual(os.path.join(self.config_dir, 'metrics.cfg'),
|
|
|
+ utils.depot_tools_config_path('metrics.cfg'))
|
|
|
+ self.move.assert_called_once_with(
|
|
|
+ os.path.join(DEPOT_TOOLS_ROOT, 'metrics.cfg'),
|
|
|
+ os.path.join(self.config_dir, 'metrics.cfg'))
|
|
|
+
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
logging.basicConfig(
|
|
|
level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
|