Skip to content

Commit

Permalink
nox task for checking linting files added + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis-Mittenzwei committed Dec 6, 2024
1 parent 2449410 commit 1c1254c
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 12 deletions.
94 changes: 94 additions & 0 deletions exasol/toolbox/nox/_gh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import nox
from nox import Session
from noxconfig import PROJECT_CONFIG
import sys
from pathlib import Path
import json
import sqlite3
from typing import Iterable


@nox.session(name="check:lint-files", python=False)
def check_lint_files(session: Session) -> None:
"""task to validate linting files"""
if not_available := files_not_available(
[".lint.json", ".lint.txt", ".security.json", ".coverage"],
PROJECT_CONFIG.root):
print(f"not available: {not_available}")
sys.exit(1)

error = False
if msg := check_lint_json(PROJECT_CONFIG.root):
print(f"error in [.lint.json]: {msg}")
error = True
if msg := check_security_json(PROJECT_CONFIG.root):
print(f"error in [.security.json]: {msg}")
error = True
if msg := check_coverage(PROJECT_CONFIG.root):
print(f"error in [.coverage]: {msg}")
error = True
if error:
sys.exit(1)


def files_not_available(requested: list, path: Path) -> list:
not_existing_files = requested.copy()
for file in path.iterdir():
if file.is_file():
if file.name in not_existing_files:
not_existing_files.remove(file.name)
return not_existing_files


def check_lint_json(path: Path) -> str:
path = Path(path, ".lint.json")
try:
with path.open("r") as file:
issues = json.load(file)
error = False
for issue in issues:
attributes = ["type", "module", "obj", "line", "column", "endLine",
"endColumn", "path", "symbol", "message", "message-id"]
for attribute in attributes:
error |= attribute not in issue
if error:
return "incompatible format"
return ""

except json.JSONDecodeError:
return "parsing of json not possible"


def check_security_json(path: Path) -> str:
path = Path(path, ".security.json")
try:
with path.open("r") as input_file:
json_file = json.load(input_file)
error = False
attributes = ["errors", "generated_at", "metrics", "results"]
for attribute in attributes:
error |= attribute not in json_file
if error:
return "incompatible format"
return ""

except json.JSONDecodeError:
return "parsing of json not possible"


def check_coverage(path: Path) -> str:
path = Path(path, ".coverage")
try:
conn = sqlite3.connect(path)
cursor = conn.cursor()
not_existing_tables = ["coverage_schema", "meta", "file", "line_bits"]
data = cursor.execute("select name from sqlite_schema where type == 'table'")
for row in data:
if row[0] in not_existing_tables:
not_existing_tables.remove(row[0])
conn.close()
if not_existing_tables:
return "not existing tables: " + str(not_existing_tables)
return ""
except sqlite3.Error:
return "connection to database not possible"
5 changes: 3 additions & 2 deletions exasol/toolbox/nox/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def check(session: Session) -> None:
_version,
python_files,
)


from exasol.toolbox.nox._gh import (
check_lint_files
)
# isort: on
# fmt: on
20 changes: 10 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 153 additions & 0 deletions test/unit/lint_file_check_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import json

import pytest
from pathlib import Path
import sqlite3

from exasol.toolbox.nox import _gh

@pytest.mark.parametrize(
"files,requested_files,expected",
[
(
[".lint.json", ".lint.txt", ".security.json", ".coverage"],
[".lint.json", ".lint.txt", ".security.json", ".coverage"],
[]
), (
[".lint.txt", ".security.json", ".coverage"],
[".lint.json", ".lint.txt", ".security.json", ".coverage"],
[".lint.json"]
), (
[".lint.json", ".security.json", ".coverage"],
[".lint.json", ".lint.txt", ".security.json", ".coverage"],
[".lint.txt"]
), (
[".lint.json", ".lint.txt", ".coverage"],
[".lint.json", ".lint.txt", ".security.json", ".coverage"],
[".security.json"]
), (
[".lint.json", ".lint.txt", ".security.json"],
[".lint.json", ".lint.txt", ".security.json", ".coverage"],
[".coverage"]
), (
[],
[".lint.json", ".lint.txt", ".security.json", ".coverage"],
[".lint.json", ".lint.txt", ".security.json", ".coverage"]
),
]
)
def test_check_lint_files(files, requested_files, expected, tmp_path):
path = Path(tmp_path)
for file in files:
Path(path, file).touch()

actual = _gh.files_not_available(requested_files, path)
assert actual == expected


@pytest.mark.parametrize(
"attributes,expected",
[
(
["type", "module", "obj", "line", "column", "endLine",
"endColumn", "path", "symbol", "message", "message-id"],
""
), (
["module", "obj", "line", "column", "endLine",
"endColumn", "path", "symbol", "message", "message-id"],
"incompatible format"
), (
["type", "obj", "line", "column", "endLine",
"endColumn", "path", "symbol", "message", "message-id"],
"incompatible format"
), (
["type", "module", "line", "column", "endLine",
"endColumn", "path", "symbol", "message", "message-id"],
"incompatible format"
), (
["type", "module", "obj", "column", "endLine",
"endColumn", "path", "symbol", "message", "message-id"],
"incompatible format"
), (
["type", "module", "obj", "line", "endLine",
"endColumn", "path", "symbol", "message", "message-id"],
"incompatible format"
), (
["type", "module", "obj", "line", "column",
"endColumn", "path", "symbol", "message", "message-id"],
"incompatible format"
), (
["type", "module", "obj", "line", "column", "endLine",
"path", "symbol", "message", "message-id"],
"incompatible format"
), (
["type", "module", "obj", "line", "column", "endLine",
"endColumn", "symbol", "message", "message-id"],
"incompatible format"
), (
["type", "module", "obj", "line", "column", "endLine",
"endColumn", "path", "message", "message-id"],
"incompatible format"
), (
["type", "module", "obj", "line", "column", "endLine",
"endColumn", "path", "symbol", "message-id"],
"incompatible format"
), (
["type", "module", "obj", "line", "column", "endLine",
"endColumn", "path", "symbol", "message"],
"incompatible format"
),
]
)
def test_check_lint_json(attributes, expected, tmp_path):
path = Path(tmp_path, ".lint.json")
path.touch()
attributes_dict = {}
for attribute in attributes:
attributes_dict[attribute] = None
with path.open("w") as file:
json.dump([attributes_dict], file)
actual = _gh.check_lint_json(path.parent)
assert actual == expected


@pytest.mark.parametrize(
"attributes,expected",
[
(['errors', 'generated_at', 'metrics', 'results'], ""),
(["generated_at", "metrics", "results"], "incompatible format"),
(["errors", "metrics", "results"], "incompatible format"),
(["errors", "generated_at", "results"], "incompatible format"),
(["errors", "generated_at", "metrics"], "incompatible format"),
]
)
def test_check_lint_json(attributes, expected, tmp_path):
path = Path(tmp_path, ".security.json")
path.touch()
attributes_dict = {}
for attribute in attributes:
attributes_dict[attribute] = None
with path.open("w") as file:
json.dump(attributes_dict, file)
actual = _gh.check_security_json(path.parent)
assert actual == expected


@pytest.mark.parametrize(
"tables, expected",
[
(["coverage_schema", "meta", "file", "line_bits"], ""),
(["meta", "file", "line_bits"], "not existing tables: ['coverage_schema']"),
(["coverage_schema", "file", "line_bits"], "not existing tables: ['meta']"),
(["coverage_schema", "meta", "line_bits"], "not existing tables: ['file']"),
(["coverage_schema", "meta", "file", ], "not existing tables: ['line_bits']"),
]
)
def test_check_coverage(tables, expected, tmp_path):
path = Path(tmp_path, ".coverage")
connection = sqlite3.connect(path)
cursor = connection.cursor()
for table in tables:
cursor.execute(f"CREATE TABLE IF NOT EXISTS {table} (test INTEGER)")
actual = _gh.check_coverage(path.parent)
assert actual == expected

0 comments on commit 1c1254c

Please sign in to comment.