Skip to content
Permalink
Browse files

Backporting to 2.7 fixing bug on ignoring line

  • Loading branch information
mavcunha committed Nov 27, 2019
1 parent 343c3b2 commit 41df138817682abf925795f8ea137c4495b3059a
Showing with 22 additions and 18 deletions.
  1. +5 −5 parse_config.py
  2. +2 −2 passenger_wsgi.py
  3. +11 −5 test_parse_config.py
  4. +4 −6 test_passenger_wsgi.py
@@ -1,27 +1,27 @@

def lines(file_name):
try:
with open(file_name) as file:
for line in file:
with open(file_name) as config:
for line in config:
if not ignore(line):
yield line.strip()
except OSError:
raise ValueError('File {file} not found'.format(file=file_name))


def ignore(line: str):
def ignore(line):
ls = line.strip()
return (not ls
or ls.startswith('#')
or len(ls.split()) <= 2)
or len(ls.split()) < 2)


def break_line(line):
tokens = line.split()
return tokens[-1], tokens[:-1]


def list_to_keys(value, keys: list):
def list_to_keys(value, keys):
return {key: value for key in keys}


@@ -1,12 +1,12 @@
from parse_config import parse


def valid_key(path: str):
def valid_key(path):
# will consider a valid path if has no slashes and is not None
return path is not None and not path.count('/') and path.isalnum()


def clean_path(path: str):
def clean_path(path):
return path.strip('/') if path else None


@@ -1,5 +1,5 @@
from unittest import TestCase
from unittest.mock import patch, mock_open
import unittest
from mock import patch, mock_open

import parse_config

@@ -12,13 +12,14 @@
# quid and pro should map to quo
quid pro quo
this for that
simple result
'''


class TestParseConfig(TestCase):
class TestParseConfig(unittest.TestCase):

def setUp(self) -> None:
patcher = patch("builtins.open", mock_open(read_data=DEFAULT_CFG))
def setUp(self):
patcher = patch("__builtin__.open", mock_open(read_data=DEFAULT_CFG))
self.addCleanup(patcher.stop)
self.mock_open = patcher.start()
self.urls = parse_config.parse()
@@ -68,3 +69,8 @@ def test_list_to_keys(self):
def test_parse_config(self):
self.assertEqual('quo', self.urls['pro'])
self.assertEqual('that', self.urls['this'])
self.assertEqual('result', self.urls['simple'])


if __name__ == '__main__':
unittest.main()
@@ -1,15 +1,13 @@
import string
import unittest
from unittest import mock
from unittest.mock import Mock, patch
from mock import patch, Mock

import passenger_wsgi


@patch("passenger_wsgi.parse", return_value={'key': 'value'})
class PassengerWsgiTestCase(unittest.TestCase):

def setUp(self) -> None:
def setUp(self):
self.response = Mock()

def test_empty_path_info_does_not_redirect(self, parse):
@@ -19,12 +17,12 @@ def test_empty_path_info_does_not_redirect(self, parse):
def test_valid_path_returns_false_for_invalid(self, parse):
paths = ['/some/path', '/ ', '/ ', '/$#@', '/?this=that', ' ']
for p in paths:
self.assertFalse(passenger_wsgi.valid_key(p), f'fail={p}')
self.assertFalse(passenger_wsgi.valid_key(p), 'fail={}'.format(p))

def test_valid_path_returns_true_for_valid(self, parse):
paths = ['this', 'somenumber123', '123number']
for p in paths:
self.assertTrue(passenger_wsgi.valid_key(p), f'fail={p}')
self.assertTrue(passenger_wsgi.valid_key(p), 'fail={}'.format(p))

def test_with_path_returns_valid_path_without_leading_slash(self, parse):
self.assertFalse('/' in passenger_wsgi.clean_path('//this//'))

0 comments on commit 41df138

Please sign in to comment.
You can’t perform that action at this time.