2222
2323from cli import (
2424 GENERATE_REQUEST_FILE ,
25+ BUILD_REQUEST_FILE ,
2526 LIBRARIAN_DIR ,
2627 REPO_DIR ,
2728 _build_bazel_target ,
@@ -60,6 +61,26 @@ def mock_generate_request_file(tmp_path, monkeypatch):
6061 return request_file
6162
6263
64+ @pytest .fixture
65+ def mock_build_request_file (tmp_path , monkeypatch ):
66+ """Creates the mock request file at the correct path inside a temp dir."""
67+ # Create the path as expected by the script: .librarian/build-request.json
68+ request_path = f"{ LIBRARIAN_DIR } /{ BUILD_REQUEST_FILE } "
69+ request_dir = tmp_path / os .path .dirname (request_path )
70+ request_dir .mkdir ()
71+ request_file = request_dir / os .path .basename (request_path )
72+
73+ request_content = {
74+ "id" : "google-cloud-language" ,
75+ "apis" : [{"path" : "google/cloud/language/v1" }],
76+ }
77+ request_file .write_text (json .dumps (request_content ))
78+
79+ # Change the current working directory to the temp path for the test.
80+ monkeypatch .chdir (tmp_path )
81+ return request_file
82+
83+
6384@pytest .fixture
6485def mock_generate_request_data_for_nox ():
6586 """Returns mock data for generate-request.json for nox tests."""
@@ -298,14 +319,15 @@ def test_run_individual_session_success(mocker, caplog):
298319
299320 test_session = "unit-3.9"
300321 test_library_id = "test-library"
301- _run_individual_session (test_session , test_library_id )
322+ repo = "repo"
323+ _run_individual_session (test_session , test_library_id , repo )
302324
303325 expected_command = [
304326 "nox" ,
305327 "-s" ,
306328 test_session ,
307329 "-f" ,
308- f"{ REPO_DIR } /packages/{ test_library_id } " ,
330+ f"{ REPO_DIR } /packages/{ test_library_id } /noxfile.py " ,
309331 ]
310332 mock_subprocess_run .assert_called_once_with (expected_command , text = True , check = True )
311333
@@ -320,23 +342,25 @@ def test_run_individual_session_failure(mocker):
320342 )
321343
322344 with pytest .raises (subprocess .CalledProcessError ):
323- _run_individual_session ("lint" , "another-library" )
345+ _run_individual_session ("lint" , "another-library" , "repo" )
324346
325347
326- def test_run_nox_sessions_success (mocker , mock_generate_request_data_for_nox ):
348+ def test_run_nox_sessions_success (
349+ mocker , mock_generate_request_data_for_nox , mock_build_request_file
350+ ):
327351 """Tests that _run_nox_sessions successfully runs all specified sessions."""
328352 mocker .patch ("cli._read_json_file" , return_value = mock_generate_request_data_for_nox )
329353 mocker .patch ("cli._get_library_id" , return_value = "mock-library" )
330354 mock_run_individual_session = mocker .patch ("cli._run_individual_session" )
331355
332356 sessions_to_run = ["unit-3.9" , "lint" ]
333- _run_nox_sessions (sessions_to_run , "librarian" )
357+ _run_nox_sessions (sessions_to_run , "librarian" , "repo" )
334358
335359 assert mock_run_individual_session .call_count == len (sessions_to_run )
336360 mock_run_individual_session .assert_has_calls (
337361 [
338- mocker .call ("unit-3.9" , "mock-library" ),
339- mocker .call ("lint" , "mock-library" ),
362+ mocker .call ("unit-3.9" , "mock-library" , "repo" ),
363+ mocker .call ("lint" , "mock-library" , "repo" ),
340364 ]
341365 )
342366
@@ -346,7 +370,7 @@ def test_run_nox_sessions_read_file_failure(mocker):
346370 mocker .patch ("cli._read_json_file" , side_effect = FileNotFoundError ("file not found" ))
347371
348372 with pytest .raises (ValueError , match = "Failed to run the nox session" ):
349- _run_nox_sessions (["unit-3.9" ], "librarian" )
373+ _run_nox_sessions (["unit-3.9" ], "librarian" , "repo" )
350374
351375
352376def test_run_nox_sessions_get_library_id_failure (mocker ):
@@ -358,7 +382,7 @@ def test_run_nox_sessions_get_library_id_failure(mocker):
358382 )
359383
360384 with pytest .raises (ValueError , match = "Failed to run the nox session" ):
361- _run_nox_sessions (["unit-3.9" ], "librarian" )
385+ _run_nox_sessions (["unit-3.9" ], "librarian" , "repo" )
362386
363387
364388def test_run_nox_sessions_individual_session_failure (
@@ -374,7 +398,7 @@ def test_run_nox_sessions_individual_session_failure(
374398
375399 sessions_to_run = ["unit-3.9" , "lint" ]
376400 with pytest .raises (ValueError , match = "Failed to run the nox session" ):
377- _run_nox_sessions (sessions_to_run , "librarian" )
401+ _run_nox_sessions (sessions_to_run , "librarian" , "repo" )
378402
379403 # Check that _run_individual_session was called at least once
380404 assert mock_run_individual_session .call_count > 0
@@ -416,16 +440,17 @@ def test_invalid_json(mocker):
416440 with pytest .raises (json .JSONDecodeError ):
417441 _read_json_file ("fake/path.json" )
418442
443+
419444def test_copy_files_needed_for_post_processing_success (mocker ):
420445 mock_makedirs = mocker .patch ("os.makedirs" )
421446 mock_shutil_copy = mocker .patch ("shutil.copy" )
422- _copy_files_needed_for_post_processing (' output' , ' input' , ' library_id' )
423-
447+ _copy_files_needed_for_post_processing (" output" , " input" , " library_id" )
448+
424449 mock_makedirs .assert_called ()
425450 mock_shutil_copy .assert_called_once ()
426451
427452
428453def test_clean_up_files_after_post_processing_success (mocker ):
429454 mock_shutil_rmtree = mocker .patch ("shutil.rmtree" )
430455 mock_os_remove = mocker .patch ("os.remove" )
431- _clean_up_files_after_post_processing (' output' , ' library_id' )
456+ _clean_up_files_after_post_processing (" output" , " library_id" )
0 commit comments