def test_target_prefix(self): with tempdir() as prefix: mkdir_p(join(prefix, 'first', 'envs')) mkdir_p(join(prefix, 'second', 'envs')) create_package_cache_directory(join(prefix, 'first', 'pkgs')) create_package_cache_directory(join(prefix, 'second', 'pkgs')) envs_dirs = (join(prefix, 'first', 'envs'), join(prefix, 'second', 'envs')) with env_var('CONDA_ENVS_DIRS', os.pathsep.join(envs_dirs), reset_context): # with both dirs writable, choose first reset_context((), argparse_args=AttrDict(name='blarg', func='create')) assert context.target_prefix == join(envs_dirs[0], 'blarg') # with first dir read-only, choose second PackageCacheData._cache_.clear() make_read_only(join(prefix, 'first', 'pkgs', 'urls.txt')) reset_context((), argparse_args=AttrDict(name='blarg', func='create')) assert context.target_prefix == join(envs_dirs[1], 'blarg') # if first dir is read-only but environment exists, choose first PackageCacheData._cache_.clear() mkdir_p(join(envs_dirs[0], 'blarg')) touch(join(envs_dirs[0], 'blarg', 'history')) reset_context((), argparse_args=AttrDict(name='blarg', func='create')) assert context.target_prefix == join(envs_dirs[0], 'blarg')
def test_CompilePycAction_generic(self): package_info = AttrDict(package_metadata=AttrDict(noarch=AttrDict(type=NoarchType.generic))) noarch = package_info.package_metadata and package_info.package_metadata.noarch assert noarch.type == NoarchType.generic axns = CompilePycAction.create_actions({}, package_info, self.prefix, None, ()) assert axns == () package_info = AttrDict(package_metadata=None) axns = CompilePycAction.create_actions({}, package_info, self.prefix, None, ()) assert axns == ()
def test_CompilePycAction_noarch_python(self): target_python_version = '%d.%d' % sys.version_info[:2] sp_dir = get_python_site_packages_short_path(target_python_version) transaction_context = { 'target_python_version': target_python_version, 'target_site_packages_short_path': sp_dir, } package_info = AttrDict(package_metadata=AttrDict(noarch=AttrDict(type=NoarchType.python))) file_link_actions = [ AttrDict( source_short_path='site-packages/something.py', target_short_path=get_python_noarch_target_path('site-packages/something.py', sp_dir), ), AttrDict( # this one shouldn't get compiled source_short_path='something.py', target_short_path=get_python_noarch_target_path('something.py', sp_dir), ), ] axns = CompilePycAction.create_actions(transaction_context, package_info, self.prefix, None, file_link_actions) assert len(axns) == 1 axn = axns[0] assert axn.source_full_path == join(self.prefix, win_path_ok(get_python_noarch_target_path('site-packages/something.py', sp_dir))) assert axn.target_full_path == join(self.prefix, win_path_ok(pyc_path(get_python_noarch_target_path('site-packages/something.py', sp_dir), target_python_version))) # make .py file in prefix that will be compiled mkdir_p(dirname(axn.source_full_path)) with open(axn.source_full_path, 'w') as fh: fh.write("value = 42\n") # symlink the current python python_full_path = join(self.prefix, get_python_short_path(target_python_version)) mkdir_p(dirname(python_full_path)) create_link(sys.executable, python_full_path, LinkType.softlink) axn.execute() assert isfile(axn.target_full_path) # remove the source .py file so we're sure we're importing the pyc file below rm_rf(axn.source_full_path) assert not isfile(axn.source_full_path) if (3, ) > sys.version_info >= (3, 5): # we're probably dropping py34 support soon enough anyway imported_pyc_file = load_python_file(axn.target_full_path) assert imported_pyc_file.value == 42 axn.reverse() assert not isfile(axn.target_full_path)
def test_confirm_yn_no(self): args = AttrDict({ 'dry_run': False, }) from conda.cli.common import confirm_yn with pytest.raises(CondaSystemExit): confirm_yn(args)
def test_specify_channels_cli_adding_defaults_no_condarc(self): """ When the channel haven't been specified in condarc, 'defaults' should be present when specifying channel in the cli """ reset_context((), argparse_args=AttrDict(channel=['conda-forge'])) assert context.channels == ('conda-forge', 'defaults')
def test_always_yes(self): with env_var('CONDA_ALWAYS_YES', 'true', reset_context): args = AttrDict({ 'dry_run': False, }) from conda.cli.common import confirm_yn choice = confirm_yn(args) assert choice is True
def test_confirm_yn_yes(self): args = AttrDict({ 'dry_run': False, }) from conda.cli.common import confirm_yn with captured() as c: choice = confirm_yn() assert choice is True assert "Invalid choice" in c.stdout
def test_specify_channels_cli_condarc(self): # When the channel have been specified in condarc, these channels # should be used along with the one specified reset_context((), argparse_args=AttrDict(channel=['conda-forge'])) string = dals(""" channels: ['defaults', 'conda-forge'] """) rd = odict(testdata=YamlRawParameter.make_raw_parameters('testdata', yaml_round_trip_load(string))) context._set_raw_data(rd) assert context.channels == ('defaults', 'conda-forge')
def test_prefix_cli_flag(self): envs_dirs = (join(self.prefix, 'first-envs-dir'), join(self.prefix, 'seconds-envs-dir')) with env_var('CONDA_ENVS_DIRS', os.pathsep.join(envs_dirs), reset_context): # even if prefix doesn't exist, it can be a target prefix reset_context((), argparse_args=AttrDict(prefix='./blarg')) target_prefix = join(os.getcwd(), 'blarg') assert context.target_prefix == target_prefix assert not isdir(target_prefix)
def test_dry_run_exit(self): args = AttrDict({ 'dry_run': True, }) from conda.cli.common import confirm_yn with pytest.raises(DryRunExit): confirm_yn(args) from conda.cli.common import confirm with pytest.raises(DryRunExit): confirm(args)
def test_specify_same_channels_cli_as_in_condarc(self): # When the channel have been specified in condarc, these channels # should be used along with the one specified # In this test, the given channel in cli is the same as in condarc # 'defaults' should not be added # See https://github.com/conda/conda/issues/10732 reset_context((), argparse_args=AttrDict(channel=['conda-forge'])) string = dals(""" channels: ['conda-forge'] """) rd = odict(testdata=YamlRawParameter.make_raw_parameters('testdata', yaml_round_trip_load(string))) context._set_raw_data(rd) assert context.channels == ('conda-forge', )
def test_name_cli_flag(self): envs_dirs = (join(self.prefix, 'first-envs-dir'), join(self.prefix, 'seconds-envs-dir')) with env_var('CONDA_ENVS_DIRS', os.pathsep.join(envs_dirs), reset_context): # with both dirs writable, choose first reset_context((), argparse_args=AttrDict(name='blarg')) assert context.target_prefix == join(envs_dirs[0], 'blarg') # with first dir read-only, choose second EnvsDirectory(envs_dirs[0])._is_writable = False reset_context((), argparse_args=AttrDict(name='blarg')) assert context.target_prefix == join(envs_dirs[1], 'blarg') # if first dir is read-only but environment exists, choose first EnvsDirectory._cache_.pop(envs_dirs[0]) mkdir_p(join(envs_dirs[0], 'blarg')) touch(join(envs_dirs[0], 'blarg', 'history')) reset_context((), argparse_args=AttrDict(name='blarg')) assert context.target_prefix == join(envs_dirs[0], 'blarg') EnvsDirectory._cache_ = {}
def test_specify_different_channels_cli_condarc(self): """ When the channel have been specified in condarc, these channels should be used along with the one specified In this test, the given channel in cli is different from condarc 'defaults' should not be added """ reset_context((), argparse_args=AttrDict(channel=['other'])) string = dals(""" channels: ['conda-forge'] """) rd = odict(testdata=YamlRawParameter.make_raw_parameters('testdata', yaml_round_trip_load(string))) context._set_raw_data(rd) assert context.channels == ('conda-forge', 'other')
class ExceptionTests(TestCase): def test_TooManyArgumentsError(self): expected = 2 received = 5 offending_arguments = "groot" exc = TooManyArgumentsError(expected, received, offending_arguments) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.TooManyArgumentsError'>" assert json_obj['exception_name'] == 'TooManyArgumentsError' assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) assert json_obj['expected'] == 2 assert json_obj['received'] == 5 assert json_obj['offending_arguments'] == "groot" with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip( ) == "TooManyArgumentsError: Got 5 arguments (g, r, o, o, t) but expected 2." def test_TooFewArgumentsError(self): expected = 5 received = 2 exc = TooFewArgumentsError(expected, received) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.TooFewArgumentsError'>" assert json_obj['exception_name'] == 'TooFewArgumentsError' assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) assert json_obj['expected'] == 5 assert json_obj['received'] == 2 with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip( ) == "TooFewArgumentsError: Got 2 arguments but expected 5." def test_BasicClobberError(self): source_path = "some/path/on/goodwin.ave" target_path = "some/path/to/wright.st" exc = BasicClobberError(source_path, target_path, context) t = repr(exc) with env_var("CONDA_PATH_CONFLICT", "prevent", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == dals(""" ClobberError: Conda was asked to clobber an existing path. source path: some/path/on/goodwin.ave target path: some/path/to/wright.st """).strip() def test_KnownPackageClobberError(self): target_path = "some/where/on/goodwin.ave" colliding_dist_being_linked = "Groot" colliding_linked_dist = "Liquid" exc = KnownPackageClobberError(target_path, colliding_dist_being_linked, colliding_linked_dist, context) with env_var("CONDA_PATH_CONFLICT", "prevent", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == dals(""" ClobberError: The package 'Groot' cannot be installed due to a path collision for 'some/where/on/goodwin.ave'. This path already exists in the target prefix, and it won't be removed by an uninstall action in this transaction. The path appears to be coming from the package 'Liquid', which is already installed in the prefix. """).strip() def test_UnknownPackageClobberError(self): target_path = "siebel/center/for/c.s" colliding_dist_being_linked = "Groot" exc = UnknownPackageClobberError(target_path, colliding_dist_being_linked, context) with env_var("CONDA_PATH_CONFLICT", "prevent", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == dals(""" ClobberError: The package 'Groot' cannot be installed due to a path collision for 'siebel/center/for/c.s'. This path already exists in the target prefix, and it won't be removed by an uninstall action in this transaction. The path is one that conda doesn't recognize. It may have been created by another package manager. """).strip() def test_SharedLinkPathClobberError(self): target_path = "some/where/in/shampoo/banana" incompatible_package_dists = "Groot" exc = SharedLinkPathClobberError(target_path, incompatible_package_dists, context) with env_var("CONDA_PATH_CONFLICT", "prevent", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == dals(""" ClobberError: This transaction has incompatible packages due to a shared path. packages: G, r, o, o, t path: 'some/where/in/shampoo/banana' """).strip() def test_CondaFileNotFoundError(self): filename = "Groot" exc = PathNotFoundError(filename) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.PathNotFoundError'>" assert json_obj['exception_name'] == 'PathNotFoundError' assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == "PathNotFoundError: Groot" def test_DirectoryNotFoundError(self): directory = "Groot" exc = DirectoryNotFoundError(directory) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.DirectoryNotFoundError'>" assert json_obj['exception_name'] == 'DirectoryNotFoundError' assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) assert json_obj['path'] == "Groot" with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == "DirectoryNotFoundError: Groot" def test_MD5MismatchError(self): url = "https://download.url/path/to/file.tar.bz2" target_full_path = "/some/path/on/disk/another-name.tar.bz2" expected_md5sum = "abc123" actual_md5sum = "deadbeef" exc = ChecksumMismatchError(url, target_full_path, "md5", expected_md5sum, actual_md5sum) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.ChecksumMismatchError'>" assert json_obj['exception_name'] == 'ChecksumMismatchError' assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) assert json_obj['url'] == url assert json_obj['target_full_path'] == target_full_path assert json_obj['expected_checksum'] == expected_md5sum assert json_obj['actual_checksum'] == actual_md5sum with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == dals(""" ChecksumMismatchError: Conda detected a mismatch between the expected content and downloaded content for url 'https://download.url/path/to/file.tar.bz2'. download saved to: /some/path/on/disk/another-name.tar.bz2 expected md5: abc123 actual md5: deadbeef """).strip() def test_PackageNotFoundError(self): package = "Potato" with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: exc = PackagesNotFoundError((package, )) conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.PackagesNotFoundError'>" assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == dals(""" PackagesNotFoundError: The following packages are missing from the target environment: - Potato """).strip() def test_CondaRevisionError(self): message = "Potato" exc = CondaRevisionError(message) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.CondaRevisionError'>" assert json_obj['exception_name'] == 'CondaRevisionError' assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == "CondaRevisionError: Potato." def test_CondaKeyError(self): key = "Potato" message = "Potato is not a key." exc = CondaKeyError(key, message) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.CondaKeyError'>" assert json_obj['exception_name'] == 'CondaKeyError' assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) assert json_obj['key'] == "Potato" with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip( ) == "CondaKeyError: 'Potato': Potato is not a key." def test_CondaHTTPError(self): msg = "Potato" url = "https://download.url/path/to/Potato.tar.gz" status_code = "Potato" reason = "COULD NOT CONNECT" elapsed_time = 1.24 exc = CondaHTTPError(msg, url, status_code, reason, elapsed_time) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.CondaHTTPError'>" assert json_obj['exception_name'] == 'CondaHTTPError' assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) assert json_obj['url'] == url assert json_obj['status_code'] == status_code assert json_obj['reason'] == reason assert json_obj['elapsed_time'] == elapsed_time with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert dals(""" CondaHTTPError: HTTP Potato COULD NOT CONNECT for url <https://download.url/path/to/Potato.tar.gz> Elapsed: 1.24 Potato """).strip() in c.stderr.strip() def test_CommandNotFoundError_simple(self): cmd = "instate" exc = CommandNotFoundError(cmd) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.CommandNotFoundError'>" assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == ( "CommandNotFoundError: No command 'conda instate'.\n" "Did you mean 'conda install'?") def test_CommandNotFoundError_conda_build(self): cmd = "build" exc = CommandNotFoundError(cmd) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.CommandNotFoundError'>" assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == ( "CommandNotFoundError: To use 'conda build', install conda-build.") @patch('requests.post', side_effect=( AttrDict(headers=AttrDict(Location='somewhere.else'), status_code=302, raise_for_status=lambda: None), AttrDict(raise_for_status=lambda: None), )) def test_print_unexpected_error_message_upload_1(self, post_mock): with env_var('CONDA_REPORT_ERRORS', 'true', stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: ExceptionHandler()(_raise_helper, AssertionError()) username = getpass.getuser() assert username_not_in_post_mock(post_mock, username) assert post_mock.call_count == 2 assert c.stdout == '' assert "conda version" in c.stderr @patch('requests.post', side_effect=( AttrDict(headers=AttrDict(Location='somewhere.else'), status_code=302, raise_for_status=lambda: None), AttrDict(headers=AttrDict(Location='somewhere.again'), status_code=301, raise_for_status=lambda: None), AttrDict(raise_for_status=lambda: None), )) def test_print_unexpected_error_message_upload_2(self, post_mock): with env_var('CONDA_JSON', 'true', stack_callback=conda_tests_ctxt_mgmt_def_pol): with env_var('CONDA_YES', 'yes', stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: ExceptionHandler()(_raise_helper, AssertionError()) username = getpass.getuser() assert username_not_in_post_mock(post_mock, username) assert post_mock.call_count == 3 assert len(json.loads(c.stdout)['conda_info']['channels']) >= 2 assert not c.stderr @patch('requests.post', side_effect=( AttrDict(headers=AttrDict(Location='somewhere.else'), status_code=302, raise_for_status=lambda: None), AttrDict(raise_for_status=lambda: None), )) @patch('conda.exceptions.input', return_value='y') @patch('conda.exceptions.os.isatty', return_value=True) def test_print_unexpected_error_message_upload_3(self, isatty_mock, input_mock, post_mock): with captured() as c: ExceptionHandler()(_raise_helper, AssertionError()) username = getpass.getuser() assert username_not_in_post_mock(post_mock, username) assert input_mock.call_count == 1 assert post_mock.call_count == 2 assert c.stdout == '' assert "conda version" in c.stderr @patch('requests.post', side_effect=( AttrDict(headers=AttrDict(Location='somewhere.else'), status_code=302, raise_for_status=lambda: None), AttrDict(raise_for_status=lambda: None), )) @patch('getpass.getuser', return_value='some name') def test_print_unexpected_error_message_upload_username_with_spaces( self, pwuid, post_mock): with env_var('CONDA_REPORT_ERRORS', 'true', stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: ExceptionHandler()(_raise_helper, AssertionError()) error_data = json.loads(post_mock.call_args[1].get("data")) assert error_data.get("has_spaces") == True assert error_data.get("is_ascii") == True assert post_mock.call_count == 2 assert c.stdout == '' assert "conda version" in c.stderr @patch('requests.post', side_effect=( AttrDict(headers=AttrDict(Location='somewhere.else'), status_code=302, raise_for_status=lambda: None), AttrDict(raise_for_status=lambda: None), )) @patch('getpass.getuser', return_value='my√nameΩ') def test_print_unexpected_error_message_upload_username_with_unicode( self, pwuid, post_mock): with env_var('CONDA_REPORT_ERRORS', 'true', stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: ExceptionHandler()(_raise_helper, AssertionError()) error_data = json.loads(post_mock.call_args[1].get("data")) assert error_data.get("has_spaces") == False assert error_data.get("is_ascii") == False assert post_mock.call_count == 2 assert c.stdout == '' assert "conda version" in c.stderr @patch('requests.post', return_value=None) @patch('conda.exceptions.input', return_value='n') def test_print_unexpected_error_message_opt_out_1(self, input_mock, post_mock): with env_var('CONDA_REPORT_ERRORS', 'false', stack_callback=conda_tests_ctxt_mgmt_def_pol): e = AssertionError() with captured() as c: ExceptionHandler()(_raise_helper, AssertionError()) assert input_mock.call_count == 0 assert post_mock.call_count == 0 assert c.stdout == '' print(c.stderr, file=sys.stderr) assert "conda version" in c.stderr @patch('requests.post', return_value=None) @patch('conda.exceptions.input', return_value='n') @patch('conda.exceptions.os.isatty', return_value=True) def test_print_unexpected_error_message_opt_out_2(self, isatty_mock, input_mock, post_mock): with captured() as c: ExceptionHandler()(_raise_helper, AssertionError()) assert input_mock.call_count == 1 assert post_mock.call_count == 0 assert c.stdout == '' assert "conda version" in c.stderr def test_BinaryPrefixReplacementError(self): new_data_length = 1104 original_data_length = 1404 new_prefix = "some/where/on/goodwin.ave" path = "some/where/by/boneyard/creek" placeholder = "save/my/spot/in/374" exc = BinaryPrefixReplacementError(path, placeholder, new_prefix, original_data_length, new_data_length) with env_var("CONDA_JSON", "yes", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) json_obj = json.loads(c.stdout) assert not c.stderr assert json_obj[ 'exception_type'] == "<class 'conda.exceptions.BinaryPrefixReplacementError'>" assert json_obj['exception_name'] == 'BinaryPrefixReplacementError' assert json_obj['message'] == text_type(exc) assert json_obj['error'] == repr(exc) assert json_obj['new_data_length'] == 1104 assert json_obj['original_data_length'] == 1404 assert json_obj['new_prefix'] == new_prefix assert json_obj['path'] == path assert json_obj['placeholder'] == placeholder with env_var("CONDA_JSON", "no", stack_callback=conda_tests_ctxt_mgmt_def_pol): with captured() as c: conda_exception_handler(_raise_helper, exc) assert not c.stdout assert c.stderr.strip() == dals(""" BinaryPrefixReplacementError: Refusing to replace mismatched data length in binary file. path: some/where/by/boneyard/creek placeholder: save/my/spot/in/374 new prefix: some/where/on/goodwin.ave original data Length: 1404 new data length: 1104 """).strip()
def test_CreatePythonEntryPointAction_noarch_python(self): target_python_version = '%d.%d' % sys.version_info[:2] transaction_context = { 'target_python_version': target_python_version, } package_info = AttrDict(package_metadata=AttrDict(noarch=AttrDict( type=NoarchType.python, entry_points=( 'command1=some.module:main', 'command2=another.somewhere:go', ), ))) axns = CreatePythonEntryPointAction.create_actions( transaction_context, package_info, self.prefix, LinkType.hardlink) grouped_axns = groupby(lambda ax: isinstance(ax, LinkPathAction), axns) windows_exe_axns = grouped_axns.get(True, ()) assert len(windows_exe_axns) == (2 if on_win else 0) py_ep_axns = grouped_axns.get(False, ()) assert len(py_ep_axns) == 2 py_ep_axn = py_ep_axns[0] command, module, func = parse_entry_point_def( 'command1=some.module:main') assert command == 'command1' if on_win: target_short_path = "%s\\%s-script.py" % ( get_bin_directory_short_path(), command) else: target_short_path = "%s/%s" % (get_bin_directory_short_path(), command) assert py_ep_axn.target_full_path == join(self.prefix, target_short_path) assert py_ep_axn.module == module == 'some.module' assert py_ep_axn.func == func == 'main' mkdir_p(dirname(py_ep_axn.target_full_path)) py_ep_axn.execute() assert isfile(py_ep_axn.target_full_path) if not on_win: assert is_executable(py_ep_axn.target_full_path) with open(py_ep_axn.target_full_path) as fh: lines = fh.readlines() first_line = lines[0].strip() last_line = lines[-1].strip() if not on_win: python_full_path = join( self.prefix, get_python_short_path(target_python_version)) assert first_line == "#!%s" % python_full_path assert last_line == "sys.exit(%s())" % func py_ep_axn.reverse() assert not isfile(py_ep_axn.target_full_path) if on_win: windows_exe_axn = windows_exe_axns[0] target_short_path = "%s\\%s.exe" % (get_bin_directory_short_path(), command) assert windows_exe_axn.target_full_path == join( self.prefix, target_short_path) mkdir_p(dirname(windows_exe_axn.target_full_path)) windows_exe_axn.verify() windows_exe_axn.execute() assert isfile(windows_exe_axn.target_full_path) assert is_executable(windows_exe_axn.target_full_path) src = compute_md5sum( join(context.conda_prefix, 'Scripts/conda.exe')) assert src == compute_md5sum(windows_exe_axn.target_full_path) windows_exe_axn.reverse() assert not isfile(windows_exe_axn.target_full_path)
def test_CreatePythonEntryPointAction_generic(self): package_info = AttrDict(package_metadata=None) axns = CreatePythonEntryPointAction.create_actions({}, package_info, self.prefix, None) assert axns == ()