import subprocess from pathlib import Path from majordome import local_files current_dir = Path(__file__).parent tmp_dir = current_dir / "tmp" def test_save_local_file(clean_tmpdir): file_content = b"some bytes" filename = "toto_titi.tutu" file_to_save = tmp_dir / filename tmp_files = list(tmp_dir.glob("*")) assert len(tmp_files) == 0 local_files.save(file_to_save, file_content) tmp_files = list(tmp_dir.glob("*")) assert len(tmp_files) == 1 tmp_file = tmp_files[0] assert tmp_file.name == filename assert tmp_file.read_bytes() == file_content def test_make_file_executable(clean_tmpdir): file = tmp_dir / "toto_titi.tutu" file.parent.mkdir(parents=True, exist_ok=True) file.touch() assert file.stat().st_mode == 0o100664 local_files.set_execution_rights(file) assert file.stat().st_mode == 0o100775 def test_download_and_make_executable(clean_tmpdir): file_content = b"some bytes" filename = "toto_titi.tutu" file_to_save = tmp_dir / filename tmp_files = list(tmp_dir.glob("*")) assert len(tmp_files) == 0 local_files.save_and_make_executable(file_to_save, file_content) tmp_files = list(tmp_dir.glob("*")) assert len(tmp_files) == 1 tmp_file = tmp_files[0] assert tmp_file.name == filename assert tmp_file.read_bytes() == file_content assert tmp_file.stat().st_mode == 0o100775 def test_install_package(): file = Path("/home/stan/Téléchargements/freetube_0.23.2_amd64.deb") local_files.install_package(file) apt = subprocess.Popen(("apt", "list", "--installed"), stdout=subprocess.PIPE) output = subprocess.check_output(("grep", "freetube"), stdin=apt.stdout) apt.wait() assert "0.23.2" in output.decode("utf-8") def test_run(): file = Path("/home/stan/Logiciels/Nextcloud-3.14.2-x86_64.AppImage") local_files.run(file) ps = subprocess.Popen(("ps", "-ef"), stdout=subprocess.PIPE) output = subprocess.check_output(("grep", str(file)), stdin=ps.stdout) ps.wait() assert output is not None assert str(file) in output.decode("utf-8")