def test_list_withFile_filesDoesNotExist(self, mock_error, mock_exists, mock_validateconfig):

        # given
        unknown_file = 'unknown_file'

        # when
        try:
            settings.list_(config='file', file_=unknown_file)
            self.fail('expected to exit but did not')  # pragma: no cover
        except SystemExit:
            pass

        # then
        mock_validateconfig.assert_called_once()
        mock_error.assert_called_once_with('no such file {!r}'.format(unknown_file))
    def test_list_format_pretty(self, mock_checkoutput,
                                mock_prettyformatconfig,
                                mock_resolveconfigoption, mock_validateconfig):

        # given
        mock_resolveconfigoption.return_value = None
        config_values = [
            'section.keys.key1=value1', 'section.keys.key2=value2',
            'sec.key=value'
        ]
        mock_checkoutput.return_value = '\x00'.join(config_values).replace(
            '=', os.linesep) + '\x00'
        format_result = ['formatted results']
        mock_prettyformatconfig.return_value = format_result

        # when
        pretty_output = settings.list_(format_=settings.FormatOption.PRETTY)

        # then
        self.assertEqual(pretty_output, format_result[0])

        mock_validateconfig.assert_called_once()
        mock_resolveconfigoption.assert_called_once_with(None)
        mock_checkoutput.assert_called_once_with(
            ['git', 'config', '--list', '--null'])
    def test_list_withFile_filesDoesNotExist(self, mock_error, mock_exists,
                                             mock_validateconfig):

        # given
        unknown_file = 'unknown_file'

        # when
        try:
            settings.list_(config=unknown_file)
            self.fail('expected to exit but did not')  # pragma: no cover
        except SystemExit:
            pass

        # then
        mock_validateconfig.assert_called_once()
        mock_error.assert_called_once_with(
            'no such file {!r}'.format(unknown_file))
    def test_list_noConfigsFound(self, mock_stdout, mock_validateconfig):

        # given
        mock_stdout.return_value = ''

        # when
        actual_values = settings.list_(config='system')

        # then
        self.assertFalse(actual_values)
        mock_validateconfig.assert_called_once()
        mock_stdout.assert_called_once_with(['git', 'config', '--list', '--null', '--system'])
    def test_list_keysOnly(self, mock_checkoutput, mock_validateconfig):

        # given
        config_values = ['section.key1=value1', 'section.key2=value2']
        mock_checkoutput.return_value = '\x00'.join(config_values).replace('=', os.linesep) + '\x00'

        # when
        actual_values = settings.list_(keys=True)

        # then
        self.assertEqual(sorted(actual_values.splitlines()), ['key1', 'key2'])
        mock_validateconfig.assert_called_once()
        mock_checkoutput.assert_called_once_with(['git', 'config', '--list', '--null'])
    def test_list_count(self, mock_checkoutput, mock_validateconfig):

        # given
        config_values = ['section.key1=value1', 'section.key2=value2']
        mock_checkoutput.return_value = '\x00'.join(config_values).replace('=', os.linesep) + '\x00'

        # when
        actual_count = settings.list_(count=True)

        # then
        self.assertEqual(actual_count, '2')
        mock_validateconfig.assert_called_once()
        mock_checkoutput.assert_called_once_with(['git', 'config', '--list', '--null'])
    def test_list_withConfig(self, mock_stdout, mock_validateconfig):

        # given
        config_values = ['section.key1=value1', 'section.key2=value2']
        mock_stdout.return_value = '\x00'.join(config_values).replace('=', os.linesep) + '\x00'

        # when
        actual_values = settings.list_(config='global')

        # then
        self.assertEqual(sorted(actual_values.splitlines()), config_values)
        mock_validateconfig.assert_called_once()
        mock_stdout.assert_called_once_with(['git', 'config', '--list', '--null', '--global'])
    def test_list_prettyFormat(self, mock_checkoutput, mock_prettyformatconfig, mock_validateconfig):

        # given
        config_values = ['section.keys.key1=value1', 'section.keys.key2=value2', 'sec.key=value']
        mock_checkoutput.return_value = '\x00'.join(config_values).replace('=', os.linesep) + '\x00'
        format_result = ['formatted results']
        mock_prettyformatconfig.return_value = format_result

        # when
        pretty_output = settings.list_(format_='pretty')

        # then
        self.assertEqual(pretty_output, format_result[0])
        mock_validateconfig.assert_called_once()
        mock_checkoutput.assert_called_once_with(['git', 'config', '--list', '--null'])
    def test_list_withFile(self, mock_checkoutput, mock_exists, mock_validateconfig):

        # given
        file_path = '/file/path'
        config_values = ['section.key1=value1', 'section.key2=value2']
        mock_checkoutput.return_value = '\x00'.join(config_values).replace('=', os.linesep) + '\x00'

        # when
        actual_values = settings.list_(config='file', file_=file_path)

        # then
        self.assertEqual(sorted(actual_values.splitlines()), config_values)
        mock_validateconfig.assert_called_once()
        mock_exists.assert_called_once_with(file_path)
        mock_checkoutput.assert_called_once_with(['git', 'config', '--list', '--null', '--file', file_path])
    def test_list_noConfigsFound(self, mock_stdout, mock_resolveconfigoption,
                                 mock_validateconfig):

        # given
        mock_resolveconfigoption.return_value = git.ConfigOption.SYSTEM
        mock_stdout.return_value = ''

        # when
        actual_values = settings.list_(config='system')

        # then
        self.assertFalse(actual_values)

        mock_validateconfig.assert_called_once()
        mock_resolveconfigoption.assert_called_once_with('system')
        mock_stdout.assert_called_once_with(
            ['git', 'config', '--list', '--null', '--system'])
    def test_list(self, mock_checkoutput, mock_resolveconfigoption,
                  mock_validateconfig):

        # given
        mock_resolveconfigoption.return_value = None
        config_values = ['section.key1=value1', 'section.key2=value2']
        mock_checkoutput.return_value = '\x00'.join(config_values).replace(
            '=', os.linesep) + '\x00'

        # when
        actual_values = settings.list_()

        # then
        self.assertEqual(sorted(actual_values.splitlines()), config_values)

        mock_validateconfig.assert_called_once()
        mock_resolveconfigoption.assert_called_once_with(None)
        mock_checkoutput.assert_called_once_with(
            ['git', 'config', '--list', '--null'])
    def test_list_format_count(self, mock_checkoutput,
                               mock_resolveconfigoption, mock_validateconfig):

        # given
        mock_resolveconfigoption.return_value = None
        config_values = ['section.key1=value1', 'section.key2=value2']
        mock_checkoutput.return_value = '\x00'.join(config_values).replace(
            '=', os.linesep) + '\x00'

        # when
        actual_count = settings.list_(format_=settings.FormatOption.COUNT)

        # then
        self.assertEqual(actual_count, '2')

        mock_validateconfig.assert_called_once()
        mock_resolveconfigoption.assert_called_once_with(None)
        mock_checkoutput.assert_called_once_with(
            ['git', 'config', '--list', '--null'])
    def test_list_withConfig(self, mock_stdout, mock_resolveconfigoption,
                             mock_validateconfig):

        # given
        mock_resolveconfigoption.return_value = git.ConfigOption.GLOBAL
        config_values = ['section.key1=value1', 'section.key2=value2']
        mock_stdout.return_value = '\x00'.join(config_values).replace(
            '=', os.linesep) + '\x00'

        # when
        actual_values = settings.list_(config='global')

        # then
        self.assertEqual(sorted(actual_values.splitlines()), config_values)

        mock_validateconfig.assert_called_once()
        mock_resolveconfigoption.assert_called_once_with('global')
        mock_stdout.assert_called_once_with(
            ['git', 'config', '--list', '--null', '--global'])
    def test_list_withFile(self, mock_checkoutput, mock_exists,
                           mock_resolveconfigoption, mock_validateconfig):

        # given
        file_path = '/file/path'
        mock_resolveconfigoption.return_value = file_path
        config_values = ['section.key1=value1', 'section.key2=value2']
        mock_checkoutput.return_value = '\x00'.join(config_values).replace(
            '=', os.linesep) + '\x00'

        # when
        actual_values = settings.list_(config=file_path)

        # then
        self.assertEqual(sorted(actual_values.splitlines()), config_values)

        mock_validateconfig.assert_called_once()
        mock_resolveconfigoption.assert_called_once_with(file_path)
        mock_exists.assert_called_once_with(file_path)
        mock_checkoutput.assert_called_once_with(
            ['git', 'config', '--list', '--null', '--file', file_path])