Skip to content

Commit c9636ce

Browse files
author
Jeremi Do Dinh
authored
PYSCAN-2: Add tests for the args parsing. (#16)
1 parent 4c61c84 commit c9636ce

File tree

3 files changed

+52
-32
lines changed

3 files changed

+52
-32
lines changed

src/py_sonar_scanner/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with this program; if not, write to the Free Software Foundation,
1818
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
#
20+
2021
from py_sonar_scanner.configuration import Configuration
2122
from py_sonar_scanner.environment import Environment
2223

src/py_sonar_scanner/configuration.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
#
2020
from __future__ import annotations
21-
import argparse
2221
import os
2322
import sys
2423
from typing import Union
@@ -37,43 +36,14 @@ def __init__(self):
3736
self.sonar_scanner_executable_path = ""
3837
self.scan_arguments = []
3938

40-
def setup(self):
39+
def setup(self) -> None:
4140
"""This is executed when run from the command line"""
42-
parser = argparse.ArgumentParser()
4341

44-
# Required positional argument
45-
parser.add_argument("arg", help="Required positional argument")
46-
47-
# Optional argument flag which defaults to False
48-
parser.add_argument("-f", "--flag", action="store_true", default=False)
49-
50-
# Optional argument which requires a parameter (eg. -d test)
51-
parser.add_argument("-n", "--name", action="store", dest="name")
52-
53-
# Optional verbosity counter (eg. -v, -vv, -vvv, etc.)
54-
parser.add_argument("-v", "--verbose", action="count", default=0, help="Verbosity (-v, -vv, etc)")
55-
56-
# Specify output of "--version"
57-
# parser.add_argument(
58-
# "--version",
59-
# action="version",
60-
# version="%(prog)s (version {version})".format(version=__version__))
61-
62-
# args = parser.parse_args()
63-
64-
scan_arguments = []
65-
scan_arguments.extend(self._read_jvm_args())
42+
scan_arguments = sys.argv[1:]
6643
scan_arguments.extend(self._read_toml_args())
6744

6845
self.scan_arguments = scan_arguments
6946

70-
def _read_jvm_args(self) -> list[str]:
71-
scan_arguments = []
72-
for arg in sys.argv:
73-
if arg.startswith("-D"):
74-
scan_arguments.append(arg)
75-
return scan_arguments
76-
7747
def _read_toml_args(self) -> list[str]:
7848
scan_arguments: list[str] = []
7949
try:

tests/test_configuration.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# Sonar Scanner Python
3+
# Copyright (C) 2011-2023 SonarSource SA.
4+
# mailto:info AT sonarsource DOT com
5+
#
6+
# This program is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 3 of the License, or (at your option) any later version.
10+
# This program is distributed in the hope that it will be useful,
11+
#
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with this program; if not, write to the Free Software Foundation,
18+
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
#
20+
import unittest
21+
from unittest.mock import patch, Mock
22+
from py_sonar_scanner.configuration import Configuration
23+
24+
25+
class TestConfiguration(unittest.TestCase):
26+
@patch("py_sonar_scanner.configuration.sys")
27+
def test_argument_parsing_empty_toml(self, mock_sys):
28+
configuration = Configuration()
29+
configuration._read_toml_args = Mock(return_value=[])
30+
31+
mock_sys.argv = ["path/to/scanner/py-sonar-scanner"]
32+
configuration.setup()
33+
self.assertListEqual(configuration.scan_arguments, [])
34+
35+
mock_sys.argv = ["path/to/scanner/py-sonar-scanner", "-DSomeJVMArg"]
36+
configuration.setup()
37+
self.assertListEqual(configuration.scan_arguments, ["-DSomeJVMArg"])
38+
39+
mock_sys.argv = [
40+
"path/to/scanner/py-sonar-scanner",
41+
"-DSomeJVMArg",
42+
"-DAnotherJVMArg",
43+
"-dNotAJVMArg",
44+
"-SomeNonsense",
45+
]
46+
configuration.setup()
47+
self.assertListEqual(
48+
configuration.scan_arguments, ["-DSomeJVMArg", "-DAnotherJVMArg", "-dNotAJVMArg", "-SomeNonsense"]
49+
)

0 commit comments

Comments
 (0)