Skip to content

Commit

Permalink
feat: Implement rosemary coverage command
Browse files Browse the repository at this point in the history
  • Loading branch information
drorganvidez committed Mar 25, 2024
1 parent a6e26b7 commit c5b9a16
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ uploads/
app.log
.DS_Store
rosemary.egg-info/
build/
build/
.coverage
htmlcov/
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ use:
rosemary test zenodo
```

### Code Coverage

The `rosemary coverage` command facilitates running code coverage analysis for your Flask project using `pytest-cov`.
This command simplifies the process of assessing test coverage.

#### Command Usage

- **All Modules**: To run coverage analysis for all modules within the `app/blueprints` directory and generate an HTML report, use:

```
rosemary coverage
```

- **Specific Module**: If you wish to run coverage analysis for a specific module, include the
module name:

```
rosemary coverage <module_name>
```

#### Command Options

- **--html**: Generates an HTML coverage report. The report is saved in the `htmlcov` directory
at the root of your project. Example: `rosemary coverage --html`

## Deploy in production (Docker Compose)

```
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ certifi==2024.2.2
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
coverage==7.4.4
cryptography==42.0.5
dnspython==2.6.1
email_validator==2.1.1
Expand All @@ -29,6 +30,7 @@ pycparser==2.21
pyflakes==3.2.0
PyMySQL==1.1.0
pytest==8.1.1
pytest-cov==5.0.0
python-dotenv==1.0.1
requests==2.31.0
rosemary @ file:///app
Expand Down
2 changes: 2 additions & 0 deletions rosemary/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import click

from rosemary.commands.coverage import coverage
from rosemary.commands.linter import linter
from rosemary.commands.update import update
from rosemary.commands.info import info
Expand Down Expand Up @@ -30,6 +31,7 @@ def cli():
cli.add_command(env)
cli.add_command(test)
cli.add_command(linter)
cli.add_command(coverage)

if __name__ == '__main__':
cli()
30 changes: 30 additions & 0 deletions rosemary/commands/coverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import click
import subprocess
import os


@click.command('coverage', help="Runs pytest coverage on the blueprints directory or a specific module.")
@click.argument('module_name', required=False)
@click.option('--html', is_flag=True, help="Generates an HTML coverage report.")
def coverage(module_name, html):
base_path = 'app/blueprints'
test_path = base_path

if module_name:
test_path = os.path.join(base_path, module_name)
if not os.path.exists(test_path):
click.echo(click.style(f"Module '{module_name}' does not exist.", fg='red'))
return
click.echo(f"Running coverage for the '{module_name}' module...")
else:
click.echo("Running coverage for all modules...")

coverage_cmd = ['pytest', '--cov=' + test_path, test_path]

if html:
coverage_cmd.extend(['--cov-report', 'html'])

try:
subprocess.run(coverage_cmd, check=True)
except subprocess.CalledProcessError as e:
click.echo(click.style(f"Error running coverage: {e}", fg='red'))

0 comments on commit c5b9a16

Please sign in to comment.