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..11413b933 --- /dev/null +++ b/tests/test_cli_convertor.py @@ -0,0 +1,83 @@ +# 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' + expected_infile = 'infile.rdf' + expected_outfile = 'outfile.json' + + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + + 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' + expected_outfile = 'outfile.json' + + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + + 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 + expected_infile = 'infile.rdf' + + infile, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to) + + 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, outfile = determine_infile_and_outfile(infile_given, outfile_given, src, from_, to)