def test_config_command_remove_force(): # Finally, test --remove, --remove-key with make_temp_condarc() as rc: run_command(Commands.CONFIG, '--file', rc, '--add', 'channels', 'test') run_command(Commands.CONFIG, '--file', rc, '--set', 'always_yes', 'true') stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--remove', 'channels', 'test') assert stdout == stderr == '' assert yaml_round_trip_load(_read_test_condarc(rc)) == { 'channels': ['defaults'], 'always_yes': True } stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--remove', 'channels', 'test', use_exception_handler=True) assert stdout == '' assert "CondaKeyError: 'channels': 'test' is not in the 'channels' " \ "key of the config file" in stderr stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--remove', 'disallow', 'python', use_exception_handler=True) assert stdout == '' assert "CondaKeyError: 'disallow': key 'disallow' " \ "is not in the config file" in stderr stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--remove-key', 'always_yes') assert stdout == stderr == '' assert yaml_round_trip_load(_read_test_condarc(rc)) == { 'channels': ['defaults'] } stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--remove-key', 'always_yes', use_exception_handler=True) assert stdout == '' assert "CondaKeyError: 'always_yes': key 'always_yes' " \ "is not in the config file" in stderr
def test_signing_metadata_url_base(self): SIGNING_URL_BASE = "https://conda.example.com/pkgs" string = f"signing_metadata_url_base: {SIGNING_URL_BASE}" reset_context() rd = odict(testdata=YamlRawParameter.make_raw_parameters('testdata', yaml_round_trip_load(string))) context._set_raw_data(rd) assert context.signing_metadata_url_base == SIGNING_URL_BASE
def test_create_env_no_default_packages(self): with make_temp_envs_dir() as envs_dir: with env_var('CONDA_ENVS_DIRS', envs_dir, stack_callback=conda_tests_ctxt_mgmt_def_pol): # set packages run_conda_command(CondaCommands.CONFIG, envs_dir, "--add", "create_default_packages", "pip") run_conda_command(CondaCommands.CONFIG, envs_dir, "--add", "create_default_packages", "flask") stdout, stderr, _ = run_conda_command(CondaCommands.CONFIG, envs_dir, "--show") yml_obj = yaml_round_trip_load(stdout) assert yml_obj['create_default_packages'] == ['flask', 'pip'] assert not package_is_installed(envs_dir, 'python=2') assert not package_is_installed(envs_dir, 'pytz') assert not package_is_installed(envs_dir, 'flask') env_name = str(uuid4())[:8] prefix = join(envs_dir, env_name) run_command(Commands.CREATE, env_name, support_file('env_with_dependencies.yml'), "--no-default-packages") assert exists(prefix) assert package_is_installed(prefix, 'python=2') assert package_is_installed(prefix, 'pytz') assert not package_is_installed(prefix, 'flask')
def _get_expandvars_context(attr, config_expr, env_value): with mock.patch.dict(os.environ, {"TEST_VAR": env_value}): reset_context(()) string = f"{attr}: {config_expr}" rd = odict(testdata=YamlRawParameter.make_raw_parameters('testdata', yaml_round_trip_load(string))) context._set_raw_data(rd) return getattr(context, attr)
def setUp(self): string = dals(""" custom_channels: darwin: https://some.url.somewhere/stuff chuck: http://another.url:8080/with/path custom_multichannels: michele: - https://do.it.with/passion - learn_from_every_thing steve: - more-downloads migrated_custom_channels: darwin: s3://just/cant chuck: file:///var/lib/repo/ migrated_channel_aliases: - https://conda.anaconda.org channel_alias: ftp://new.url:8082 conda-build: root-dir: /some/test/path proxy_servers: http: http://user:[email protected]:8080 https: none ftp: sftp: '' ftps: false rsync: 'false' aggressive_update_packages: [] channel_priority: false """) reset_context(()) rd = odict(testdata=YamlRawParameter.make_raw_parameters( 'testdata', yaml_round_trip_load(string))) context._set_raw_data(rd)
def test_create_advanced_pip(self): with make_temp_envs_dir() as envs_dir: with env_vars( { 'CONDA_ENVS_DIRS': envs_dir, 'CONDA_DLL_SEARCH_MODIFICATION_ENABLE': 'true', }, stack_callback=conda_tests_ctxt_mgmt_def_pol): env_name = str(uuid4())[:8] run_command(Commands.CREATE, env_name, support_file('pip_argh.yml')) out_file = join(envs_dir, 'test_env.yaml') # make sure that the export reconsiders the presence of pip interop being enabled PrefixData._cache_.clear() with env_vars({ 'CONDA_ENVS_DIRS': envs_dir, }, stack_callback=conda_tests_ctxt_mgmt_def_pol): # note: out of scope of pip interop var. Should be enabling conda pip interop itself. run_command(Commands.EXPORT, env_name, out_file) with open(out_file) as f: d = yaml_round_trip_load(f) assert {'pip': ['argh==0.26.2']} in d['dependencies']
def test_client_ssl_cert(self): string = dals(""" client_ssl_cert_key: /some/key/path """) reset_context() rd = odict(testdata=YamlRawParameter.make_raw_parameters('testdata', yaml_round_trip_load(string))) context._set_raw_data(rd) pytest.raises(ValidationError, context.validate_configuration)
def test_dump(): obj = dict([ ('a_seq', [1, 2, 3]), ('a_map', { 'a_key': 'a_value' }), ]) assert obj == yaml_round_trip_load(yaml_round_trip_dump(obj))
def test_channels_defaults_condarc(self): # no channels provided in cli reset_context(()) 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_config_command_show(): # test alphabetical yaml output with make_temp_condarc() as rc: stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--show') output_keys = yaml_round_trip_load(stdout).keys() assert stderr == '' assert sorted(output_keys) == [item for item in output_keys]
def test_signing_metadata_url_base_empty_default_channels(self): string = dals(""" default_channels: [] """) reset_context() rd = odict(testdata=YamlRawParameter.make_raw_parameters('testdata', yaml_round_trip_load(string))) context._set_raw_data(rd) assert len(context.default_channels) is 0 assert context.signing_metadata_url_base is None
def test_map_parameter_must_be_map(self): # regression test for conda/conda#3467 string = dals(""" proxy_servers: bad values """) data = odict(s1=YamlRawParameter.make_raw_parameters( 's1', yaml_round_trip_load(string))) config = SampleConfiguration()._set_raw_data(data) raises(InvalidTypeError, config.validate_all)
def test_set_rc_without_user_rc(): if os.path.exists(sys_rc_path): # Backup system rc_config with open(sys_rc_path, 'r') as fh: sys_rc_config_backup = yaml_round_trip_load(fh) restore_sys_rc_config_backup = True else: restore_sys_rc_config_backup = False if os.path.exists(user_rc_path): # Backup user rc_config with open(user_rc_path, 'r') as fh: user_rc_config_backup = yaml_round_trip_load(fh) # Remove user rc_path os.remove(user_rc_path) restore_user_rc_config_backup = True else: restore_user_rc_config_backup = False try: # Write custom system sys_rc_config with open(sys_rc_path, 'w') as rc: rc.write(yaml_round_trip_dump({'channels': ['conda-forge']})) except (OSError, IOError): # In case, we don't have writing right to the system rc config file pytest.skip("No writing right to root prefix.") # This would create a user rc_config stdout, stderr, return_code = run_command(Commands.CONFIG, '--add', 'channels', 'test') assert stdout == stderr == '' assert yaml_round_trip_load(_read_test_condarc(user_rc_path)) == { 'channels': ['test', 'conda-forge'] } if restore_user_rc_config_backup: # Restore previous user rc_config with open(user_rc_path, 'w') as rc: rc.write(yaml_round_trip_dump(user_rc_config_backup)) if restore_sys_rc_config_backup: # Restore previous system rc_config with open(sys_rc_path, 'w') as rc: rc.write(yaml_round_trip_dump(sys_rc_config_backup))
def test_config_set(): # Test the config set command # Make sure it accepts only boolean values for boolean keys and any value for string keys with make_temp_condarc() as rc: stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--set', 'always_yes', 'yes') assert stdout == '' assert stderr == '' with open(rc) as fh: content = yaml_round_trip_load(fh.read()) assert content['always_yes'] is True stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--set', 'always_yes', 'no') assert stdout == '' assert stderr == '' with open(rc) as fh: content = yaml_round_trip_load(fh.read()) assert content['always_yes'] is False stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--set', 'proxy_servers.http', '1.2.3.4:5678') assert stdout == '' assert stderr == '' with open(rc) as fh: content = yaml_round_trip_load(fh.read()) assert content['always_yes'] is False assert content['proxy_servers'] == {'http': '1.2.3.4:5678'} stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--set', 'ssl_verify', 'false') assert stdout == '' assert stderr == '' stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--get', 'ssl_verify') assert stdout.strip() == '--set ssl_verify False' assert stderr == ''
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_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_yaml_complex(): test_string = dals(""" single_bool: false single_str: no # comment here a_seq_1: - 1 - 2 - 3 a_seq_2: - 1 # with comment - two: 2 - 3 a_map: # comment field1: true field2: yes # final comment """) python_structure = { 'single_bool': False, 'single_str': 'no', 'a_seq_1': [ 1, 2, 3, ], 'a_seq_2': [ 1, { 'two': 2 }, 3, ], 'a_map': { 'field1': True, 'field2': 'yes', }, } loaded_from_string = yaml_round_trip_load(test_string) assert python_structure == loaded_from_string dumped_from_load = yaml_round_trip_dump(loaded_from_string) print(dumped_from_load) assert dumped_from_load == test_string
def test_set_check_types(key, str_value, py_value): with make_temp_condarc() as rc: stdout, stderr, _ = run_command(Commands.CONFIG, '--file', rc, '--set', key, str_value, use_exception_handler=True) assert stdout == stderr == '' with open(rc) as fh: content = yaml_round_trip_load(fh.read()) if "." in key: key = key.split(".", 1)[0] assert content[key] == py_value
def test_to_yaml_returns_yaml_parseable_string(self): random_name = 'random{}'.format(random.randint(100, 200)) e = env.Environment(name=random_name, channels=['javascript'], dependencies=['nodejs']) expected = { 'name': random_name, 'channels': ['javascript'], 'dependencies': ['nodejs'] } actual = yaml_round_trip_load(StringIO(e.to_yaml())) self.assertEqual(expected, actual)
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')
def load_from_string_data(*seq): return odict((f, YamlRawParameter.make_raw_parameters( f, yaml_round_trip_load(test_yaml_raw[f]))) for f in seq)
def test_config_command_parser(): # Now test the YAML "parser" # Channels is normal content. # create_default_packages has extra spaces in list items condarc = """\ channels: - test - defaults create_default_packages : - ipython - numpy changeps1: false # Here is a comment always_yes: true """ # First verify that this itself is valid YAML assert yaml_round_trip_load(condarc) == { 'channels': ['test', 'defaults'], 'create_default_packages': ['ipython', 'numpy'], 'changeps1': False, 'always_yes': True } with make_temp_condarc(condarc) as rc: stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--get', use_exception_handler=True) print(stdout) assert stdout.strip() == """\ --set always_yes True --set changeps1 False --add channels 'defaults' # lowest priority --add channels 'test' # highest priority --add create_default_packages 'numpy' --add create_default_packages 'ipython'\ """ with open(rc, 'r') as fh: print(fh.read()) stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--prepend', 'channels', 'mychannel') assert stdout == stderr == '' with open(rc, 'r') as fh: print(fh.read()) assert _read_test_condarc(rc) == """\ channels: - mychannel - test - defaults create_default_packages: - ipython - numpy changeps1: false # Here is a comment always_yes: true """ stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--set', 'changeps1', 'true') assert stdout == stderr == '' assert _read_test_condarc(rc) == """\ channels: - mychannel - test - defaults create_default_packages: - ipython - numpy changeps1: true # Here is a comment always_yes: true """ # Test adding a new list key. We couldn't test this above because it # doesn't work yet with odd whitespace condarc = """\ channels: - test - defaults always_yes: true """ with make_temp_condarc(condarc) as rc: stdout, stderr, return_code = run_command(Commands.CONFIG, '--file', rc, '--add', 'disallowed_packages', 'perl') assert stdout == stderr == '' assert _read_test_condarc(rc) == condarc + """\