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