diff --git a/tests/contrib/test_multistore_file.py b/tests/contrib/test_multistore_file.py index 5e1fe7711..5e1d29b9e 100644 --- a/tests/contrib/test_multistore_file.py +++ b/tests/contrib/test_multistore_file.py @@ -19,7 +19,7 @@ import os import stat import tempfile -import unittest +import unittest2 from oauth2client import util from oauth2client.client import OAuth2Credentials @@ -48,7 +48,7 @@ def filename(self): return self.filename_str -class MultistoreFileTests(unittest.TestCase): +class MultistoreFileTests(unittest2.TestCase): def tearDown(self): try: @@ -105,25 +105,25 @@ def test_read_only_file_fail_lock(self): ['some-scope', 'some-other-scope']) store.put(credentials) - if os.name == 'posix': + if os.name == 'posix': # pragma: NO COVER self.assertTrue(store._multistore._read_only) os.chmod(FILENAME, 0o600) + @unittest2.skipIf(not hasattr(os, 'symlink'), 'No symlink available') def test_multistore_no_symbolic_link_files(self): - if hasattr(os, 'symlink'): - SYMFILENAME = FILENAME + 'sym' - os.symlink(FILENAME, SYMFILENAME) - store = multistore_file.get_credential_storage( - SYMFILENAME, - 'some_client_id', - 'user-agent/1.0', - ['some-scope', 'some-other-scope']) - try: - self.assertRaises( - locked_file.CredentialsFileSymbolicLinkError, - store.get) - finally: - os.unlink(SYMFILENAME) + SYMFILENAME = FILENAME + 'sym' + os.symlink(FILENAME, SYMFILENAME) + store = multistore_file.get_credential_storage( + SYMFILENAME, + 'some_client_id', + 'user-agent/1.0', + ['some-scope', 'some-other-scope']) + try: + self.assertRaises( + locked_file.CredentialsFileSymbolicLinkError, + store.get) + finally: + os.unlink(SYMFILENAME) def test_multistore_non_existent_file(self): store = multistore_file.get_credential_storage( @@ -155,7 +155,7 @@ def test_multistore_file(self): self.assertEquals(None, credentials) - if os.name == 'posix': + if os.name == 'posix': # pragma: NO COVER self.assertEquals( 0o600, stat.S_IMODE(os.stat(FILENAME).st_mode)) diff --git a/tests/test_client.py b/tests/test_client.py index 9b1c81933..018727cde 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -305,33 +305,23 @@ def test_get_environment_variable_file_error(self): expected_err_msg): _get_environment_variable_file() + @mock.patch('os.name', new='nt') + @mock.patch.dict(os.environ, {'APPDATA': DATA_DIR}, clear=True) def test_get_well_known_file_on_windows(self): - ORIGINAL_ISDIR = os.path.isdir - try: - os.path.isdir = lambda path: True - well_known_file = datafile( - os.path.join(client._CLOUDSDK_CONFIG_DIRECTORY, - _WELL_KNOWN_CREDENTIALS_FILE)) - os.name = 'nt' - os.environ['APPDATA'] = DATA_DIR - self.assertEqual(well_known_file, _get_well_known_file()) - finally: - os.path.isdir = ORIGINAL_ISDIR - + well_known_file = datafile( + os.path.join(client._CLOUDSDK_CONFIG_DIRECTORY, + _WELL_KNOWN_CREDENTIALS_FILE)) + self.assertEqual(well_known_file, _get_well_known_file()) + + @mock.patch.dict(os.environ, + {client._CLOUDSDK_CONFIG_ENV_VAR: 'CUSTOM_DIR'}, + clear=True) def test_get_well_known_file_with_custom_config_dir(self): - ORIGINAL_ENVIRON = os.environ - ORIGINAL_ISDIR = os.path.isdir - CUSTOM_DIR = 'CUSTOM_DIR' + CUSTOM_DIR = os.environ[client._CLOUDSDK_CONFIG_ENV_VAR] EXPECTED_FILE = os.path.join(CUSTOM_DIR, _WELL_KNOWN_CREDENTIALS_FILE) - try: - os.environ = {client._CLOUDSDK_CONFIG_ENV_VAR: CUSTOM_DIR} - os.path.isdir = lambda path: True - well_known_file = _get_well_known_file() - self.assertEqual(well_known_file, EXPECTED_FILE) - finally: - os.environ = ORIGINAL_ENVIRON - os.path.isdir = ORIGINAL_ISDIR + well_known_file = _get_well_known_file() + self.assertEqual(well_known_file, EXPECTED_FILE) def test_get_adc_from_file_service_account(self): credentials_file = datafile( @@ -357,17 +347,16 @@ def test_save_to_well_known_file_service_account(self): self.assertEqual('ABCDEF', d['private_key_id']) os.remove(temp_credential_file) - def test_save_well_known_file_with_non_existent_config_dir(self): + @mock.patch('os.path.isdir', return_value=False) + def test_save_well_known_file_with_non_existent_config_dir(self, + isdir_mock): credential_file = datafile( os.path.join('gcloud', _WELL_KNOWN_CREDENTIALS_FILE)) credentials = _get_application_default_credential_from_file( credential_file) - ORIGINAL_ISDIR = os.path.isdir - try: - os.path.isdir = lambda path: False - self.assertRaises(OSError, save_to_well_known_file, credentials) - finally: - os.path.isdir = ORIGINAL_ISDIR + self.assertRaises(OSError, save_to_well_known_file, credentials) + config_dir = os.path.join(os.path.expanduser('~'), '.config', 'gcloud') + isdir_mock.assert_called_once_with(config_dir) def test_get_adc_from_file_authorized_user(self): credentials_file = datafile(os.path.join( diff --git a/tests/test_file.py b/tests/test_file.py index 7ecc6205f..4bf56bf72 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -79,16 +79,16 @@ def test_non_existent_file_storage(self): credentials = s.get() self.assertEquals(None, credentials) + @unittest2.skipIf(not hasattr(os, 'symlink'), 'No symlink available') def test_no_sym_link_credentials(self): - if hasattr(os, 'symlink'): - SYMFILENAME = FILENAME + '.sym' - os.symlink(FILENAME, SYMFILENAME) - s = file.Storage(SYMFILENAME) - try: - with self.assertRaises(file.CredentialsFileSymbolicLinkError): - s.get() - finally: - os.unlink(SYMFILENAME) + SYMFILENAME = FILENAME + '.sym' + os.symlink(FILENAME, SYMFILENAME) + s = file.Storage(SYMFILENAME) + try: + with self.assertRaises(file.CredentialsFileSymbolicLinkError): + s.get() + finally: + os.unlink(SYMFILENAME) def test_pickle_and_json_interop(self): # Write a file with a pickled OAuth2Credentials. @@ -177,7 +177,7 @@ def test_token_refresh_good_store(self): new_cred.access_token = 'bar' s.put(new_cred) - credentials._refresh(lambda x: x) + credentials._refresh(None) self.assertEquals(credentials.access_token, 'bar') def test_token_refresh_stream_body(self): @@ -238,7 +238,7 @@ def test_access_token_credentials(self): self.assertTrue(os.path.exists(FILENAME)) - if os.name == 'posix': + if os.name == 'posix': # pragma: NO COVER mode = os.stat(FILENAME).st_mode self.assertEquals('0o600', oct(stat.S_IMODE(mode)))