Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #431 from dhermes/test-cover-100
Browse files Browse the repository at this point in the history
100% line coverage in tests.
  • Loading branch information
nathanielmanistaatgoogle committed Feb 22, 2016
2 parents c66e4f2 + cd0cbe3 commit 9f89019
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 59 deletions.
36 changes: 18 additions & 18 deletions tests/contrib/test_multistore_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import os
import stat
import tempfile
import unittest
import unittest2

from oauth2client import util
from oauth2client.client import OAuth2Credentials
Expand Down Expand Up @@ -48,7 +48,7 @@ def filename(self):
return self.filename_str


class MultistoreFileTests(unittest.TestCase):
class MultistoreFileTests(unittest2.TestCase):

def tearDown(self):
try:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))

Expand Down
49 changes: 19 additions & 30 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
22 changes: 11 additions & 11 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)))

Expand Down

0 comments on commit 9f89019

Please sign in to comment.