Exemple #1
0
def test_update__update_then_get() -> None:
    resolution_a = Resolution.make(value="")
    resolution_b = Resolution.make(value="")
    cache = make_cache()
    cache.update(("alpha", ), resolution=resolution_a)
    cache.update(("alpha", ), resolution=resolution_b)
    assert len(cache.resolutions) == 1
    assert cache.get(("alpha", )) == resolution_b
Exemple #2
0
 def resolve(self, support: ResolutionSupport) -> Resolution:
     if self.raises_cannot_resolve_error:
         raise CannotResolveError("cannot reticulate splines")
     return Resolution.make(
         value=self.return_value,
         expires_at=self.return_expires_at,
     )
Exemple #3
0
class ResolutionCache:
    """
    Local cache of resolutions.

    Args:
        context: An environment variable could have different meanings in
                 different contexts. For example,
        path: Path to cache. Uses the user's home directory by default.
    """
    def __init__(
        self,
        context: str,
        logger: Optional[Logger] = None,
        path: Optional[Path] = None,
    ) -> None:
        self.logger = logger or get_logger()
        self.context = context
        self.path = path or Path.home().absolute().joinpath(".wevcache")
        self.resolutions: Dict[Tuple[str, ...], Dict[str, Any]] = {}
        self.logger.debug(
            'ResolutionCache: context="%s" path="%s"',
            self.context,
            self.path,
        )

    def get(self, names: Tuple[str, ...]) -> Optional[Resolution]:
        """
        Gets a cached resolution, or `None` if not cached.
        """
        if store := self.resolutions.get(names, None):
            if values := store.get("values", None):
                if isinstance(values, list):
                    store["values"] = tuple(cast(List[str], values))
            return Resolution(store=store)
Exemple #4
0
 def resolve(self, support: ResolutionSupport) -> Resolution:
     return Resolution.make(
         value=Authoriser(
             domain=self.domain,
             account=self.account,
             profile=self.profile,
             region=self.region,
         ).token,
         expires_at=datetime.now() + timedelta(seconds=self.duration),
     )
Exemple #5
0
def test_write_then_read(cache_path: Path, logger: Logger) -> None:
    resolution = Resolution.make(value="bar")
    writer = make_cache(logger=logger, path=cache_path)
    writer.update(("foo", ), resolution=resolution)
    writer.save()

    reader = make_cache(path=cache_path)
    reader.load()
    assert len(reader.resolutions) == 1
    assert reader.get(("foo", )) == resolution
Exemple #6
0
    def resolve(self, support: ResolutionSupport) -> Resolution:
        """
        Resolves the environment variable.

        Args:
            logger: Logger. Do not log confidential information.

        Returns:
            Resolution.
        """
        value = (self.value if isinstance(self.value, str) else
                 self.separator.join(self.value))
        return Resolution.make(
            value=value,
            expires_at=datetime.now() + timedelta(seconds=60),
        )
Exemple #7
0
 def resolution(self) -> Optional[Resolution]:
     """
     Gets the resolution.
     """
     if store := self.store.get("resolution", None):
         return Resolution(store=store)
Exemple #8
0
    with patch("wev.resolver.get_plugin", return_value=plugin) as patched:
        yield patched


@fixture
def get_plugin_cannot_resolve_error() -> Iterator[PluginBase]:
    plugin = MockPlugin({}, raises_cannot_resolve_error=True)
    with patch("wev.resolver.get_plugin", return_value=plugin) as patched:
        yield patched


@mark.parametrize(
    "resolution, expect",
    [
        (None, False),
        (Resolution.make(value=""), False),
        (
            Resolution.make(
                value=None, expires_at=datetime.now() - timedelta(seconds=60)
            ),
            False,
        ),
        (
            Resolution.make(
                value=None, expires_at=datetime.now() + timedelta(seconds=60)
            ),
            True,
        ),
    ],
)
def test_fresh_resolution(resolution: Optional[Resolution], expect: bool) -> None: