From 9e533e03ebed776712979c0c21c9eb50dd0f6fa1 Mon Sep 17 00:00:00 2001 From: Alexander Sergeev Date: Tue, 5 Sep 2023 14:44:10 +0200 Subject: [PATCH] `os.getenv` replaced with `os.environ` (#178) * os.getenv -> os.environ * exception raising added * skip env var added * tests skipped accordingly * telegram skip added * build environment published in github actions * module level skipping removed * test marked telegram * tutorial skipping added * lint fixed * tutorial test venv skip allowed * telegram tutorial ends * telegram tokens in gh actions * all skips allowed * review comments fixed * docker marker order reverted --- .github/workflows/build_and_publish_docs.yml | 5 ++++ .github/workflows/test_coverage.yml | 5 ++++ .github/workflows/test_full.yml | 10 ++++++++ tests/context_storages/test_dbs.py | 24 +++++++++---------- tests/messengers/telegram/test_tutorials.py | 2 +- tutorials/context_storages/2_postgresql.py | 6 ++--- tutorials/context_storages/3_mongodb.py | 6 ++--- tutorials/context_storages/4_redis.py | 2 +- tutorials/context_storages/5_mysql.py | 6 ++--- .../context_storages/7_yandex_database.py | 4 ++-- tutorials/messengers/telegram/1_basic.py | 4 +--- tutorials/messengers/telegram/2_buttons.py | 4 +--- .../telegram/3_buttons_with_callback.py | 4 +--- tutorials/messengers/telegram/4_conditions.py | 4 +--- .../telegram/5_conditions_with_media.py | 4 +--- .../telegram/6_conditions_extras.py | 4 +--- .../messengers/telegram/7_polling_setup.py | 4 +--- .../messengers/telegram/8_webhook_setup.py | 4 +--- 18 files changed, 53 insertions(+), 49 deletions(-) diff --git a/.github/workflows/build_and_publish_docs.yml b/.github/workflows/build_and_publish_docs.yml index d05847331..4da0c6ec0 100644 --- a/.github/workflows/build_and_publish_docs.yml +++ b/.github/workflows/build_and_publish_docs.yml @@ -39,6 +39,11 @@ jobs: make venv - name: build documentation + env: + TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }} + TG_API_ID: ${{ secrets.TG_API_ID }} + TG_API_HASH: ${{ secrets.TG_API_HASH }} + TG_BOT_USERNAME: ${{ secrets.TG_BOT_USERNAME }} run: | make doc diff --git a/.github/workflows/test_coverage.yml b/.github/workflows/test_coverage.yml index 16416d9d6..b1d91e381 100644 --- a/.github/workflows/test_coverage.yml +++ b/.github/workflows/test_coverage.yml @@ -50,5 +50,10 @@ jobs: touch venv # disable venv target - name: run tests + env: + TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }} + TG_API_ID: ${{ secrets.TG_API_ID }} + TG_API_HASH: ${{ secrets.TG_API_HASH }} + TG_BOT_USERNAME: ${{ secrets.TG_BOT_USERNAME }} run: | make test TEST_ALLOW_SKIP=telegram diff --git a/.github/workflows/test_full.yml b/.github/workflows/test_full.yml index 66134f756..4e7a24bb1 100644 --- a/.github/workflows/test_full.yml +++ b/.github/workflows/test_full.yml @@ -43,6 +43,11 @@ jobs: shell: bash - name: run pytest + env: + TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }} + TG_API_ID: ${{ secrets.TG_API_ID }} + TG_API_HASH: ${{ secrets.TG_API_HASH }} + TG_BOT_USERNAME: ${{ secrets.TG_BOT_USERNAME }} run: | if [ "$RUNNER_OS" == "Linux" ]; then source <(cat .env_file | sed 's/=/=/' | sed 's/^/export /') @@ -72,6 +77,11 @@ jobs: shell: bash - name: run pytest + env: + TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }} + TG_API_ID: ${{ secrets.TG_API_ID }} + TG_API_HASH: ${{ secrets.TG_API_HASH }} + TG_BOT_USERNAME: ${{ secrets.TG_BOT_USERNAME }} run: | source <(cat .env_file | sed 's/=/=/' | sed 's/^/export /') pytest --tb=long -vv --cache-clear --no-cov --allow-skip=all tests/ diff --git a/tests/context_storages/test_dbs.py b/tests/context_storages/test_dbs.py index eb2c63da9..de39c39dc 100644 --- a/tests/context_storages/test_dbs.py +++ b/tests/context_storages/test_dbs.py @@ -130,9 +130,9 @@ def test_mongo(testing_context, context_id): db = context_storage_factory( "mongodb://{}:{}@localhost:27017/{}".format( - os.getenv("MONGO_INITDB_ROOT_USERNAME"), - os.getenv("MONGO_INITDB_ROOT_PASSWORD"), - os.getenv("MONGO_INITDB_ROOT_USERNAME"), + os.environ["MONGO_INITDB_ROOT_USERNAME"], + os.environ["MONGO_INITDB_ROOT_PASSWORD"], + os.environ["MONGO_INITDB_ROOT_USERNAME"], ) ) generic_test(db, testing_context, context_id) @@ -143,7 +143,7 @@ def test_mongo(testing_context, context_id): @pytest.mark.skipif(not redis_available, reason="Redis dependencies missing") @pytest.mark.docker def test_redis(testing_context, context_id): - db = context_storage_factory("redis://{}:{}@localhost:6379/{}".format("", os.getenv("REDIS_PASSWORD"), "0")) + db = context_storage_factory("redis://{}:{}@localhost:6379/{}".format("", os.environ["REDIS_PASSWORD"], "0")) generic_test(db, testing_context, context_id) asyncio.run(delete_redis(db)) @@ -154,9 +154,9 @@ def test_redis(testing_context, context_id): def test_postgres(testing_context, context_id): db = context_storage_factory( "postgresql+asyncpg://{}:{}@localhost:5432/{}".format( - os.getenv("POSTGRES_USERNAME"), - os.getenv("POSTGRES_PASSWORD"), - os.getenv("POSTGRES_DB"), + os.environ["POSTGRES_USERNAME"], + os.environ["POSTGRES_PASSWORD"], + os.environ["POSTGRES_DB"], ) ) generic_test(db, testing_context, context_id) @@ -177,9 +177,9 @@ def test_sqlite(testing_file, testing_context, context_id): def test_mysql(testing_context, context_id): db = context_storage_factory( "mysql+asyncmy://{}:{}@localhost:3307/{}".format( - os.getenv("MYSQL_USERNAME"), - os.getenv("MYSQL_PASSWORD"), - os.getenv("MYSQL_DATABASE"), + os.environ["MYSQL_USERNAME"], + os.environ["MYSQL_PASSWORD"], + os.environ["MYSQL_DATABASE"], ) ) generic_test(db, testing_context, context_id) @@ -192,8 +192,8 @@ def test_mysql(testing_context, context_id): def test_ydb(testing_context, context_id): db = context_storage_factory( "{}{}".format( - os.getenv("YDB_ENDPOINT"), - os.getenv("YDB_DATABASE"), + os.environ["YDB_ENDPOINT"], + os.environ["YDB_DATABASE"], ), table_name="test", ) diff --git a/tests/messengers/telegram/test_tutorials.py b/tests/messengers/telegram/test_tutorials.py index 625bad386..c92e0e048 100644 --- a/tests/messengers/telegram/test_tutorials.py +++ b/tests/messengers/telegram/test_tutorials.py @@ -27,7 +27,7 @@ "3_buttons_with_callback", ], ) -def test_client_tutorials_without_telegram(tutorial_module_name): +def test_client_tutorials_without_telegram(tutorial_module_name, env_vars): tutorial_module = importlib.import_module(f"tutorials.{dot_path_to_addon}.{tutorial_module_name}") pipeline = tutorial_module.pipeline happy_path = tutorial_module.happy_path diff --git a/tutorials/context_storages/2_postgresql.py b/tutorials/context_storages/2_postgresql.py index b191c99b6..57693cbd1 100644 --- a/tutorials/context_storages/2_postgresql.py +++ b/tutorials/context_storages/2_postgresql.py @@ -23,9 +23,9 @@ # %% db_uri = "postgresql+asyncpg://{}:{}@localhost:5432/{}".format( - os.getenv("POSTGRES_USERNAME"), - os.getenv("POSTGRES_PASSWORD"), - os.getenv("POSTGRES_DB"), + os.environ["POSTGRES_USERNAME"], + os.environ["POSTGRES_PASSWORD"], + os.environ["POSTGRES_DB"], ) db = context_storage_factory(db_uri) diff --git a/tutorials/context_storages/3_mongodb.py b/tutorials/context_storages/3_mongodb.py index 449502a4d..f95b0ab08 100644 --- a/tutorials/context_storages/3_mongodb.py +++ b/tutorials/context_storages/3_mongodb.py @@ -23,9 +23,9 @@ # %% db_uri = "mongodb://{}:{}@localhost:27017/{}".format( - os.getenv("MONGO_INITDB_ROOT_USERNAME"), - os.getenv("MONGO_INITDB_ROOT_PASSWORD"), - os.getenv("MONGO_INITDB_ROOT_USERNAME"), + os.environ["MONGO_INITDB_ROOT_USERNAME"], + os.environ["MONGO_INITDB_ROOT_PASSWORD"], + os.environ["MONGO_INITDB_ROOT_USERNAME"], ) db = context_storage_factory(db_uri) diff --git a/tutorials/context_storages/4_redis.py b/tutorials/context_storages/4_redis.py index 29c1df613..bb3ced51a 100644 --- a/tutorials/context_storages/4_redis.py +++ b/tutorials/context_storages/4_redis.py @@ -22,7 +22,7 @@ # %% -db_uri = "redis://{}:{}@localhost:6379/{}".format("", os.getenv("REDIS_PASSWORD"), "0") +db_uri = "redis://{}:{}@localhost:6379/{}".format("", os.environ["REDIS_PASSWORD"], "0") db = context_storage_factory(db_uri) diff --git a/tutorials/context_storages/5_mysql.py b/tutorials/context_storages/5_mysql.py index 4f2cd885f..8243f4d06 100644 --- a/tutorials/context_storages/5_mysql.py +++ b/tutorials/context_storages/5_mysql.py @@ -23,9 +23,9 @@ # %% db_uri = "mysql+asyncmy://{}:{}@localhost:3307/{}".format( - os.getenv("MYSQL_USERNAME"), - os.getenv("MYSQL_PASSWORD"), - os.getenv("MYSQL_DATABASE"), + os.environ["MYSQL_USERNAME"], + os.environ["MYSQL_PASSWORD"], + os.environ["MYSQL_DATABASE"], ) db = context_storage_factory(db_uri) diff --git a/tutorials/context_storages/7_yandex_database.py b/tutorials/context_storages/7_yandex_database.py index 865a9d2e1..75cb54f42 100644 --- a/tutorials/context_storages/7_yandex_database.py +++ b/tutorials/context_storages/7_yandex_database.py @@ -31,8 +31,8 @@ # db_uri="grpc://localhost:2136/local" db_uri = "{}{}".format( - os.getenv("YDB_ENDPOINT"), - os.getenv("YDB_DATABASE"), + os.environ["YDB_ENDPOINT"], + os.environ["YDB_DATABASE"], ) db = context_storage_factory(db_uri) diff --git a/tutorials/messengers/telegram/1_basic.py b/tutorials/messengers/telegram/1_basic.py index 91758d7b0..81855bde5 100644 --- a/tutorials/messengers/telegram/1_basic.py +++ b/tutorials/messengers/telegram/1_basic.py @@ -60,7 +60,7 @@ # %% -interface = PollingTelegramInterface(token=os.getenv("TG_BOT_TOKEN", "")) +interface = PollingTelegramInterface(token=os.environ["TG_BOT_TOKEN"]) # %% @@ -73,8 +73,6 @@ def main(): - if not os.getenv("TG_BOT_TOKEN"): - print("`TG_BOT_TOKEN` variable needs to be set to use TelegramInterface.") pipeline.run() diff --git a/tutorials/messengers/telegram/2_buttons.py b/tutorials/messengers/telegram/2_buttons.py index 0443aa0e6..d395c75e1 100644 --- a/tutorials/messengers/telegram/2_buttons.py +++ b/tutorials/messengers/telegram/2_buttons.py @@ -90,7 +90,7 @@ }, } -interface = PollingTelegramInterface(token=os.getenv("TG_BOT_TOKEN", "")) +interface = PollingTelegramInterface(token=os.environ["TG_BOT_TOKEN"]) # this variable is only for testing happy_path = ( @@ -158,8 +158,6 @@ def main(): - if not os.getenv("TG_BOT_TOKEN"): - print("`TG_BOT_TOKEN` variable needs to be set to use TelegramInterface.") pipeline.run() diff --git a/tutorials/messengers/telegram/3_buttons_with_callback.py b/tutorials/messengers/telegram/3_buttons_with_callback.py index cf457a4a8..c3ecf4d70 100644 --- a/tutorials/messengers/telegram/3_buttons_with_callback.py +++ b/tutorials/messengers/telegram/3_buttons_with_callback.py @@ -138,7 +138,7 @@ ), ) -interface = PollingTelegramInterface(token=os.getenv("TG_BOT_TOKEN", "")) +interface = PollingTelegramInterface(token=os.environ["TG_BOT_TOKEN"]) # %% @@ -151,8 +151,6 @@ def main(): - if not os.getenv("TG_BOT_TOKEN"): - print("`TG_BOT_TOKEN` variable needs to be set to use TelegramInterface.") pipeline.run() diff --git a/tutorials/messengers/telegram/4_conditions.py b/tutorials/messengers/telegram/4_conditions.py index 95e251648..1a49b2f09 100644 --- a/tutorials/messengers/telegram/4_conditions.py +++ b/tutorials/messengers/telegram/4_conditions.py @@ -102,7 +102,7 @@ # %% -interface = PollingTelegramInterface(token=os.getenv("TG_BOT_TOKEN", "")) +interface = PollingTelegramInterface(token=os.environ["TG_BOT_TOKEN"]) # %% @@ -115,8 +115,6 @@ def main(): - if not os.getenv("TG_BOT_TOKEN"): - print("`TG_BOT_TOKEN` variable needs to be set to use TelegramInterface.") pipeline.run() diff --git a/tutorials/messengers/telegram/5_conditions_with_media.py b/tutorials/messengers/telegram/5_conditions_with_media.py index e5e6dfe0c..49c02f921 100644 --- a/tutorials/messengers/telegram/5_conditions_with_media.py +++ b/tutorials/messengers/telegram/5_conditions_with_media.py @@ -41,7 +41,7 @@ # %% -interface = PollingTelegramInterface(token=os.getenv("TG_BOT_TOKEN", "")) +interface = PollingTelegramInterface(token=os.environ["TG_BOT_TOKEN"]) # %% @@ -167,8 +167,6 @@ def extract_data(ctx: Context, _: Pipeline): # A function to extract data with def main(): - if not os.getenv("TG_BOT_TOKEN"): - print("`TG_BOT_TOKEN` variable needs to be set to use TelegramInterface.") pipeline.run() diff --git a/tutorials/messengers/telegram/6_conditions_extras.py b/tutorials/messengers/telegram/6_conditions_extras.py index f86215aef..bc1ef5e6a 100644 --- a/tutorials/messengers/telegram/6_conditions_extras.py +++ b/tutorials/messengers/telegram/6_conditions_extras.py @@ -93,7 +93,7 @@ # %% -interface = PollingTelegramInterface(token=os.getenv("TG_BOT_TOKEN", "")) +interface = PollingTelegramInterface(token=os.environ["TG_BOT_TOKEN"]) # %% @@ -106,8 +106,6 @@ def main(): - if not os.getenv("TG_BOT_TOKEN"): - print("`TG_BOT_TOKEN` variable needs to be set to use TelegramInterface.") pipeline.run() diff --git a/tutorials/messengers/telegram/7_polling_setup.py b/tutorials/messengers/telegram/7_polling_setup.py index 7c2ab2d6d..68a2b1f0a 100644 --- a/tutorials/messengers/telegram/7_polling_setup.py +++ b/tutorials/messengers/telegram/7_polling_setup.py @@ -33,7 +33,7 @@ # %% interface = PollingTelegramInterface( - token=os.getenv("TG_BOT_TOKEN", ""), + token=os.environ["TG_BOT_TOKEN"], interval=2, allowed_updates=update_types, timeout=30, @@ -53,8 +53,6 @@ def main(): - if not os.getenv("TG_BOT_TOKEN"): - print("`TG_BOT_TOKEN` variable needs to be set to use TelegramInterface.") pipeline.run() diff --git a/tutorials/messengers/telegram/8_webhook_setup.py b/tutorials/messengers/telegram/8_webhook_setup.py index 4ebc62a6a..709cf5e3e 100644 --- a/tutorials/messengers/telegram/8_webhook_setup.py +++ b/tutorials/messengers/telegram/8_webhook_setup.py @@ -35,7 +35,7 @@ # %% -interface = CallbackTelegramInterface(token=os.getenv("TG_BOT_TOKEN", "")) +interface = CallbackTelegramInterface(token=os.environ["TG_BOT_TOKEN"]) # %% @@ -49,8 +49,6 @@ def main(): - if not os.getenv("TG_BOT_TOKEN"): - print("`TG_BOT_TOKEN` variable needs to be set to use TelegramInterface.") pipeline.run()