Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
testing for creation and export with variables

fixed issue where variables not exported without --from-history
  • Loading branch information
AlbertDeFusco committed Aug 17, 2020
1 parent 13e1831 commit b592f44
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion conda_env/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ def from_environment(name, prefix, no_builds=False, ignore_channels=False, from_
canonical_name = prec.channel.canonical_name
if canonical_name not in channels:
channels.insert(0, canonical_name)
return Environment(name=name, dependencies=dependencies, channels=channels, prefix=prefix)
return Environment(name=name, dependencies=dependencies, channels=channels, prefix=prefix,
variables=variables)


def from_yaml(yamlstr, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions tests/conda_env/support/valid_keys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ channels:
- anaconda
dependencies:
- nltk
variables:
VARIABLE: value
prefix: /something/miniconda/envs/test
63 changes: 63 additions & 0 deletions tests/conda_env/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
- defaults
'''

environment_1_with_variables = '''
name: env-1
dependencies:
- python
channels:
- defaults
variables:
DUDE: woah
SWEET: yaaa
'''

environment_2 = '''
name: env-1
dependencies:
Expand Down Expand Up @@ -242,6 +253,26 @@ def test_create_valid_env(self):
len([env for env in parsed['envs'] if env.endswith(test_env_name_1)]), 0
)

def test_create_valid_env_with_variables(self):
'''
Creates an environment.yml file and
creates and environment with it
'''

create_env(environment_1_with_variables)
run_env_command(Commands.ENV_CREATE, None)
self.assertTrue(env_is_created(test_env_name_1))

o, e = run_env_command(Commands.ENV_CONFIG, test_env_name_1, "vars", "list", "--json", '-n', test_env_name_1)
output_env_vars = json.loads(o)
assert output_env_vars == {'DUDE': 'woah', "SWEET": "yaaa"}

o, e = run_conda_command(Commands.INFO, None, "--json")
parsed = json.loads(o)
self.assertNotEqual(
len([env for env in parsed['envs'] if env.endswith(test_env_name_1)]), 0
)

@pytest.mark.integration
def test_conda_env_create_http(self):
'''
Expand Down Expand Up @@ -461,6 +492,38 @@ def test_env_export(self):
assert len(env_description['dependencies'])
for spec_str in env_description['dependencies']:
assert spec_str.count('=') == 1


run_env_command(Commands.ENV_REMOVE, test_env_name_2)
assert not env_is_created(test_env_name_2)

def test_env_export_with_variables(self):
"""
Test conda env export
"""

run_conda_command(Commands.CREATE, test_env_name_2, "flask")
assert env_is_created(test_env_name_2)

run_env_command(Commands.ENV_CONFIG, test_env_name_2, "vars", "set", "DUDE=woah", "SWEET=yaaa", "-n", test_env_name_2)

snowflake, e, = run_env_command(Commands.ENV_EXPORT, test_env_name_2)

with Utf8NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as env_yaml:
env_yaml.write(snowflake)
env_yaml.flush()
env_yaml.close()

run_env_command(Commands.ENV_REMOVE, test_env_name_2)
self.assertFalse(env_is_created(test_env_name_2))
run_env_command(Commands.ENV_CREATE, None, "--file", env_yaml.name)
self.assertTrue(env_is_created(test_env_name_2))

snowflake, e = run_env_command(Commands.ENV_EXPORT, test_env_name_2, '--no-builds')
assert not e.strip()
env_description = yaml_safe_load(snowflake)
assert len(env_description['variables'])
assert env_description['variables'].keys()

run_env_command(Commands.ENV_REMOVE, test_env_name_2)
assert not env_is_created(test_env_name_2)
Expand Down

0 comments on commit b592f44

Please sign in to comment.