Skip to content

Commit

Permalink
Fix str/bytes error
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jan 21, 2020
1 parent 967419e commit 60a1fce
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
7 changes: 4 additions & 3 deletions ckan/lib/config_tool.py
@@ -1,6 +1,7 @@
# encoding: utf-8

from __future__ import print_function
import six
import re

INSERT_NEW_SECTIONS_BEFORE_SECTION = 'app:main'
Expand All @@ -23,7 +24,7 @@ def config_edit_using_merge_file(config_filepath, merge_config_filepath):
'''
# Read and parse the merge config filepath
with open(merge_config_filepath, 'rb') as f:
input_lines = [line.rstrip('\n') for line in f]
input_lines = [six.ensure_str(line).rstrip('\n') for line in f]
desired_options_dict = parse_config(input_lines)
desired_options = desired_options_dict.values()
# Make the changes
Expand All @@ -34,7 +35,7 @@ def config_edit(config_filepath, desired_options, edit=False):
'''Writes the desired_options to the config file.'''
# Read and parse the existing config file
with open(config_filepath, 'rb') as f:
input_lines = [line.rstrip('\n') for line in f]
input_lines = [six.ensure_str(line).rstrip('\n') for line in f]
existing_options_dict = parse_config(input_lines)
existing_options = existing_options_dict.values()

Expand All @@ -45,7 +46,7 @@ def config_edit(config_filepath, desired_options, edit=False):
# write the file with the changes
output = make_changes(input_lines, new_sections, changes)
with open(config_filepath, 'wb') as f:
f.write('\n'.join(output) + '\n')
f.write(six.ensure_binary('\n'.join(output) + '\n'))


def parse_option_string(section, option_string, raise_on_error=False):
Expand Down
4 changes: 3 additions & 1 deletion ckan/tests/cli/test_config_tool.py
Expand Up @@ -34,7 +34,9 @@ def test_config_unset_debug(cli, config_file):
assert _parse(config_file).get(u'app:main', u'debug') == u'true'
result = cli.invoke(
ckan,
[u'config-tool', str(config_file), u'debug=false'])
[u'config-tool', str(config_file), u'debug=false']

)
assert not result.exit_code
assert _parse(config_file).get(u'app:main', u'debug') == u'false'

Expand Down
9 changes: 9 additions & 0 deletions ckan/tests/helpers.py
Expand Up @@ -30,6 +30,7 @@

from flask.testing import Client as FlaskClient
from flask.wrappers import Response
from click.testing import CliRunner
import pytest
import mock
import rq
Expand Down Expand Up @@ -172,6 +173,14 @@ def body_contains(res, content):
return content in body


class CKANCliRunner(CliRunner):
def invoke(self, *args, **kwargs):
# prevent cli runner from str/bytes exceptions
kwargs.setdefault(u'complete_var', u'_CKAN_COMPLETE')
return super(CKANCliRunner, self).invoke(*args, **kwargs)



class CKANResponse(Response):
@property
def body(self):
Expand Down
17 changes: 10 additions & 7 deletions ckan/tests/pytest_ckan/fixtures.py
Expand Up @@ -28,16 +28,19 @@
"""

import pytest
import functools
import smtplib


from click.testing import CliRunner
import pytest
import six
import mock
import smtplib
import rq

import ckan.tests.helpers as test_helpers
import ckan.plugins
import ckan.lib.search as search
import rq

from ckan.common import config


Expand Down Expand Up @@ -110,16 +113,16 @@ def test_dataset_search(self, app):
def cli(ckan_config):
"""Provides object for invoking CLI commands from tests.
This is pure `click.testing.CliRunner`, so all examples from
`Click docs
This is subclass of `click.testing.CliRunner`, so all examples
from `Click docs
<https://click.palletsprojects.com/en/master/testing/>`_ are valid
for it.
"""
env = {
u'CKAN_INI': ckan_config[u'__file__']
}
return CliRunner(env=env)
return test_helpers.CKANCliRunner(env=env)


@pytest.fixture(scope=u"session")
Expand Down

0 comments on commit 60a1fce

Please sign in to comment.