Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ version-file = "src/usethis/_version.py"
[tool.ruff]
line-length = 88

src = [ "src" ]
lint.select = [
"A",
"C4",
Expand Down Expand Up @@ -178,7 +177,7 @@ type = "layers"
layers = [
"bitbucket | github | pre_commit | pytest | ruff",
"uv | pydantic | sonarqube",
"pyproject | yaml | python",
"project | pyproject | yaml | python",
]
containers = [ "usethis._integrations" ]
exhaustive = true
Empty file.
10 changes: 10 additions & 0 deletions src/usethis/_integrations/project/layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pathlib import Path
from typing import Literal


def get_source_dir_str() -> Literal["src", "."]:
src_dir = Path.cwd() / "src"

if src_dir.exists() and src_dir.is_dir():
return "src"
return "."
9 changes: 8 additions & 1 deletion src/usethis/_integrations/sonarqube/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pydantic import TypeAdapter

from usethis._integrations.project.layout import get_source_dir_str
from usethis._integrations.pyproject.core import get_config_value
from usethis._integrations.python.version import get_python_version
from usethis._integrations.sonarqube.errors import (
Expand Down Expand Up @@ -61,11 +62,17 @@ def get_sonar_project_properties() -> str:
raise CoverageReportConfigNotFoundError(msg)

# No file, so construct the contents
source_dir_str = get_source_dir_str()
if source_dir_str == ".":
sources = "./"
else:
sources = f"./{source_dir_str}"

text = f"""\
sonar.projectKey={project_key}
sonar.language=py
sonar.python.version={python_version}
sonar.sources=./src
sonar.sources={sources}
sonar.tests=./tests
sonar.python.coverage.reportPaths={coverage_output}
sonar.verbose={"true" if verbose else "false"}
Expand Down
13 changes: 8 additions & 5 deletions src/usethis/_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
LocalRepo,
UriRepo,
)
from usethis._integrations.project.layout import get_source_dir_str
from usethis._integrations.pyproject.config import PyProjectConfig
from usethis._integrations.pyproject.core import (
PyProjectTOMLValueAlreadySetError,
Expand Down Expand Up @@ -280,7 +281,7 @@ def get_pyproject_configs(self) -> list[PyProjectConfig]:
PyProjectConfig(
id_keys=["tool", "coverage", "run"],
value={
"source": ["src"],
"source": [get_source_dir_str()],
"omit": ["*/pytest-of-*/*"],
},
),
Expand Down Expand Up @@ -316,17 +317,19 @@ def dev_deps(self) -> list[Dependency]:
return [Dependency(name="deptry")]

def print_how_to_use(self) -> None:
box_print("Run 'deptry src' to run deptry.")
_dir = get_source_dir_str()
box_print(f"Run 'deptry {_dir}' to run deptry.")

def get_pre_commit_repos(self) -> list[LocalRepo | UriRepo]:
_dir = get_source_dir_str()
return [
LocalRepo(
repo="local",
hooks=[
HookDefinition(
id="deptry",
name="deptry",
entry="uv run --frozen deptry src",
entry=f"uv run --frozen deptry {_dir}",
language=Language("system"),
always_run=True,
pass_filenames=False,
Expand All @@ -339,14 +342,15 @@ def get_pyproject_id_keys(self) -> list[list[str]]:
return [["tool", "deptry"]]

def get_bitbucket_steps(self) -> list[BitbucketStep]:
_dir = get_source_dir_str()
return [
BitbucketStep(
name="Run Deptry",
caches=["uv"],
script=BitbucketScript(
[
BitbucketScriptItemAnchor(name="install-uv"),
"uv run deptry src",
f"uv run deptry {_dir}",
]
),
)
Expand Down Expand Up @@ -573,7 +577,6 @@ def get_pyproject_configs(self) -> list[PyProjectConfig]:
PyProjectConfig(
id_keys=["tool", "ruff"],
value={
"src": ["src"],
"line-length": 88,
"lint": {"select": []},
},
Expand Down
39 changes: 39 additions & 0 deletions tests/usethis/_integrations/project/test_layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pathlib import Path

from usethis._integrations.project.layout import get_source_dir_str
from usethis._test import change_cwd


class TestGetSourceDirStr:
def test_has_src(self, tmp_path: Path):
# Arrange
(tmp_path / "src").mkdir()

# Act
with change_cwd(tmp_path):
result = get_source_dir_str()

# Assert
assert result == "src"

def test_no_src(self, tmp_path: Path):
# Arrange
(tmp_path / "foo").mkdir()

# Act
with change_cwd(tmp_path):
result = get_source_dir_str()

# Assert
assert result == "."

def test_src_file_not_dir(self, tmp_path: Path):
# Arrange
(tmp_path / "src").touch()

# Act
with change_cwd(tmp_path):
result = get_source_dir_str()

# Assert
assert result == "."
14 changes: 7 additions & 7 deletions tests/usethis/_integrations/sonarqube/test_sonarqube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_different_python_version(self, tmp_path: Path):
sonar.projectKey=foobar
sonar.language=py
sonar.python.version=3.10
sonar.sources=./src
sonar.sources=./
sonar.tests=./tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.verbose=false
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_no_pin_python(self, tmp_path: Path):
sonar.projectKey=foobar
sonar.language=py
sonar.python.version={get_python_version()}
sonar.sources=./src
sonar.sources=./
sonar.tests=./tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.verbose=false
Expand Down Expand Up @@ -156,7 +156,7 @@ def test_different_project_key(self, tmp_path: Path):
sonar.projectKey=different
sonar.language=py
sonar.python.version=3.12
sonar.sources=./src
sonar.sources=./
sonar.tests=./tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.verbose=false
Expand Down Expand Up @@ -191,7 +191,7 @@ def test_set_verbose_true(self, tmp_path: Path):
sonar.projectKey=foobar
sonar.language=py
sonar.python.version=3.12
sonar.sources=./src
sonar.sources=./
sonar.tests=./tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.verbose=true
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_patch_version_ignored(self, tmp_path: Path):
sonar.projectKey=foobar
sonar.language=py
sonar.python.version=3.12
sonar.sources=./src
sonar.sources=./
sonar.tests=./tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.verbose=false
Expand Down Expand Up @@ -277,7 +277,7 @@ def test_exclusions(self, tmp_path: Path):
sonar.projectKey=foobar
sonar.language=py
sonar.python.version=3.12
sonar.sources=./src
sonar.sources=./
sonar.tests=./tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.verbose=false
Expand Down Expand Up @@ -309,7 +309,7 @@ def test_different_coverage_file_location(self, tmp_path: Path):
sonar.projectKey=foobar
sonar.language=py
sonar.python.version=3.12
sonar.sources=./src
sonar.sources=./
sonar.tests=./tests
sonar.python.coverage.reportPaths=./test-reports/cov.xml
sonar.verbose=false
Expand Down