@@ -245,9 +245,12 @@ def test_the_format_asks_git_for_the_hash_and_the_body(self):
245245 self .assertEqual (main .COMMIT_LOG_FORMAT , "--pretty=format:%H%x00%B%x00" )
246246 self .assertNotIn ("\x00 " , main .COMMIT_LOG_FORMAT )
247247
248- def test_an_empty_message_does_not_shift_the_hashes_after_it (self ):
248+ def test_an_empty_message_is_kept_and_does_not_shift_the_hashes (self ):
249249 output = f"{ SHA_A } \x00 \n \x00 \n { SHA_B } \x00 feat: second\n \x00 "
250- self .assertEqual (main .parse_commit_messages (output ), [(SHA_B , "feat: second" )])
250+ self .assertEqual (
251+ main .parse_commit_messages (output ),
252+ [(SHA_A , "" ), (SHA_B , "feat: second" )],
253+ )
251254
252255 def test_empty_output_is_no_commits (self ):
253256 self .assertEqual (main .parse_commit_messages ("" ), [])
@@ -2455,6 +2458,7 @@ def test_updates_existing_comment_when_changed(self):
24552458 def test_skips_when_comment_is_up_to_date (self ):
24562459 body = main .render_pr_comment ([fail_scope ()])
24572460 existing = MagicMock (body = body )
2461+ existing .user .type = "Bot"
24582462 mock_pull_request = MagicMock ()
24592463 mock_pull_request .get_comments .return_value = [existing ]
24602464 mock_repo = MagicMock ()
@@ -2904,6 +2908,14 @@ def test_bot_comment_from_an_older_version_is_adopted(self):
29042908 self .assertIs (target , legacy )
29052909 self .assertEqual (stale , [])
29062910
2911+ def test_human_comment_with_the_marker_is_never_touched (self ):
2912+ """A person can paste the marker too, e.g. quoting a report."""
2913+ quoted = self ._comment (f"{ main .COMMENT_MARKER } \n quoted" , user_type = "User" )
2914+ mine = self ._comment (f"{ main .COMMENT_MARKER } \n report" )
2915+ self .assertEqual (main ._find_own_comments ([quoted , mine ]), (mine , []))
2916+ self .assertEqual (main ._find_own_comments ([mine , quoted ]), (mine , []))
2917+ self .assertEqual (main ._find_own_comments ([quoted ]), (None , []))
2918+
29072919 def test_no_comments_yields_nothing_to_update (self ):
29082920 target , stale = main ._find_own_comments ([])
29092921 self .assertIsNone (target )
@@ -4096,3 +4108,116 @@ def test_a_missing_pygithub_warns(self):
40964108
40974109if __name__ == "__main__" :
40984110 unittest .main ()
4111+
4112+
4113+ class TestBooleanInputs (unittest .TestCase ):
4114+ """A boolean input is true or false; anything else fails the step."""
4115+
4116+ def setUp (self ):
4117+ main .INVALID_FLAGS .clear ()
4118+ self .addCleanup (main .INVALID_FLAGS .clear )
4119+
4120+ def test_true_and_false_in_any_case_and_padding (self ):
4121+ for value , expected in (
4122+ ("true" , True ),
4123+ (" True " , True ),
4124+ ("FALSE" , False ),
4125+ ("false" , False ),
4126+ ):
4127+ with patch .dict (os .environ , {"MESSAGE" : value }):
4128+ self .assertIs (main .env_flag ("MESSAGE" ), expected , value )
4129+ self .assertEqual (main .INVALID_FLAGS , [])
4130+
4131+ def test_unset_input_takes_the_default (self ):
4132+ with patch .dict (os .environ , {}, clear = True ):
4133+ self .assertFalse (main .env_flag ("MESSAGE" ))
4134+ self .assertEqual (main .INVALID_FLAGS , [])
4135+
4136+ def test_other_values_are_recorded (self ):
4137+ for value in ("yes" , "on" , "1" , "maybe" ):
4138+ with patch .dict (os .environ , {"MESSAGE" : value }):
4139+ self .assertFalse (main .env_flag ("MESSAGE" ))
4140+ self .assertEqual (
4141+ main .INVALID_FLAGS ,
4142+ [
4143+ ("MESSAGE" , "yes" ),
4144+ ("MESSAGE" , "on" ),
4145+ ("MESSAGE" , "1" ),
4146+ ("MESSAGE" , "maybe" ),
4147+ ],
4148+ )
4149+
4150+ def test_invalid_input_fails_before_any_check (self ):
4151+ main .INVALID_FLAGS .append (("PR_TITLE" , "yes" ))
4152+ out = io .StringIO ()
4153+ with (
4154+ patch ("sys.stdout" , out ),
4155+ patch ("main.run_commit_check" ) as run ,
4156+ self .assertRaises (SystemExit ) as cm ,
4157+ ):
4158+ main .main ()
4159+ self .assertEqual (cm .exception .code , 1 )
4160+ run .assert_not_called ()
4161+ self .assertIn (
4162+ "::error::Input pr-title must be true or false, got 'yes'" , out .getvalue ()
4163+ )
4164+
4165+
4166+ class TestPrCommentApiUrl (unittest .TestCase ):
4167+ """The PR comment goes to the same API host as every other call (GHES)."""
4168+
4169+ def test_comment_client_uses_github_api_url (self ):
4170+ github_module = MagicMock ()
4171+ pull_request = MagicMock ()
4172+ pull_request .get_comments .return_value = []
4173+ github_module .Github .return_value .get_repo .return_value .get_issue .return_value = pull_request
4174+ with (
4175+ patch ("main.PR_COMMENTS_ENABLED" , True ),
4176+ patch ("main.is_fork_pr_with_readonly_token" , return_value = False ),
4177+ patch ("main.get_pr_number" , return_value = 7 ),
4178+ patch .dict (
4179+ os .environ ,
4180+ {
4181+ "GITHUB_EVENT_NAME" : "pull_request" ,
4182+ "GITHUB_TOKEN" : "t" ,
4183+ "GITHUB_REPOSITORY" : "o/r" ,
4184+ "GITHUB_API_URL" : "https://ghe.example.com/api/v3" ,
4185+ },
4186+ ),
4187+ patch .dict (sys .modules , {"github" : github_module }),
4188+ ):
4189+ main .add_pr_comments ([fail_scope ()])
4190+ _ , kwargs = github_module .Github .call_args
4191+ self .assertEqual (kwargs ["base_url" ], "https://ghe.example.com/api/v3" )
4192+
4193+
4194+ class TestEmptyCommitMessage (unittest .TestCase ):
4195+ """An empty message is checked as empty, never as HEAD's message."""
4196+
4197+ def test_an_empty_message_goes_to_the_cli_as_an_empty_file (self ):
4198+ seen : list [tuple [list [str ], str | None , str ]] = []
4199+
4200+ def run (command , ** kwargs ):
4201+ path = command [- 1 ]
4202+ with open (path , encoding = "utf-8" ) as f :
4203+ seen .append ((command , kwargs .get ("input" ), f .read ()))
4204+ return MagicMock (
4205+ returncode = 1 ,
4206+ stdout = json_output (make_check ("allow_empty_commits" , status = "fail" )),
4207+ stderr = "" ,
4208+ )
4209+
4210+ with (
4211+ patch ("main.subprocess.run" , side_effect = run ),
4212+ patch ("main._local_author" , return_value = None ),
4213+ ):
4214+ results = main .run_pr_message_checks ([("a1" , "" )])
4215+ command , piped , content = seen [0 ]
4216+ self .assertEqual (command [:4 ], ["commit-check" , "--format" , "json" , "--message" ])
4217+ self .assertIsNone (piped )
4218+ self .assertEqual (content , "" )
4219+ self .assertFalse (os .path .exists (command [- 1 ]))
4220+ self .assertEqual (
4221+ [(s .label , s .sha , s .status ) for s in results ],
4222+ [("Commit 1/1" , "a1" , "fail" )],
4223+ )
0 commit comments