def test_raises_if_abspath_is_not_absolute(self, tmpdir): basedir = pathlib.Path(tmpdir) abspath = basedir / "some_file" abspath.write_text("some content") relpath = abspath.relative_to(basedir) with pytest.raises(ValueError) as exc_info: _fileutils.create_relpath(abspath=relpath, basedir=basedir) assert "Argument 'abspath' must be absolute" in str(exc_info.value)
def test_raises_if_abspath_is_not_contained_in_basedir(self, tmpdir): basedir = pathlib.Path(tmpdir) with tempfile.NamedTemporaryFile() as tmpfile: abspath = pathlib.Path(tmpfile.name) with pytest.raises(ValueError) as exc_info: _fileutils.create_relpath(abspath=abspath, basedir=basedir) assert f"'{abspath}' does not start with '{basedir}'" in str( exc_info.value)
def test_raises_if_abspath_does_not_exist(self, tmpdir): basedir = pathlib.Path(tmpdir) with tempfile.NamedTemporaryFile(dir=basedir) as tmpfile: # tmpfile is destroyed when exiting this context manager non_existing_file = pathlib.Path(tmpfile.name) with pytest.raises(FileNotFoundError) as exc_info: _fileutils.create_relpath(abspath=non_existing_file, basedir=basedir) assert f"No such file or directory: '{non_existing_file}'" in str( exc_info.value)
def discover_dirty_files( repo_root: pathlib.Path, ) -> List[_fileutils.RelativePath]: """ Returns: A list of relative file paths for files that need to be sanitized. """ git_dir_relpath = ".git" relpaths = (_fileutils.create_relpath(path, repo_root) for path in repo_root.rglob("*") if path.is_file() and path.relative_to(repo_root).parts[0] != git_dir_relpath) return [sp for sp in relpaths if _syntax.file_is_dirty(sp, repo_root)]
def test_target_branch_with_iso8859_file(self, fake_repo): """Test sanitize-repo when there are ISO8859 files in the repo. This is to ensure that we can sanitize using ISO8859 without errors. """ in_src_path = testhelpers.get_resource("iso8859.txt") in_dst_path = fake_repo.path / in_src_path.name shutil.copy(in_src_path, in_dst_path) sanitized_src_path = testhelpers.get_resource("sanitized-iso8859.txt") sanitized_dst_path = fake_repo.path / sanitized_src_path.name shutil.copy(sanitized_src_path, sanitized_dst_path) fake_repo.repo.git.add(sanitized_dst_path) fake_repo.repo.git.add(in_dst_path) fake_repo.repo.git.commit("-m", "Add ISO8859 files") in_rel = _fileutils.create_relpath(in_dst_path, fake_repo.path) sanitzed_rel = _fileutils.create_relpath(sanitized_dst_path, fake_repo.path) fake_repo.file_infos.append( _FileInfo( original_text=in_rel.read_text_relative_to(fake_repo.path), expected_text=sanitzed_rel.read_text_relative_to( fake_repo.path), abspath=in_dst_path, relpath=in_rel.__str__(), encoding=in_rel.encoding, )) target_branch = "student-version" run_repobee(f"sanitize repo --target-branch {target_branch} " f"--repo-root {fake_repo.path}".split()) fake_repo.repo.git.checkout(target_branch) fake_repo.repo.git.reset("--hard") assert_expected_text_in_files(fake_repo.file_infos)