Skip to content

Commit b49e705

Browse files
SCANPY-235 Fix documentation and behavior of --toml-path (#300)
1 parent 06a25ce commit b49e705

File tree

7 files changed

+87
-7
lines changed

7 files changed

+87
-7
lines changed

CLI_ARGS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
| `--sonar-source-encoding`, `-Dsonar.sourceEncoding` | Encoding of the source files. For example, UTF-8, MacRoman, Shift_JIS |
9797
| `--sonar-user-home`, `-Dsonar.userHome` | Base sonar directory, ~/.sonar by default |
9898
| `--sonar-working-directory`, `-Dsonar.working.directory` | Path to the working directory used by the Sonar scanner during a project analysis to store temporary data |
99-
| `--toml-path` | Path to the pyproject.toml file. If not provided, it will look in the SONAR_PROJECT_BASE_DIR |
99+
| `--toml-path` | Path to the pyproject.toml file or to the folder containing it. If not provided, it will look in the SONAR_PROJECT_BASE_DIR |
100100
| `-Dsonar.python.skipUnchanged` | Equivalent to --sonar-python-skip-unchanged |
101101
| `-Dsonar.python.xunit.skipDetails` | Equivalent to -Dsonar.python.xunit.skipDetails |
102102
| `-Dsonar.qualitygate.wait` | Equivalent to --sonar-qualitygate-wait |

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,18 @@ Properties in `pyproject.toml` files are expected to be provided in camel case.
8585
project-key=My Project key # valid alias for projectKey
8686
```
8787

88-
By default, the scanner will expect the `pyproject.toml` file to be present in the current directory. However, its path can be provided manually through the `toml-path` CLI argument as well as through the `sonar.projectBaseDir` argument. For instance:
88+
By default, the scanner will expect the `pyproject.toml` file to be present in the current directory. However, its path can be provided manually through the `toml-path` CLI argument as well as through the `sonar.projectBaseDir` argument. The `--toml-path` argument accepts either the path to the `pyproject.toml` file itself or to the folder containing it. For instance:
8989

9090
```
9191
pysonar --toml-path "path/to/pyproject.toml"
9292
```
9393

94+
Or equivalently:
95+
96+
```
97+
pysonar --toml-path "path/to"
98+
```
99+
94100
Or:
95101

96102
```

src/pysonar_scanner/configuration/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def __create_parser(cls):
142142
parser.add_argument(
143143
"--toml-path",
144144
type=str,
145-
help="Path to the pyproject.toml file. If not provided, it will look in the SONAR_PROJECT_BASE_DIR",
145+
help="Path to the pyproject.toml file or to the folder containing it. If not provided, it will look in the SONAR_PROJECT_BASE_DIR",
146146
)
147147

148148
parser.add_argument(

src/pysonar_scanner/configuration/configuration_loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def load() -> dict[Key, Any]:
4949
base_dir = Path(cli_properties.get(SONAR_PROJECT_BASE_DIR, "."))
5050

5151
toml_path_property = cli_properties.get("toml-path", ".")
52-
toml_dir = Path(toml_path_property) if "toml-path" in cli_properties else base_dir
53-
toml_properties = TomlConfigurationLoader.load(toml_dir)
52+
toml_path = Path(toml_path_property) if "toml-path" in cli_properties else base_dir
53+
toml_properties = TomlConfigurationLoader.load(toml_path)
5454
coverage_properties = CoverageRCConfigurationLoader.load_exclusion_properties(base_dir)
5555

5656
resolved_properties = get_static_default_properties()

src/pysonar_scanner/configuration/pyproject_toml.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ def __init__(self, sonar_properties: Dict[str, str], project_properties: Dict[st
3737

3838
class TomlConfigurationLoader:
3939
@staticmethod
40-
def load(base_dir: Path) -> TomlProperties:
41-
filepath = base_dir / "pyproject.toml"
40+
def load(toml_path: Path) -> TomlProperties:
41+
if toml_path.name == "pyproject.toml":
42+
filepath = toml_path
43+
else:
44+
filepath = toml_path / "pyproject.toml"
4245
if not os.path.isfile(filepath):
4346
logging.debug(f"No pyproject.toml at {filepath}")
4447
return TomlProperties({}, {})

tests/unit/test_configuration_loader.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,58 @@ def test_load_pyproject_toml_from_toml_path(self, mock_get_os, mock_get_arch):
308308
}
309309
self.assertDictEqual(configuration, expected_configuration)
310310

311+
@patch(
312+
"sys.argv",
313+
[
314+
"myscript.py",
315+
"--token",
316+
"myToken",
317+
"--sonar-project-key",
318+
"myProjectKey",
319+
"--toml-path",
320+
"custom/path/pyproject.toml",
321+
],
322+
)
323+
def test_load_pyproject_toml_from_toml_path_with_file(self, mock_get_os, mock_get_arch):
324+
self.fs.create_dir("custom/path")
325+
self.fs.create_file(
326+
"custom/path/pyproject.toml",
327+
contents=(
328+
"""
329+
[tool.sonar]
330+
projectKey = "custom-path-project-key"
331+
project-name = "Custom Path Project"
332+
sources = "src/main"
333+
tests= "src/test"
334+
scanner.javaHeapSize = "8000Mb"
335+
"""
336+
),
337+
)
338+
configuration = ConfigurationLoader.load()
339+
expected_configuration = {
340+
SONAR_TOKEN: "myToken",
341+
SONAR_PROJECT_KEY: "myProjectKey",
342+
SONAR_PROJECT_NAME: "Custom Path Project",
343+
SONAR_SOURCES: "src/main",
344+
SONAR_TESTS: "src/test",
345+
SONAR_SCANNER_APP: "python",
346+
SONAR_SCANNER_APP_VERSION: "1.0",
347+
SONAR_SCANNER_BOOTSTRAP_START_TIME: configuration[SONAR_SCANNER_BOOTSTRAP_START_TIME],
348+
SONAR_VERBOSE: False,
349+
SONAR_SCANNER_SKIP_JRE_PROVISIONING: False,
350+
SONAR_PROJECT_BASE_DIR: os.getcwd(),
351+
SONAR_SCANNER_CONNECT_TIMEOUT: 5,
352+
SONAR_SCANNER_SOCKET_TIMEOUT: 60,
353+
SONAR_SCANNER_RESPONSE_TIMEOUT: 0,
354+
SONAR_SCANNER_KEYSTORE_PASSWORD: "changeit",
355+
SONAR_SCANNER_TRUSTSTORE_PASSWORD: "changeit",
356+
SONAR_SCANNER_OS: Os.LINUX.value,
357+
SONAR_SCANNER_ARCH: Arch.X64.value,
358+
TOML_PATH: "custom/path/pyproject.toml",
359+
SONAR_SCANNER_JAVA_HEAP_SIZE: "8000Mb",
360+
}
361+
self.assertDictEqual(configuration, expected_configuration)
362+
311363
@patch(
312364
"sys.argv",
313365
[

tests/unit/test_pyproject_toml.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ def test_load_toml_file_from_custom_dir(self):
167167
self.assertEqual(properties.sonar_properties.get("sonar.projectKey"), "custom-path-project")
168168
self.assertEqual(properties.sonar_properties.get("sonar.projectName"), "Custom Path Project")
169169

170+
def test_load_toml_file_from_direct_file_path(self):
171+
self.fs.create_dir("custom/path")
172+
self.fs.create_file(
173+
"custom/path/pyproject.toml",
174+
contents="""
175+
[tool.sonar]
176+
projectKey = "direct-file-project"
177+
projectName = "Direct File Project"
178+
""",
179+
)
180+
properties = TomlConfigurationLoader.load(Path("custom/path/pyproject.toml"))
181+
182+
self.assertEqual(properties.sonar_properties.get("sonar.projectKey"), "direct-file-project")
183+
self.assertEqual(properties.sonar_properties.get("sonar.projectName"), "Direct File Project")
184+
185+
def test_load_toml_file_from_direct_file_path_missing(self):
186+
properties = TomlConfigurationLoader.load(Path("nonexistent/pyproject.toml"))
187+
self.assertEqual(len(properties.sonar_properties), 0)
188+
170189
def test_load_toml_file_project_content(self):
171190
self.fs.create_file(
172191
"pyproject.toml",

0 commit comments

Comments
 (0)