1+ import os
2+ import tempfile
3+
4+ import pytest
5+
16from gendiff .gendiff import generate_diff , read_file_json , sort_list
27
3- path1 = '/home/zk/python-project-50/file1.json'
8+
9+ @pytest .fixture
10+ def temp_dir_with_files ():
11+ with tempfile .TemporaryDirectory () as temp_dir :
12+ file1_path = os .path .join (temp_dir , 'file1.json' )
13+ file2_path = os .path .join (temp_dir , 'file2.json' )
14+
15+ with open (file1_path , 'w' ) as f :
16+ f .write ('{\n '
17+ '"host": "hexlet.io",\n '
18+ '"timeout": 50,\n '
19+ '"proxy": "123.234.53.22",\n '
20+ '"follow": false\n '
21+ '}' )
22+ with open (file2_path , 'w' ) as f :
23+ f .write ('{\n '
24+ '"timeout": 20,\n '
25+ '"verbose": true,\n '
26+ '"host": "hexlet.io"\n '
27+ '}' )
28+
29+ yield temp_dir
30+
31+
432data1 = {
533 "host" : "hexlet.io" ,
634 "timeout" : 50 ,
735 "proxy" : "123.234.53.22" ,
836 "follow" : False
937}
1038
11- path2 = '/home/zk/python-project-50/file2.json'
1239data2 = {
1340 "timeout" : 20 ,
1441 "verbose" : True ,
1542 "host" : "hexlet.io"
1643}
1744
1845
19- def test_read_file_json ():
20- assert read_file_json (path1 ) == data1
46+ def test_read_file_json (temp_dir_with_files ):
47+ file1_path = os .path .join (temp_dir_with_files , 'file1.json' )
48+ file2_path = os .path .join (temp_dir_with_files , 'file2.json' )
49+ assert read_file_json (file1_path ) == data1
2150
22- assert read_file_json (path2 ) == data2
51+ assert read_file_json (file2_path ) == data2
2352
2453
2554def test_sort_list ():
@@ -35,7 +64,7 @@ def test_sort_list():
3564 ]
3665
3766
38- def test_generate_diff ():
67+ def test_generate_diff (temp_dir_with_files ):
3968 right_str = ("{\n "
4069 " - follow: false\n "
4170 " host: hexlet.io\n "
@@ -44,4 +73,6 @@ def test_generate_diff():
4473 " + timeout: 20\n "
4574 " + verbose: true\n "
4675 "}" )
47- assert generate_diff (path1 , path2 ) == right_str
76+ file1_path = os .path .join (temp_dir_with_files , 'file1.json' )
77+ file2_path = os .path .join (temp_dir_with_files , 'file2.json' )
78+ assert generate_diff (file1_path , file2_path ) == right_str
0 commit comments