Skip to content

Commit

Permalink
fix: issue with indent function (#257)
Browse files Browse the repository at this point in the history
io.StringIO wrapper around lines is needed to handle cases when lines is equal to None.
  • Loading branch information
Artem Rys authored Jul 7, 2021
1 parent fdf2894 commit 898fdb4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
27 changes: 16 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pytest = "^6.2"
pytest-splunk-addon = { version = "^1.7", extras = [ "docker" ] }
pytest-cov = "^2.12.1"
coverage = "^5.5"
parameterized = "^0.8.1"

[tool.poetry.scripts]
ucc-gen="splunk_add_on_ucc_framework:main"
Expand Down
13 changes: 7 additions & 6 deletions splunk_add_on_ucc_framework/uccrestbuilder/endpoint/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from io import StringIO

from future import standard_library
from splunktaucclib.rest_handler.schema import RestSchema

Expand Down Expand Up @@ -179,13 +181,12 @@ def indent(lines, spaces=1):
:param spaces: times of four
:return:
"""
if lines is None:
return ""
string_io = StringIO(str(lines))
indentation = spaces * 4
prefix = " " * indentation
intended_lines = []
for line in lines:
lines = []
for line in string_io:
if line != "\n":
line = prefix + line
intended_lines.append(line)
return "".join(intended_lines)
lines.append(line)
return "".join(lines)
37 changes: 37 additions & 0 deletions tests/unit/test_uccrestbuilder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# Copyright 2021 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import unittest

from parameterized import parameterized
from splunk_add_on_ucc_framework.uccrestbuilder.endpoint import base


class IndentTest(unittest.TestCase):
@parameterized.expand(
[
(None, " None"),
(
"\nmax_len=4096,\nmin_len=0,\n",
"\n max_len=4096,\n min_len=0,\n",
),
(
"validator.String(\n max_len=4096,\n min_len=0,\n)",
" validator.String(\n max_len=4096,\n min_len=0,\n )",
),
]
)
def test_indent(self, lines, expected):
self.assertEqual(base.indent(lines), expected)

0 comments on commit 898fdb4

Please sign in to comment.