Skip to content
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

Eric/cus 9 add tests for all modes #33

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ markdown_security_temp.md
*.pyc
test.py
*.cpython-312.pyc`
file_generator.py
file_generator.py
.coverage
.env.local
111 changes: 110 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]

[project.optional-dependencies]
test = [
"pytest>=7.4.0",
"pytest-cov>=4.1.0",
"pytest-mock>=3.12.0",
"pytest-asyncio>=0.23.0",
"pytest-watch >=4.2.0"
]
dev = [
"ruff>=0.3.0",
]

[project.scripts]
socketcli = "socketsecurity.socketcli:cli"

Expand All @@ -45,4 +57,101 @@ include = [
]

[tool.setuptools.dynamic]
version = {attr = "socketsecurity.__version__"}
version = {attr = "socketsecurity.__version__"}


pythonpath = "."

[tool.coverage.run]
source = ["socketsecurity"]
omit = ["tests/*", "**/__init__.py"]

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"def __repr__",
"if __name__ == .__main__.:",
"raise NotImplementedError",
"if TYPE_CHECKING:",
]

[tool.ruff]
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv",
]

[tool.ruff.lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
# McCabe complexity (`C901`) by default.
select = [
"E4", "E7", "E9", "F", # Current rules
"I", # isort
"F401", # Unused imports
"F403", # Star imports
"F405", # Star imports undefined
"F821", # Undefined names
]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[tool.ruff.lint.isort]
known-first-party = ["socketsecurity"]

[tool.ruff.format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

# Enable auto-formatting of code examples in docstrings. Markdown,
# reStructuredText code/literal blocks and doctests are all supported.
#
# This is currently disabled by default, but it is planned for this
# to be opt-out in the future.
docstring-code-format = false

# Set the line length limit used when formatting code snippets in
# docstrings.
#
# This only has an effect when the `docstring-code-format` setting is
# enabled.
docstring-code-line-length = "dynamic"
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
testpaths = tests/unit
addopts = -vv --no-cov --tb=short -ra
python_files = test_*.py
186 changes: 186 additions & 0 deletions socketsecurity/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
from dataclasses import dataclass
from typing import List, Optional
import argparse
import os

@dataclass
class CliConfig:
api_token: str
repo: Optional[str]
branch: str = ""
committer: Optional[List[str]] = None
pr_number: str = "0"
commit_message: Optional[str] = None
default_branch: bool = False
target_path: str = "./"
scm: str = "api"
sbom_file: Optional[str] = None
commit_sha: str = ""
generate_license: bool = False
enable_debug: bool = False
allow_unverified: bool = False
enable_json: bool = False
disable_overview: bool = False
disable_security_issue: bool = False
files: str = "[]"
ignore_commit_files: bool = False
disable_blocking: bool = False

@classmethod
def from_args(cls, args_list: Optional[List[str]] = None) -> 'CliConfig':
parser = create_argument_parser()
args = parser.parse_args(args_list)

# Get API token from env or args
api_token = os.getenv("SOCKET_SECURITY_API_KEY") or args.api_token

return cls(
api_token=api_token,
repo=args.repo,
branch=args.branch,
committer=args.committer,
pr_number=args.pr_number,
commit_message=args.commit_message,
default_branch=args.default_branch,
target_path=args.target_path,
scm=args.scm,
sbom_file=args.sbom_file,
commit_sha=args.commit_sha,
generate_license=args.generate_license,
enable_debug=args.enable_debug,
allow_unverified=args.allow_unverified,
enable_json=args.enable_json,
disable_overview=args.disable_overview,
disable_security_issue=args.disable_security_issue,
files=args.files,
ignore_commit_files=args.ignore_commit_files,
disable_blocking=args.disable_blocking
)

def create_argument_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="socketcli",
description="Socket Security CLI"
)

parser.add_argument(
"--api-token",
help="Socket Security API token (can also be set via SOCKET_SECURITY_API_KEY env var)",
required=False
)

parser.add_argument(
"--repo",
help="Repository name in owner/repo format",
required=False
)

parser.add_argument(
"--branch",
help="Branch name",
default=""
)

parser.add_argument(
"--committer",
help="Committer(s) to filter by",
nargs="*"
)

parser.add_argument(
"--pr-number",
help="Pull request number",
default="0"
)

parser.add_argument(
"--commit-message",
help="Commit message"
)

# Boolean flags
parser.add_argument(
"--default-branch",
action="store_true",
help="Use default branch"
)

parser.add_argument(
"--generate-license",
action="store_true",
help="Generate license information"
)

parser.add_argument(
"--enable-debug",
action="store_true",
help="Enable debug logging"
)

parser.add_argument(
"--allow-unverified",
action="store_true",
help="Allow unverified packages"
)

parser.add_argument(
"--enable-json",
action="store_true",
help="Output in JSON format"
)

parser.add_argument(
"--disable-overview",
action="store_true",
help="Disable overview output"
)

parser.add_argument(
"--disable-security-issue",
action="store_true",
help="Disable security issue checks"
)

parser.add_argument(
"--ignore-commit-files",
action="store_true",
help="Ignore commit files"
)

parser.add_argument(
"--disable-blocking",
action="store_true",
help="Disable blocking mode"
)

# Path and file related arguments
parser.add_argument(
"--target-path",
default="./",
help="Target path for analysis"
)

parser.add_argument(
"--scm",
default="api",
help="Source control management type"
)

parser.add_argument(
"--sbom-file",
help="SBOM file path"
)

parser.add_argument(
"--commit-sha",
default="",
help="Commit SHA"
)

parser.add_argument(
"--files",
default="[]",
help="Files to analyze (JSON array string)"
)

return parser
Loading