1#!/usr/bin/env python3 2# Copyright 2024 The ChromiumOS Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""Tests for git_utils.""" 6 7import unittest 8 9from cros_utils import git_utils 10 11 12# pylint: disable=protected-access 13 14GERRIT_OUTPUT_WITH_ONE_CL = r""" 15Enumerating objects: 4, done. 16Counting objects: 100% (4/4), done. 17Delta compression using up to 128 threads 18Compressing objects: 100% (2/2), done. 19Writing objects: 100% (3/3), 320 bytes | 106.00 KiB/s, done. 20Total 3 (delta 1), reused 1 (delta 0), pack-reused 0 (from 0) 21remote: Resolving deltas: 100% (1/1) 22remote: Processing changes: refs: 1, new: 1, done 23remote: 24remote: SUCCESS 25remote: 26remote: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5375204 DO NOT COMMIT [WIP] [NEW] 27remote: 28To https://chromium.googlesource.com/chromiumos/third_party/toolchain-utils 29 * [new reference] HEAD -> refs/for/main 30""" 31 32GERRIT_OUTPUT_WITH_TWO_CLS = r""" 33Enumerating objects: 4, done. 34Counting objects: 100% (4/4), done. 35Delta compression using up to 128 threads 36Compressing objects: 100% (2/2), done. 37Writing objects: 100% (3/3), 320 bytes | 106.00 KiB/s, done. 38Total 3 (delta 1), reused 1 (delta 0), pack-reused 0 (from 0) 39remote: Resolving deltas: 100% (1/1) 40remote: Processing changes: refs: 1, new: 1, done 41remote: 42remote: SUCCESS 43remote: 44remote: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5375204 DO NOT COMMIT [WIP] [NEW] 45remote: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5375205 DO NOT COMMIT [WIP] [NEW] 46remote: 47To https://chromium.googlesource.com/chromiumos/third_party/toolchain-utils 48 * [new reference] HEAD -> refs/for/main 49""" 50 51 52GERRIT_OUTPUT_WITH_INTERNAL_CL = r""" 53Upload project manifest-internal/ to remote branch refs/heads/main: 54 branch DO-NOT-COMMIT ( 1 commit, Tue Apr 16 08:51:25 2024 -0600): 55 456aadd0 DO NOT COMMIT 56to https://chrome-internal-review.googlesource.com (y/N)? <--yes> 57Enumerating objects: 5, done. 58Counting objects: 100% (5/5), done. 59Delta compression using up to 128 threads 60Compressing objects: 100% (3/3), done. 61Writing objects: 100% (3/3), 334 bytes | 334.00 KiB/s, done. 62Total 3 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0) 63remote: Resolving deltas: 100% (2/2) 64remote: Waiting for private key checker: 1/1 objects left 65remote: Processing changes: refs: 1, new: 1, done 66remote: 67remote: SUCCESS 68remote: 69remote: https://chrome-internal-review.googlesource.com/c/chromeos/manifest-internal/+/7190037 DO NOT COMMIT [NEW] 70remote: 71To https://chrome-internal-review.googlesource.com/chromeos/manifest-internal 72 * [new reference] DO-NOT-COMMIT -> refs/for/main 73 74---------------------------------------------------------------------- 75[OK ] manifest-internal/ DO-NOT-COMMIT 76""" 77 78 79class Test(unittest.TestCase): 80 """Tests for git_utils.""" 81 82 def test_cl_parsing_complains_if_no_output(self): 83 with self.assertRaisesRegex(ValueError, ".*; found 0"): 84 git_utils._parse_cls_from_upload_output("") 85 86 def test_cl_parsing_works_with_one_cl(self): 87 self.assertEqual( 88 git_utils._parse_cls_from_upload_output(GERRIT_OUTPUT_WITH_ONE_CL), 89 [5375204], 90 ) 91 92 def test_cl_parsing_works_with_two_cls(self): 93 self.assertEqual( 94 git_utils._parse_cls_from_upload_output(GERRIT_OUTPUT_WITH_TWO_CLS), 95 [5375204, 5375205], 96 ) 97 98 def test_cl_parsing_works_with_internal_cl(self): 99 self.assertEqual( 100 git_utils._parse_cls_from_upload_output( 101 GERRIT_OUTPUT_WITH_INTERNAL_CL 102 ), 103 [7190037], 104 ) 105 106 107if __name__ == "__main__": 108 unittest.main() 109