From 25c78f668b4af86191c7e2668f3b238c33aa70d7 Mon Sep 17 00:00:00 2001 From: Ronen Lubin Date: Sun, 29 Dec 2024 10:24:10 +0200 Subject: [PATCH] print errors to stderr instead of stdout --- .github/workflows/ci.yaml | 15 +++++++++++++++ atlas_provider_sqlalchemy/main.py | 7 ++++--- tests/atlas-standalone.hcl | 7 ++++++- tests/broken/models.py | 7 +++++++ 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 tests/broken/models.py diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 39fef32..48cb6fe 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -93,3 +93,18 @@ jobs: git --no-pager diff exit 1 fi + - name: Run Broken Models + working-directory: ./tests + run: | + # pipe the result of the next command into + stderr=$(atlas migrate diff --env sqlalchemy -c "file://atlas-standalone.hcl" --var dialect=mysql --var path="./broken" 2>&1); + exit_code=$? + # require the command to fail with exit code 1 and the error message + if [ $exit_code -ne 1 ] || [[ ! $stderr =~ "NameError: name 'DeclarativeBase' is not defined in broken/models.py" ]]; then + echo "Expected command to fail with exit code 1 and the error message 'NameError: name 'DeclarativeBase' is not defined in broken/models.py'"; + echo "Exit code: $exit_code"; + echo "Stderr: $stderr"; + exit 1; + fi + env: + ATLAS_TOKEN: ${{ secrets.ATLAS_TOKEN }} diff --git a/atlas_provider_sqlalchemy/main.py b/atlas_provider_sqlalchemy/main.py index 20d082f..3d442f1 100644 --- a/atlas_provider_sqlalchemy/main.py +++ b/atlas_provider_sqlalchemy/main.py @@ -1,4 +1,5 @@ import os +import sys from enum import Enum from pathlib import Path @@ -38,11 +39,11 @@ def load(dialect: Dialect = Dialect.mysql, try: run(dialect, path, skip_errors) except ModuleImportError as e: - print(e) - print("To skip on failed import, run: atlas-provider-sqlalchemy --skip-errors") + print(e, file=sys.stderr) + print("To skip on failed import, run: atlas-provider-sqlalchemy --skip-errors", file=sys.stderr) exit(1) except ModelsNotFoundError as e: - print(e) + print(e, file=sys.stderr) exit(1) diff --git a/tests/atlas-standalone.hcl b/tests/atlas-standalone.hcl index 8fe7e15..add7085 100644 --- a/tests/atlas-standalone.hcl +++ b/tests/atlas-standalone.hcl @@ -2,6 +2,11 @@ variable "dialect" { type = string } +variable "path" { + type = string + default = "models" +} + locals { dev_url = { mysql = "docker://mysql/8/dev" @@ -17,7 +22,7 @@ data "external_schema" "sqlalchemy" { "run", "python3", "../atlas_provider_sqlalchemy/main.py", - "--path", "models", + "--path", var.path, "--dialect", var.dialect, // mysql | postgresql | sqlite | mssql ] } diff --git a/tests/broken/models.py b/tests/broken/models.py new file mode 100644 index 0000000..c02d0fc --- /dev/null +++ b/tests/broken/models.py @@ -0,0 +1,7 @@ +# from typing import List, Optional +# from sqlalchemy import ForeignKey, String +# from sqlalchemy.orm import Mapped, mapped_column, relationship, DeclarativeBase + +# This is a broken model file that will raise an error when trying to import it. +class Base(DeclarativeBase): + pass