@@ -154,6 +154,84 @@ def test_main_missing_user_in_org_chart(tmp_path, monkeypatch, caplog):
154154
155155 # Should NOT contain the log message about
156156 # "Original commit author: X, with manager: Y"
157- assert not any (isinstance (msg , str ) and "Original commit author:"
158- in msg and "with manager:" in msg for msg in
157+ assert not any (isinstance (msg , str ) and "Original commit author:"
158+ in msg and "with manager:" in msg for msg in
159159 info_calls )
160+
161+
162+ def test_contributors_missing_from_org_chart_excluded (tmp_path , monkeypatch ):
163+ """Test that contributors missing from org chart are excluded from
164+ InnerSource analysis."""
165+ # Switch working directory to tmp_path
166+ monkeypatch .chdir (tmp_path )
167+
168+ # Create org-data.json with some users
169+ org_data = {
170+ "original_author" : {"manager" : "manager1" },
171+ "manager1" : {"manager" : "director1" },
172+ "user1" : {"manager" : "manager1" }
173+ }
174+
175+ org_file = tmp_path / "org-data.json"
176+ org_file .write_text (json .dumps (org_data ), encoding = "utf-8" )
177+
178+ # Mock GitHub repository and commit data
179+ mock_original_author = MagicMock ()
180+ mock_original_author .login = "original_author"
181+
182+ mock_commit = MagicMock ()
183+ mock_commit .author = mock_original_author
184+
185+ # Mock contributors - include one that's not in org_data
186+ mock_contributor1 = MagicMock ()
187+ mock_contributor1 .login = "unknown_contributor" # Not in org_data
188+
189+ mock_repo = MagicMock ()
190+ mock_repo .full_name = "test/repo"
191+ mock_repo .commits .return_value = [mock_commit ]
192+ mock_repo .contributors .return_value = [mock_contributor1 ]
193+ # Mock empty pull requests and issues to avoid infinite loops
194+ mock_repo .pull_requests .return_value = iter ([])
195+ mock_repo .issues .return_value = iter ([])
196+
197+ mock_github = MagicMock ()
198+ mock_github .repository .return_value = mock_repo
199+
200+ # Mock environment variables
201+ mock_env_vars = MagicMock ()
202+ mock_env_vars .github_token = "fake_token"
203+ mock_env_vars .github_enterprise_hostname = None
204+ mock_env_vars .github_org = "test"
205+ mock_env_vars .github_repo = "repo"
206+ mock_env_vars .gh_app_id = None
207+ mock_env_vars .gh_app_installation_id = None
208+ mock_env_vars .gh_app_private_key_bytes = None
209+ mock_env_vars .gh_app_enterprise_only = False
210+ mock_env_vars .report_title = "Test Report"
211+ mock_env_vars .output_file = "test_output.md"
212+ mock_env_vars .chunk_size = 100
213+
214+ # Apply mocks
215+ with patch ('measure_innersource.get_env_vars' ,
216+ return_value = mock_env_vars ), \
217+ patch ('measure_innersource.auth_to_github' ,
218+ return_value = mock_github ), \
219+ patch ('measure_innersource.setup_logging' ) as mock_setup_logging , \
220+ patch ('measure_innersource.write_to_markdown' ), \
221+ patch ('measure_innersource.evaluate_markdown_file_size' ):
222+
223+ # Configure logging to capture our test
224+ mock_logger = MagicMock ()
225+ mock_setup_logging .return_value = mock_logger
226+
227+ with patch ('measure_innersource.get_logger' ,
228+ return_value = mock_logger ):
229+ # Call main function
230+ mi .main ()
231+
232+ # Verify that warning was logged about missing contributor
233+ mock_logger .warning .assert_any_call (
234+ "Contributor '%s' not found in org chart. "
235+ "Excluding from InnerSource analysis." ,
236+ "unknown_contributor"
237+ )
0 commit comments