-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from betatim/config-file
[MRG] Add support for a local config file
- Loading branch information
Showing
4 changed files
with
139 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from unittest.mock import call | ||
from unittest.mock import Mock | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from osfclient.tests.mocks import MockArgs | ||
|
||
from osfclient import cli | ||
|
||
|
||
@patch('osfclient.cli.os.path.exists', return_value=True) | ||
@patch('osfclient.cli.configparser.ConfigParser') | ||
def test_config_file(MockConfigParser, os_path_exists): | ||
MockConfigParser().__getitem__ = Mock(return_value={'project': '1234'}) | ||
|
||
config = cli.config_from_file() | ||
|
||
assert config == {'project': '1234'} | ||
|
||
assert call.read('.osfcli.config') in MockConfigParser().mock_calls | ||
assert call('osf') in MockConfigParser().__getitem__.mock_calls | ||
|
||
|
||
def test_config_from_env_replace_username(): | ||
def simple_getenv(key): | ||
if key == 'OSF_USERNAME': | ||
return 'theusername' | ||
|
||
with patch('osfclient.cli.os.getenv', side_effect=simple_getenv): | ||
config = cli.config_from_env({'username': 'notusername'}) | ||
|
||
assert config == {'username': 'theusername'} | ||
|
||
|
||
def test_config_from_env_username(): | ||
def simple_getenv(key): | ||
if key == 'OSF_USERNAME': | ||
return None | ||
|
||
with patch('osfclient.cli.os.getenv', side_effect=simple_getenv): | ||
config = cli.config_from_env({'username': 'theusername'}) | ||
|
||
assert config == {'username': 'theusername'} | ||
|
||
|
||
def test_config_from_env_replace_project(): | ||
def simple_getenv(key): | ||
if key == 'OSF_PROJECT': | ||
return 'theproject' | ||
|
||
with patch('osfclient.cli.os.getenv', side_effect=simple_getenv): | ||
config = cli.config_from_env({'project': 'notproject'}) | ||
|
||
assert config == {'project': 'theproject'} | ||
|
||
|
||
def test_config_from_env_project(): | ||
def simple_getenv(key): | ||
if key == 'OSF_PROJECT': | ||
return None | ||
|
||
with patch('osfclient.cli.os.getenv', side_effect=simple_getenv): | ||
config = cli.config_from_env({'project': 'theproject'}) | ||
|
||
assert config == {'project': 'theproject'} | ||
|
||
|
||
def test_config_project(capsys): | ||
# No project in args or the config, should sys.exit(1) | ||
args = MockArgs(project=None) | ||
|
||
def simple_config(key): | ||
return {} | ||
|
||
with patch('osfclient.cli.config_from_env', side_effect=simple_config): | ||
with pytest.raises(SystemExit): | ||
cli._setup_osf(args) | ||
|
||
out, err = capsys.readouterr() | ||
expected = ('specify a project ID via the command line, configuration ' | ||
'file or environment variable') | ||
assert expected in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters