| 1 |
# Copyright 2016 Facundo Batista, Nicolás Demarchi |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify it |
| 4 |
# under the terms of the GNU General Public License version 3, as published |
| 5 |
# by the Free Software Foundation. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, but |
| 8 |
# WITHOUT ANY WARRANTY; without even the implied warranties of |
| 9 |
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 10 |
# PURPOSE. See the GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License along |
| 13 |
# with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 |
# |
| 15 |
# For further info, check https://github.com/PyAr/fades |
| 16 |
|
| 17 |
"""Tests for file_options.""" |
| 18 |
|
| 19 |
import argparse |
| 20 |
import unittest |
| 21 |
from configparser import ConfigParser |
| 22 |
from unittest.mock import patch |
| 23 |
|
| 24 |
from fades import file_options |
| 25 |
|
| 26 |
|
| 27 |
class OptionsFileTestCase(unittest.TestCase): |
| 28 |
"""Check file_options.options_from_file().""" |
| 29 |
|
| 30 |
def setUp(self): |
| 31 |
self.argparser = argparse.ArgumentParser() |
| 32 |
self.argparser.add_argument |
| 33 |
self.argparser.add_argument('-f', '--foo', action='store_true') |
| 34 |
self.argparser.add_argument('-b', '--bar', action='store') |
| 35 |
self.argparser.add_argument('-d', '--dependency', action='append') |
| 36 |
self.argparser.add_argument('positional', nargs='?', default=None) |
| 37 |
|
| 38 |
def build_parser(self, args): |
| 39 |
config_parser = ConfigParser() |
| 40 |
config_parser['fades'] = args |
| 41 |
return config_parser |
| 42 |
|
| 43 |
@patch("fades.file_options.CONFIG_FILES", ('/foo/none', '/dev/null')) |
| 44 |
def test_no_config_files(self): |
| 45 |
args = self.argparser.parse_args([]) |
| 46 |
result = file_options.options_from_file(args) |
| 47 |
|
| 48 |
self.assertEqual(args, result) |
| 49 |
self.assertIsInstance(args, argparse.Namespace) |
| 50 |
|
| 51 |
@patch("fades.file_options.CONFIG_FILES", ('mock.ini',)) |
| 52 |
@patch("configparser.ConfigParser.items") |
| 53 |
def test_single_config_file_no_cli(self, mocked_parser): |
| 54 |
mocked_parser.return_value = [('foo', 'true'), ('bar', 'hux')] |
| 55 |
args = self.argparser.parse_args(['positional']) |
| 56 |
result = file_options.options_from_file(args) |
| 57 |
|
| 58 |
self.assertTrue(result.foo) |
| 59 |
self.assertEqual(result.bar, 'hux') |
| 60 |
self.assertIsInstance(args, argparse.Namespace) |
| 61 |
|
| 62 |
@patch("fades.file_options.CONFIG_FILES", ('mock.ini',)) |
| 63 |
@patch("configparser.ConfigParser.items") |
| 64 |
def test_single_config_file_with_cli(self, mocked_parser): |
| 65 |
mocked_parser.return_value = [('foo', 'false'), ('bar', 'hux'), ('no_in_cli', 'testing')] |
| 66 |
args = self.argparser.parse_args(['--foo', '--bar', 'other', 'positional']) |
| 67 |
result = file_options.options_from_file(args) |
| 68 |
|
| 69 |
self.assertTrue(result.foo) |
| 70 |
self.assertEqual(result.bar, 'other') |
| 71 |
self.assertEqual(result.no_in_cli, 'testing') |
| 72 |
self.assertIsInstance(args, argparse.Namespace) |
| 73 |
|
| 74 |
@patch("fades.file_options.CONFIG_FILES", ('mock.ini',)) |
| 75 |
@patch("configparser.ConfigParser.items") |
| 76 |
def test_single_config_file_with_mergeable(self, mocked_parser): |
| 77 |
mocked_parser.return_value = [('dependency', 'two')] |
| 78 |
args = self.argparser.parse_args( |
| 79 |
['--foo', '--bar', 'other', '--dependency', 'one', 'positional']) |
| 80 |
result = file_options.options_from_file(args) |
| 81 |
|
| 82 |
self.assertTrue(result.foo) |
| 83 |
self.assertEqual(result.bar, 'other') |
| 84 |
self.assertEqual(result.dependency, ['one', 'two']) |
| 85 |
self.assertIsInstance(args, argparse.Namespace) |
| 86 |
|
| 87 |
@patch("fades.file_options.CONFIG_FILES", ('mock.ini',)) |
| 88 |
@patch("configparser.ConfigParser.items") |
| 89 |
def test_single_config_file_complex_mergeable(self, mocked_parser): |
| 90 |
mocked_parser.return_value = [('dependency', 'requests>=2.1,<2.8,!=2.6.5')] |
| 91 |
args = self.argparser.parse_args( |
| 92 |
['--foo', '--bar', 'other', '--dependency', 'one', 'positional']) |
| 93 |
result = file_options.options_from_file(args) |
| 94 |
|
| 95 |
self.assertTrue(result.foo) |
| 96 |
self.assertEqual(result.bar, 'other') |
| 97 |
self.assertEqual(result.dependency, ['one', 'requests>=2.1,<2.8,!=2.6.5']) |
| 98 |
self.assertIsInstance(args, argparse.Namespace) |
| 99 |
|
| 100 |
@patch("fades.file_options.CONFIG_FILES", ('mock.ini', 'mock2.ini')) |
| 101 |
@patch("configparser.ConfigParser.items") |
| 102 |
def test_two_config_file_with_mergeable(self, mocked_parser): |
| 103 |
mocked_parser.side_effect = [ |
| 104 |
[('dependency', 'two')], |
| 105 |
[('dependency', 'three')], |
| 106 |
] |
| 107 |
args = self.argparser.parse_args( |
| 108 |
['--foo', '--bar', 'other', '--dependency', 'one', 'positional']) |
| 109 |
result = file_options.options_from_file(args) |
| 110 |
|
| 111 |
self.assertTrue(result.foo) |
| 112 |
self.assertEqual(result.bar, 'other') |
| 113 |
self.assertEqual(result.dependency, ['one', 'two', 'three']) |
| 114 |
self.assertIsInstance(args, argparse.Namespace) |
| 115 |
|
| 116 |
@patch("fades.file_options.CONFIG_FILES", ('mock.ini', 'mock2.ini')) |
| 117 |
@patch("configparser.ConfigParser.items") |
| 118 |
def test_two_config_file_with_booleans(self, mocked_parser): |
| 119 |
mocked_parser.side_effect = [ |
| 120 |
[('foo', 'true')], |
| 121 |
[('foo', 'false')], |
| 122 |
] |
| 123 |
args = self.argparser.parse_args([]) |
| 124 |
result = file_options.options_from_file(args) |
| 125 |
|
| 126 |
self.assertFalse(result.foo) |
| 127 |
self.assertIsInstance(args, argparse.Namespace) |
| 128 |
|
| 129 |
@patch("fades.file_options.CONFIG_FILES", ('mock.ini', 'mock2.ini')) |
| 130 |
@patch("configparser.ConfigParser.items") |
| 131 |
def test_two_config_file_override_by_cli(self, mocked_parser): |
| 132 |
mocked_parser.side_effect = [ |
| 133 |
[('bar', 'no_this')], |
| 134 |
[('bar', 'no_this_b')], |
| 135 |
] |
| 136 |
args = self.argparser.parse_args(['--bar', 'this']) |
| 137 |
result = file_options.options_from_file(args) |
| 138 |
|
| 139 |
self.assertEqual(result.bar, 'this') |
| 140 |
self.assertIsInstance(args, argparse.Namespace) |
| 141 |
|
| 142 |
@patch("fades.file_options.CONFIG_FILES", ('mock.ini', 'mock2.ini', 'mock3.ini')) |
| 143 |
@patch("configparser.ConfigParser.items") |
| 144 |
def test_three_config_file_override(self, mocked_parser): |
| 145 |
mocked_parser.side_effect = [ |
| 146 |
[('bar', 'no_this')], |
| 147 |
[('bar', 'neither_this')], |
| 148 |
[('bar', 'this')], |
| 149 |
] |
| 150 |
args = self.argparser.parse_args([]) |
| 151 |
result = file_options.options_from_file(args) |
| 152 |
|
| 153 |
self.assertEqual(result.bar, 'this') |
| 154 |
self.assertIsInstance(args, argparse.Namespace) |