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

fix: better handle missing static analysis info #141

Merged
merged 1 commit into from
Oct 6, 2023
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
11 changes: 11 additions & 0 deletions services/static_analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ def _analyze_single_change(
)
if not head_analysis_file_data and not base_analysis_file_data:
return None
if head_analysis_file_data is None or base_analysis_file_data is None:
log.warning(
"Failed to load snapshot for file. Fallback to all lines in the file",
extra=dict(
file_path=change.after_filepath,
is_missing_head=(head_analysis_file_data is None),
is_missing_base=(base_analysis_file_data is None),
),
)
return {"all": True, "lines": None}

for base_line in change.lines_only_on_base:
corresponding_exec_line = (
base_analysis_file_data.get_corresponding_executable_line(base_line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def test_analyze_single_change_base_change(self, dbsession, mock_storage):
(
10,
{
"len": 1,
"len": 0,
"extra_connected_lines": [20],
},
),
Expand Down Expand Up @@ -574,6 +574,18 @@ def test_analyze_single_change_base_change(self, dbsession, mock_storage):
changed_snapshot_base.content_location,
changed_snapshot_head.content_location,
) == {"all": True, "lines": None}
assert service._analyze_single_change(
dbsession,
DiffChange(
before_filepath="path/changed.py",
after_filepath="path/changed.py",
change_type=DiffChangeType.modified,
lines_only_on_base=[],
lines_only_on_head=[11],
),
changed_snapshot_base.content_location,
changed_snapshot_head.content_location,
) == {"all": False, "lines": {10}}
assert service._analyze_single_change(
dbsession,
DiffChange(
Expand All @@ -587,6 +599,117 @@ def test_analyze_single_change_base_change(self, dbsession, mock_storage):
changed_snapshot_head.content_location,
) == {"all": False, "lines": set()}

def test_analyze_single_change_base_change_missing_head_snapshot(
self, dbsession, mock_storage
):
repository = RepositoryFactory.create()
dbsession.add(repository)
dbsession.flush()
changed_snapshot_base = StaticAnalysisSingleFileSnapshotFactory.create(
repository=repository
)
changed_snapshot_head = StaticAnalysisSingleFileSnapshotFactory.create(
repository=repository
)
dbsession.add_all(
[
changed_snapshot_base,
changed_snapshot_head,
]
)
dbsession.flush()
mock_storage.write_file(
"archive",
changed_snapshot_base.content_location,
json.dumps(
{
"functions": [
{
"identifier": "banana_function",
"start_line": 3,
"end_line": 8,
}
],
"statements": [
(
1,
{
"len": 0,
"line_surety_ancestorship": None,
"extra_connected_lines": [],
},
),
(
2,
{
"len": 0,
"line_surety_ancestorship": 1,
"extra_connected_lines": [],
},
),
],
}
),
)
head_static_analysis = StaticAnalysisSuiteFactory.create(
commit__repository=repository
)
base_static_analysis = StaticAnalysisSuiteFactory.create(
commit__repository=repository
)
dbsession.add(head_static_analysis)
dbsession.add(base_static_analysis)
dbsession.flush()
service = StaticAnalysisComparisonService(
base_static_analysis=base_static_analysis,
head_static_analysis=head_static_analysis,
git_diff=[
DiffChange(
before_filepath="path/changed.py",
after_filepath="path/changed.py",
change_type=DiffChangeType.modified,
lines_only_on_base=[],
lines_only_on_head=[20],
),
],
)
assert service._analyze_single_change(
dbsession,
DiffChange(
before_filepath="path/changed.py",
after_filepath="path/changed.py",
change_type=DiffChangeType.modified,
lines_only_on_base=[],
lines_only_on_head=[20],
),
changed_snapshot_base.content_location,
changed_snapshot_head.content_location,
) == {"all": True, "lines": None}
assert service._analyze_single_change(
dbsession,
DiffChange(
before_filepath="path/changed.py",
after_filepath="path/changed.py",
change_type=DiffChangeType.modified,
lines_only_on_base=[],
lines_only_on_head=[11],
),
changed_snapshot_base.content_location,
changed_snapshot_head.content_location,
) == {"all": True, "lines": None}
assert service._analyze_single_change(
dbsession,
DiffChange(
before_filepath="path/changed.py",
after_filepath="path/changed.py",
change_type=DiffChangeType.modified,
lines_only_on_base=[],
lines_only_on_head=[99, 100],
),
changed_snapshot_base.content_location,
changed_snapshot_head.content_location,
) == {"all": True, "lines": None}

def test_analyze_single_change_function_based(self, dbsession, mock_storage):
repository = RepositoryFactory.create()
dbsession.add(repository)
Expand Down
Loading