Skip to content

Commit b0c967f

Browse files
authored
add: strict behavior and tests (#4)
* add: strick option * add: test and fix preprocessor * bump version * fix: README.md * add: yaml test
1 parent cb74a42 commit b0c967f

File tree

14 files changed

+1048
-7
lines changed

14 files changed

+1048
-7
lines changed

.github/workflows/python-test.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Python package
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
python-version: ["3.8", "3.9"]
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Set up Python ${{ matrix.python-version }}
16+
uses: actions/setup-python@v3
17+
with:
18+
python-version: ${{ matrix.python-version }}
19+
- name: Install dependencies
20+
run: |
21+
python -m pip install --upgrade pip
22+
pip3 install .
23+
pip3 install --upgrade foliantcontrib.test_framework
24+
npm install -g widdershins
25+
- name: Test with unittest
26+
run: |
27+
python3 -m unittest discover

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,5 @@ ENV/
100100
# mypy
101101
.mypy_cache/
102102

103-
.DS_store
103+
.DS_store
104+
.swaggercache

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ preprocessors:
4040
mode: widdershins
4141
template: swagger.j2
4242
environment: env.yaml
43-
43+
strict: false
4444
```
4545
4646
`spec_url`
@@ -65,6 +65,9 @@ preprocessors:
6565
`environment`
6666
: 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.
6767

68+
`strict`
69+
: If the `strict` option is enabled, then if a critical error is detected, the build will be aborted after applying the preprocessor.
70+
6871
## Usage
6972

7073
Add a `<swaggerdoc></swaggerdoc>` 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
128131
To customize the output create a template which suits your needs. Then supply the path to it in the `template` parameter.
129132

130133
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`.
134+
135+
## Tests
136+
137+
To run the tests locally, you will need to install NodeJS.
138+
If you have NodeJS installed, then run:
139+
```bash
140+
./test.sh
141+
```
142+
Alternatively, you can also use a Docker to run the tests. To do so, run:
143+
```bash
144+
./test_in_docker.sh
145+
```

changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.2.6
2+
3+
- Add: strict behavior option and tests
4+
15
# 1.2.5
26

37
- Fix: a specific version of ruamel.yaml was used

foliant/preprocessors/swaggerdoc/swaggerdoc.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from foliant.contrib.combined_options import validate_in
2929
from foliant.preprocessors.utils.preprocessor_ext import BasePreprocessorExt
3030
from foliant.preprocessors.utils.preprocessor_ext import allow_fail
31-
31+
from foliant.utils import output
3232

3333
class Preprocessor(BasePreprocessorExt):
3434
tags = ('swaggerdoc',)
@@ -40,7 +40,8 @@ class Preprocessor(BasePreprocessorExt):
4040
'json_path': '', # deprecated
4141
'spec_path': '',
4242
'mode': 'widdershins',
43-
'template': 'swagger.j2'
43+
'template': 'swagger.j2',
44+
'strict': True
4445
}
4546

4647
def __init__(self, *args, **kwargs):
@@ -87,8 +88,14 @@ def _gather_specs(self,
8788
self.logger.debug(f'Using spec from {url} ({filename})')
8889
return filename
8990
except (HTTPError, URLError) as e:
90-
self._warning(f'\nCannot retrieve swagger spec file from url {url}. Skipping.',
91-
error=e)
91+
msg = f'\nCannot retrieve swagger spec file from url {url}.'
92+
if self.options['strict']:
93+
self.logger.error(msg)
94+
output(f'ERROR: {msg}')
95+
os._exit(1)
96+
else:
97+
self._warning(f'{msg}. Skipping.',
98+
error=e)
9299
local_path = Path(path_)
93100
if local_path:
94101
# dest = self._swagger_tmp / f'swagger_spec'

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
description=SHORT_DESCRIPTION,
1717
long_description=LONG_DESCRIPTION,
1818
long_description_content_type='text/markdown',
19-
version='1.2.5',
19+
version='1.2.6',
2020
author='Daniil Minukhin',
2121
author_email='[email protected]',
2222
packages=['foliant.preprocessors.swaggerdoc'],

test.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
# before testing make sure that you have installed the fresh version of preprocessor:
4+
pip3 install .
5+
# also make sure that fresh version of test framework is installed:
6+
pip3 install --upgrade foliantcontrib.test_framework
7+
8+
# install dependencies
9+
npm install -g widdershins
10+
11+
python3 -m unittest discover -v

test_in_docker.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# Write Dockerfile
4+
echo "FROM python:3.9.21-alpine3.20" > Dockerfile
5+
echo "RUN apk add --no-cache --upgrade bash && pip install --no-build-isolation pyyaml==5.4.1" >> Dockerfile
6+
echo "RUN apk add nodejs npm" >> Dockerfile
7+
8+
# Run tests in docker
9+
docker build . -t test-swaggerdoc:latest
10+
11+
docker run --rm -it -v "./:/app/" -w /app/ test-swaggerdoc:latest "./test.sh"
12+
13+
# Remove Dockerfile
14+
rm Dockerfile

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)