def test_temporary_cd(): """Test temporary_cd() context manager""" initial_dir = os.getcwd() temporary_dir = '/' from openforcefield.utils import temporary_cd with temporary_cd(temporary_dir): assert os.getcwd() == temporary_dir assert os.getcwd() == initial_dir
def run_script_file(file_path): """Run through the shell a python script.""" with tempfile.TemporaryDirectory() as tmp_dir: with temporary_cd(tmp_dir): cmd = ["python", file_path] if "conformer_energies.py" in file_path: cmd.append("--filename") mol_file = get_data_file_path( "molecules/ruxolitinib_conformers.sdf") cmd.append(mol_file) try: subprocess.check_call(cmd) except subprocess.CalledProcessError: raise Exception(f"Example {file_path} failed")
def call(self, cmd, raise_err=True): """Helper function to execute direct CLI calls""" if type(cmd) == str: cmd = cmd.split() with tempfile.TemporaryDirectory() as tmp_dir: with temporary_cd(tmp_dir): proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() if err and raise_err: raise Exception(err) return out.decode(), err.decode()
def test_p_prefix(self, toolkit): """Ensure the output file has the -p prefix""" registry = make_registry(toolkit) ethanol = get_data_file_path("molecules/ethanol.sdf") mols_out = generate_conformers( molecule=ethanol, forcefield="openff-1.0.0.offxml", registry=registry, prefix="test_ethanol", ) with tempfile.TemporaryDirectory() as tmp_dir: with temporary_cd(tmp_dir): write_mols( mols=mols_out, toolkit_registry=registry, molecule=ethanol, prefix="test_ethanol", ) assert pathlib.Path("test_ethanol_0.sdf").is_file()