@@ -336,6 +336,65 @@ def test_missing_coverage_file_location_error(self, tmp_path: Path):
336336 with pytest .raises (CoverageReportConfigNotFoundError ):
337337 get_sonar_project_properties ()
338338
339+ def test_project_key_argument (self , tmp_path : Path ):
340+ with change_cwd (tmp_path ), PyprojectTOMLManager ():
341+ # Arrange
342+ uv_python_pin ("3.12" )
343+ ensure_pyproject_toml ()
344+ PyprojectTOMLManager ().set_value (
345+ keys = ["tool" , "coverage" , "xml" , "output" ], value = "coverage.xml"
346+ )
347+
348+ # Act
349+ result = get_sonar_project_properties (project_key = "cli-key" )
350+
351+ # Assert
352+ assert (
353+ result
354+ == """\
355+ sonar.projectKey=cli-key
356+ sonar.language=py
357+ sonar.python.version=3.12
358+ sonar.sources=./
359+ sonar.tests=./tests
360+ sonar.python.coverage.reportPaths=coverage.xml
361+ sonar.verbose=false
362+ sonar.exclusions=tests/*
363+ """
364+ )
365+
366+ def test_project_key_argument_overrides_pyproject (self , tmp_path : Path ):
367+ with change_cwd (tmp_path ), PyprojectTOMLManager ():
368+ # Arrange
369+ uv_python_pin ("3.12" )
370+ ensure_pyproject_toml ()
371+ PyprojectTOMLManager ().set_value (
372+ keys = ["tool" , "usethis" , "sonarqube" , "project-key" ],
373+ value = "from-pyproject" ,
374+ )
375+ PyprojectTOMLManager ().set_value (
376+ keys = ["tool" , "coverage" , "xml" , "output" ], value = "coverage.xml"
377+ )
378+
379+ # Act
380+ result = get_sonar_project_properties (project_key = "from-cli" )
381+
382+ # Assert
383+ assert "sonar.projectKey=from-cli\n " in result
384+
385+ def test_project_key_argument_invalid (self , tmp_path : Path ):
386+ with change_cwd (tmp_path ), PyprojectTOMLManager ():
387+ # Arrange
388+ uv_python_pin ("3.12" )
389+ ensure_pyproject_toml ()
390+ PyprojectTOMLManager ().set_value (
391+ keys = ["tool" , "coverage" , "xml" , "output" ], value = "coverage.xml"
392+ )
393+
394+ # Act, Assert
395+ with pytest .raises (InvalidSonarQubeProjectKeyError ):
396+ get_sonar_project_properties (project_key = "invalid key!" )
397+
339398 def test_flat_layout_exclusions_already_has_tests (self , tmp_path : Path ):
340399 # When using flat layout and tests/* is already in exclusions,
341400 # it should not be added again.
0 commit comments