def test_write_read_files(self): '''test_write_read_files will test the functions write_file and read_file ''' print("Testing utils.write_file...") from spython.utils import write_file import json tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_file(tmpfile, "hello!") self.assertTrue(os.path.exists(tmpfile)) print("Testing utils.read_file...") from spython.utils import read_file content = read_file(tmpfile)[0] self.assertEqual("hello!", content) from spython.utils import write_json print("Testing utils.write_json...") print("...Case 1: Providing bad json") bad_json = {"Wakkawakkawakka'}": [{True}, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) with self.assertRaises(TypeError) as cm: write_json(bad_json, tmpfile) print("...Case 2: Providing good json") good_json = {"Wakkawakkawakka": [True, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_json(good_json, tmpfile) with open(tmpfile, 'r') as filey: content = json.loads(filey.read()) self.assertTrue(isinstance(content, dict)) self.assertTrue("Wakkawakkawakka" in content)
def test_write_bad_json(tmp_path): from spython.utils import write_json bad_json = {"Wakkawakkawakka'}": [{True}, "2", 3]} tmpfile = str(tmp_path / "json_file.txt") assert not os.path.exists(tmpfile) with pytest.raises(TypeError): write_json(bad_json, tmpfile)
def test_write_json(tmp_path): import json from spython.utils import write_json good_json = {"Wakkawakkawakka": [True, "2", 3]} tmpfile = str(tmp_path / "good_json_file.txt") assert not os.path.exists(tmpfile) write_json(good_json, tmpfile) with open(tmpfile, "r") as f: content = json.loads(f.read()) assert isinstance(content, dict) assert "Wakkawakkawakka" in content
def main(args, options, parser): """This function serves as a wrapper around the DockerParser, SingularityParser, DockerWriter, and SingularityParser converters. We can either save to file if args.outfile is defined, or print to the console if not. """ # We need something to work with if not args.files: parser.print_help() sys.exit(1) # Get the user specified input and output files outfile = None if len(args.files) > 1: outfile = args.files[1] # First try to get writer and parser, if not defined will return None writer = get_writer(args.writer) parser = get_parser(args.parser) # If the user wants to auto-detect the type if args.parser == "auto": if "dockerfile" in args.files[0].lower(): parser = get_parser("docker") elif "singularity" in args.files[0].lower(): parser = get_parser("singularity") # If the parser still isn't defined, no go. if parser is None: bot.exit( "Please provide a Dockerfile or Singularity recipe, or define the --parser type." ) # If the writer needs auto-detect if args.writer == "auto": if parser.name == "docker": writer = get_writer("singularity") else: writer = get_writer("docker") # If the writer still isn't defined, no go if writer is None: bot.exit("Please define the --writer type.") # Initialize the chosen parser recipeParser = parser(args.files[0]) # By default, discover entrypoint / cmd from Dockerfile entrypoint = "/bin/bash" force = False if args.entrypoint is not None: entrypoint = args.entrypoint # This is only done if the user intended to print json here recipeParser.entrypoint = args.entrypoint recipeParser.cmd = None force = True if args.json: if outfile is not None: if not os.path.exists(outfile): if force: write_json(outfile, recipeParser.recipe.json()) else: bot.exit("%s exists, set --force to overwrite." % outfile) else: print(json.dumps(recipeParser.recipe.json(), indent=4)) else: # Do the conversion recipeWriter = writer(recipeParser.recipe) result = recipeWriter.convert(runscript=entrypoint, force=force) # If the user specifies an output file, save to it if outfile is not None: write_file(outfile, result) # Otherwise, convert and print to screen else: print(result)