blob: e574946ba4c969102c916ed5f5a5de3c60704628 [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
Dan Willemsen745b4ad2015-10-06 15:23:19 -070029def fixture(*paths):
30 """Return a path relative to tests/fixtures.
31 """
32 return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
33
David Pursehouse819827a2020-02-12 15:20:19 +090034
Mike Frysinger84094102020-02-11 02:10:28 -050035class RepoWrapperTestCase(unittest.TestCase):
36 """TestCase for the wrapper module."""
David Pursehouse819827a2020-02-12 15:20:19 +090037
Dan Willemsen745b4ad2015-10-06 15:23:19 -070038 def setUp(self):
Mike Frysinger84094102020-02-11 02:10:28 -050039 """Load the wrapper module every time."""
Dan Willemsen745b4ad2015-10-06 15:23:19 -070040 wrapper._wrapper_module = None
41 self.wrapper = wrapper.Wrapper()
42
Mike Frysinger84094102020-02-11 02:10:28 -050043 if not is_python3():
44 self.assertRegex = self.assertRegexpMatches
45
46
47class RepoWrapperUnitTest(RepoWrapperTestCase):
48 """Tests helper functions in the repo wrapper
49 """
50
Dan Willemsen745b4ad2015-10-06 15:23:19 -070051 def test_get_gitc_manifest_dir_no_gitc(self):
52 """
53 Test reading a missing gitc config file
54 """
55 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
56 val = self.wrapper.get_gitc_manifest_dir()
57 self.assertEqual(val, '')
58
59 def test_get_gitc_manifest_dir(self):
60 """
61 Test reading the gitc config file and parsing the directory
62 """
63 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
64 val = self.wrapper.get_gitc_manifest_dir()
65 self.assertEqual(val, '/test/usr/local/google/gitc')
66
67 def test_gitc_parse_clientdir_no_gitc(self):
68 """
69 Test parsing the gitc clientdir without gitc running
70 """
71 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
72 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
73 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
74
75 def test_gitc_parse_clientdir(self):
76 """
77 Test parsing the gitc clientdir
78 """
79 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
80 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
81 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
82 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test')
83 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test')
84 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test')
85 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test')
86 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'), 'test')
87 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None)
88 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None)
89
David Pursehouse819827a2020-02-12 15:20:19 +090090
Mike Frysinger84094102020-02-11 02:10:28 -050091class SetGitTrace2ParentSid(RepoWrapperTestCase):
92 """Check SetGitTrace2ParentSid behavior."""
93
94 KEY = 'GIT_TRACE2_PARENT_SID'
95 VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$')
96
97 def test_first_set(self):
98 """Test env var not yet set."""
99 env = {}
100 self.wrapper.SetGitTrace2ParentSid(env)
101 self.assertIn(self.KEY, env)
102 value = env[self.KEY]
103 self.assertRegex(value, self.VALID_FORMAT)
104
105 def test_append(self):
106 """Test env var is appended."""
107 env = {self.KEY: 'pfx'}
108 self.wrapper.SetGitTrace2ParentSid(env)
109 self.assertIn(self.KEY, env)
110 value = env[self.KEY]
111 self.assertTrue(value.startswith('pfx/'))
112 self.assertRegex(value[4:], self.VALID_FORMAT)
113
114 def test_global_context(self):
115 """Check os.environ gets updated by default."""
116 os.environ.pop(self.KEY, None)
117 self.wrapper.SetGitTrace2ParentSid()
118 self.assertIn(self.KEY, os.environ)
119 value = os.environ[self.KEY]
120 self.assertRegex(value, self.VALID_FORMAT)
121
122
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700123if __name__ == '__main__':
124 unittest.main()