Getting to 100% line coverage in tests.

This commit is contained in:
Danny Hermes
2016-02-21 10:07:08 -08:00
parent c66e4f201f
commit cd0cbe311c
3 changed files with 47 additions and 58 deletions

View File

@@ -19,7 +19,7 @@ import errno
import os
import stat
import tempfile
import unittest
import unittest2
from oauth2client import util
from oauth2client.client import OAuth2Credentials
@@ -48,7 +48,7 @@ class _MockLockedFile(object):
return self.filename_str
class MultistoreFileTests(unittest.TestCase):
class MultistoreFileTests(unittest2.TestCase):
def tearDown(self):
try:
@@ -105,25 +105,25 @@ class MultistoreFileTests(unittest.TestCase):
['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 @@ class MultistoreFileTests(unittest.TestCase):
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))

View File

@@ -305,33 +305,23 @@ class GoogleCredentialsTests(unittest2.TestCase):
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 @@ class GoogleCredentialsTests(unittest2.TestCase):
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(

View File

@@ -79,16 +79,16 @@ class OAuth2ClientFileTests(unittest2.TestCase):
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 @@ class OAuth2ClientFileTests(unittest2.TestCase):
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 @@ class OAuth2ClientFileTests(unittest2.TestCase):
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)))