From 05cdc9b203320c647f5c6a5969cea28d5182bc63 Mon Sep 17 00:00:00 2001 From: Meret Behrens Date: Thu, 13 Oct 2022 11:22:29 +0200 Subject: [PATCH 1/2] [issue-152] fix cli convertor and add testcase Signed-off-by: Meret Behrens --- spdx/cli_tools/convertor.py | 65 ++++++++++++++++++---------- tests/test_cli_convertor.py | 86 +++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 23 deletions(-) create mode 100644 tests/test_cli_convertor.py diff --git a/spdx/cli_tools/convertor.py b/spdx/cli_tools/convertor.py index 07c27752f..005d92a23 100644 --- a/spdx/cli_tools/convertor.py +++ b/spdx/cli_tools/convertor.py @@ -22,31 +22,16 @@ def print_help_msg(command): with click.Context(command) as ctx: click.echo(command.get_help(ctx)) -@click.command() -@click.argument("src", nargs=-1) -@click.option("--infile", "-i", help="The file to be converted ") -@click.option("--outfile", "-o", help="The file after converting") -@click.option( - "--to", - "-t", - type=click.Choice(["json", "rdf", "yaml", "xml", "tag"], case_sensitive=False) -) -@click.option( - "--from", - "-f", - "from_", - type=click.Choice(["tag", "rdf"], case_sensitive=False)) -@click.option("--force", is_flag=True, help="convert even if there are some parsing errors or inconsistencies") -def main(infile, outfile, src, from_, to, force): - """ - CLI-TOOL for converting a RDF or TAG file to RDF, JSON, YAML, TAG or XML format. - - To use : run 'convertor -f -t ' command on terminal or use ' convertor --infile --outfile ' - - """ - if infile is None and outfile is None and len(src) == 2: +def determine_infile_and_outfile(infile, outfile, src, from_, to): + if infile is not None and outfile is not None: """ when the CLI is of given format: + ' convertor ---infile ---outfile . + """ + return infile, outfile + + elif infile is None and outfile is None and len(src) == 2: + """ ' convertor -f/--from -t/--to . """ infile = src[0] @@ -58,6 +43,7 @@ def main(infile, outfile, src, from_, to, force): if to is not None: outfile_path = os.path.splitext(outfile)[0] outfile = outfile_path + "." + to + return infile, outfile elif infile is None and outfile is not None: """ @@ -67,6 +53,7 @@ def main(infile, outfile, src, from_, to, force): if from_ is not None: infile_path = os.path.splitext(infile)[0] infile = infile_path + "." + from_ + return infile, outfile elif infile is not None and outfile is None: """ @@ -76,9 +63,41 @@ def main(infile, outfile, src, from_, to, force): if to is not None: outfile_path = os.path.splitext(outfile)[0] outfile = outfile_path + "." + to + return infile, outfile + else: + raise ValueError("Given arguments for convertor are invalid.") + + +@click.command() +@click.argument("src", nargs=-1) +@click.option("--infile", "-i", help="The file to be converted ") +@click.option("--outfile", "-o", help="The file after converting") +@click.option( + "--to", + "-t", + type=click.Choice(["json", "rdf", "yaml", "xml", "tag"], case_sensitive=False) +) +@click.option( + "--from", + "-f", + "from_", + type=click.Choice(["tag", "rdf"], case_sensitive=False)) +@click.option("--force", is_flag=True, help="convert even if there are some parsing errors or inconsistencies") +def main(infile, outfile, src, from_, to, force): + """ + CLI-TOOL for converting a RDF or TAG file to RDF, JSON, YAML, TAG or XML format. + + To use : run 'convertor -f -t ' command on terminal or use ' convertor --infile --outfile ' + + """ + try: + infile, outfile = determine_infile_and_outfile(infile, outfile, src, from_, to) + except ValueError as err: + print(err) print_help_msg(main) return + doc, errors = parse_file(infile) if errors: print("Errors while parsing: ", errors) diff --git a/tests/test_cli_convertor.py b/tests/test_cli_convertor.py new file mode 100644 index 000000000..6b4bf2ba1 --- /dev/null +++ b/tests/test_cli_convertor.py @@ -0,0 +1,86 @@ +# Copyright (c) 2022 spdx tool contributors +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +from unittest import TestCase + +from spdx.cli_tools.convertor import determine_infile_and_outfile + +from tests.testing_utils import raises + +class TestConvertor(TestCase): + maxDiff = None + + def test_determine_input_with_known_i_o_format(self): + infile_given = 'infile.rdf' + outfile_given = 'outfile.json' + src = () + from_ = None + to = None + + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + + assert infile == infile_given + assert outfile == outfile_given + + def test_determine_input_with_unknown_i_o_format(self): + infile_given = None + outfile_given = None + src = ('infile.in', 'outfile.out') + from_ = 'rdf' + to = 'json' + + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + + infile_path = os.path.splitext(src[0])[0] + infile_given = infile_path + "." + from_ + outfile_path = os.path.splitext(src[1])[0] + outfile_given = outfile_path + "." + to + assert infile == infile_given + assert outfile == outfile_given + + def test_determine_input_with_known_i_format_unknown_o_format(self): + infile_given = 'infile.rdf' + outfile_given = None + src = ('outfile') + from_ = None + to = 'json' + + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + + outfile_path = os.path.splitext(src[0])[0] + outfile_given = outfile_path + "." + to + assert infile == infile_given + assert outfile == outfile_given + + def test_determine_input_with_unknown_i_format_known_o_format(self): + infile_given = None + outfile_given = 'outfile.json' + src = ('infile') + from_ = 'rdf' + to = None + + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + + infile_path = os.path.splitext(src[0])[0] + infile_given = infile_path + "." + from_ + assert infile == infile_given + assert outfile == outfile_given + + @raises(ValueError) + def test_determine_input_with_invalid_arguments(self): + infile_given = None + outfile_given = None + src = () + from_ = None + to = None + + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) From b1e22ace8f145e0a44152f700819d52153874dec Mon Sep 17 00:00:00 2001 From: Meret Behrens Date: Thu, 13 Oct 2022 17:13:47 +0200 Subject: [PATCH 2/2] [issue-152] shorten testcase to test only determine_infile_and_outfile Signed-off-by: Meret Behrens --- tests/test_cli_convertor.py | 61 ++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/tests/test_cli_convertor.py b/tests/test_cli_convertor.py index 6b4bf2ba1..11413b933 100644 --- a/tests/test_cli_convertor.py +++ b/tests/test_cli_convertor.py @@ -16,6 +16,7 @@ from tests.testing_utils import raises + class TestConvertor(TestCase): maxDiff = None @@ -37,50 +38,46 @@ def test_determine_input_with_unknown_i_o_format(self): src = ('infile.in', 'outfile.out') from_ = 'rdf' to = 'json' + expected_infile = 'infile.rdf' + expected_outfile = 'outfile.json' infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) - infile_path = os.path.splitext(src[0])[0] - infile_given = infile_path + "." + from_ - outfile_path = os.path.splitext(src[1])[0] - outfile_given = outfile_path + "." + to - assert infile == infile_given - assert outfile == outfile_given + assert infile == expected_infile + assert outfile == expected_outfile def test_determine_input_with_known_i_format_unknown_o_format(self): - infile_given = 'infile.rdf' - outfile_given = None - src = ('outfile') - from_ = None - to = 'json' + infile_given = 'infile.rdf' + outfile_given = None + src = ('outfile',) + from_ = None + to = 'json' + expected_outfile = 'outfile.json' - infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) - outfile_path = os.path.splitext(src[0])[0] - outfile_given = outfile_path + "." + to - assert infile == infile_given - assert outfile == outfile_given + assert infile == infile_given + assert outfile == expected_outfile def test_determine_input_with_unknown_i_format_known_o_format(self): - infile_given = None - outfile_given = 'outfile.json' - src = ('infile') - from_ = 'rdf' - to = None + infile_given = None + outfile_given = 'outfile.json' + src = ('infile',) + from_ = 'rdf' + to = None + expected_infile = 'infile.rdf' - infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) - infile_path = os.path.splitext(src[0])[0] - infile_given = infile_path + "." + from_ - assert infile == infile_given - assert outfile == outfile_given + assert infile == expected_infile + assert outfile == outfile_given @raises(ValueError) def test_determine_input_with_invalid_arguments(self): - infile_given = None - outfile_given = None - src = () - from_ = None - to = None + infile_given = None + outfile_given = None + src = () + from_ = None + to = None - infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to)