blob: a98c4130c461a15cb3d7adaf20bb154d8b38f132 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Dan Willemsen745b4ad2015-10-06 15:23:19 -07002#
3# Copyright (C) 2015 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
Mike Frysinger87deaef2019-07-26 21:14:55 -040017"""Unittests for the wrapper.py module."""
18
19from __future__ import print_function
20
Dan Willemsen745b4ad2015-10-06 15:23:19 -070021import os
Mike Frysinger84094102020-02-11 02:10:28 -050022import re
Dan Willemsen745b4ad2015-10-06 15:23:19 -070023import unittest
24
Mike Frysinger84094102020-02-11 02:10:28 -050025from pyversion import is_python3
Dan Willemsen745b4ad2015-10-06 15:23:19 -070026import wrapper
27
David Pursehouse819827a2020-02-12 15:20:19 +090028
Mike Frysinger8ddff5c2020-02-09 15:00:25 -050029if is_python3():
30 from unittest import mock
31 from io import StringIO
32else:
33 import mock
34 from StringIO import StringIO
35
36
Dan Willemsen745b4ad2015-10-06 15:23:19 -070037def fixture(*paths):
38 """Return a path relative to tests/fixtures.
39 """
40 return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
41
David Pursehouse819827a2020-02-12 15:20:19 +090042
Mike Frysinger84094102020-02-11 02:10:28 -050043class RepoWrapperTestCase(unittest.TestCase):
44 """TestCase for the wrapper module."""
David Pursehouse819827a2020-02-12 15:20:19 +090045
Dan Willemsen745b4ad2015-10-06 15:23:19 -070046 def setUp(self):
Mike Frysinger84094102020-02-11 02:10:28 -050047 """Load the wrapper module every time."""
Dan Willemsen745b4ad2015-10-06 15:23:19 -070048 wrapper._wrapper_module = None
49 self.wrapper = wrapper.Wrapper()
50
Mike Frysinger84094102020-02-11 02:10:28 -050051 if not is_python3():
52 self.assertRegex = self.assertRegexpMatches
53
54
55class RepoWrapperUnitTest(RepoWrapperTestCase):
56 """Tests helper functions in the repo wrapper
57 """
58
Mike Frysinger8ddff5c2020-02-09 15:00:25 -050059 def test_version(self):
60 """Make sure _Version works."""
61 with self.assertRaises(SystemExit) as e:
62 with mock.patch('sys.stdout', new_callable=StringIO) as stdout:
63 with mock.patch('sys.stderr', new_callable=StringIO) as stderr:
64 self.wrapper._Version()
65 self.assertEqual(0, e.exception.code)
66 self.assertEqual('', stderr.getvalue())
67 self.assertIn('repo launcher version', stdout.getvalue())
68
Mike Frysingerd8fda902020-02-14 00:24:38 -050069 def test_init_parser(self):
70 """Make sure 'init' GetParser works."""
71 parser = self.wrapper.GetParser(gitc_init=False)
72 opts, args = parser.parse_args([])
73 self.assertEqual([], args)
74 self.assertIsNone(opts.manifest_url)
75
76 def test_gitc_init_parser(self):
77 """Make sure 'gitc-init' GetParser works."""
78 parser = self.wrapper.GetParser(gitc_init=True)
79 opts, args = parser.parse_args([])
80 self.assertEqual([], args)
81 self.assertIsNone(opts.manifest_file)
82
Dan Willemsen745b4ad2015-10-06 15:23:19 -070083 def test_get_gitc_manifest_dir_no_gitc(self):
84 """
85 Test reading a missing gitc config file
86 """
87 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
88 val = self.wrapper.get_gitc_manifest_dir()
89 self.assertEqual(val, '')
90
91 def test_get_gitc_manifest_dir(self):
92 """
93 Test reading the gitc config file and parsing the directory
94 """
95 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
96 val = self.wrapper.get_gitc_manifest_dir()
97 self.assertEqual(val, '/test/usr/local/google/gitc')
98
99 def test_gitc_parse_clientdir_no_gitc(self):
100 """
101 Test parsing the gitc clientdir without gitc running
102 """
103 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
104 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
105 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
106
107 def test_gitc_parse_clientdir(self):
108 """
109 Test parsing the gitc clientdir
110 """
111 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
112 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
113 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
114 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test')
115 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test')
116 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test')
117 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test')
David Pursehouse3cda50a2020-02-13 13:17:03 +0900118 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'),
119 'test')
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700120 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None)
121 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None)
122
David Pursehouse819827a2020-02-12 15:20:19 +0900123
Mike Frysinger84094102020-02-11 02:10:28 -0500124class SetGitTrace2ParentSid(RepoWrapperTestCase):
125 """Check SetGitTrace2ParentSid behavior."""
126
127 KEY = 'GIT_TRACE2_PARENT_SID'
128 VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$')
129
130 def test_first_set(self):
131 """Test env var not yet set."""
132 env = {}
133 self.wrapper.SetGitTrace2ParentSid(env)
134 self.assertIn(self.KEY, env)
135 value = env[self.KEY]
136 self.assertRegex(value, self.VALID_FORMAT)
137
138 def test_append(self):
139 """Test env var is appended."""
140 env = {self.KEY: 'pfx'}
141 self.wrapper.SetGitTrace2ParentSid(env)
142 self.assertIn(self.KEY, env)
143 value = env[self.KEY]
144 self.assertTrue(value.startswith('pfx/'))
145 self.assertRegex(value[4:], self.VALID_FORMAT)
146
147 def test_global_context(self):
148 """Check os.environ gets updated by default."""
149 os.environ.pop(self.KEY, None)
150 self.wrapper.SetGitTrace2ParentSid()
151 self.assertIn(self.KEY, os.environ)
152 value = os.environ[self.KEY]
153 self.assertRegex(value, self.VALID_FORMAT)
154
155
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700156if __name__ == '__main__':
157 unittest.main()