def compare_directories(directory, ref_directory, ignore=()): extra_files = [] missing_files = [] for root, dirs, files in os.walk(directory): for single_file in [f for f in files if f not in ignore]: path = os.path.join(root, single_file) rel_path = os.path.relpath(path, directory) ref_path = os.path.join(ref_directory, rel_path) if os.path.exists(ref_path): if os.path.splitext(ref_path)[-1] == '.gro': utils.assert_gro_equal(path, ref_path) compare(path, ref_path) else: extra_files.append(rel_path) for root, dirs, files in os.walk(ref_directory): for single_file in [f for f in files if f not in ignore]: path = os.path.join(root, single_file) rel_path = os.path.relpath(path, ref_directory) ref_path = os.path.join(directory, rel_path) if not os.path.exists(ref_path): missing_files.append(rel_path) assert not bool(extra_files), ("The following files are unexpected: {}" .format(extra_files)) assert not bool(missing_files), ("The following files are missing: {}" .format(missing_files))
def run_and_compare(arguments, input_dir, ref_gro, ref_top, ref_stdout, ref_stderr, runner=_run_external): """ Run insane and compare its output against a reference """ # Create the command as a list for subprocess.Popen. # The arguments can be pass to the current function as a string or as a # list of arguments. If they are passed as a string, they need to be # converted to a list. arguments = _arguments_as_list(arguments) # The name of the output gro file must be provided to insane for insane to # work. Since we also need that file name, let's get it from insane's # arguments. gro_output = _output_from_arguments(arguments, option='-o') if ref_top is not None: top_output = _output_from_arguments(arguments, option='-p') # We want insane to run in a temporary directory. This allows to keep the # file system clean, and it avoids mixing output of different tests. with utils.tempdir(): out, err, returncode = run_insane(arguments, input_dir, runner=runner) assert not returncode assert os.path.exists(gro_output) if os.path.splitext(gro_output)[-1] == '.gro': utils.assert_gro_equal(gro_output, ref_gro) else: compare(gro_output, ref_gro) compare(utils.ContextStringIO(out), ref_stdout) compare(utils.ContextStringIO(err), ref_stderr) if ref_top is not None: compare(top_output, ref_top)
def compare_directories(directory, ref_directory, ignore=()): extra_files = [] missing_files = [] for root, dirs, files in os.walk(directory): for single_file in [f for f in files if f not in ignore]: path = os.path.join(root, single_file) rel_path = os.path.relpath(path, directory) ref_path = os.path.join(ref_directory, rel_path) if os.path.exists(ref_path): if os.path.splitext(ref_path)[-1] == '.gro': utils.assert_gro_equal(path, ref_path) compare(path, ref_path) else: extra_files.append(rel_path) for root, dirs, files in os.walk(ref_directory): for single_file in [f for f in files if f not in ignore]: path = os.path.join(root, single_file) rel_path = os.path.relpath(path, ref_directory) ref_path = os.path.join(directory, rel_path) if not os.path.exists(ref_path): missing_files.append(rel_path) assert not bool(extra_files), ( "The following files are unexpected: {}".format(extra_files)) assert not bool(missing_files), ( "The following files are missing: {}".format(missing_files))
def test_equal(self): """ Make sure that identical files do not fail. """ with utils.tempdir(): with open('ref.gro', 'w') as outfile: print(textwrap.dedent(self.ref_gro_content), file=outfile, end='') utils.assert_gro_equal('ref.gro', 'ref.gro')
def test_diff_in_tolerance(self): """ Make sure that small errors in coordinates are not caught. """ gro_content = """\ INSANE! Membrane UpperLeaflet>POPC=1 LowerLeaflet>POPC=1 4 1POPC NC3 1 2.111 14.647 11.951 1POPC PO4 2 2.177 14.644 11.651 1POPC GL1 3 2.128 14.642 11.352 # Is within tolerance 1POPC GL2 4 1.961 14.651 11.351 10 10 10""" with utils.tempdir(): with open('ref.gro', 'w') as outfile: print(textwrap.dedent(self.ref_gro_content), file=outfile, end='') with open('content.gro', 'w') as outfile: print(textwrap.dedent(gro_content), file=outfile, end='') utils.assert_gro_equal('content.gro', 'ref.gro')
def run_and_compare(arguments, input_dir, ref_gro, ref_top, ref_stdout, ref_stderr, runner=_run_external): """ Run program and compare its output against a reference """ # Create the command as a list for subprocess.Popen. # The arguments can be pass to the current function as a string or as a # list of arguments. If they are passed as a string, they need to be # converted to a list. arguments = _arguments_as_list(arguments) # The name of the output file must be provided to program for program to # work. Since we also need that file name, let's get it from program's # arguments. ## Magic here -- Expect DRAGONS gro_output = _output_from_arguments(arguments, option='-o') if ref_top is not None: top_output = _output_from_arguments(arguments, option='-p') # We want program to run in a temporary directory. This allows to keep the # file system clean, and it avoids mixing output of different tests. with utils.tempdir(): out, err, returncode = run_program(arguments, input_dir, runner=runner) assert not returncode assert os.path.exists(gro_output) if os.path.splitext(gro_output)[-1] == '.gro': utils.assert_gro_equal(gro_output, ref_gro) else: compare(gro_output, ref_gro) compare(utils.ContextStringIO(out), ref_stdout) compare(utils.ContextStringIO(err), ref_stderr) if ref_top is not None: compare(top_output, ref_top)