Skip to content

Commit

Permalink
chore: 53 add bats testing (#123)
Browse files Browse the repository at this point in the history
_Recreated this PR after rebasing with main._

Review the CONTRIBUTING.md file to setup your development station for
BATS testing.

---------

Co-authored-by: Sascha Bates <sbates@users.noreply.github.com>
  • Loading branch information
stujfiter and sbates authored Jun 14, 2023
1 parent b2de029 commit 673fe53
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 40 deletions.
98 changes: 59 additions & 39 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ We use `python-semantic-versioning` for automated versioning on merges to main.
Like this:
`chore: my PR title`

Valid prefixes are defined in the [angular documentation](https://www.businessreport.com/business/big-companies-continue-to-back-pride-events-despite-pushback)
Valid prefixes are defined in the [angular documentation](https://github.com/angular/angular/blob/main/CONTRIBUTING.md#commit)

# Environment Requirements

## Mac Only (for now!)

Note that, as of today, this repo is being built on and tested against macOS 12.6 Monterey. Windows support will be coming soon™.

## Supported Languages

SeCureLI has Slalom-maintained templates for security management of the following languages.
Expand All @@ -38,63 +34,87 @@ SeCureLI is currently in alpha, with support for additional languages planned, i

This repo was started against Python 3.9.9, which released 11/15/2021. Security support will last until 10/05/2025. Newer versions should be fine, older versions will likely not work.

## C Compiler

Certain dependencies are implemented as C extensions. Under certain circumstances, you may need to compile the package from sources on your machine. You’ll need a C compiler and Python header files, such as Xcode and Xcode’s command line tools for the Mac, if this is the case. Generally you’ll be guided through this process as you attempt to resolve dependencies (see `poetry install` below).

Do no setup for this requirement unless prompted to do so, and then follow the instructions given.

# Cloning the Repo and Setup
## Setup macOS
As of June 9, 2023, this repo is being built on and tested against macOS 13.4 Ventura.

- Install Homebrew if needed
- https://brew.sh
- Run these three commands (or something like them) in your terminal to add Homebrew to your PATH:
_Note about the C Compiler: Certain dependencies are implemented as C extensions. Under certain circumstances, you may need to compile the package from sources on your machine. You’ll need a C compiler and Python header files, such as Xcode and Xcode’s command line tools for the Mac, if this is the case. Generally you’ll be guided through this process as you attempt to resolve dependencies (see `poetry install` below)._

```commandline
echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /Users/[username]/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/[username]/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
```
___Do no setup for this requirement__ unless prompted to do so, and then follow the instructions given._

- Using Homebrew, install python if needed (installs 3.9.6 as of 11/04/2022)
- `brew install python`
- Install Homebrew if needed: https://brew.sh
- Install python `brew install python`
- Install [Poetry](https://python-poetry.org/docs/)
- Troubleshooting: If you get an error about python and virtual environments, and you have just installed python using Homebrew, you may need to close and relaunch your terminal
- Restart your terminal
- Similar to the next steps in the homebrew section, follow the instructions to register Poetry with your PATH, running something like the following:

```commandline
echo '# Add Poetry bin directory to the PATH' >> /Users/tristanl/.zprofile
echo 'export PATH="/Users/[username]/.local/bin:$PATH"' >> /Users/[username]/.zprofile
source ~/.zprofile
```
- Jump to [Setup (all Operating Systems)](#Setup-all-Operating-Systems)

- Install [PyCharm Community Edition](https://www.jetbrains.com/pycharm/download/#section=mac)
- Launch PyCharm and create a new sample project
- Use the Tools menu and select `Create Command-line Launcher...`
- Troubleshooting: You may need to create a new project in order to see the Tools menu
- Perform a one-time configuration of Poetry into PyCharm. Follow the instructions on PyCharm’s website
- [https://www.jetbrains.com/help/pycharm/poetry.html](https://www.jetbrains.com/help/pycharm/poetry.html)
- Clone the repo
- `git clone git@bitbucket.org:slalom-consulting/secureli.git`
## Setup Windows™
Windows™ contributing setup will be coming soon.

## Setup Linux

As of June 9, 2023, this repo is being built on and tested against Ubuntu Jammy 22.04 LTS

- Update apt: `sudo apt update`
- Install git & curl: `sudo apt install git curl`
- (Optional) Install Python: _Ubuntu Jammy comes with Python 3.10 pre-installed_
- Install [Poetry](https://python-poetry.org/docs/)
- `curl -sSL https://install.python-poetry.org | python3 -`
- Follow the instructions to add poetry to your shell's $PATH

# Setup (all Operating Systems)
- Install BATS (Bash Automated Testing System)
- `cd $HOME`
- `git clone https://github.com/bats-core/bats-core.git`
- `sudo ./bats-core/install.sh /usr/local`
- `sudo git clone https://github.com/bats-core/bats-support.git /usr/local/lib/bats-support`
- `sudo git clone https://github.com/bats-core/bats-assert.git /usr/local/lib/bats-assert`
- Add `export BATS_LIBS_ROOT="/usr/local/lib"` to your shell's configuration file (e.x. ~/.bashrc)
- Restart your terminal


- Clone the secureli repo
- `git clone https://github.com/slalombuild/secureli.git`
- `cd secureli`


- Activate a virtual environment using Poetry
- `poetry shell`
- This will activate a new virtual environment, and PyCharm should automatically pick this up.
- To leave this virtual environment, use `exit`, not `deactivate`


- Install your dependencies with Poetry
- `poetry install`


- Run the tests and calculate code coverage
- `poe test`
- Open the `htmlcov/index.html` file to view your coverage report


- Try it out!
- With the virtual environment still activated, and having installed all dependencies (i.e. `poetry shell && poetry install`), run `secureli` and check out the Usage instructions
- After the first run, you can run end-to-end BATS tests with `poe e2e`


# PyCharm
- Install [PyCharm Community Edition](https://www.jetbrains.com/pycharm/download/#section=mac)
- Launch PyCharm and create a new sample project
- Use the Tools menu and select `Create Command-line Launcher...`
- Troubleshooting: You may need to create a new project in order to see the Tools menu
- Perform a one-time configuration of Poetry into PyCharm. Follow the instructions on PyCharm’s website
- [https://www.jetbrains.com/help/pycharm/poetry.html](https://www.jetbrains.com/help/pycharm/poetry.html)
- Open the new repo with PyCharm
- `charm .` (assuming you set up the Command-line Launcher above 👆)
- Say “OK” when prompted to create a poetry environment using pyproject.toml
- From the terminal, either in PyCharm or in the OS Terminal, type `secureli` and press Enter. You should see SeCureLI’s documentation appear, with a list of supported commands.
- Run the tests
- `pytest`
- Calculate code coverage
- `coverage run -m pytest && coverage html`
- Open the `htmlcov/index.html` file to view your coverage report
- Try it out!
- With the virtual environment still activated, and having installed (i.e. `poetry shell && poetry install`), run `secureli` and check out the Usage instructions
- Right now, all it does is try to determine what the language is of the repo

## Create your first Run/Debug Configuration

Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ secureli = "secureli.main:app"
coverage = ["test", "coverage_report"]
coverage_report = "coverage report"
coverage_run = "coverage run -m pytest"
coverage_html = "coverage html"
docker-build-app = "docker build -t secureli . -f Dockerfile_secureli --progress=plain --no-cache"
docker-build-homebrew = "docker build -t homebrew . -f Dockerfile_homebrew --no-cache --progress=plain"
docker-build-pypi = "docker build -t pypi . -f Dockerfile_pypi --no-cache --progress=plain"
Expand All @@ -21,7 +22,8 @@ secureli_init = "secureli init -y"
install = "poetry install"
lint = "black --check ."
precommit = "pre-commit run -a"
test = ["init", "lint", "coverage_run"]
test = ["init", "lint", "coverage_run", "coverage_html"]
e2e = "bats tests/end-to-end/test.bats"

[tool.poetry.dependencies]
python = "^3.9"
Expand Down
15 changes: 15 additions & 0 deletions tests/end-to-end/test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
setup() {
load "${BATS_LIBS_ROOT}/bats-support/load"
load "${BATS_LIBS_ROOT}/bats-assert/load"
}

@test "can run secureli init" {
run python secureli/main.py init -ry
assert_output --partial 'SeCureLI has not been setup yet.'
assert_output --partial 'SeCureLI has been installed successfully (language = Python)'
}

@test "can run secureli scan" {
run python secureli/main.py scan -y
assert_output --partial 'Scan executed successfully and detected no issues!'
}

0 comments on commit 673fe53

Please sign in to comment.