1#!/usr/bin/env python3
2# -*- coding:utf-8 -*-
3# Copyright 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Integration tests for the config module (via PREUPLOAD.cfg)."""
18
19from __future__ import print_function
20
21import argparse
22import os
23import re
24import sys
25
26
27REPOTOOLS = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
28REPO_ROOT = os.path.dirname(os.path.dirname(REPOTOOLS))
29
30
31def assertEqual(msg, exp, actual):
32    """Assert |exp| equals |actual|."""
33    assert exp == actual, '%s: expected "%s" but got "%s"' % (msg, exp, actual)
34
35
36def assertEnv(var, value):
37    """Assert |var| is set in the environment as |value|."""
38    assert var in os.environ, '$%s missing in environment' % (var,)
39    assertEqual('env[%s]' % (var,), value, os.environ[var])
40
41
42def check_commit_id(commit):
43    """Check |commit| looks like a git commit id."""
44    assert len(commit) == 40, 'commit "%s" must be 40 chars' % (commit,)
45    assert re.match(r'^[a-f0-9]+$', commit), \
46        'commit "%s" must be all hex' % (commit,)
47
48
49def check_commit_msg(msg):
50    """Check the ${PREUPLOAD_COMMIT_MESSAGE} setting."""
51    assert len(msg) > 1, 'commit message must be at least 2 bytes: %s'
52
53
54def check_repo_root(root):
55    """Check the ${REPO_ROOT} setting."""
56    assertEqual('REPO_ROOT', REPO_ROOT, root)
57
58
59def check_files(files):
60    """Check the ${PREUPLOAD_FILES} setting."""
61    assert files
62
63
64def check_env():
65    """Verify all exported env vars look sane."""
66    assertEnv('REPO_PROJECT', 'platform/tools/repohooks')
67    assertEnv('REPO_PATH', 'tools/repohooks')
68    assertEnv('REPO_REMOTE', 'aosp')
69    check_commit_id(os.environ['REPO_LREV'])
70    print(os.environ['REPO_RREV'])
71    check_commit_id(os.environ['PREUPLOAD_COMMIT'])
72
73
74def get_parser():
75    """Return a command line parser."""
76    parser = argparse.ArgumentParser(description=__doc__)
77    parser.add_argument('--check-env', action='store_true',
78                        help='Check all exported env vars.')
79    parser.add_argument('--commit-id',
80                        help='${PREUPLOAD_COMMIT} setting.')
81    parser.add_argument('--commit-msg',
82                        help='${PREUPLOAD_COMMIT_MESSAGE} setting.')
83    parser.add_argument('--repo-root',
84                        help='${REPO_ROOT} setting.')
85    parser.add_argument('files', nargs='+',
86                        help='${PREUPLOAD_FILES} paths.')
87    return parser
88
89
90def main(argv):
91    """The main entry."""
92    parser = get_parser()
93    opts = parser.parse_args(argv)
94
95    try:
96        if opts.check_env:
97            check_env()
98        if opts.commit_id is not None:
99            check_commit_id(opts.commit_id)
100        if opts.commit_msg is not None:
101            check_commit_msg(opts.commit_msg)
102        if opts.repo_root is not None:
103            check_repo_root(opts.repo_root)
104        check_files(opts.files)
105    except AssertionError as e:
106        print('error: %s' % (e,), file=sys.stderr)
107        return 1
108
109    return 0
110
111
112if __name__ == '__main__':
113    sys.exit(main(sys.argv[1:]))
114