@@ -1928,6 +1928,40 @@ def test_invalidate_environment_no_sync_skips_cleanup(sushi_context, mocker: Moc
19281928 state_sync_mock .delete_expired_environments .assert_not_called ()
19291929
19301930
1931+ def test_invalidate_environment_nonexistent_raises (sushi_context , mocker : MockerFixture ) -> None :
1932+ """Invalidating an environment that does not exist should error instead of
1933+ reporting success, so a mistyped name is caught rather than silently accepted."""
1934+ state_sync_mock = mocker .patch .object (
1935+ type (sushi_context ), "state_sync" , new_callable = mocker .PropertyMock
1936+ ).return_value
1937+ state_sync_mock .get_environment .return_value = None
1938+
1939+ with pytest .raises (SQLMeshError , match = "Environment 'doesnotexist' was not found" ):
1940+ sushi_context .invalidate_environment ("doesnotexist" , must_exist = True )
1941+
1942+ state_sync_mock .invalidate_environment .assert_not_called ()
1943+
1944+
1945+ def test_invalidate_environment_nonexistent_is_a_noop_by_default (
1946+ sushi_context , mocker : MockerFixture
1947+ ) -> None :
1948+ """Without must_exist, invalidating a missing environment stays a no-op.
1949+
1950+ Internal callers depend on this. `GithubController.try_invalidate_pr_environment`
1951+ invalidates the PR environment after a prod deploy, and that environment may never
1952+ have been created — a forward-only deploy, for instance. Raising there turns a
1953+ routine cleanup into a failed deploy.
1954+ """
1955+ state_sync_mock = mocker .patch .object (
1956+ type (sushi_context ), "state_sync" , new_callable = mocker .PropertyMock
1957+ ).return_value
1958+ state_sync_mock .get_environment .return_value = None
1959+
1960+ sushi_context .invalidate_environment ("doesnotexist" )
1961+
1962+ state_sync_mock .invalidate_environment .assert_called_once_with ("doesnotexist" )
1963+
1964+
19311965@pytest .mark .slow
19321966def test_plan_default_end (sushi_context_pre_scheduling : Context ):
19331967 prod_plan_builder = sushi_context_pre_scheduling .plan_builder ("prod" )
0 commit comments