1#!/usr/bin/env python3 2# 3# Copyright 2019, 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 17"""Unittests for aidegen_main.""" 18 19from __future__ import print_function 20 21import os 22import sys 23import unittest 24from unittest import mock 25 26from aidegen import aidegen_main 27from aidegen import constant 28from aidegen import unittest_constants 29from aidegen.lib import aidegen_metrics 30from aidegen.lib import common_util 31from aidegen.lib import eclipse_project_file_gen 32from aidegen.lib import errors 33from aidegen.lib import ide_util 34from aidegen.lib import module_info 35from aidegen.lib import native_util 36from aidegen.lib import native_module_info 37from aidegen.lib import native_project_info 38from aidegen.lib import project_config 39from aidegen.lib import project_file_gen 40from aidegen.lib import project_info 41from aidegen.vscode import vscode_workspace_file_gen 42from aidegen.vscode import vscode_native_project_file_gen 43 44 45# pylint: disable=protected-access 46# pylint: disable=too-many-arguments 47# pylint: disable=too-many-statements 48# pylint: disable=too-many-locals 49class AidegenMainUnittests(unittest.TestCase): 50 """Unit tests for aidegen_main.py""" 51 52 def _init_project_config(self, args): 53 """Initialize project configurations.""" 54 self.assertIsNotNone(args) 55 config = project_config.ProjectConfig(args) 56 config.init_environment() 57 58 def test_parse_args(self): 59 """Test _parse_args with different conditions.""" 60 args = aidegen_main._parse_args([]) 61 self.assertEqual(args.targets, ['']) 62 self.assertEqual(args.ide[0], 'u') 63 target = 'tradefed' 64 args = aidegen_main._parse_args([target]) 65 self.assertEqual(args.targets, [target]) 66 depth = '2' 67 args = aidegen_main._parse_args(['-d', depth]) 68 self.assertEqual(args.depth, int(depth)) 69 args = aidegen_main._parse_args(['-v']) 70 self.assertTrue(args.verbose) 71 args = aidegen_main._parse_args(['-v']) 72 self.assertTrue(args.verbose) 73 args = aidegen_main._parse_args(['-i', 's']) 74 self.assertEqual(args.ide[0], 's') 75 args = aidegen_main._parse_args(['-i', 'e']) 76 self.assertEqual(args.ide[0], 'e') 77 args = aidegen_main._parse_args(['-p', unittest_constants.TEST_MODULE]) 78 self.assertEqual(args.ide_installed_path, 79 unittest_constants.TEST_MODULE) 80 args = aidegen_main._parse_args(['-n']) 81 self.assertTrue(args.no_launch) 82 args = aidegen_main._parse_args(['-r']) 83 self.assertTrue(args.config_reset) 84 args = aidegen_main._parse_args(['-s']) 85 self.assertTrue(args.skip_build) 86 self.assertEqual(args.exclude_paths, None) 87 excludes = 'path/to/a', 'path/to/b' 88 args = aidegen_main._parse_args(['-e', excludes]) 89 self.assertEqual(args.exclude_paths, [excludes]) 90 91 @mock.patch.object(project_config.ProjectConfig, 'init_environment') 92 @mock.patch.object(project_config, 'ProjectConfig') 93 @mock.patch.object(project_file_gen.ProjectFileGenerator, 94 'generate_ide_project_files') 95 @mock.patch.object(eclipse_project_file_gen.EclipseConf, 96 'generate_ide_project_files') 97 def test_generate_project_files(self, mock_eclipse, mock_ide, mock_config, 98 mock_init): 99 """Test _generate_project_files with different conditions.""" 100 projects = ['module_a', 'module_v'] 101 args = aidegen_main._parse_args([projects, '-i', 'e']) 102 mock_init.return_value = None 103 self._init_project_config(args) 104 mock_config.ide_name = constant.IDE_ECLIPSE 105 aidegen_main._generate_project_files(projects) 106 self.assertTrue(mock_eclipse.called_with(projects)) 107 mock_config.ide_name = constant.IDE_ANDROID_STUDIO 108 aidegen_main._generate_project_files(projects) 109 self.assertTrue(mock_ide.called_with(projects)) 110 mock_config.ide_name = constant.IDE_INTELLIJ 111 aidegen_main._generate_project_files(projects) 112 self.assertTrue(mock_ide.called_with(projects)) 113 114 @mock.patch.object(aidegen_main, 'main_with_message') 115 @mock.patch.object(aidegen_main, 'main_without_message') 116 def test_main(self, mock_without, mock_with): 117 """Test main with conditions.""" 118 aidegen_main.main(['-s']) 119 self.assertEqual(mock_without.call_count, 1) 120 aidegen_main.main(['']) 121 self.assertEqual(mock_with.call_count, 1) 122 123 @mock.patch.object(aidegen_metrics, 'starts_asuite_metrics') 124 @mock.patch.object(project_config, 'is_whole_android_tree') 125 @mock.patch.object(aidegen_metrics, 'ends_asuite_metrics') 126 @mock.patch.object(aidegen_main, 'main_with_message') 127 def test_main_with_normal(self, mock_main, mock_ends_metrics, 128 mock_is_whole_tree, mock_starts_metrics): 129 """Test main with normal conditions.""" 130 aidegen_main.main(['-h']) 131 self.assertFalse(mock_main.called) 132 mock_is_whole_tree.return_value = True 133 aidegen_main.main(['']) 134 mock_starts_metrics.assert_called_with([constant.ANDROID_TREE]) 135 self.assertFalse(mock_ends_metrics.called) 136 aidegen_main.main(['-n']) 137 mock_ends_metrics.assert_called_with(constant.EXIT_CODE_NORMAL) 138 139 @mock.patch.object(aidegen_metrics, 'ends_asuite_metrics') 140 @mock.patch.object(aidegen_main, 'main_with_message') 141 def test_main_with_build_fail_errors(self, mock_main, mock_ends_metrics): 142 """Test main with raising build failure error conditions.""" 143 mock_main.side_effect = errors.BuildFailureError 144 with self.assertRaises(errors.BuildFailureError): 145 aidegen_main.main(['']) 146 _, exc_value, exc_traceback = sys.exc_info() 147 msg = str(exc_value) 148 mock_ends_metrics.assert_called_with( 149 constant.EXIT_CODE_AIDEGEN_EXCEPTION, exc_traceback, msg) 150 151 @mock.patch.object(aidegen_metrics, 'ends_asuite_metrics') 152 @mock.patch.object(aidegen_main, 'main_with_message') 153 def test_main_with_io_errors(self, mock_main, mock_ends_metrics): 154 """Test main with raising IO error conditions.""" 155 mock_main.side_effect = IOError 156 with self.assertRaises(IOError): 157 aidegen_main.main(['']) 158 _, exc_value, exc_traceback = sys.exc_info() 159 msg = str(exc_value) 160 mock_ends_metrics.assert_called_with(constant.EXIT_CODE_EXCEPTION, 161 exc_traceback, msg) 162 163 @mock.patch.object(aidegen_main, '_launch_ide') 164 def test_main_with_wrong_ide_path(self, mock_launch_ide): 165 """Test main function with wrong ide path.""" 166 target = 'libui' 167 args = [target, '-p', '/opt/abc/bin/idea.sh'] 168 aidegen_main.main(args) 169 self.assertFalse(mock_launch_ide.called) 170 171 def test_get_targets_from_args(self): 172 """Test get targets from different arguments.""" 173 self.assertEqual( 174 aidegen_main._get_targets_from_args([''], True), []) 175 self.assertEqual( 176 aidegen_main._get_targets_from_args(['.'], False), ['.']) 177 self.assertEqual( 178 aidegen_main._get_targets_from_args([''], False), ['.']) 179 self.assertEqual( 180 aidegen_main._get_targets_from_args(['.'], True), []) 181 self.assertEqual( 182 aidegen_main._get_targets_from_args(['test'], False), ['test']) 183 184 @mock.patch.object(aidegen_main, '_launch_ide') 185 @mock.patch.object(ide_util, 'get_ide_util_instance') 186 def test_launch_native_projects_without_ide_object( 187 self, mock_get_ide, mock_launch_ide): 188 """Test _launch_native_projects function without ide object.""" 189 target = 'libui' 190 args = aidegen_main._parse_args([target, '-i', 'e']) 191 aidegen_main._launch_native_projects(None, args, []) 192 self.assertFalse(mock_get_ide.called) 193 self.assertFalse(mock_launch_ide.called) 194 195 @mock.patch.object(aidegen_main, '_launch_ide') 196 @mock.patch.object(ide_util, 'get_ide_util_instance') 197 def test_launch_native_projects_with_ide_object( 198 self, mock_get_ide, mock_launch_ide): 199 """Test _launch_native_projects function without ide object.""" 200 target = 'libui' 201 args = aidegen_main._parse_args([target, '-i', 'e']) 202 ide_util_obj = 'some_obj' 203 mock_get_ide.return_value = None 204 aidegen_main._launch_native_projects(ide_util_obj, args, []) 205 self.assertTrue(mock_get_ide.called_with('c')) 206 self.assertFalse(mock_launch_ide.called) 207 mock_get_ide.reset_mock() 208 mock_launch_ide.reset_mock() 209 args.ide = ['j'] 210 aidegen_main._launch_native_projects(ide_util_obj, args, []) 211 self.assertTrue(mock_get_ide.called_with('c')) 212 self.assertFalse(mock_launch_ide.called) 213 mock_get_ide.reset_mock() 214 mock_launch_ide.reset_mock() 215 mock_get_ide.return_value = 'some_native_obj' 216 aidegen_main._launch_native_projects(ide_util_obj, args, []) 217 self.assertTrue(mock_get_ide.called_with('c')) 218 self.assertTrue(mock_launch_ide.called) 219 mock_get_ide.reset_mock() 220 mock_launch_ide.reset_mock() 221 args.ide = ['e'] 222 aidegen_main._launch_native_projects(ide_util_obj, args, []) 223 self.assertTrue(mock_get_ide.called_with('c')) 224 self.assertTrue(mock_launch_ide.called) 225 mock_get_ide.reset_mock() 226 mock_launch_ide.reset_mock() 227 args.ide = ['s'] 228 aidegen_main._launch_native_projects(ide_util_obj, args, []) 229 self.assertFalse(mock_get_ide.called) 230 self.assertTrue(mock_launch_ide.called) 231 232 @mock.patch('builtins.print') 233 def test_launch_ide(self, mock_print): 234 """Test _launch_ide function with config parameter.""" 235 mock_ide_util = mock.MagicMock() 236 mock_ide_util.launch_ide.return_value = None 237 mock_ide_util.config_ide.return_value = None 238 launch_path = '/test/launch/ide/method' 239 aidegen_main._launch_ide(mock_ide_util, launch_path) 240 self.assertTrue(mock_ide_util.config_ide.called) 241 self.assertTrue(mock_ide_util.launch_ide.called) 242 mock_print.return_value = None 243 244 @mock.patch.object(project_config.ProjectConfig, 'init_environment') 245 @mock.patch('builtins.print') 246 @mock.patch.object(aidegen_main, '_launch_vscode') 247 @mock.patch.object(aidegen_main, '_launch_native_projects') 248 @mock.patch.object(native_util, 'generate_clion_projects') 249 @mock.patch.object(native_project_info.NativeProjectInfo, 250 'generate_projects') 251 @mock.patch.object(aidegen_main, '_create_and_launch_java_projects') 252 def test_launch_ide_by_module_contents(self, mock_j, mock_c_prj, mock_genc, 253 mock_c, mock_vs, mock_print, 254 mock_init): 255 """Test _launch_ide_by_module_contents with different conditions.""" 256 args = aidegen_main._parse_args(['', '-i', 's']) 257 mock_init.return_value = None 258 self._init_project_config(args) 259 ide_obj = 'ide_obj' 260 test_all = False 261 lang = constant.C_CPP 262 java_targets = {constant.JAVA: ['a', 'b', 'c'], 263 constant.C_CPP: None, constant.RUST: None} 264 265 aidegen_main._launch_ide_by_module_contents(args, ide_obj, lang, 266 java_targets, test_all) 267 268 self.assertFalse(mock_vs.called) 269 self.assertTrue(mock_print.called) 270 self.assertFalse(mock_j.called) 271 self.assertFalse(mock_c_prj.called) 272 self.assertFalse(mock_genc.called) 273 self.assertFalse(mock_c.called) 274 275 lang = constant.JAVA 276 none_targets = {constant.JAVA: None, 277 constant.C_CPP: None, 278 constant.RUST: None} 279 280 aidegen_main._launch_ide_by_module_contents(args, ide_obj, lang, 281 none_targets) 282 283 self.assertFalse(mock_vs.called) 284 self.assertTrue(mock_print.called) 285 self.assertFalse(mock_j.called) 286 self.assertFalse(mock_c_prj.called) 287 self.assertFalse(mock_genc.called) 288 self.assertFalse(mock_c.called) 289 290 c_targets = {constant.JAVA: None, 291 constant.C_CPP: ['1', '2', '3'], 292 constant.RUST: None} 293 294 aidegen_main._launch_ide_by_module_contents(args, ide_obj, lang, 295 c_targets, False) 296 self.assertFalse(mock_vs.called) 297 self.assertTrue(mock_print.called) 298 self.assertFalse(mock_j.called) 299 self.assertFalse(mock_c_prj.called) 300 self.assertFalse(mock_genc.called) 301 self.assertFalse(mock_c.called) 302 303 test_all = True 304 305 aidegen_main._launch_ide_by_module_contents(args, ide_obj, lang, 306 none_targets, test_all) 307 self.assertTrue(mock_vs.called) 308 self.assertFalse(mock_j.called) 309 self.assertFalse(mock_genc.called) 310 self.assertFalse(mock_c.called) 311 mock_vs.reset_mock() 312 313 lang_targets = {constant.JAVA: ['a', 'b', 'c'], 314 constant.C_CPP: ['1', '2', '3'], 315 constant.RUST: None} 316 317 aidegen_main._launch_ide_by_module_contents(args, ide_obj, lang, 318 lang_targets) 319 self.assertFalse(mock_vs.called) 320 self.assertTrue(mock_j.called) 321 self.assertFalse(mock_genc.called) 322 self.assertFalse(mock_c.called) 323 324 mock_vs.reset_mock() 325 mock_c.reset_mock() 326 mock_genc.reset_mock() 327 mock_j.reset_mock() 328 args = aidegen_main._parse_args(['', '-l', 'c']) 329 mock_init.return_value = None 330 self._init_project_config(args) 331 lang = constant.C_CPP 332 aidegen_main._launch_ide_by_module_contents(args, ide_obj, lang, 333 lang_targets) 334 self.assertTrue(mock_c_prj.called) 335 self.assertFalse(mock_vs.called) 336 self.assertTrue(mock_genc.called) 337 self.assertTrue(mock_c.called) 338 self.assertFalse(mock_j.called) 339 340 mock_vs.reset_mock() 341 mock_c.reset_mock() 342 mock_genc.reset_mock() 343 mock_j.reset_mock() 344 aidegen_main._launch_ide_by_module_contents(args, ide_obj, lang, 345 c_targets) 346 self.assertFalse(mock_vs.called) 347 self.assertTrue(mock_genc.called) 348 self.assertTrue(mock_c.called) 349 self.assertFalse(mock_j.called) 350 351 mock_vs.reset_mock() 352 mock_c.reset_mock() 353 mock_genc.reset_mock() 354 mock_j.reset_mock() 355 lang = constant.JAVA 356 aidegen_main._launch_ide_by_module_contents(args, ide_obj, lang, 357 java_targets) 358 self.assertFalse(mock_vs.called) 359 self.assertTrue(mock_j.called) 360 self.assertFalse(mock_c.called) 361 self.assertFalse(mock_genc.called) 362 363 args = aidegen_main._parse_args(['frameworks/base', '-i', 'c']) 364 mock_vs.reset_mock() 365 mock_c.reset_mock() 366 mock_genc.reset_mock() 367 mock_c_prj.reset_mock() 368 mock_j.reset_mock() 369 lang = constant.C_CPP 370 aidegen_main._launch_ide_by_module_contents(args, ide_obj, lang, 371 lang_targets) 372 self.assertFalse(mock_vs.called) 373 self.assertFalse(mock_j.called) 374 self.assertTrue(mock_c.called) 375 self.assertTrue(mock_c_prj.called) 376 self.assertTrue(mock_genc.called) 377 378 args = aidegen_main._parse_args(['frameworks/base']) 379 mock_vs.reset_mock() 380 mock_c.reset_mock() 381 mock_genc.reset_mock() 382 mock_c_prj.reset_mock() 383 mock_j.reset_mock() 384 os.environ[constant.AIDEGEN_TEST_MODE] = 'true' 385 lang = constant.JAVA 386 aidegen_main._launch_ide_by_module_contents(args, None, lang, 387 lang_targets) 388 self.assertFalse(mock_vs.called) 389 self.assertTrue(mock_j.called) 390 self.assertFalse(mock_c.called) 391 self.assertFalse(mock_c_prj.called) 392 self.assertFalse(mock_genc.called) 393 del os.environ[constant.AIDEGEN_TEST_MODE] 394 395 @mock.patch.object(aidegen_main, '_launch_ide') 396 @mock.patch.object(aidegen_main, '_generate_project_files') 397 @mock.patch.object(project_info.ProjectInfo, 'multi_projects_locate_source') 398 @mock.patch.object(project_info.ProjectInfo, 'generate_projects') 399 def test_create_and_launch_java_projects(self, mock_prj, mock_compile, 400 mock_gen_file, mock_launch): 401 """Test _create_and_launch_java_projects.""" 402 ide_obj = 'test_ide' 403 target = ['a', 'b'] 404 mock_prj_list = mock.MagicMock() 405 mock_prj_list.project_absolute_path = 'test_path' 406 prj = [mock_prj_list] 407 mock_prj.return_value = prj 408 aidegen_main._create_and_launch_java_projects(ide_obj, target) 409 self.assertTrue(mock_prj.called_with(target)) 410 self.assertTrue(mock_compile.called_with(prj)) 411 self.assertTrue(mock_gen_file.called_with(prj)) 412 self.assertTrue(mock_launch.called_with(ide_obj)) 413 mock_launch.reset_mock() 414 415 aidegen_main._create_and_launch_java_projects(None, target) 416 self.assertFalse(mock_launch.called) 417 418 @mock.patch.object(aidegen_main, '_launch_ide') 419 @mock.patch.object(vscode_workspace_file_gen, 420 'generate_code_workspace_file') 421 @mock.patch.object(common_util, 'get_related_paths') 422 def test_launch_vscode_without_ide_object( 423 self, mock_get_rel, mock_gen_code, mock_launch_ide): 424 """Test _launch_vscode function without ide object.""" 425 mock_get_rel.return_value = 'rel', 'abs' 426 aidegen_main._launch_vscode(None, mock.Mock(), ['Settings'], [], []) 427 self.assertTrue(mock_get_rel.called) 428 self.assertTrue(mock_gen_code.called) 429 self.assertFalse(mock_launch_ide.called) 430 431 @mock.patch.object(aidegen_main, '_launch_ide') 432 @mock.patch.object(vscode_workspace_file_gen, 433 'generate_code_workspace_file') 434 @mock.patch.object(common_util, 'get_related_paths') 435 def test_launch_vscode_with_ide_object( 436 self, mock_get_rel, mock_gen_code, mock_get_ide): 437 """Test _launch_vscode function with ide object.""" 438 mock_get_rel.return_value = 'rel', 'abs' 439 aidegen_main._launch_vscode( 440 mock.Mock(), mock.Mock(), ['Settings'], [], []) 441 self.assertTrue(mock_get_rel.called) 442 self.assertTrue(mock_gen_code.called) 443 self.assertTrue(mock_get_ide.called) 444 445 @mock.patch.object(aidegen_main, '_launch_vscode') 446 @mock.patch.object(aidegen_main, '_launch_ide') 447 @mock.patch.object(vscode_workspace_file_gen, 448 'generate_code_workspace_file') 449 @mock.patch.object(common_util, 'get_related_paths') 450 def test_launch_vscode_with_both_languages( 451 self, mock_get_rel, mock_gen_code, mock_get_ide, mock_vscode): 452 """Test _launch_vscode function without ide object.""" 453 mock_get_rel.return_value = 'rel', 'abs' 454 aidegen_main._launch_vscode(None, mock.Mock(), ['Settings'], [], True) 455 self.assertFalse(mock_get_rel.called) 456 self.assertFalse(mock_gen_code.called) 457 self.assertFalse(mock_get_ide.called) 458 self.assertTrue(mock_vscode.called) 459 460 @mock.patch('builtins.print') 461 @mock.patch.object(aidegen_main, '_launch_ide') 462 @mock.patch.object(vscode_workspace_file_gen, 463 'generate_code_workspace_file') 464 @mock.patch.object(common_util, 'get_android_root_dir') 465 @mock.patch.object(aidegen_main, '_get_rust_project_paths') 466 @mock.patch.object(aidegen_main, '_get_cc_project_paths') 467 @mock.patch.object(aidegen_main, '_get_java_project_paths') 468 def test_launch_vscode_with_logic(self, mock_get_java, mock_get_cc, 469 mock_get_rust, mock_get_root, mock_gen, 470 mock_launch, mock_print): 471 """Test _launch_vscode with the logic tests.""" 472 aidegen_main._launch_vscode(None, mock.Mock(), [], [], []) 473 self.assertFalse(mock_get_java.called) 474 self.assertFalse(mock_get_cc.called) 475 self.assertFalse(mock_get_rust.called) 476 self.assertFalse(mock_get_root.called) 477 self.assertFalse(mock_gen.called) 478 self.assertFalse(mock_launch.called) 479 self.assertTrue(mock_print.called) 480 481 mock_get_java.mock_reset() 482 mock_get_cc.mock_reset() 483 mock_get_rust.mock_reset() 484 mock_get_root.mock_reset() 485 mock_gen.mock_reset() 486 mock_launch.mock_reset() 487 mock_print.mock_reset() 488 489 aidegen_main._launch_vscode( 490 mock.Mock(), mock.Mock(), ['Java'], ['CC'], ['Rust']) 491 self.assertTrue(mock_get_java.called) 492 self.assertTrue(mock_get_cc.called) 493 self.assertTrue(mock_get_rust.called) 494 self.assertTrue(mock_get_root.called) 495 self.assertTrue(mock_gen.called) 496 self.assertTrue(mock_launch.called) 497 498 @mock.patch.object(common_util, 'get_related_paths') 499 def test_get_java_project_paths(self, mock_get_related): 500 """Test _get_java_project_paths with conditions.""" 501 abs_path = 'a/b/c/d' 502 rel_path = 'c/d' 503 mock_get_related.return_value = rel_path, abs_path 504 self.assertEqual( 505 [abs_path], aidegen_main._get_java_project_paths( 506 ['Java'], mock.Mock())) 507 mock_get_related.return_value = None, None 508 self.assertEqual( 509 [], aidegen_main._get_java_project_paths(['Java'], mock.Mock())) 510 511 @mock.patch.object( 512 vscode_native_project_file_gen.VSCodeNativeProjectFileGenerator, 513 'generate_c_cpp_properties_json_file') 514 @mock.patch.object( 515 vscode_native_project_file_gen, 'VSCodeNativeProjectFileGenerator') 516 @mock.patch.object(common_util, 'get_related_paths') 517 @mock.patch.object( 518 native_project_info.NativeProjectInfo, 'generate_projects') 519 @mock.patch.object(native_module_info, 'NativeModuleInfo') 520 def test_get_cc_project_paths(self, mock_mod_info, mock_gen, mock_get_rel, 521 mock_gen_vs, mock_gen_vs_file): 522 """Test _get_cc_project_paths with conditions.""" 523 mock_get_rel.return_value = None, None 524 self.assertEqual([], aidegen_main._get_cc_project_paths(['Java'])) 525 self.assertTrue(mock_mod_info.called) 526 self.assertTrue(mock_gen.called) 527 self.assertTrue(mock_get_rel.called) 528 self.assertFalse(mock_gen_vs.called) 529 self.assertFalse(mock_gen_vs_file.called) 530 531 mock_mod_info.mock_reset() 532 mock_gen.mock_reset() 533 mock_get_rel.mock_reset() 534 mock_gen_vs.mock_reset() 535 mock_gen_vs_file.mock_reset() 536 537 abs_path = 'a/b/c/d' 538 rel_path = 'c/d' 539 mock_get_rel.return_value = rel_path, abs_path 540 self.assertEqual([abs_path], aidegen_main._get_cc_project_paths(['CC'])) 541 self.assertTrue(mock_mod_info.called) 542 self.assertTrue(mock_gen.called) 543 self.assertTrue(mock_get_rel.called) 544 self.assertTrue(mock_gen_vs.called) 545 546 def test_get_rust_project_paths(self): 547 """Test _get_rust_project_paths with conditions.""" 548 abs_path = 'a/b/c/d' 549 rel_path = 'c/d' 550 root = 'a/b' 551 self.assertEqual( 552 [abs_path], aidegen_main._get_rust_project_paths([abs_path], root)) 553 self.assertEqual( 554 [abs_path], aidegen_main._get_rust_project_paths([rel_path], root)) 555 556 @mock.patch.object(aidegen_main, '_launch_ide_by_module_contents') 557 @mock.patch.object(native_util, 'get_java_cc_and_rust_projects') 558 @mock.patch.object(native_module_info, 'NativeModuleInfo') 559 @mock.patch.object(module_info, 'AidegenModuleInfo') 560 @mock.patch.object(ide_util, 'get_ide_util_instance') 561 @mock.patch.object(project_config, 'ProjectConfig') 562 def test_aidegen_main(self, mock_config, mock_get_ide, mock_mod_info, 563 mock_native, mock_get_project, mock_launch_ide): 564 """Test aidegen_main function with conditions.""" 565 target = 'Settings' 566 args = aidegen_main._parse_args([target, '-i', 'v']) 567 config = mock.Mock() 568 config.targets = [target] 569 config.ide_name = constant.IDE_VSCODE 570 mock_config.return_value = config 571 ide = mock.Mock() 572 mock_get_ide.return_value = ide 573 mock_get_project.return_value = config.targets, [], [] 574 aidegen_main.aidegen_main(args) 575 self.assertTrue(mock_config.called) 576 self.assertTrue(mock_get_ide.called) 577 self.assertTrue(mock_mod_info.called) 578 self.assertTrue(mock_native.called) 579 self.assertTrue(mock_get_project.called) 580 mock_launch_ide.assert_called_with( 581 args, ide, constant.JAVA, 582 {constant.JAVA: [target], constant.C_CPP: [], constant.RUST: []}, 583 True) 584 585 mock_config.mock_reset() 586 mock_get_ide.mock_reset() 587 mock_mod_info.mock_reset() 588 mock_native.mock_reset() 589 mock_get_project.mock_reset() 590 mock_launch_ide.mock_reset() 591 592 args = aidegen_main._parse_args([target]) 593 config.ide_name = constant.IDE_INTELLIJ 594 aidegen_main.aidegen_main(args) 595 self.assertTrue(mock_config.called) 596 self.assertTrue(mock_get_ide.called) 597 self.assertTrue(mock_mod_info.called) 598 self.assertTrue(mock_native.called) 599 self.assertTrue(mock_get_project.called) 600 mock_launch_ide.assert_called_with( 601 args, ide, constant.JAVA, 602 {constant.JAVA: [target], constant.C_CPP: [], constant.RUST: []}, 603 False) 604 605 606if __name__ == '__main__': 607 unittest.main() 608