-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1bf12e
commit 91bfc93
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
64
tests/codemods/sonar/test_sonar_timezone_aware_datetime.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |