@@ -64,9 +64,8 @@ def test_closes_early_if_already_added(self, mocker):
6464 site_packages = "./site-packages"
6565
6666 hub_init_file = MockFile ()
67- ns_init_file = MockFile ()
6867 mock_open = mocker .patch ("guardrails.hub.validator_package_service.open" )
69- mock_open .side_effect = [hub_init_file , ns_init_file ]
68+ mock_open .side_effect = [hub_init_file ]
7069
7170 mock_hub_read = mocker .patch .object (hub_init_file , "read" )
7271 mock_hub_read .return_value = (
@@ -77,29 +76,14 @@ def test_closes_early_if_already_added(self, mocker):
7776 hub_write_spy = mocker .spy (hub_init_file , "write" )
7877 hub_close_spy = mocker .spy (hub_init_file , "close" )
7978
80- mock_ns_read = mocker .patch .object (ns_init_file , "read" )
81- mock_ns_read .return_value = (
82- "from guardrails_ai_grhub_id import helper, TestValidator" # noqa
83- )
84-
85- ns_seek_spy = mocker .spy (ns_init_file , "seek" )
86- ns_write_spy = mocker .spy (ns_init_file , "write" )
87- ns_close_spy = mocker .spy (ns_init_file , "close" )
88-
89- mock_is_file = mocker .patch (
90- "guardrails.hub.validator_package_service.os.path.isfile"
91- )
92- mock_is_file .return_value = True
93-
9479 from guardrails .hub .validator_package_service import ValidatorPackageService
9580
9681 manifest = cast (Manifest , manifest )
9782 ValidatorPackageService .add_to_hub_inits (manifest , site_packages )
9883
99- assert mock_open .call_count == 2
84+ assert mock_open .call_count == 1
10085 open_calls = [
10186 call ("./site-packages/guardrails/hub/__init__.py" , "a+" ),
102- call ("./site-packages/guardrails/hub/guardrails_ai/__init__.py" , "a+" ),
10387 ]
10488 mock_open .assert_has_calls (open_calls )
10589
@@ -108,14 +92,6 @@ def test_closes_early_if_already_added(self, mocker):
10892 assert hub_write_spy .call_count == 0
10993 assert hub_close_spy .call_count == 1
11094
111- mock_is_file .assert_called_once_with (
112- "./site-packages/guardrails/hub/guardrails_ai/__init__.py"
113- )
114- assert ns_seek_spy .call_count == 1
115- assert mock_ns_read .call_count == 1
116- assert ns_write_spy .call_count == 0
117- assert ns_close_spy .call_count == 1
118-
11995 def test_appends_import_line_if_not_present (self , mocker ):
12096 manifest = Manifest .from_dict (
12197 {
@@ -135,9 +111,8 @@ def test_appends_import_line_if_not_present(self, mocker):
135111 site_packages = "./site-packages"
136112
137113 hub_init_file = MockFile ()
138- ns_init_file = MockFile ()
139114 mock_open = mocker .patch ("guardrails.hub.validator_package_service.open" )
140- mock_open .side_effect = [hub_init_file , ns_init_file ]
115+ mock_open .side_effect = [hub_init_file ]
141116
142117 mock_hub_read = mocker .patch .object (hub_init_file , "read" )
143118 mock_hub_read .return_value = "from guardrails.hub.other_org.other_validator.validator import OtherValidator" # noqa
@@ -146,27 +121,14 @@ def test_appends_import_line_if_not_present(self, mocker):
146121 hub_write_spy = mocker .spy (hub_init_file , "write" )
147122 hub_close_spy = mocker .spy (hub_init_file , "close" )
148123
149- mock_ns_read = mocker .patch .object (ns_init_file , "read" )
150- mock_ns_read .return_value = ""
151-
152- ns_seek_spy = mocker .spy (ns_init_file , "seek" )
153- ns_write_spy = mocker .spy (ns_init_file , "write" )
154- ns_close_spy = mocker .spy (ns_init_file , "close" )
155-
156- mock_is_file = mocker .patch (
157- "guardrails.hub.validator_package_service.os.path.isfile"
158- )
159- mock_is_file .return_value = True
160-
161124 from guardrails .hub .validator_package_service import ValidatorPackageService
162125
163126 manifest = cast (Manifest , manifest )
164127 ValidatorPackageService .add_to_hub_inits (manifest , site_packages )
165128
166- assert mock_open .call_count == 2
129+ assert mock_open .call_count == 1
167130 open_calls = [
168131 call ("./site-packages/guardrails/hub/__init__.py" , "a+" ),
169- call ("./site-packages/guardrails/hub/guardrails_ai/__init__.py" , "a+" ),
170132 ]
171133 mock_open .assert_has_calls (open_calls )
172134
@@ -187,21 +149,6 @@ def test_appends_import_line_if_not_present(self, mocker):
187149
188150 assert hub_close_spy .call_count == 1
189151
190- mock_is_file .assert_called_once_with (
191- "./site-packages/guardrails/hub/guardrails_ai/__init__.py"
192- )
193-
194- assert ns_seek_spy .call_count == 2
195- ns_seek_calls = [call (0 , 0 ), call (0 , 2 )]
196- ns_seek_spy .assert_has_calls (ns_seek_calls )
197-
198- assert mock_ns_read .call_count == 1
199- assert ns_write_spy .call_count == 1
200- ns_write_spy .assert_called_once_with (
201- "from guardrails_ai_grhub_id import TestValidator" # noqa
202- )
203- assert ns_close_spy .call_count == 1
204-
205152 def test_creates_namespace_init_if_not_exists (self , mocker ):
206153 manifest = Manifest .from_dict (
207154 {
@@ -221,20 +168,12 @@ def test_creates_namespace_init_if_not_exists(self, mocker):
221168 site_packages = "./site-packages"
222169
223170 hub_init_file = MockFile ()
224- ns_init_file = MockFile ()
225171 mock_open = mocker .patch ("guardrails.hub.validator_package_service.open" )
226- mock_open .side_effect = [hub_init_file , ns_init_file ]
172+ mock_open .side_effect = [hub_init_file ]
227173
228174 mock_hub_read = mocker .patch .object (hub_init_file , "read" )
229175 mock_hub_read .return_value = "from guardrails_ai_grhub_id import TestValidator" # noqa
230176
231- mock_ns_read = mocker .patch .object (ns_init_file , "read" )
232- mock_ns_read .return_value = ""
233-
234- ns_seek_spy = mocker .spy (ns_init_file , "seek" )
235- ns_write_spy = mocker .spy (ns_init_file , "write" )
236- ns_close_spy = mocker .spy (ns_init_file , "close" )
237-
238177 mock_is_file = mocker .patch (
239178 "guardrails.hub.validator_package_service.os.path.isfile"
240179 )
@@ -245,25 +184,12 @@ def test_creates_namespace_init_if_not_exists(self, mocker):
245184 manifest = cast (Manifest , manifest )
246185 ValidatorPackageService .add_to_hub_inits (manifest , site_packages )
247186
248- assert mock_open .call_count == 2
187+ assert mock_open .call_count == 1
249188 open_calls = [
250189 call ("./site-packages/guardrails/hub/__init__.py" , "a+" ),
251- call ("./site-packages/guardrails/hub/guardrails_ai/__init__.py" , "w" ),
252190 ]
253191 mock_open .assert_has_calls (open_calls )
254192
255- mock_is_file .assert_called_once_with (
256- "./site-packages/guardrails/hub/guardrails_ai/__init__.py"
257- )
258-
259- assert ns_seek_spy .call_count == 0
260- assert mock_ns_read .call_count == 0
261- assert ns_write_spy .call_count == 1
262- ns_write_spy .assert_called_once_with (
263- "from guardrails_ai_grhub_id import TestValidator" # noqa
264- )
265- assert ns_close_spy .call_count == 1
266-
267193
268194class TestReloadModule :
269195 @patch ("guardrails.hub.validator_package_service.importlib" )
0 commit comments