Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/opal-common/opal_common/confi/confi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import inspect
import json
import logging
import os
import string
from collections import OrderedDict
from functools import partial, wraps
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union

from decouple import Csv, UndefinedValueError, config, text_type, undefined
from decouple import AutoConfig, Csv, UndefinedValueError, text_type, undefined
from opal_common.authentication.casting import cast_private_key, cast_public_key
from opal_common.authentication.types import EncryptionKeyFormat, PrivateKey, PublicKey
from opal_common.confi.cli import get_cli_object_for_config_objects
Expand All @@ -22,6 +23,13 @@
from typer import Typer


def build_config() -> AutoConfig:
return AutoConfig(os.environ.get("CONFI_PATH") or os.getcwd())


config = build_config()


class Placeholder(object):
"""Placeholder instead of default value for decouple."""

Expand Down
39 changes: 39 additions & 0 deletions packages/opal-common/opal_common/tests/test_confi_config_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from opal_common.confi.confi import build_config

# Unique key that is not present in the real environment, so decouple resolves
# it from the .env file we write rather than from os.environ.
_TEST_KEY = "OPAL_TEST_CONFI_ENV_FOLDER_KEY"


def _write_env_file(directory, value):
(directory / ".env").write_text(f"{_TEST_KEY}={value}\n")


def test_reads_env_file_from_confi_path(tmp_path, monkeypatch):
_write_env_file(tmp_path, "from_confi_path")
monkeypatch.setenv("CONFI_PATH", str(tmp_path))

assert build_config()(_TEST_KEY) == "from_confi_path"


def test_falls_back_to_current_working_directory(tmp_path, monkeypatch):
_write_env_file(tmp_path, "from_cwd")
monkeypatch.delenv("CONFI_PATH", raising=False)
monkeypatch.chdir(tmp_path)

assert build_config()(_TEST_KEY) == "from_cwd"


def test_confi_path_takes_precedence_over_cwd(tmp_path, monkeypatch):
confi_path_dir = tmp_path / "config_dir"
confi_path_dir.mkdir()
_write_env_file(confi_path_dir, "from_confi_path")

cwd_dir = tmp_path / "cwd_dir"
cwd_dir.mkdir()
_write_env_file(cwd_dir, "from_cwd")

monkeypatch.setenv("CONFI_PATH", str(confi_path_dir))
monkeypatch.chdir(cwd_dir)

assert build_config()(_TEST_KEY) == "from_confi_path"