File: roll_merge_gerrit_test.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 3,491 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env vpython3
# Copyright 2023 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import base64
import contextlib
import io
import os
import sys
import unittest

from mock import patch
from pathlib import Path

import roll_merge_gerrit


TEST_DATA = Path(__file__).resolve().parent / 'testdata'
HAPPY_PATH_LOG = TEST_DATA / 'roll_merge_gerrit_happy_path.txt'

ROLL_COMMIT_MESSAGE = """V8 roll from 123abc to 456def

https://chromium.googlesource.com/v8/v8.git/+log/123abc..456def

2038-01-19 v8-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com Version 1.2.3
"""

V8_VERSION_FILE_AT_CHERRY_PICK = """
#define V8_MAJOR_VERSION 1
#define V8_MINOR_VERSION 2
#define V8_BUILD_NUMBER 3
#define V8_PATCH_LEVEL 0
""".encode('utf-8')

V8_VERSION_FILE_AT_ROLL = """
#define V8_MAJOR_VERSION 1
#define V8_MINOR_VERSION 2
#define V8_BUILD_NUMBER 1
#define V8_PATCH_LEVEL 0
""".encode('utf-8')

V8_VERSION_ROLL_RESPONSE = io.StringIO(
    base64.b64encode(V8_VERSION_FILE_AT_ROLL).decode('utf-8'))


class TestStats(unittest.TestCase):

  @patch(
      'gerrit_util.QueryChanges',
      return_value=[{
          'subject': ROLL_COMMIT_MESSAGE.splitlines()[0],
          'current_revision': '789acf',
          'revisions': {
              '789acf': {
                  'commit': {
                      'message': ROLL_COMMIT_MESSAGE
                  }
              }
          }
      }])
  @patch(
      'gerrit_util.CherryPick', return_value={
          '_number': 42,
          'change_id': 23
      })
  @patch(
      'gerrit_util.GetFileContents',
      return_value=V8_VERSION_FILE_AT_CHERRY_PICK)
  @patch(
      'gerrit_util.CallGerritApi',
      side_effect=[
          None, {
              'labels': {
                  'Code-Review': {
                      'all': [{
                          'value': 1
                      }]
                  }
              }
          }, {
              'ref': 'refs/heads/roll',
              'revision': 'beefdead'
          }, {
              'revision': 'deadbeefce'
          }
      ])
  @patch('gerrit_util.ChangeEdit')
  @patch('gerrit_util.GetChangeCommit', side_effect=[
      {'commit': 'deadbeef', 'subject': 'Fix everything'},
      {'commit': 'deadbeefce'}])
  @patch('gerrit_util.SetChangeEditMessage')
  @patch('gerrit_util.PublishChangeEdit')
  @patch('gerrit_util.SetReview')
  @patch('gerrit_util.AddReviewers')
  @patch('gerrit_util.SubmitChange',
         return_value={'status': 'MERGED', 'project': 'v8/v8'})
  @patch('gerrit_util.CreateGerritTag')
  @patch('gerrit_util.CreateHttpConn')
  @patch('gerrit_util.ReadHttpResponse',
         return_value=V8_VERSION_ROLL_RESPONSE)
  def test_happy_path(self, *args):
    """Test the path that succeeds in every step.

    The test data above is composed of dummy values, designed
    to get the script through from end to end.
    """
    stdout = io.StringIO()
    with contextlib.redirect_stdout(stdout):
      roll_merge_gerrit.main(['deadbeef'])
    actual_stdout = stdout.getvalue().replace('\r', '')
    if os.environ.get('GENERATE') == 'true':
      with open(HAPPY_PATH_LOG, 'w') as f:
        f.write(actual_stdout)
    with open(HAPPY_PATH_LOG) as f:
      self.assertEqual(
          f.read(),
          actual_stdout,
          'Call testing with the GENERATE=true env var to update the log.')


if __name__ == '__main__':
  unittest.main()