From 14fd7792eea5f782b6baa4c608d18f4a5b9f677c Mon Sep 17 00:00:00 2001 From: Connor Martin Date: Mon, 17 Jun 2019 12:13:36 -0500 Subject: [PATCH] add json imports --- conda_env/cli/main_export.py | 3 ++- conda_env/env.py | 8 ++++++-- tests/conda_env/test_cli.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/conda_env/cli/main_export.py b/conda_env/cli/main_export.py index a5a6215e5ca..6021faa7c77 100644 --- a/conda_env/cli/main_export.py +++ b/conda_env/cli/main_export.py @@ -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 @@ -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) diff --git a/conda_env/env.py b/conda_env/env.py index 7b81d984c56..43147fdaa23 100644 --- a/conda_env/env.py +++ b/conda_env/env.py @@ -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 @@ -217,7 +218,7 @@ 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 @@ -225,7 +226,10 @@ def to_dict(self): 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() diff --git a/tests/conda_env/test_cli.py b/tests/conda_env/test_cli.py index 56e7f9f794b..ff92f51d2fa 100644 --- a/tests/conda_env/test_cli.py +++ b/tests/conda_env/test_cli.py @@ -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 @@ -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): """