From b0c967fbfafccbbd8c1569c032cdca8918eafe61 Mon Sep 17 00:00:00 2001
From: Timur Osmanov <54434686+TOsmanov@users.noreply.github.com>
Date: Wed, 22 Jan 2025 07:42:14 +0300
Subject: [PATCH] add: strict behavior and tests (#4)
* add: strick option
* add: test and fix preprocessor
* bump version
* fix: README.md
* add: yaml test
---
.github/workflows/python-test.yml | 27 +
.gitignore | 3 +-
README.md | 17 +-
changelog.md | 4 +
.../preprocessors/swaggerdoc/swaggerdoc.py | 15 +-
setup.py | 2 +-
test.sh | 11 +
test_in_docker.sh | 14 +
tests/__init__.py | 0
tests/data/expected/test1.md | 628 ++++++++++++++++++
tests/data/input/test1.md | 1 +
tests/data/petstore_spec.json | 177 +++++
tests/data/petstore_spec.yaml | 111 ++++
tests/test_swaggerdoc.py | 45 ++
14 files changed, 1048 insertions(+), 7 deletions(-)
create mode 100644 .github/workflows/python-test.yml
create mode 100755 test.sh
create mode 100755 test_in_docker.sh
create mode 100644 tests/__init__.py
create mode 100644 tests/data/expected/test1.md
create mode 100644 tests/data/input/test1.md
create mode 100644 tests/data/petstore_spec.json
create mode 100644 tests/data/petstore_spec.yaml
create mode 100644 tests/test_swaggerdoc.py
diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml
new file mode 100644
index 0000000..c8b11aa
--- /dev/null
+++ b/.github/workflows/python-test.yml
@@ -0,0 +1,27 @@
+name: Python package
+
+on: [push]
+
+jobs:
+ build:
+
+ runs-on: ubuntu-latest
+ strategy:
+ matrix:
+ python-version: ["3.8", "3.9"]
+
+ steps:
+ - uses: actions/checkout@v3
+ - name: Set up Python ${{ matrix.python-version }}
+ uses: actions/setup-python@v3
+ with:
+ python-version: ${{ matrix.python-version }}
+ - name: Install dependencies
+ run: |
+ python -m pip install --upgrade pip
+ pip3 install .
+ pip3 install --upgrade foliantcontrib.test_framework
+ npm install -g widdershins
+ - name: Test with unittest
+ run: |
+ python3 -m unittest discover
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index 81954e2..29faac9 100755
--- a/.gitignore
+++ b/.gitignore
@@ -100,4 +100,5 @@ ENV/
# mypy
.mypy_cache/
-.DS_store
\ No newline at end of file
+.DS_store
+.swaggercache
diff --git a/README.md b/README.md
index 5c9736a..6c34481 100755
--- a/README.md
+++ b/README.md
@@ -40,7 +40,7 @@ preprocessors:
mode: widdershins
template: swagger.j2
environment: env.yaml
-
+ strict: false
```
`spec_url`
@@ -65,6 +65,9 @@ preprocessors:
`environment`
: Only for `widdershins` mode. Parameters for widdershins converter. You can either pass a string containing relative path to YAML or JSON file with all parameters (like in example above) or specify all parameters in YAML format under this key. [More info](https://github.com/mermade/widdershins) on widdershins parameters.
+`strict`
+: If the `strict` option is enabled, then if a critical error is detected, the build will be aborted after applying the preprocessor.
+
## Usage
Add a `` tag at the position in the document where the generated documentation should be inserted:
@@ -128,3 +131,15 @@ In `jinja` mode the output markdown is generated by the [Jinja2](http://jinja.po
To customize the output create a template which suits your needs. Then supply the path to it in the `template` parameter.
If you wish to use the default template as a starting point, build the foliant project with `swaggerdoc` preprocessor turned on. After the first build the default template will appear in your foliant project dir under name `swagger.j2`.
+
+## Tests
+
+To run the tests locally, you will need to install NodeJS.
+If you have NodeJS installed, then run:
+```bash
+./test.sh
+```
+Alternatively, you can also use a Docker to run the tests. To do so, run:
+```bash
+./test_in_docker.sh
+```
diff --git a/changelog.md b/changelog.md
index 2da3c73..25feb66 100755
--- a/changelog.md
+++ b/changelog.md
@@ -1,3 +1,7 @@
+# 1.2.6
+
+- Add: strict behavior option and tests
+
# 1.2.5
- Fix: a specific version of ruamel.yaml was used
diff --git a/foliant/preprocessors/swaggerdoc/swaggerdoc.py b/foliant/preprocessors/swaggerdoc/swaggerdoc.py
index 9ffcde0..f0dd9f3 100644
--- a/foliant/preprocessors/swaggerdoc/swaggerdoc.py
+++ b/foliant/preprocessors/swaggerdoc/swaggerdoc.py
@@ -28,7 +28,7 @@
from foliant.contrib.combined_options import validate_in
from foliant.preprocessors.utils.preprocessor_ext import BasePreprocessorExt
from foliant.preprocessors.utils.preprocessor_ext import allow_fail
-
+from foliant.utils import output
class Preprocessor(BasePreprocessorExt):
tags = ('swaggerdoc',)
@@ -40,7 +40,8 @@ class Preprocessor(BasePreprocessorExt):
'json_path': '', # deprecated
'spec_path': '',
'mode': 'widdershins',
- 'template': 'swagger.j2'
+ 'template': 'swagger.j2',
+ 'strict': True
}
def __init__(self, *args, **kwargs):
@@ -87,8 +88,14 @@ def _gather_specs(self,
self.logger.debug(f'Using spec from {url} ({filename})')
return filename
except (HTTPError, URLError) as e:
- self._warning(f'\nCannot retrieve swagger spec file from url {url}. Skipping.',
- error=e)
+ msg = f'\nCannot retrieve swagger spec file from url {url}.'
+ if self.options['strict']:
+ self.logger.error(msg)
+ output(f'ERROR: {msg}')
+ os._exit(1)
+ else:
+ self._warning(f'{msg}. Skipping.',
+ error=e)
local_path = Path(path_)
if local_path:
# dest = self._swagger_tmp / f'swagger_spec'
diff --git a/setup.py b/setup.py
index 2870b8d..de9282b 100755
--- a/setup.py
+++ b/setup.py
@@ -16,7 +16,7 @@
description=SHORT_DESCRIPTION,
long_description=LONG_DESCRIPTION,
long_description_content_type='text/markdown',
- version='1.2.5',
+ version='1.2.6',
author='Daniil Minukhin',
author_email='ddddsa@gmail.com',
packages=['foliant.preprocessors.swaggerdoc'],
diff --git a/test.sh b/test.sh
new file mode 100755
index 0000000..225cf12
--- /dev/null
+++ b/test.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+# before testing make sure that you have installed the fresh version of preprocessor:
+pip3 install .
+# also make sure that fresh version of test framework is installed:
+pip3 install --upgrade foliantcontrib.test_framework
+
+# install dependencies
+npm install -g widdershins
+
+python3 -m unittest discover -v
diff --git a/test_in_docker.sh b/test_in_docker.sh
new file mode 100755
index 0000000..d4a8698
--- /dev/null
+++ b/test_in_docker.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+# Write Dockerfile
+echo "FROM python:3.9.21-alpine3.20" > Dockerfile
+echo "RUN apk add --no-cache --upgrade bash && pip install --no-build-isolation pyyaml==5.4.1" >> Dockerfile
+echo "RUN apk add nodejs npm" >> Dockerfile
+
+# Run tests in docker
+docker build . -t test-swaggerdoc:latest
+
+docker run --rm -it -v "./:/app/" -w /app/ test-swaggerdoc:latest "./test.sh"
+
+# Remove Dockerfile
+rm Dockerfile
\ No newline at end of file
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/data/expected/test1.md b/tests/data/expected/test1.md
new file mode 100644
index 0000000..27ec7e0
--- /dev/null
+++ b/tests/data/expected/test1.md
@@ -0,0 +1,628 @@
+---
+title: Swagger Petstore v1.0.0
+language_tabs:
+ - shell: Shell
+ - http: HTTP
+ - javascript: JavaScript
+ - ruby: Ruby
+ - python: Python
+ - php: PHP
+ - java: Java
+ - go: Go
+toc_footers: []
+includes: []
+search: true
+highlight_theme: darkula
+headingLevel: 2
+
+---
+
+
+
+
Swagger Petstore v1.0.0
+
+> Scroll down for code samples, example requests and responses. Select a language for code samples from the tabs above or the mobile navigation menu.
+
+Base URLs:
+
+* http://petstore.swagger.io/v1
+
+ License: MIT
+
+