xref: /aosp_15_r20/tools/asuite/aidegen/lib/config_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2019, The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for AidegenConfig class."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Workerimport os
20*c2e18aaaSAndroid Build Coastguard Workerimport shutil
21*c2e18aaaSAndroid Build Coastguard Workerimport tempfile
22*c2e18aaaSAndroid Build Coastguard Workerimport unittest
23*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
24*c2e18aaaSAndroid Build Coastguard Worker
25*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen import constant
26*c2e18aaaSAndroid Build Coastguard Worker
27*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import common_util
28*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import config
29*c2e18aaaSAndroid Build Coastguard Worker
30*c2e18aaaSAndroid Build Coastguard Worker
31*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=protected-access
32*c2e18aaaSAndroid Build Coastguard Workerclass AidegenConfigUnittests(unittest.TestCase):
33*c2e18aaaSAndroid Build Coastguard Worker    """Unit tests for config.py"""
34*c2e18aaaSAndroid Build Coastguard Worker
35*c2e18aaaSAndroid Build Coastguard Worker    _TMP_DIR = None
36*c2e18aaaSAndroid Build Coastguard Worker
37*c2e18aaaSAndroid Build Coastguard Worker    def setUp(self):
38*c2e18aaaSAndroid Build Coastguard Worker        """Prepare the testdata related path."""
39*c2e18aaaSAndroid Build Coastguard Worker        AidegenConfigUnittests._TMP_DIR = tempfile.mkdtemp()
40*c2e18aaaSAndroid Build Coastguard Worker        config.AidegenConfig._CONFIG_DIR = os.path.join(
41*c2e18aaaSAndroid Build Coastguard Worker            AidegenConfigUnittests._TMP_DIR, '.config', 'asuite', 'aidegen')
42*c2e18aaaSAndroid Build Coastguard Worker        config.AidegenConfig._CONFIG_FILE_PATH = os.path.join(
43*c2e18aaaSAndroid Build Coastguard Worker            config.AidegenConfig._CONFIG_DIR,
44*c2e18aaaSAndroid Build Coastguard Worker            config.AidegenConfig._DEFAULT_CONFIG_FILE)
45*c2e18aaaSAndroid Build Coastguard Worker        config.AidegenConfig._ENABLE_DEBUG_DIR = os.path.join(
46*c2e18aaaSAndroid Build Coastguard Worker            config.AidegenConfig._CONFIG_DIR,
47*c2e18aaaSAndroid Build Coastguard Worker            config.AidegenConfig._ENABLE_DEBUG_CONFIG_DIR)
48*c2e18aaaSAndroid Build Coastguard Worker        config.AidegenConfig.DEBUG_ENABLED_FILE_PATH = os.path.join(
49*c2e18aaaSAndroid Build Coastguard Worker            config.AidegenConfig._CONFIG_DIR,
50*c2e18aaaSAndroid Build Coastguard Worker            config.AidegenConfig._ENABLE_DEBUG_CONFIG_FILE)
51*c2e18aaaSAndroid Build Coastguard Worker
52*c2e18aaaSAndroid Build Coastguard Worker    def tearDown(self):
53*c2e18aaaSAndroid Build Coastguard Worker        """Clear the testdata related path."""
54*c2e18aaaSAndroid Build Coastguard Worker        shutil.rmtree(AidegenConfigUnittests._TMP_DIR)
55*c2e18aaaSAndroid Build Coastguard Worker
56*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('json.load')
57*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('builtins.open')
58*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
59*c2e18aaaSAndroid Build Coastguard Worker    def test_load_aidegen_config(self, mock_file_exists, mock_file_open,
60*c2e18aaaSAndroid Build Coastguard Worker                                 mock_json_load):
61*c2e18aaaSAndroid Build Coastguard Worker        """Test loading aidegen config."""
62*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = False
63*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
64*c2e18aaaSAndroid Build Coastguard Worker        cfg._load_aidegen_config()
65*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_file_open.called)
66*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_json_load.called)
67*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = True
68*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
69*c2e18aaaSAndroid Build Coastguard Worker        cfg._load_aidegen_config()
70*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_file_open.called)
71*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_json_load.called)
72*c2e18aaaSAndroid Build Coastguard Worker
73*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('logging.info')
74*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('logging.error')
75*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('builtins.open')
76*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
77*c2e18aaaSAndroid Build Coastguard Worker    def test_error_load_aidegen_config(self, mock_file_exists, mock_file_open,
78*c2e18aaaSAndroid Build Coastguard Worker                                       mock_error, mock_info):
79*c2e18aaaSAndroid Build Coastguard Worker        """Test loading aidegen config with errors."""
80*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = True
81*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
82*c2e18aaaSAndroid Build Coastguard Worker        mock_file_open.side_effect = IOError()
83*c2e18aaaSAndroid Build Coastguard Worker        with self.assertRaises(IOError):
84*c2e18aaaSAndroid Build Coastguard Worker            cfg._load_aidegen_config()
85*c2e18aaaSAndroid Build Coastguard Worker            self.assertTrue(mock_error.called)
86*c2e18aaaSAndroid Build Coastguard Worker            self.assertFalse(mock_info.called)
87*c2e18aaaSAndroid Build Coastguard Worker        mock_file_open.reset()
88*c2e18aaaSAndroid Build Coastguard Worker        mock_file_open.side_effect = ValueError()
89*c2e18aaaSAndroid Build Coastguard Worker        cfg._load_aidegen_config()
90*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_info.called)
91*c2e18aaaSAndroid Build Coastguard Worker
92*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('json.dump')
93*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('builtins.open')
94*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.AidegenConfig, '_is_config_modified')
95*c2e18aaaSAndroid Build Coastguard Worker    def test_aidegen_config_no_changed(self, mock_is_config_modified,
96*c2e18aaaSAndroid Build Coastguard Worker                                       mock_file_open, mock_json_dump):
97*c2e18aaaSAndroid Build Coastguard Worker        """Skip saving aidegen config when no configuration data is modified."""
98*c2e18aaaSAndroid Build Coastguard Worker        mock_is_config_modified.return_value = False
99*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
100*c2e18aaaSAndroid Build Coastguard Worker        cfg._save_aidegen_config()
101*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_file_open.called)
102*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_json_dump.called)
103*c2e18aaaSAndroid Build Coastguard Worker
104*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('json.dump')
105*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('builtins.open')
106*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.AidegenConfig, '_is_config_modified')
107*c2e18aaaSAndroid Build Coastguard Worker    def test_update_aidegen_config(self, mock_is_config_modified,
108*c2e18aaaSAndroid Build Coastguard Worker                                   mock_file_open, mock_json_dump):
109*c2e18aaaSAndroid Build Coastguard Worker        """Save the aidegen config once any configuration data is modified."""
110*c2e18aaaSAndroid Build Coastguard Worker        mock_is_config_modified.return_value = True
111*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
112*c2e18aaaSAndroid Build Coastguard Worker        cfg._save_aidegen_config()
113*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_file_open.called)
114*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_json_dump.called)
115*c2e18aaaSAndroid Build Coastguard Worker
116*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('logging.warning')
117*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.AidegenConfig, '_gen_enable_debugger_config')
118*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.AidegenConfig, '_gen_androidmanifest')
119*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.AidegenConfig, '_gen_enable_debug_sub_dir')
120*c2e18aaaSAndroid Build Coastguard Worker    def test_create_enable_debugger(self, mock_debug, mock_androidmanifest,
121*c2e18aaaSAndroid Build Coastguard Worker                                    mock_enable, mock_warning):
122*c2e18aaaSAndroid Build Coastguard Worker        """Test create_enable_debugger_module."""
123*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
124*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(cfg.create_enable_debugger_module(''))
125*c2e18aaaSAndroid Build Coastguard Worker        mock_debug.side_effect = IOError()
126*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.create_enable_debugger_module(''))
127*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_warning.called)
128*c2e18aaaSAndroid Build Coastguard Worker        mock_debug.side_effect = OSError()
129*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.create_enable_debugger_module(''))
130*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_warning.called)
131*c2e18aaaSAndroid Build Coastguard Worker        mock_androidmanifest.side_effect = IOError()
132*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.create_enable_debugger_module(''))
133*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_warning.called)
134*c2e18aaaSAndroid Build Coastguard Worker        mock_androidmanifest.side_effect = OSError()
135*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.create_enable_debugger_module(''))
136*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_warning.called)
137*c2e18aaaSAndroid Build Coastguard Worker        mock_enable.side_effect = IOError()
138*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.create_enable_debugger_module(''))
139*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_warning.called)
140*c2e18aaaSAndroid Build Coastguard Worker        mock_enable.side_effect = OSError()
141*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.create_enable_debugger_module(''))
142*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_warning.called)
143*c2e18aaaSAndroid Build Coastguard Worker
144*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'file_generate')
145*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
146*c2e18aaaSAndroid Build Coastguard Worker    def test_gen_debugger_config(self, mock_file_exists, mock_file_generate):
147*c2e18aaaSAndroid Build Coastguard Worker        """Test generating the enable debugger config."""
148*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
149*c2e18aaaSAndroid Build Coastguard Worker        android_sdk_version = ''
150*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = False
151*c2e18aaaSAndroid Build Coastguard Worker        cfg._gen_enable_debugger_config(android_sdk_version)
152*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_file_generate.called)
153*c2e18aaaSAndroid Build Coastguard Worker
154*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.stat')
155*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'file_generate')
156*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
157*c2e18aaaSAndroid Build Coastguard Worker    def test_androidmanifest_no_changed(self, mock_file_exists,
158*c2e18aaaSAndroid Build Coastguard Worker                                        mock_file_generate, mock_file_stat):
159*c2e18aaaSAndroid Build Coastguard Worker        """No generate the AndroidManifest.xml when it exists and size > 0."""
160*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
161*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = True
162*c2e18aaaSAndroid Build Coastguard Worker        mock_file_stat.return_value.st_size = 1
163*c2e18aaaSAndroid Build Coastguard Worker        cfg._gen_androidmanifest()
164*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_file_generate.called)
165*c2e18aaaSAndroid Build Coastguard Worker
166*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.stat')
167*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'file_generate')
168*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
169*c2e18aaaSAndroid Build Coastguard Worker    def test_override_androidmanifest(self, mock_file_exists,
170*c2e18aaaSAndroid Build Coastguard Worker                                      mock_file_generate, mock_file_stat):
171*c2e18aaaSAndroid Build Coastguard Worker        """Override the AndroidManifest.xml when the file size is zero."""
172*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
173*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = True
174*c2e18aaaSAndroid Build Coastguard Worker        mock_file_stat.return_value.st_size = 0
175*c2e18aaaSAndroid Build Coastguard Worker        cfg._gen_androidmanifest()
176*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_file_generate.called)
177*c2e18aaaSAndroid Build Coastguard Worker
178*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'file_generate')
179*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
180*c2e18aaaSAndroid Build Coastguard Worker    def test_gen_androidmanifest(self, mock_file_exists, mock_file_generate):
181*c2e18aaaSAndroid Build Coastguard Worker        """Generate the AndroidManifest.xml when it doesn't exist."""
182*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
183*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = False
184*c2e18aaaSAndroid Build Coastguard Worker        cfg._gen_androidmanifest()
185*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_file_generate.called)
186*c2e18aaaSAndroid Build Coastguard Worker
187*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.makedirs')
188*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
189*c2e18aaaSAndroid Build Coastguard Worker    def test_config_folder_exists(self, mock_folder_exists, mock_makedirs):
190*c2e18aaaSAndroid Build Coastguard Worker        """Skipping create the config folder once it exists."""
191*c2e18aaaSAndroid Build Coastguard Worker        mock_folder_exists.return_value = True
192*c2e18aaaSAndroid Build Coastguard Worker        config.AidegenConfig()
193*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_makedirs.called)
194*c2e18aaaSAndroid Build Coastguard Worker
195*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.makedirs')
196*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
197*c2e18aaaSAndroid Build Coastguard Worker    def test_create_config_folder(self, mock_folder_exists, mock_makedirs):
198*c2e18aaaSAndroid Build Coastguard Worker        """Create the config folder when it doesn't exist."""
199*c2e18aaaSAndroid Build Coastguard Worker        mock_folder_exists.return_value = False
200*c2e18aaaSAndroid Build Coastguard Worker        config.AidegenConfig()
201*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_makedirs.called)
202*c2e18aaaSAndroid Build Coastguard Worker
203*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.isfile')
204*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('builtins.open', create=True)
205*c2e18aaaSAndroid Build Coastguard Worker    def test_deprecated_intellij_version(self, mock_open, mock_isfile):
206*c2e18aaaSAndroid Build Coastguard Worker        """Test deprecated_intellij_version."""
207*c2e18aaaSAndroid Build Coastguard Worker        # Test the idea.sh file contains the deprecated string.
208*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
209*c2e18aaaSAndroid Build Coastguard Worker        expected_data = ('#!/bin/sh\n\n'
210*c2e18aaaSAndroid Build Coastguard Worker                         'SUMMARY="This version of IntelliJ Community Edition '
211*c2e18aaaSAndroid Build Coastguard Worker                         'is no longer supported."\n')
212*c2e18aaaSAndroid Build Coastguard Worker        mock_open.side_effect = [
213*c2e18aaaSAndroid Build Coastguard Worker            mock.mock_open(read_data=expected_data).return_value
214*c2e18aaaSAndroid Build Coastguard Worker        ]
215*c2e18aaaSAndroid Build Coastguard Worker        mock_isfile.return_value = True
216*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(cfg.deprecated_intellij_version(0))
217*c2e18aaaSAndroid Build Coastguard Worker
218*c2e18aaaSAndroid Build Coastguard Worker        # Test the idea.sh file doesn't contain the deprecated string.
219*c2e18aaaSAndroid Build Coastguard Worker        expected_data = ('#!/bin/sh\n\n'
220*c2e18aaaSAndroid Build Coastguard Worker                         'JAVA_BIN="$JDK/bin/java"\n'
221*c2e18aaaSAndroid Build Coastguard Worker                         '"$JAVA_BIN" \\n')
222*c2e18aaaSAndroid Build Coastguard Worker        mock_open.side_effect = [
223*c2e18aaaSAndroid Build Coastguard Worker            mock.mock_open(read_data=expected_data).return_value
224*c2e18aaaSAndroid Build Coastguard Worker        ]
225*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.deprecated_intellij_version(0))
226*c2e18aaaSAndroid Build Coastguard Worker
227*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.AidegenConfig, 'deprecated_studio_version')
228*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.AidegenConfig, 'deprecated_intellij_version')
229*c2e18aaaSAndroid Build Coastguard Worker    def test_deprecated_version(self, mock_inj, mock_studio):
230*c2e18aaaSAndroid Build Coastguard Worker        """Test deprecated_version."""
231*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
232*c2e18aaaSAndroid Build Coastguard Worker        ide_name = constant.IDE_INTELLIJ
233*c2e18aaaSAndroid Build Coastguard Worker        test_path = ''
234*c2e18aaaSAndroid Build Coastguard Worker        cfg.deprecated_version(ide_name, test_path)
235*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_inj.called)
236*c2e18aaaSAndroid Build Coastguard Worker
237*c2e18aaaSAndroid Build Coastguard Worker        ide_name = constant.IDE_ANDROID_STUDIO
238*c2e18aaaSAndroid Build Coastguard Worker        cfg.deprecated_version(ide_name, test_path)
239*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_studio.called)
240*c2e18aaaSAndroid Build Coastguard Worker        test_ide = ''
241*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.deprecated_version(test_ide, test_path))
242*c2e18aaaSAndroid Build Coastguard Worker
243*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(os.path, 'isfile')
244*c2e18aaaSAndroid Build Coastguard Worker    def test_deprecated_studio_version(self, mock_is_file):
245*c2e18aaaSAndroid Build Coastguard Worker        """Test deprecated_studio_version."""
246*c2e18aaaSAndroid Build Coastguard Worker        test_sh_name = 'test.sh'
247*c2e18aaaSAndroid Build Coastguard Worker        # temp_dir/test
248*c2e18aaaSAndroid Build Coastguard Worker        temp_base = os.path.join(self._TMP_DIR, 'test')
249*c2e18aaaSAndroid Build Coastguard Worker        os.mkdir(temp_base)
250*c2e18aaaSAndroid Build Coastguard Worker        # temp_dir/test/studio/bin
251*c2e18aaaSAndroid Build Coastguard Worker        os.mkdir(os.path.join(temp_base, 'studio'))
252*c2e18aaaSAndroid Build Coastguard Worker        test_bin_path = os.path.join(self._TMP_DIR, 'test', 'studio', 'bin')
253*c2e18aaaSAndroid Build Coastguard Worker        os.mkdir(test_bin_path)
254*c2e18aaaSAndroid Build Coastguard Worker        # /temp_dir/test/studio/bin/test.sh
255*c2e18aaaSAndroid Build Coastguard Worker        test_sh_path = os.path.join(self._TMP_DIR, 'test', 'studio', 'bin',
256*c2e18aaaSAndroid Build Coastguard Worker                                    test_sh_name)
257*c2e18aaaSAndroid Build Coastguard Worker        # Real test.sh doesn't exist.
258*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
259*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(cfg.deprecated_studio_version(test_sh_path))
260*c2e18aaaSAndroid Build Coastguard Worker        # The /temp_dir/test/studio/lib doesn't exist case.
261*c2e18aaaSAndroid Build Coastguard Worker        mock_is_file.return_value = True
262*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(cfg.deprecated_studio_version(test_sh_path))
263*c2e18aaaSAndroid Build Coastguard Worker        # The /temp_dir/test/studio/lib exists.
264*c2e18aaaSAndroid Build Coastguard Worker        test_lib_path = os.path.join(self._TMP_DIR, 'test', 'studio', 'lib')
265*c2e18aaaSAndroid Build Coastguard Worker        os.mkdir(test_lib_path)
266*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.deprecated_studio_version(test_sh_path))
267*c2e18aaaSAndroid Build Coastguard Worker        shutil.rmtree(temp_base)
268*c2e18aaaSAndroid Build Coastguard Worker
269*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.isfile')
270*c2e18aaaSAndroid Build Coastguard Worker    def test_idea_path_not_file(self, mock_isfile):
271*c2e18aaaSAndroid Build Coastguard Worker        """Test deprecated_intellij_version."""
272*c2e18aaaSAndroid Build Coastguard Worker        # Test the idea_path is not a file.
273*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
274*c2e18aaaSAndroid Build Coastguard Worker        mock_isfile.return_value = False
275*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(cfg.deprecated_intellij_version(0))
276*c2e18aaaSAndroid Build Coastguard Worker
277*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.AidegenConfig, 'deprecated_version')
278*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.AidegenConfig, 'deprecated_intellij_version')
279*c2e18aaaSAndroid Build Coastguard Worker    def test_preferred_version(self, mock_deprecated_intj, mock_deprecated):
280*c2e18aaaSAndroid Build Coastguard Worker        """Test get preferred IntelliJ version."""
281*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
282*c2e18aaaSAndroid Build Coastguard Worker        cfg._config['preferred_version'] = ''
283*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg.preferred_version(), None)
284*c2e18aaaSAndroid Build Coastguard Worker
285*c2e18aaaSAndroid Build Coastguard Worker        result = 'test_intellij'
286*c2e18aaaSAndroid Build Coastguard Worker        cfg._config['IntelliJ_preferred_version'] = result
287*c2e18aaaSAndroid Build Coastguard Worker        mock_deprecated.return_value = False
288*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg.preferred_version(constant.IDE_INTELLIJ), result)
289*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg.preferred_version(result), None)
290*c2e18aaaSAndroid Build Coastguard Worker        mock_deprecated.return_value = True
291*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg.preferred_version(constant.IDE_INTELLIJ), None)
292*c2e18aaaSAndroid Build Coastguard Worker
293*c2e18aaaSAndroid Build Coastguard Worker        mock_deprecated_intj.return_value = False
294*c2e18aaaSAndroid Build Coastguard Worker        cfg._config['preferred_version'] = 'a'
295*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg.preferred_version(), 'a')
296*c2e18aaaSAndroid Build Coastguard Worker        mock_deprecated_intj.return_value = True
297*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg.preferred_version(), None)
298*c2e18aaaSAndroid Build Coastguard Worker
299*c2e18aaaSAndroid Build Coastguard Worker    def test_set_preferred_version(self):
300*c2e18aaaSAndroid Build Coastguard Worker        """Test set_preferred_version."""
301*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
302*c2e18aaaSAndroid Build Coastguard Worker        cfg._config[config.AidegenConfig._KEY_APPEND] = 'Yes'
303*c2e18aaaSAndroid Build Coastguard Worker        cfg.set_preferred_version('test', None)
304*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg._config[config.AidegenConfig._KEY_APPEND], 'test')
305*c2e18aaaSAndroid Build Coastguard Worker        cfg.set_preferred_version('test', constant.IDE_INTELLIJ)
306*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg._config['IntelliJ_preferred_version'], 'test')
307*c2e18aaaSAndroid Build Coastguard Worker
308*c2e18aaaSAndroid Build Coastguard Worker    def test_set_plugin_preference(self):
309*c2e18aaaSAndroid Build Coastguard Worker        """Test set_plugin_preference."""
310*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
311*c2e18aaaSAndroid Build Coastguard Worker        cfg._config[config.AidegenConfig._KEY_PLUGIN_PREFERENCE] = 'yes'
312*c2e18aaaSAndroid Build Coastguard Worker        cfg.plugin_preference = 'no'
313*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg._config[
314*c2e18aaaSAndroid Build Coastguard Worker            config.AidegenConfig._KEY_PLUGIN_PREFERENCE], 'no')
315*c2e18aaaSAndroid Build Coastguard Worker
316*c2e18aaaSAndroid Build Coastguard Worker    def test_get_plugin_preference(self):
317*c2e18aaaSAndroid Build Coastguard Worker        """Test get_plugin_preference."""
318*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
319*c2e18aaaSAndroid Build Coastguard Worker        cfg._config[config.AidegenConfig._KEY_PLUGIN_PREFERENCE] = 'yes'
320*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(cfg.plugin_preference, 'yes')
321*c2e18aaaSAndroid Build Coastguard Worker
322*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.makedirs')
323*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
324*c2e18aaaSAndroid Build Coastguard Worker    def test_gen_enable_debug_sub_dir(self, mock_file_exists, mock_makedirs):
325*c2e18aaaSAndroid Build Coastguard Worker        """Test _gen_enable_debug_sub_dir."""
326*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.AidegenConfig()
327*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = True
328*c2e18aaaSAndroid Build Coastguard Worker        cfg._gen_enable_debug_sub_dir('a')
329*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_makedirs.called)
330*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = False
331*c2e18aaaSAndroid Build Coastguard Worker        cfg._gen_enable_debug_sub_dir('a')
332*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_makedirs.called)
333*c2e18aaaSAndroid Build Coastguard Worker
334*c2e18aaaSAndroid Build Coastguard Worker
335*c2e18aaaSAndroid Build Coastguard Workerclass IdeaPropertiesUnittests(unittest.TestCase):
336*c2e18aaaSAndroid Build Coastguard Worker    """Unit tests for IdeaProperties class."""
337*c2e18aaaSAndroid Build Coastguard Worker
338*c2e18aaaSAndroid Build Coastguard Worker    _CONFIG_DIR = None
339*c2e18aaaSAndroid Build Coastguard Worker
340*c2e18aaaSAndroid Build Coastguard Worker    def setUp(self):
341*c2e18aaaSAndroid Build Coastguard Worker        """Prepare the testdata related path."""
342*c2e18aaaSAndroid Build Coastguard Worker        IdeaPropertiesUnittests._CONFIG_DIR = tempfile.mkdtemp()
343*c2e18aaaSAndroid Build Coastguard Worker
344*c2e18aaaSAndroid Build Coastguard Worker    def tearDown(self):
345*c2e18aaaSAndroid Build Coastguard Worker        """Clear the testdata related path."""
346*c2e18aaaSAndroid Build Coastguard Worker        shutil.rmtree(IdeaPropertiesUnittests._CONFIG_DIR)
347*c2e18aaaSAndroid Build Coastguard Worker
348*c2e18aaaSAndroid Build Coastguard Worker    def test_set_default_properties(self):
349*c2e18aaaSAndroid Build Coastguard Worker        """Test creating the idea.properties with default content."""
350*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.IdeaProperties(IdeaPropertiesUnittests._CONFIG_DIR)
351*c2e18aaaSAndroid Build Coastguard Worker        cfg._set_default_idea_properties()
352*c2e18aaaSAndroid Build Coastguard Worker        expected_data = cfg._PROPERTIES_CONTENT.format(
353*c2e18aaaSAndroid Build Coastguard Worker            KEY_FILE_SIZE=cfg._KEY_FILESIZE,
354*c2e18aaaSAndroid Build Coastguard Worker            VALUE_FILE_SIZE=cfg._FILESIZE_LIMIT)
355*c2e18aaaSAndroid Build Coastguard Worker        generated_file = os.path.join(IdeaPropertiesUnittests._CONFIG_DIR,
356*c2e18aaaSAndroid Build Coastguard Worker                                      cfg._PROPERTIES_FILE)
357*c2e18aaaSAndroid Build Coastguard Worker        generated_content = common_util.read_file_content(generated_file)
358*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(expected_data, generated_content)
359*c2e18aaaSAndroid Build Coastguard Worker
360*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'read_file_content')
361*c2e18aaaSAndroid Build Coastguard Worker    def test_reset_max_file_size(self, mock_content):
362*c2e18aaaSAndroid Build Coastguard Worker        """Test reset the file size limit when it's smaller than 100000."""
363*c2e18aaaSAndroid Build Coastguard Worker        mock_content.return_value = ('# custom IntelliJ IDEA properties\n'
364*c2e18aaaSAndroid Build Coastguard Worker                                     'idea.max.intellisense.filesize=5000')
365*c2e18aaaSAndroid Build Coastguard Worker        expected_data = ('# custom IntelliJ IDEA properties\n'
366*c2e18aaaSAndroid Build Coastguard Worker                         'idea.max.intellisense.filesize=100000')
367*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.IdeaProperties(IdeaPropertiesUnittests._CONFIG_DIR)
368*c2e18aaaSAndroid Build Coastguard Worker        cfg._reset_max_file_size()
369*c2e18aaaSAndroid Build Coastguard Worker        generated_file = os.path.join(IdeaPropertiesUnittests._CONFIG_DIR,
370*c2e18aaaSAndroid Build Coastguard Worker                                      cfg._PROPERTIES_FILE)
371*c2e18aaaSAndroid Build Coastguard Worker        with open(generated_file, 'r', encoding='utf-8') as properties_file:
372*c2e18aaaSAndroid Build Coastguard Worker            generated_content = properties_file.read()
373*c2e18aaaSAndroid Build Coastguard Worker        self.assertEqual(expected_data, generated_content)
374*c2e18aaaSAndroid Build Coastguard Worker
375*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'file_generate')
376*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(common_util, 'read_file_content')
377*c2e18aaaSAndroid Build Coastguard Worker    def test_no_reset_max_file_size(self, mock_content, mock_gen_file):
378*c2e18aaaSAndroid Build Coastguard Worker        """Test when the file size is larger than 100000."""
379*c2e18aaaSAndroid Build Coastguard Worker        mock_content.return_value = ('# custom IntelliJ IDEA properties\n'
380*c2e18aaaSAndroid Build Coastguard Worker                                     'idea.max.intellisense.filesize=110000')
381*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.IdeaProperties(IdeaPropertiesUnittests._CONFIG_DIR)
382*c2e18aaaSAndroid Build Coastguard Worker        cfg._reset_max_file_size()
383*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_gen_file.called)
384*c2e18aaaSAndroid Build Coastguard Worker
385*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.IdeaProperties, '_reset_max_file_size')
386*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.IdeaProperties, '_set_default_idea_properties')
387*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
388*c2e18aaaSAndroid Build Coastguard Worker    def test_set_idea_properties_called(self, mock_file_exists,
389*c2e18aaaSAndroid Build Coastguard Worker                                        mock_set_default,
390*c2e18aaaSAndroid Build Coastguard Worker                                        mock_reset_file_size):
391*c2e18aaaSAndroid Build Coastguard Worker        """Test _set_default_idea_properties() method is called."""
392*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = False
393*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.IdeaProperties(IdeaPropertiesUnittests._CONFIG_DIR)
394*c2e18aaaSAndroid Build Coastguard Worker        cfg.set_max_file_size()
395*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_set_default.called)
396*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_reset_file_size.called)
397*c2e18aaaSAndroid Build Coastguard Worker
398*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.IdeaProperties, '_reset_max_file_size')
399*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch.object(config.IdeaProperties, '_set_default_idea_properties')
400*c2e18aaaSAndroid Build Coastguard Worker    @mock.patch('os.path.exists')
401*c2e18aaaSAndroid Build Coastguard Worker    def test_reset_properties_called(self, mock_file_exists, mock_set_default,
402*c2e18aaaSAndroid Build Coastguard Worker                                     mock_reset_file_size):
403*c2e18aaaSAndroid Build Coastguard Worker        """Test _reset_max_file_size() method is called."""
404*c2e18aaaSAndroid Build Coastguard Worker        mock_file_exists.return_value = True
405*c2e18aaaSAndroid Build Coastguard Worker        cfg = config.IdeaProperties(IdeaPropertiesUnittests._CONFIG_DIR)
406*c2e18aaaSAndroid Build Coastguard Worker        cfg.set_max_file_size()
407*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(mock_set_default.called)
408*c2e18aaaSAndroid Build Coastguard Worker        self.assertTrue(mock_reset_file_size.called)
409*c2e18aaaSAndroid Build Coastguard Worker
410*c2e18aaaSAndroid Build Coastguard Worker
411*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
412*c2e18aaaSAndroid Build Coastguard Worker    unittest.main()
413