Skip to content

Commit

Permalink
Now HTP returns 1 when there is a problem in the parsing of the confi…
Browse files Browse the repository at this point in the history
…g file
  • Loading branch information
JulienTD authored and fwininger committed Feb 15, 2022
1 parent 8d0fe27 commit 3f1e9c9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ jobs:
pytest --cov=hashtheplanet tests/
- name: Run app
run: |
echo "https://github.com/jashkenas/underscore.git" > tech.csv
hashtheplanet --input ./tech.csv --output output.db
echo '{"git":{"targets":["https://github.com/jashkenas/underscore.git"]},"npm":{"targets":["underscore"]}}' > tech.json
hashtheplanet --input ./tech.json --output output.db
10 changes: 2 additions & 8 deletions hashtheplanet/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
This module handles the config file.
"""
import json
from json.decoder import JSONDecodeError
from typing import Dict, List
from loguru import logger

from hashtheplanet.resources.git_resource import GitResource
from hashtheplanet.resources.npm_resource import NpmResource
Expand All @@ -24,12 +22,8 @@ def parse(self, config_path: str):
"""
This method parses the config file and loads it in the class.
"""
try:
with open(config_path, "r", encoding="utf-8") as file_fp:
self._config = json.load(file_fp)

except (OSError, JSONDecodeError) as exception:
logger.error(exception)
with open(config_path, "r", encoding="utf-8") as file_fp:
self._config = json.load(file_fp)

def get_targets(self, resource_name: str) -> List[str]:
"""
Expand Down
4 changes: 3 additions & 1 deletion hashtheplanet/core/hashtheplanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
# standard imports
import argparse
from json import JSONDecodeError
import os
import sys
from contextlib import contextmanager
Expand Down Expand Up @@ -96,8 +97,9 @@ def compute_hashs(self):

logger.info("Computing done")

except OSError as error:
except (OSError, JSONDecodeError) as error:
logger.error(f"Error: {error}")
sys.exit(1)

def analyze_file(self, file_path: str) -> Tuple[str, dict]:
"""
Expand Down
3 changes: 1 addition & 2 deletions hashtheplanet/resources/git_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,10 @@ def _filter_stored_tags(stored_versions: List[VersionTable], found_tags: List[Ta

if len(stored_versions) == len(found_tags):
return []
stored_tags_name = [element.version for element in stored_versions]
for found_tag_idx, found_tag in enumerate(found_tags):
last_found_tag_idx = found_tag_idx - 1

if found_tag_idx >= len(stored_tags_name) or found_tag.name != stored_tags_name[found_tag_idx]:
if found_tag_idx >= len(stored_versions) or found_tag.name != stored_versions[found_tag_idx]:

# this verification permits to know if it's the first to be added,
# and if it's the case, then we add the one before to permits to make a diff
Expand Down
21 changes: 13 additions & 8 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Unit tests for Config class.
"""
#standard imports
from json import JSONDecodeError
from typing import Dict
from unittest import mock
from unittest.mock import MagicMock, mock_open, patch
Expand Down Expand Up @@ -36,18 +37,22 @@ def test_parse():
config._config = {}

with mock.patch("builtins.open", get_mock_open(files)) as mock_open:
config.parse("wrongly_formatted.json")
assert mock_open.called is True
assert mock_open.call_count == 1
assert len(config._config) == 0
try:
config.parse("wrongly_formatted.json")
except JSONDecodeError:
assert mock_open.called is True
assert mock_open.call_count == 1
assert len(config._config) == 0

config._config = {}

with mock.patch("builtins.open", MagicMock(side_effect=OSError("error"))) as mock_open:
config.parse("tech_list.json")
assert mock_open.called is True
assert mock_open.call_count == 1
assert len(config._config) == 0
try:
config.parse("tech_list.json")
except OSError:
assert mock_open.called is True
assert mock_open.call_count == 1
assert len(config._config) == 0

def test_get_targets():
"""
Expand Down
25 changes: 18 additions & 7 deletions tests/core/test_hashtheplanet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import sys
from typing import Dict
from unittest import mock
from unittest.mock import MagicMock, mock_open, patch
Expand Down Expand Up @@ -118,7 +119,7 @@ def test_find_hash():
htp.find_hash("hash")
find_hash_mock.assert_called_once()

def test_compute_hashs():
def test_compute_hashes():
files = {
"foobar.txt": """
{
Expand Down Expand Up @@ -149,17 +150,27 @@ def test_compute_hashs():
mock_compute_hashes.reset_mock()

htp = HashThePlanet("output.txt", "empty.txt")
htp.compute_hashs()

mock_open.assert_called_once_with("empty.txt", "r", encoding="utf-8")
mock_compute_hashes.assert_not_called()
try:
htp.compute_hashs()
except SystemExit as e:
assert e.code == 1
mock_open.assert_called_once_with("empty.txt", "r", encoding="utf-8")
mock_compute_hashes.assert_not_called()
else:
assert False

with mock.patch.object(Config, "parse", MagicMock(side_effect=OSError("error"))) as mock_open, \
mock.patch.object(logger, "error") as mock_error_logger:

htp = HashThePlanet("output.txt", "foobar.txt")
htp.compute_hashs()

mock_error_logger.assert_called_once()
try:
htp.compute_hashs()
except SystemExit as e:
assert e.code == 1
mock_error_logger.assert_called_once()
else:
assert False

def test_analyze_file():
foobar_hash = "27dd147c7347026fe21b432a2297303bb9990462d886e55facda103598c687fc"
Expand Down
27 changes: 12 additions & 15 deletions tests/resources/test_git_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,20 +290,17 @@ def test_filter_stored_tags():
"""
Should test the behavior of tags when some of them are already downloaded
"""
class MockedVersionTable():
def __init__(self, version) -> None:
self.version = version

class MockedTag():
def __init__(self, name) -> None:
self.name = name

git_resource = GitResource(None)
stored_versions = [
MockedVersionTable("A"),
MockedVersionTable("B"),
MockedVersionTable("C"),
MockedVersionTable("D")
"A",
"B",
"C",
"D"
]
repository_versions = [
MockedTag("A"),
Expand All @@ -321,9 +318,9 @@ def __init__(self, name) -> None:
assert [tag.name for tag in result] == ["D", "E"]

stored_versions = [
MockedVersionTable("A"),
MockedVersionTable("B"),
MockedVersionTable("C"),
"A",
"B",
"C",
]
repository_versions = [
MockedTag("B"),
Expand All @@ -341,10 +338,10 @@ def __init__(self, name) -> None:
assert [tag.name for tag in result] == ["B", "C", "D", "E"]

stored_versions = [
MockedVersionTable("A"),
MockedVersionTable("B"),
MockedVersionTable("D"),
MockedVersionTable("E"),
"A",
"B",
"D",
"E",
]
repository_versions = [
MockedTag("A"),
Expand All @@ -364,7 +361,7 @@ def __init__(self, name) -> None:
assert [tag.name for tag in result] == ["B", "C", "D", "E"]

stored_versions = [
MockedVersionTable("A"),
"A",
]
repository_versions = [
MockedTag("A"),
Expand Down

0 comments on commit 3f1e9c9

Please sign in to comment.