Skip to content

Commit

Permalink
Add default value to Change.diffSide in CodeTF
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed Feb 6, 2024
1 parent de74fb3 commit cccc8a7
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 22 deletions.
11 changes: 11 additions & 0 deletions src/codemodder/change.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class Result(Enum):
SKIPPED = "skipped"


class DiffSide(Enum):
LEFT = "left"
RIGHT = "right"


@dataclass
class PackageAction:
action: Action
Expand All @@ -31,6 +36,11 @@ def to_json(self):
class Change:
lineNumber: int
description: str
# All of our changes are currently treated as additive, so it makes sense
# for the comments to appear on the RIGHT side of the split diff. Eventually we
# may want to differentiate between LEFT and RIGHT, but for now we'll just
# default to RIGHT.
diffSide: DiffSide = field(default=DiffSide.RIGHT)
properties: dict = field(default_factory=dict)
packageActions: list[PackageAction] = field(default_factory=list)

Expand All @@ -40,6 +50,7 @@ def to_json(self):
"lineNumber": str(self.lineNumber),
"description": self.description,
"properties": self.properties,
"diffSide": self.diffSide.value.lower(),
"packageActions": [pa.to_json() for pa in self.packageActions],
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def build_changes(
# Contextual comments should be added to the right side of split diffs
properties={
"contextual_description": True,
# TODO: `contextual_description_position` is deprecated in
# favor of Change.diffSide.
# We're keeping it here for backwards compatibility but it
# should eventually be removed.
"contextual_description_position": "right",
},
packageActions=[
Expand Down
1 change: 1 addition & 0 deletions src/codemodder/report/codetf_reporter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from os.path import abspath

from codemodder import __version__
from codemodder.logging import logger

Expand Down
11 changes: 7 additions & 4 deletions tests/dependency_management/test_pyproject_writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from textwrap import dedent
import pytest

from codemodder.change import DiffSide
from codemodder.dependency_management.pyproject_writer import PyprojectWriter
from codemodder.dependency import DefusedXML, Security
from codemodder.project_analysis.file_parsers.package_store import (
Expand Down Expand Up @@ -35,7 +36,7 @@ def test_update_pyproject_dependencies(tmpdir, dry_run):

store = PackageStore(
type=FileType.REQ_TXT,
file=str(pyproject_toml),
file=pyproject_toml,
dependencies=set(),
py_versions=[">=3.10.0"],
)
Expand Down Expand Up @@ -88,13 +89,15 @@ def test_update_pyproject_dependencies(tmpdir, dry_run):

assert change_one.lineNumber == 16
assert change_one.description == DefusedXML.build_description()
assert change_one.diffSide == DiffSide.RIGHT
assert change_one.properties == {
"contextual_description": True,
"contextual_description_position": "right",
}
change_two = changeset.changes[1]
assert change_two.lineNumber == 17
assert change_two.description == Security.build_description()
assert change_two.diffSide == DiffSide.RIGHT
assert change_two.properties == {
"contextual_description": True,
"contextual_description_position": "right",
Expand Down Expand Up @@ -124,7 +127,7 @@ def test_add_same_dependency_only_once(tmpdir):

store = PackageStore(
type=FileType.REQ_TXT,
file=str(pyproject_toml),
file=pyproject_toml,
dependencies=set(),
py_versions=[">=3.10.0"],
)
Expand Down Expand Up @@ -178,7 +181,7 @@ def test_dont_add_existing_dependency(tmpdir):

store = PackageStore(
type=FileType.REQ_TXT,
file=str(pyproject_toml),
file=pyproject_toml,
dependencies=set([Security.requirement]),
py_versions=[">=3.10.0"],
)
Expand All @@ -204,7 +207,7 @@ def test_pyproject_no_dependencies(tmpdir):

store = PackageStore(
type=FileType.REQ_TXT,
file=str(pyproject_toml),
file=pyproject_toml,
dependencies=set(),
py_versions=[">=3.10.0"],
)
Expand Down
9 changes: 7 additions & 2 deletions tests/dependency_management/test_requirements_txt_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from pathlib import Path

import pytest

from codemodder.change import DiffSide
from codemodder.dependency_management.requirements_txt_writer import (
RequirementsTxtWriter,
)
Expand Down Expand Up @@ -48,13 +51,15 @@ def test_add_dependencies_preserve_comments(self, tmpdir, dry_run):
change_one = changeset.changes[0]
assert change_one.lineNumber == 4
assert change_one.description == DefusedXML.build_description()
assert change_one.diffSide == DiffSide.RIGHT
assert change_one.properties == {
"contextual_description": True,
"contextual_description_position": "right",
}
change_two = changeset.changes[1]
assert change_two.lineNumber == 5
assert change_two.description == Security.build_description()
assert change_two.diffSide == DiffSide.RIGHT
assert change_two.properties == {
"contextual_description": True,
"contextual_description_position": "right",
Expand Down Expand Up @@ -123,7 +128,7 @@ def test_dependency_file_no_terminating_newline(self, tmpdir):

store = PackageStore(
type=FileType.REQ_TXT,
file=str(dependency_file),
file=dependency_file,
dependencies=set(),
py_versions=[],
)
Expand Down
25 changes: 16 additions & 9 deletions tests/dependency_management/test_setup_py_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from textwrap import dedent

import pytest

from codemodder.change import DiffSide
from codemodder.dependency_management.setup_py_writer import SetupPyWriter
from codemodder.project_analysis.file_parsers.package_store import (
PackageStore,
Expand Down Expand Up @@ -34,7 +37,7 @@ def test_update_setuppy_comma_single_element_newline(tmpdir):

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
file=dependency_file,
dependencies=set(),
py_versions=[">=3.6"],
)
Expand Down Expand Up @@ -138,7 +141,7 @@ def test_update_setuppy_dependencies(tmpdir, dry_run):

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
file=dependency_file,
dependencies=set(),
py_versions=[">=3.6"],
)
Expand Down Expand Up @@ -191,13 +194,15 @@ def test_update_setuppy_dependencies(tmpdir, dry_run):

assert change_one.lineNumber == 14
assert change_one.description == DefusedXML.build_description()
assert change_one.diffSide == DiffSide.RIGHT
assert change_one.properties == {
"contextual_description": True,
"contextual_description_position": "right",
}
change_two = changeset.changes[1]
assert change_two.lineNumber == 14
assert change_two.description == Security.build_description()
assert change_two.diffSide == DiffSide.RIGHT
assert change_two.properties == {
"contextual_description": True,
"contextual_description_position": "right",
Expand All @@ -224,7 +229,7 @@ def test_other_setup_func(tmpdir):

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
file=dependency_file,
dependencies=set(),
py_versions=[">=3.6"],
)
Expand Down Expand Up @@ -262,7 +267,7 @@ def test_not_setup_file(tmpdir):

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
file=dependency_file,
dependencies=set(),
py_versions=[">=3.6"],
)
Expand Down Expand Up @@ -293,7 +298,7 @@ def test_setup_call_no_install_requires(tmpdir):

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
file=dependency_file,
dependencies=set(),
py_versions=[">=3.6"],
)
Expand Down Expand Up @@ -325,7 +330,7 @@ def test_setup_no_existing_requirements(tmpdir):

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
file=dependency_file,
dependencies=set(),
py_versions=[">=3.6"],
)
Expand Down Expand Up @@ -357,7 +362,7 @@ def test_setup_call_bad_install_requires(tmpdir):

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
file=dependency_file,
dependencies=set(),
py_versions=[">=3.6"],
)
Expand Down Expand Up @@ -397,7 +402,7 @@ def test_setup_call_requirements_separate(tmpdir):

store = PackageStore(
type=FileType.SETUP_PY,
file=str(dependency_file),
file=dependency_file,
dependencies=set(),
py_versions=[">=3.6"],
)
Expand Down Expand Up @@ -453,13 +458,15 @@ def test_setup_call_requirements_separate(tmpdir):

assert change_one.lineNumber == 14
assert change_one.description == DefusedXML.build_description()
assert change_one.diffSide == DiffSide.RIGHT
assert change_one.properties == {
"contextual_description": True,
"contextual_description_position": "right",
}
change_two = changeset.changes[1]
assert change_two.lineNumber == 14
assert change_two.description == Security.build_description()
assert change_two.diffSide == DiffSide.RIGHT
assert change_two.properties == {
"contextual_description": True,
"contextual_description_position": "right",
Expand Down
19 changes: 12 additions & 7 deletions tests/dependency_management/test_setupcfgt_writer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from textwrap import dedent

import mock
import pytest

from codemodder.change import DiffSide
from codemodder.dependency_management.setupcfg_writer import SetupCfgWriter
from codemodder.dependency import DefusedXML, Security
from codemodder.project_analysis.file_parsers.package_store import (
Expand Down Expand Up @@ -32,7 +34,7 @@ def test_update_dependencies(tmpdir, dry_run):

store = PackageStore(
type=FileType.SETUP_CFG,
file=str(setup_cfg),
file=setup_cfg,
dependencies=set(),
py_versions=[">=3.7"],
)
Expand Down Expand Up @@ -80,13 +82,15 @@ def test_update_dependencies(tmpdir, dry_run):

assert change_one.lineNumber == 13
assert change_one.description == DefusedXML.build_description()
assert change_one.diffSide == DiffSide.RIGHT
assert change_one.properties == {
"contextual_description": True,
"contextual_description_position": "right",
}
change_two = changeset.changes[1]
assert change_two.lineNumber == 14
assert change_two.description == Security.build_description()
assert change_two.diffSide == DiffSide.RIGHT
assert change_two.properties == {
"contextual_description": True,
"contextual_description_position": "right",
Expand Down Expand Up @@ -114,7 +118,7 @@ def test_add_same_dependency_only_once(tmpdir):

store = PackageStore(
type=FileType.SETUP_CFG,
file=str(setup_cfg),
file=setup_cfg,
dependencies=set(),
py_versions=[">=3.7"],
)
Expand Down Expand Up @@ -164,7 +168,7 @@ def test_dont_add_existing_dependency(tmpdir):

store = PackageStore(
type=FileType.SETUP_CFG,
file=str(setup_cfg),
file=setup_cfg,
dependencies=set([Security.requirement]),
py_versions=[">=3.7"],
)
Expand Down Expand Up @@ -194,7 +198,7 @@ def test_no_dependencies(tmpdir):

store = PackageStore(
type=FileType.SETUP_CFG,
file=str(setup_cfg),
file=setup_cfg,
dependencies=set(),
py_versions=[">=3.7"],
)
Expand Down Expand Up @@ -227,7 +231,7 @@ def test_cfg_bad_formatting(tmpdir):

store = PackageStore(
type=FileType.SETUP_CFG,
file=str(setup_cfg),
file=setup_cfg,
dependencies=set(),
py_versions=[">=3.7"],
)
Expand Down Expand Up @@ -263,7 +267,7 @@ def test_cfg_cant_build_newlines(_, tmpdir):

store = PackageStore(
type=FileType.SETUP_CFG,
file=str(setup_cfg),
file=setup_cfg,
dependencies=set(),
py_versions=[">=3.7"],
)
Expand Down Expand Up @@ -293,7 +297,7 @@ def test_cfg_inline_dependencies(tmpdir):

store = PackageStore(
type=FileType.SETUP_CFG,
file=str(setup_cfg),
file=setup_cfg,
dependencies=set(),
py_versions=[">=3.7"],
)
Expand Down Expand Up @@ -333,6 +337,7 @@ def test_cfg_inline_dependencies(tmpdir):

assert change_one.lineNumber == 10
assert change_one.description == Security.build_description()
assert change_one.diffSide == DiffSide.RIGHT
assert change_one.properties == {
"contextual_description": True,
"contextual_description_position": "right",
Expand Down
2 changes: 2 additions & 0 deletions tests/report/test_codetf_reporter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json

import mock
import pytest

from codemodder.report.codetf_reporter import CodeTF


Expand Down
Loading

0 comments on commit cccc8a7

Please sign in to comment.