File: roll_merge_gerrit_test.py

package info (click to toggle)
nodejs 22.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 246,928 kB
  • sloc: cpp: 1,582,349; javascript: 582,017; ansic: 82,400; python: 60,561; sh: 4,009; makefile: 2,263; asm: 1,732; pascal: 1,565; perl: 248; lisp: 222; xml: 42
file content (87 lines) | stat: -rw-r--r-- 2,763 bytes parent folder | download | duplicates (4)
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
#!/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'

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': 'Update V8 to version 1.2.3'}])
  @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()