-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add comment in .conf files #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.venv | ||
.venv* | ||
.idea | ||
.mypy_cache | ||
__pycache__ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ header: | |
- "*.lock" | ||
- ".*" | ||
- "renovate.json" | ||
- testdata/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,14 @@ | |
import configparser | ||
import io | ||
import unittest | ||
import os | ||
|
||
import addonfactory_splunk_conf_parser_lib as conf_parser | ||
|
||
CONF_PATH = os.path.join( | ||
os.path.dirname(os.path.realpath(__file__)), "testdata", "test.conf" | ||
) | ||
|
||
|
||
class TABConfigParserTest(unittest.TestCase): | ||
def test_read(self): | ||
|
@@ -49,6 +54,9 @@ def test_read(self): | |
parser.get("sourcetype:deprecated", "EVAL-deprecated_field"), '"old_value"' | ||
) | ||
self.assertEqual(parser.get("eventtype=some_eventtype", "tag"), "enabled") | ||
self.assertEqual( | ||
parser.top_comments, ["\n", "#\n", "# Very big copyright comment.\n", "#\n"] | ||
) | ||
|
||
def test_read_incorrect_conf(self): | ||
conf = """ | ||
|
@@ -175,40 +183,43 @@ def test_options(self): | |
expected_options = ["__name__", "EVAL-field1", "FIELDALIAS-field2"] | ||
self.assertEqual(expected_options, parser.options("source")) | ||
|
||
def test_item_dict(self): | ||
conf = """ | ||
# | ||
# Very big copyright comment. | ||
# | ||
[source] | ||
# This is a very simple field aliasing. | ||
FIELDALIAS-field1 = field2 AS field1 | ||
EVAL-field3 = case(isnotnull(field4), "success",\ | ||
isnotnull(field5), "failure") | ||
|
||
[sourcetype:deprecated] | ||
EVAL-deprecated_field = "old_value" | ||
def test_item_dict_when_preserve_comment_False(self): | ||
parser = conf_parser.TABConfigParser() | ||
parser.read(CONF_PATH) | ||
expected_item_dict = { | ||
"source": { | ||
"FIELDALIAS-field1": "field2 AS field1", | ||
"EVAL-field3": 'case(isnotnull(field4), "success",\\\nisnotnull(field5), "failure")', | ||
}, | ||
"sourcetype:deprecated": {"EVAL-deprecated_field": '"old_value"'}, | ||
"eventtype=some_eventtype": { | ||
"tag1": "enabled", | ||
"tag2": "enabled", | ||
}, | ||
} | ||
# Default value of preserve_comments is False | ||
self.assertEqual(expected_item_dict, parser.item_dict()) | ||
|
||
[eventtype=some_eventtype] | ||
tag1 = enabled | ||
tag2 = enabled | ||
""" | ||
def test_item_dict_when_preserve_comment_True(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have a test which has |
||
parser = conf_parser.TABConfigParser() | ||
parser.read_string(conf) | ||
parser.read(CONF_PATH) | ||
expected_item_dict = { | ||
"source": { | ||
"__COMMENTS__0": "# This is a very simple field aliasing.\n", | ||
"FIELDALIAS-field1": "field2 AS field1", | ||
"EVAL-field3": 'case(isnotnull(field4), "success", isnotnull(field5), "failure")', | ||
"EVAL-field3": 'case(isnotnull(field4), "success",\\\nisnotnull(field5), "failure")', | ||
"__COMMENTS__1": "\n", | ||
}, | ||
"sourcetype:deprecated": { | ||
"EVAL-deprecated_field": '"old_value"', | ||
"__COMMENTS__2": "\n", | ||
}, | ||
"eventtype=some_eventtype": { | ||
"tag1": "enabled", | ||
"tag2": "enabled", | ||
"tag1": "enabled ; Inline comment example using semicolon", | ||
"tag2": "enabled # Inline comment using hash", | ||
}, | ||
} | ||
self.assertEqual(expected_item_dict, parser.item_dict()) | ||
self.assertEqual(expected_item_dict, parser.item_dict(preserve_comments=True)) | ||
|
||
def test_item_dict_when_there_is_empty_stanza(self): | ||
conf = """ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# | ||
# Very big copyright comment. | ||
# | ||
|
||
|
||
|
||
|
||
[source] | ||
# This is a very simple field aliasing. | ||
FIELDALIAS-field1 = field2 AS field1 | ||
EVAL-field3 = case(isnotnull(field4), "success",\ | ||
isnotnull(field5), "failure") | ||
|
||
[sourcetype:deprecated] | ||
EVAL-deprecated_field = "old_value" | ||
|
||
[eventtype=some_eventtype] | ||
tag1 = enabled ; Inline comment example using semicolon | ||
tag2 = enabled # Inline comment using hash |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you could inline the
conf
and parametrize the test to stay consistent with other tests but totally up to youThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did try it earlier with
@pytest.mark.parametrize
but for some reason test were failing.