From 4d49c70dd84f0f4431a0bd792b0e31000ebc4b59 Mon Sep 17 00:00:00 2001 From: Vinicius Gubiani Ferreira Date: Tue, 21 Feb 2023 15:41:51 -0300 Subject: [PATCH] feat: Add useful info when using on terminal Instead of getting info about memory position, now we receive info that helps us navigate the nginx conf structure on some cases. For example: Before: After: --- .github/workflows/ci.yml | 7 +++++-- nginx.py | 15 +++++++++++++++ tests.py | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bfdab10..436cc8a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,9 @@ name: Python package -on: [push] +on: + push: + pull_request: + types: [opened, synchronize] jobs: build: @@ -29,4 +32,4 @@ jobs: flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | - python tests.py \ No newline at end of file + python tests.py diff --git a/nginx.py b/nginx.py index e5b6574..7ed9699 100755 --- a/nginx.py +++ b/nginx.py @@ -252,6 +252,9 @@ def __init__(self, comment, inline=False): self.comment = comment self.inline = inline + def __repr__(self): + return "".format(self.comment) + @property def as_list(self): """Return comment as nested list of strings.""" @@ -299,6 +302,9 @@ def __init__(self, value, *args): super(Location, self).__init__(value, *args) self.name = 'location' + def __repr__(self): + return "".format(self.value) + class Events(Container): """Container for Event-based options.""" @@ -344,6 +350,9 @@ def __init__(self, value, *args): super(Upstream, self).__init__(value, *args) self.name = 'upstream' + def __repr__(self): + return "".format(self.value) + class Geo(Container): """ @@ -366,6 +375,9 @@ def __init__(self, value, *args): super(Map, self).__init__(value, *args) self.name = 'map' + def __repr__(self): + return "".format(self.value) + class Stream(Container): """Container for stream sections in the main NGINX conf file.""" @@ -388,6 +400,9 @@ def __init__(self, name, value): self.name = name self.value = value + def __repr__(self): + return "".format(self.name) + @property def as_list(self): """Return key as nested list of strings.""" diff --git a/tests.py b/tests.py index 1877bda..f644945 100755 --- a/tests.py +++ b/tests.py @@ -39,6 +39,11 @@ { server unix:/tmp/php-fcgi.socket; } +map $request_body $tmp_body +{ + "" "-"; + default $request_body; +} server { listen 80; # This comment should be present; @@ -354,6 +359,25 @@ def test_server_without_last_linebreak(self): self.assertTrue(nginx.loads(TESTBLOCK_CASE_13) is not None) self.assertTrue(nginx.loads(TESTBLOCK_CASE_14) is not None) + def test_useful_info_on_terminal(self): + data = nginx.loads(TESTBLOCK_CASE_2) + + upstream = data.children[0] + self.assertEqual(str(upstream), '') + + key = upstream.children[0] + self.assertEqual(str(key), '') + + nginx_map = data.children[1] + self.assertEqual(str(nginx_map), '') + + server = data.children[2] + comment = server.children[1] + self.assertEqual(str(comment), '') + + location = server.children[-1] + self.assertEqual(str(location), '') + if __name__ == '__main__': unittest.main()