Skip to content

Commit

Permalink
Merge pull request #18 from codesquadnest/pb/zuulci-lint
Browse files Browse the repository at this point in the history
Rename to zuulcilint
  • Loading branch information
pmpbaptista authored Dec 5, 2023
2 parents dd28f04 + 1cab22a commit 10ad97b
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 47 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,3 @@ dmypy.json

# ruff
.ruff_cache/

# Generated by setuptools-scm
zuullint/_version.py
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
- id: zuullint
- id: zuulcilint
name: Zuul Job Linter
description: Verifies if Zuul CI jobs files have valid syntax.
language: python
files: '^(zuul.d/|.?zuul).*\.yaml$'
pass_filenames: true
verbose: true
entry: zuullint
entry: zuulcilint
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

# zuullint
# zuulcilint

## Validate from the command line

```
pip install zuullint
pip install zuulcilint
usage: zuullint [-h] [--version] [--check-playbook-paths] [--schema SCHEMA] file [file ...]
usage: zuulcilint [-h] [--version] [--check-playbook-paths] [--schema SCHEMA] file [file ...]
positional arguments:
file file(s) or paths to lint
Expand All @@ -28,10 +28,10 @@ options:
Add the code below to your `.pre-commit-config.yaml` file:

```yaml
- repo: https://github.com/codesquadnest/zuullint.git
- repo: https://github.com/codesquadnest/zuulcilint.git
rev: "1.0"
hooks:
- id: zuullint
- id: zuulcilint
```
Expand All @@ -47,7 +47,7 @@ validation by adding the following to `settings.json`:

```json
"yaml.schemas": {
"https://raw.githubusercontent.com/codesquadnest/zuullint/master/zuullint/zuul-schema.json": ["*zuul.d/*.yaml", "*/.zuul.yaml"]
"https://raw.githubusercontent.com/codesquadnest/zuulcilint/master/zuulcilint/zuul-schema.json": ["*zuul.d/*.yaml", "*/.zuul.yaml"]
},
"yaml.customTags": [
"!encrypted/pkcs1-oaep array"
Expand Down
14 changes: 7 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[tool.poetry]
name = "zuullint"
name = "zuulcilint"
version = "0.2.1"
description = "Zuul CI linter"
authors = ["Pedro Baptista <pedro.miguel.baptista@gmail.com>"]
license = "MIT"
readme = "README.md"
keywords = ["zuul", "ci", "linter"]
include = ["pyproject.toml", "LICENSE", "README.md", "zuullint/zuul-schema.yaml"]
include = ["pyproject.toml", "LICENSE", "README.md", "zuulcilint/zuul-schema.yaml"]
exclude = ["tests/*"]
packages = [{include = "zuullint"}]
packages = [{include = "zuulcilint"}]

[tool.poetry.scripts]
zuullint = "zuullint.__main__:main"
zuulcilint = "zuulcilint.__main__:main"

[tool.poetry.dependencies]
python = "^3.8, <4.0"
Expand Down Expand Up @@ -52,7 +52,7 @@ extend-ignore = "E203,E501"

# Configurations for pytest
[tool.pytest]
addopts = ["-ra", "--cov=zuullint", "--cov-report=term-missing"]
addopts = ["-ra", "--cov=zuulcilint", "--cov-report=term-missing"]
testpaths = ["tests"]

[toolpytest.cov]
Expand Down Expand Up @@ -97,13 +97,13 @@ legacy_tox_ini = """
poetry
commands =
poetry install -v
poetry run pytest --cov=zuullint --cov-report=term-missing
poetry run pytest --cov=zuulcilint --cov-report=term-missing
[testenv:lint]
deps =
ruff
commands =
ruff zuullint tests
ruff zuulcilint tests
[testenv:pre-commit]
skip_install = true
Expand Down
8 changes: 4 additions & 4 deletions tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import pathlib
import tempfile

import zuullint.checker as zuullint_checker
import zuullint.utils as zuullint_utils
import zuulcilint.checker as zuulcilint_checker
import zuulcilint.utils as zuulcilint_utils


def setup_zuul_job_yaml():
Expand All @@ -35,9 +35,9 @@ def setup_zuul_job_yaml():
def test_check_job_playbook_paths():
"""Test that check_job_playbook_paths() returns a list of invalid paths."""
tmp_path = setup_zuul_job_yaml()
jobs = zuullint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")
jobs = zuulcilint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")

assert zuullint_checker.check_job_playbook_paths(jobs[0].get("job")) == [
assert zuulcilint_checker.check_job_playbook_paths(jobs[0].get("job")) == [
"playbooks/pre-run.yaml",
"playbooks/run.yaml",
"playbooks/post-run.yaml",
Expand Down
36 changes: 18 additions & 18 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

import zuullint.utils as zuullint_utils
import zuulcilint.utils as zuulcilint_utils


def setup_tmp_list_of_files():
Expand Down Expand Up @@ -50,15 +50,15 @@ def test_get_schema_valid():
tmp_schema = pathlib.Path(tempfile.mkstemp()[1])
with pathlib.Path.open(tmp_schema, "w", encoding="utf-8") as f:
f.write("{}")
assert zuullint_utils.get_zuul_schema(tmp_schema) == {}
assert zuulcilint_utils.get_zuul_schema(tmp_schema) == {}


def test_get_schema_file_not_found():
"""Test that get_schema() exits when the schema file is not found."""
tmp_schema = pathlib.Path(tempfile.mkstemp()[1])
tmp_schema.unlink()
try:
zuullint_utils.get_zuul_schema(tmp_schema)
zuulcilint_utils.get_zuul_schema(tmp_schema)
except SystemExit:
pytest.raises(FileNotFoundError)

Expand All @@ -69,25 +69,25 @@ def test_get_schema_invalid_json():
with pathlib.Path.open(tmp_schema, "w", encoding="utf-8") as f:
f.write("{- foo = bar}")
with pytest.raises(SystemExit):
zuullint_utils.get_zuul_schema(tmp_schema)
zuulcilint_utils.get_zuul_schema(tmp_schema)


def test_get_zuul_yaml_files_find():
"""Test that get_zuul_yaml_files() finds files."""
tmp_path = setup_tmp_list_of_files()
default_len = 2
zuul_yaml_files = [
file.name for file in zuullint_utils.get_zuul_yaml_files(tmp_path)
file.name for file in zuulcilint_utils.get_zuul_yaml_files(tmp_path)
]
assert len(zuul_yaml_files) == default_len
assert "file0.yaml" in zuul_yaml_files
assert "file1.yaml" in zuul_yaml_files

tmp_path = tmp_path / "subdir"
tmp_path.mkdir()
assert len(zuullint_utils.get_zuul_yaml_files(tmp_path)) == 0
assert len(zuulcilint_utils.get_zuul_yaml_files(tmp_path)) == 0

assert len(zuullint_utils.get_zuul_yaml_files(tmp_path / "invalid_path")) == 0
assert len(zuulcilint_utils.get_zuul_yaml_files(tmp_path / "invalid_path")) == 0


def test_get_zuul_yaml_files_skip():
Expand All @@ -98,18 +98,18 @@ def test_get_zuul_yaml_files_skip():
tmp_path = tmp_path / "file2.yaml"
with pathlib.Path.open(tmp_path, "w", encoding="utf-8") as f:
f.write("hello")
assert len(zuullint_utils.get_zuul_yaml_files(tmp_path)) == 1
assert len(zuulcilint_utils.get_zuul_yaml_files(tmp_path)) == 1

tmp_path = tmp_path.parent / "file3.yml"
with pathlib.Path.open(tmp_path, "w", encoding="utf-8") as f:
f.write("hello")
assert len(zuullint_utils.get_zuul_yaml_files(tmp_path)) == 0
assert len(zuulcilint_utils.get_zuul_yaml_files(tmp_path)) == 0


def test_get_jobs_from_zuul_yaml():
"""Test that get_jobs_from_zuul_yaml() returns a list of jobs."""
tmp_path = setup_zuul_job_yaml()
jobs = zuullint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")
jobs = zuulcilint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")
assert len(jobs) == 1
assert jobs[0]["job"]["name"] == "test-job"

Expand All @@ -120,7 +120,7 @@ def test_get_jobs_from_zuul_yaml_invalid_yaml():
with pathlib.Path.open(tmp_path / "job.yaml", "w", encoding="utf-8") as f:
f.write("{- foo = bar}")
with pytest.raises(SystemExit):
zuullint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")
zuulcilint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")


def test_get_jobs_from_zuul_yaml_no_jobs():
Expand All @@ -133,23 +133,23 @@ def test_get_jobs_from_zuul_yaml_no_jobs():
name: test-pipeline
""",
)
assert len(zuullint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")) == 0
assert len(zuulcilint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")) == 0


def test_get_jobs_from_zuul_yaml_no_file():
"""Test return an empty list when no file is found."""
tmp_path = setup_zuul_job_yaml()
try:
zuullint_utils.get_jobs_from_zuul_yaml(tmp_path / "invalid_file")
zuulcilint_utils.get_jobs_from_zuul_yaml(tmp_path / "invalid_file")
except SystemExit:
pytest.raises(FileNotFoundError)


def test_get_playbook_paths_from_job():
"""Test that get_playbook_paths_from_job() returns a list of playbook paths."""
tmp_path = setup_zuul_job_yaml()
jobs = zuullint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")
playbook_paths = zuullint_utils.get_playbook_paths_from_job(jobs[0].get("job"))
jobs = zuulcilint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")
playbook_paths = zuulcilint_utils.get_playbook_paths_from_job(jobs[0].get("job"))
size = 3
assert len(playbook_paths) == size
assert playbook_paths[0] == "playbooks/pre-run.yaml"
Expand All @@ -167,14 +167,14 @@ def test_get_playbook_paths_from_job_no_playbook_paths():
name: test-job
""",
)
jobs = zuullint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")
playbook_paths = zuullint_utils.get_playbook_paths_from_job(jobs[0].get("job"))
jobs = zuulcilint_utils.get_jobs_from_zuul_yaml(tmp_path / "job.yaml")
playbook_paths = zuulcilint_utils.get_playbook_paths_from_job(jobs[0].get("job"))
assert len(playbook_paths) == 0


def test_get_files_with_extension():
"""Test return a list of files with the specified extension."""
tmp_path = setup_tmp_list_of_files()
files = zuullint_utils.get_files_with_extension(tmp_path, "yaml")
files = zuulcilint_utils.get_files_with_extension(tmp_path, "yaml")
size = 2
assert len(files) == size
4 changes: 2 additions & 2 deletions tests/test_zuullint.py → tests/test_zuulcilint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_invalid():
[
"python",
"-m",
"zuullint",
"zuulcilint",
"tests/zuul_data_invalid/zuul-config-invalid.yaml",
],
)
Expand All @@ -40,7 +40,7 @@ def test_valid():
"""
try:
subprocess.call(
["python", "zuullint", "-m", "tests/zuul_data"],
["python", "zuulcilint", "-m", "tests/zuul_data"],
)
except subprocess.CalledProcessError as e:
pytest.fail(e)
1 change: 1 addition & 0 deletions zuulcilint/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init file for zuulcilint."""
8 changes: 4 additions & 4 deletions zuullint/__main__.py → zuulcilint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import yaml
from jsonschema import Draft201909Validator

import zuullint.checker as zuul_checker
import zuullint.utils as zuul_utils
import zuulcilint.checker as zuul_checker
import zuulcilint.utils as zuul_utils

# # Register custom yaml constructor for "encrypted/pkcs1-oaep"
yaml.SafeLoader.add_constructor(
Expand Down Expand Up @@ -60,11 +60,11 @@ def main():
-------
None.
"""
parser = argparse.ArgumentParser(prog="zuullint")
parser = argparse.ArgumentParser(prog="zuulcilint")
parser.add_argument(
"--version",
action="version",
version=importlib.metadata.version("zuullint"),
version=importlib.metadata.version("zuulcilint"),
)
parser.add_argument(
"--check-playbook-paths",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion zuullint/__init__.py

This file was deleted.

0 comments on commit 10ad97b

Please sign in to comment.