Skip to content

Commit

Permalink
add json imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Martin committed Jun 17, 2019
1 parent e584e72 commit 14fd779
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion conda_env/cli/main_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from argparse import RawDescriptionHelpFormatter
import os
import textwrap
import json

from conda.cli.conda_argparse import add_parser_json, add_parser_prefix

Expand Down Expand Up @@ -100,7 +101,7 @@ def execute(args, parser):
env.add_channels(args.channel)

if args.file is None:
print(env.to_dict()) if args.json else print(env.to_yaml())
print(json.dumps(env.to_dict())) if args.json else print(env.to_yaml())
else:
fp = open(args.file, 'wb')
env.to_dict(stream=fp) if args.json else env.to_yaml(stream=fp)
8 changes: 6 additions & 2 deletions conda_env/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from itertools import chain
import os
import re
import json

from conda.base.context import context
from conda.cli import common # TODO: this should never have to import form conda.cli
Expand Down Expand Up @@ -217,15 +218,18 @@ def add_channels(self, channels):
def remove_channels(self):
self.channels = []

def to_dict(self):
def to_dict(self, stream=None):
d = yaml.dict([('name', self.name)])
if self.channels:
d['channels'] = self.channels
if self.dependencies:
d['dependencies'] = self.dependencies.raw
if self.prefix:
d['prefix'] = self.prefix
return d
if stream is None:
return d
stream.write(json.dumps(d))


def to_yaml(self, stream=None):
d = self.to_dict()
Expand Down
31 changes: 31 additions & 0 deletions tests/conda_env/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from conda_env.cli.main import create_parser, do_call as do_call_conda_env
from conda_env.exceptions import EnvironmentFileExtensionNotValid, EnvironmentFileNotFound
from conda_env.yaml import load as yaml_load
from conda_env.yaml import odict

from . import support_file

Expand Down Expand Up @@ -309,6 +310,36 @@ def test_env_export(self):
run_env_command(Commands.ENV_REMOVE, test_env_name_2)
assert not env_is_created(test_env_name_2)

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

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

snowflake, e, = run_env_command(Commands.ENV_EXPORT, test_env_name_2, '--json')

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

run_env_command(Commands.ENV_REMOVE, test_env_name_2)
self.assertFalse(env_is_created(test_env_name_2))

# regression test for #6220
snowflake, e, = run_env_command(Commands.ENV_EXPORT, test_env_name_2, '--no-builds', '--json')
assert not e.strip()

env_description = odict(json.loads(snowflake))
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_list(self):
"""
Expand Down

0 comments on commit 14fd779

Please sign in to comment.