Skip to content

Commit

Permalink
Raising exceptions when requesting static files if the file is not in…
Browse files Browse the repository at this point in the history
… the repo (relative path goes out of repo) or when the file doesn't exist (#8)
  • Loading branch information
mikicz committed Jan 21, 2018
1 parent 3e0a1a5 commit ddb9eff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
10 changes: 8 additions & 2 deletions arca/_arca.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dogpile.cache import make_region, CacheRegion
from git import Repo

from .exceptions import ArcaMisconfigured
from .exceptions import ArcaMisconfigured, FileOutOfRangeError
from .backend import BaseBackend
from .result import Result
from .task import Task
Expand Down Expand Up @@ -87,7 +87,7 @@ def _make_region(self) -> CacheRegion:
region.set("last_arca_run", datetime.now().isoformat())
except ModuleNotFoundError:
raise ModuleNotFoundError("Cache backend cannot load a required library.")
except Exception as e:
except Exception:
raise ArcaMisconfigured("The provided cache is not working - most likely misconfigured.")

return region
Expand Down Expand Up @@ -214,6 +214,12 @@ def static_filename(self, repo: str, branch: str, relative_path: Union[str, Path
result = repo_path / relative_path
result = result.resolve()

if repo_path not in result.parents:
raise FileOutOfRangeError(f"{relative_path} is not inside the repository.")

if not result.exists():
raise FileNotFoundError(f"{relative_path} does not exist in the repository.")

logger.info("Static path for %s is %s", relative_path, result)

return result
6 changes: 5 additions & 1 deletion arca/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ArcaException(Exception):
pass


class ArcaMisconfigured(ArcaException):
class ArcaMisconfigured(ValueError, ArcaException):
pass


Expand All @@ -24,3 +24,7 @@ class PushToRegistryError(ArcaException):
def __init__(self, *args, full_output=None, **kwargs):
super().__init__(*args, **kwargs)
self.full_output = full_output


class FileOutOfRangeError(ValueError, ArcaException):
pass
15 changes: 12 additions & 3 deletions tests/backend/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from git import Repo

from arca import Arca, VenvBackend, DockerBackend, Task
from arca.exceptions import BuildError
from arca.exceptions import BuildError, FileOutOfRangeError

if os.environ.get("TRAVIS", False):
BASE_DIR = "/home/travis/build/{}/test_loc".format(os.environ.get("TRAVIS_REPO_SLUG", "mikicz/arca"))
Expand Down Expand Up @@ -156,6 +156,8 @@ def test_static_files(backend, file_location):
arca = Arca(backend=backend, base_dir=BASE_DIR)

git_dir = Path("/tmp/arca/") / str(uuid4())
git_url = f"file://{git_dir}"
branch = "master"

repo = Repo.init(git_dir)
if not file_location:
Expand All @@ -169,11 +171,18 @@ def test_static_files(backend, file_location):
repo.index.commit("Initial")

relative_path = Path(file_location) / "test_file.txt"
nonexistent_relative_path = Path(file_location) / "test_file2.txt"

result = arca.static_filename(f"file://{git_dir}", "master", relative_path)
result = arca.static_filename(git_url, branch, relative_path)

assert filepath.read_text() == result.read_text()

result = arca.static_filename(f"file://{git_dir}", "master", str(relative_path))
result = arca.static_filename(git_url, branch, str(relative_path))

assert filepath.read_text() == result.read_text()

with pytest.raises(FileOutOfRangeError):
arca.static_filename(git_url, branch, "../file.txt")

with pytest.raises(FileNotFoundError):
arca.static_filename(git_url, branch, nonexistent_relative_path)

0 comments on commit ddb9eff

Please sign in to comment.