Skip to content

Commit

Permalink
add: test and fix preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
TOsmanov committed Dec 23, 2024
1 parent 8c1dcd9 commit fe10469
Show file tree
Hide file tree
Showing 10 changed files with 909 additions and 13 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ ENV/
# mypy
.mypy_cache/

.DS_store
.DS_store
.swaggercache
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ preprocessors:
mode: widdershins
template: swagger.j2
environment: env.yaml
strick: false
strict: false
```
`spec_url`
Expand Down Expand Up @@ -131,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 image to run the tests. To do so, run:
```bash
./test_in_docker.sh
```
14 changes: 3 additions & 11 deletions foliant/preprocessors/swaggerdoc/swaggerdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from foliant.preprocessors.utils.preprocessor_ext import allow_fail
from foliant.utils import output


class Preprocessor(BasePreprocessorExt):
tags = ('swaggerdoc',)

Expand All @@ -42,7 +41,7 @@ class Preprocessor(BasePreprocessorExt):
'spec_path': '',
'mode': 'widdershins',
'template': 'swagger.j2',
'strict': False
'strict': True
}

def __init__(self, *args, **kwargs):
Expand All @@ -69,7 +68,6 @@ def __init__(self, *args, **kwargs):
self.options = Options(self.options,
validators={'json_path': validate_exists,
'spec_path': validate_exists})
self.critical_error = []

def _gather_specs(self,
urls: list,
Expand All @@ -93,8 +91,8 @@ def _gather_specs(self,
msg = f'\nCannot retrieve swagger spec file from url {url}.'
if self.options['strict']:
self.logger.error(msg)
self.critical_error.append(msg)
output(f'ERROR: {msg}')
os._exit(1)
else:
self._warning(f'{msg}. Skipping.',
error=e)
Expand Down Expand Up @@ -217,10 +215,4 @@ def process_swaggerdoc_blocks(self, block) -> str:

def apply(self):
self._process_tags_for_all_files(func=self.process_swaggerdoc_blocks)
if len(self.critical_error) > 0:
self.logger.info('Critical errors have occurred')
errors = '\n'.join(self.critical_error)
output(f'\nBuild failed: swaggerdoc preprocessor errors: \n{errors}\n')
os._exit(2)
else:
self.logger.info('Preprocessor applied')
self.logger.info('Preprocessor applied')
11 changes: 11 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions test_in_docker.sh
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit fe10469

Please sign in to comment.