1#
2# Copyright 2015 Google Inc.
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"""Test for gen_client module."""
17
18import os
19
20import unittest2
21
22from apitools.gen import gen_client
23from apitools.gen import test_utils
24
25
26def GetTestDataPath(*path):
27    return os.path.join(os.path.dirname(__file__), 'testdata', *path)
28
29
30def _GetContent(file_path):
31    with open(file_path) as f:
32        return f.read()
33
34
35@test_utils.RunOnlyOnPython27
36class ClientGenCliTest(unittest2.TestCase):
37
38    def testHelp_NotEnoughArguments(self):
39        with self.assertRaisesRegexp(SystemExit, '0'):
40            with test_utils.CaptureOutput() as (_, err):
41                gen_client.main([gen_client.__file__, '-h'])
42                err_output = err.getvalue()
43                self.assertIn('usage:', err_output)
44                self.assertIn('error: too few arguments', err_output)
45
46    def testGenClient_SimpleDocNoInit(self):
47        with test_utils.TempDir() as tmp_dir_path:
48            gen_client.main([
49                gen_client.__file__,
50                '--generate_cli',
51                '--init-file', 'none',
52                '--infile', GetTestDataPath('dns', 'dns_v1.json'),
53                '--outdir', tmp_dir_path,
54                '--overwrite',
55                '--root_package', 'google.apis',
56                'client'
57            ])
58            expected_files = (
59                set(['dns_v1.py']) |  # CLI files
60                set(['dns_v1_client.py', 'dns_v1_messages.py']))
61            self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
62
63    def testGenClient_SimpleDocEmptyInit(self):
64        with test_utils.TempDir() as tmp_dir_path:
65            gen_client.main([
66                gen_client.__file__,
67                '--generate_cli',
68                '--init-file', 'empty',
69                '--infile', GetTestDataPath('dns', 'dns_v1.json'),
70                '--outdir', tmp_dir_path,
71                '--overwrite',
72                '--root_package', 'google.apis',
73                'client'
74            ])
75            expected_files = (
76                set(['dns_v1.py']) |  # CLI files
77                set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']))
78            self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
79            init_file = _GetContent(os.path.join(tmp_dir_path, '__init__.py'))
80            self.assertEqual("""\"""Package marker file.\"""
81
82import pkgutil
83
84__path__ = pkgutil.extend_path(__path__, __name__)
85""", init_file)
86
87    def testGenClient_SimpleDocWithV4(self):
88        with test_utils.TempDir() as tmp_dir_path:
89            gen_client.main([
90                gen_client.__file__,
91                '--nogenerate_cli',
92                '--infile', GetTestDataPath('dns', 'dns_v1.json'),
93                '--outdir', tmp_dir_path,
94                '--overwrite',
95                '--apitools_version', '0.4.12',
96                '--root_package', 'google.apis',
97                'client'
98            ])
99            self.assertEquals(
100                set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']),
101                set(os.listdir(tmp_dir_path)))
102
103    def testGenClient_SimpleDocWithV5(self):
104        with test_utils.TempDir() as tmp_dir_path:
105            gen_client.main([
106                gen_client.__file__,
107                '--nogenerate_cli',
108                '--infile', GetTestDataPath('dns', 'dns_v1.json'),
109                '--outdir', tmp_dir_path,
110                '--overwrite',
111                '--apitools_version', '0.5.0',
112                '--root_package', 'google.apis',
113                'client'
114            ])
115            self.assertEquals(
116                set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']),
117                set(os.listdir(tmp_dir_path)))
118
119    def testGenPipPackage_SimpleDoc(self):
120        with test_utils.TempDir() as tmp_dir_path:
121            gen_client.main([
122                gen_client.__file__,
123                '--nogenerate_cli',
124                '--infile', GetTestDataPath('dns', 'dns_v1.json'),
125                '--outdir', tmp_dir_path,
126                '--overwrite',
127                '--root_package', 'google.apis',
128                'pip_package'
129            ])
130            self.assertEquals(
131                set(['apitools', 'setup.py']),
132                set(os.listdir(tmp_dir_path)))
133
134    def testGenProto_SimpleDoc(self):
135        with test_utils.TempDir() as tmp_dir_path:
136            gen_client.main([
137                gen_client.__file__,
138                '--nogenerate_cli',
139                '--infile', GetTestDataPath('dns', 'dns_v1.json'),
140                '--outdir', tmp_dir_path,
141                '--overwrite',
142                '--root_package', 'google.apis',
143                'proto'
144            ])
145            self.assertEquals(
146                set(['dns_v1_messages.proto', 'dns_v1_services.proto']),
147                set(os.listdir(tmp_dir_path)))
148