Skip to content

Commit

Permalink
add sonar codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Aug 19, 2024
1 parent f1bf12e commit 91bfc93
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/codemodder/scripts/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class DocMetadata:
"django-model-without-dunder-str",
"break-or-continue-out-of-loop",
"disable-graphql-introspection",
"timezone-aware-datetime",
]
SONAR_CODEMODS = {
name: DocMetadata(
Expand Down
2 changes: 2 additions & 0 deletions src/core_codemods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
from .sonar.sonar_secure_random import SonarSecureRandom
from .sonar.sonar_sql_parameterization import SonarSQLParameterization
from .sonar.sonar_tempfile_mktemp import SonarTempfileMktemp
from .sonar.sonar_timezone_aware_datetime import SonarTimezoneAwareDatetime
from .sonar.sonar_url_sandbox import SonarUrlSandbox
from .sql_parameterization import SQLQueryParameterization
from .str_concat_in_seq_literal import StrConcatInSeqLiteral
Expand Down Expand Up @@ -195,6 +196,7 @@
SonarDjangoModelWithoutDunderStr,
SonarBreakOrContinueOutOfLoop,
SonarDisableGraphQLIntrospection,
SonarTimezoneAwareDatetime,
],
)

Expand Down
9 changes: 9 additions & 0 deletions src/core_codemods/sonar/sonar_timezone_aware_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from core_codemods.sonar.api import SonarCodemod
from core_codemods.timezone_aware_datetime import TimezoneAwareDatetime

SonarTimezoneAwareDatetime = SonarCodemod.from_core_codemod(
name="timezone-aware-datetime",
other=TimezoneAwareDatetime,
rule_id="python:S6903",
rule_name='Using timezone-aware "datetime" objects should be preferred over using "datetime.datetime.utcnow" and "datetime.datetime.utcfromtimestamp"',
)
64 changes: 64 additions & 0 deletions tests/codemods/sonar/test_sonar_timezone_aware_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import json

from codemodder.codemods.test import BaseSASTCodemodTest
from core_codemods.sonar.sonar_timezone_aware_datetime import SonarTimezoneAwareDatetime


class TestSonarSQLParameterization(BaseSASTCodemodTest):
codemod = SonarTimezoneAwareDatetime
tool = "sonar"

def test_name(self):
assert self.codemod.name == "timezone-aware-datetime"

def test_simple(self, tmpdir):
input_code = """\
import datetime
datetime.datetime.utcnow()
timestamp = 1571595618.0
datetime.datetime.utcfromtimestamp(timestamp)
"""
expected = """\
import datetime
datetime.datetime.now(tz=datetime.timezone.utc)
timestamp = 1571595618.0
datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
"""
issues = {
"issues": [
{
"key": "AZFcGzHT5VEY3NanjlD7",
"rule": "python:S6903",
"severity": "MAJOR",
"component": "code.py",
"hash": "92aca3da1e08f944a3c408df27c54b28",
"textRange": {
"startLine": 3,
"endLine": 3,
"startOffset": 0,
"endOffset": 26,
},
"status": "OPEN",
"message": "Don't use `datetime.datetime.utcnow` to create this datetime object.",
},
{
"key": "AZFcGzHT5VEY3NanjlD8",
"rule": "python:S6903",
"severity": "MAJOR",
"component": "code.py",
"textRange": {
"startLine": 5,
"endLine": 5,
"startOffset": 0,
"endOffset": 45,
},
"status": "OPEN",
"message": "Don't use `datetime.datetime.utcfromtimestamp` to create this datetime object.",
},
]
}
self.run_and_assert(
tmpdir, input_code, expected, results=json.dumps(issues), num_changes=2
)

0 comments on commit 91bfc93

Please sign in to comment.