Skip to content

Commit

Permalink
fix mentor's comments 3
Browse files Browse the repository at this point in the history
  • Loading branch information
DSungatulin committed Mar 5, 2024
1 parent 4df4fd7 commit d2fd66c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
3 changes: 2 additions & 1 deletion gendiff/gendiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
def generate_diff(filepath1, filepath2, output_format='stylish'):
config1 = read_file_data(filepath1)
config2 = read_file_data(filepath2)
return determine_output_file_format(get_diff(config1, config2), output_format)
comparison_result = get_diff(config1, config2)
return determine_output_file_format(comparison_result, output_format)
26 changes: 11 additions & 15 deletions gendiff/parser.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import json
import yaml
import os


def get_file_format(filepath):
file = open(filepath)
if filepath.endswith('.json'):
return file, 'json'
elif filepath.endswith('.yml') or filepath.endswith('.yaml'):
return file, 'yaml'
else:
file.close()
raise TypeError("Invalid file format!")
_, file_format = os.path.splitext(filepath)
return file_format[1:]


def read_file_data(filepath):
file, file_format = get_file_format(filepath)
if file_format == 'json':
return json.load(file)
elif file_format == 'yaml':
return yaml.safe_load(file)
else:
raise TypeError("Invalid file format!")
with open(filepath) as file:
file_format = get_file_format(filepath)
if file_format == 'json':
return json.load(file)
elif file_format == 'yaml' or 'yml':
return yaml.load(file, Loader=yaml.Loader)
else:
raise TypeError("Invalid file format!")
21 changes: 14 additions & 7 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import pytest
from gendiff.parser import read_file_data


def test_read_json_file_data():
filepath = 'tests/fixtures/flat_files/file1.json'
expected_data = {'follow': False, 'host': 'hexlet.io', 'proxy': '123.234.53.22', 'timeout': 50}
actual_data = read_file_data(filepath)
assert actual_data == expected_data


def test_read_yaml_file():
filepath = 'tests/fixtures/flat_files/file2.yml'
expected_data = {'host': 'hexlet.io', 'timeout': 20, 'verbose': True}
actual_data = read_file_data(filepath)
assert actual_data == expected_data


def test_determine_file_format():
file1 = read_file_data('tests/fixtures/flat_files/file1.json')
file2 = read_file_data('tests/fixtures/flat_files/file2.yml')
assert file1 == {'follow': False, 'host': 'hexlet.io', 'proxy': '123.234.53.22', 'timeout': 50}
assert file2 == {'host': 'hexlet.io', 'timeout': 20, 'verbose': True}


def test_determine_file_format_invalid():
with pytest.raises(TypeError) as excinfo:
read_file_data('tests/fixtures/comparison_results/correct_flat_comparison.txt')
assert 'Invalid file format!' in str(excinfo.value)

0 comments on commit d2fd66c

Please sign in to comment.