11import contextlib
2+ import tempfile
23import textwrap
34from io import StringIO
45from unittest import mock
56from unittest .mock import MagicMock , mock_open
67
78import pytest
89
10+ from conda_forge_tick .lazy_json_backends import LazyJson
11+ from conda_forge_tick .os_utils import pushd
912from conda_forge_tick .utils import (
1013 DEFAULT_GRAPH_FILENAME ,
1114 _munge_dict_repr ,
@@ -93,24 +96,26 @@ def test_get_keys_default_none():
9396
9497
9598def test_load_graph ():
96- with mock .patch ("builtins.open" , mock_open (read_data = DEMO_GRAPH )) as mock_file :
99+ with tempfile .TemporaryDirectory () as tmpdir , pushd (tmpdir ):
100+ with open (LazyJson (DEFAULT_GRAPH_FILENAME ).sharded_path , "w" ) as fp :
101+ fp .write (DEMO_GRAPH )
102+
97103 gx = load_graph ()
98104
99105 assert gx is not None
100106
101107 assert gx .nodes .keys () == {"package1" , "package2" }
102108
103- mock_file .assert_has_calls ([mock .call (DEFAULT_GRAPH_FILENAME )])
104-
105109
106110def test_load_graph_empty_graph ():
107- with mock .patch ("builtins.open" , mock_open (read_data = EMPTY_JSON )) as mock_file :
111+ with tempfile .TemporaryDirectory () as tmpdir , pushd (tmpdir ):
112+ with open (LazyJson (DEFAULT_GRAPH_FILENAME ).sharded_path , "w" ) as fp :
113+ fp .write (EMPTY_JSON )
114+
108115 gx = load_graph ()
109116
110117 assert gx is None
111118
112- mock_file .assert_has_calls ([mock .call (DEFAULT_GRAPH_FILENAME )])
113-
114119
115120@mock .patch ("os.path.exists" )
116121def test_load_graph_file_does_not_exist (exists_mock : MagicMock ):
@@ -122,24 +127,24 @@ def test_load_graph_file_does_not_exist(exists_mock: MagicMock):
122127 mock_file .assert_has_calls ([mock .call (DEFAULT_GRAPH_FILENAME , "w" )])
123128
124129
125- @mock .patch ("os.path.isfile" )
126- def test_load_existing_graph (isfile_mock : MagicMock ):
127- isfile_mock .return_value = True
128- with mock .patch ("builtins.open" , mock_open (read_data = DEMO_GRAPH )) as mock_file :
130+ def test_load_existing_graph ():
131+ with tempfile .TemporaryDirectory () as tmpdir , pushd (tmpdir ):
132+ with open (LazyJson (DEFAULT_GRAPH_FILENAME ).sharded_path , "w" ) as fp :
133+ fp .write (DEMO_GRAPH )
134+
129135 gx = load_existing_graph ()
130136
131137 assert gx .nodes .keys () == {"package1" , "package2" }
132138
133- mock_file .assert_has_calls ([mock .call (DEFAULT_GRAPH_FILENAME )])
134-
135139
136140def test_load_existing_graph_empty_graph ():
137- with mock .patch ("builtins.open" , mock_open (read_data = EMPTY_JSON )) as mock_file :
141+ with tempfile .TemporaryDirectory () as tmpdir , pushd (tmpdir ):
142+ with open (LazyJson (DEFAULT_GRAPH_FILENAME ).sharded_path , "w" ) as fp :
143+ fp .write (EMPTY_JSON )
144+
138145 with pytest .raises (ValueError , match = "empty JSON" ):
139146 load_existing_graph ()
140147
141- mock_file .assert_has_calls ([mock .call (DEFAULT_GRAPH_FILENAME )])
142-
143148
144149@mock .patch ("os.path.exists" )
145150def test_load_existing_graph_file_does_not_exist (exists_mock : MagicMock ):
0 commit comments