|
1 |
| -Future home of BATS test suite for Ollama Bash Lib |
| 1 | +# Ollama Bash Lib - Testing |
| 2 | + |
| 3 | +This directory contains the test suite for the Ollama Bash Lib. The tests are written using the [Bats (Bash Automated Testing System)](https://github.com/bats-core/bats-core). |
| 4 | + |
| 5 | +## Setting up the Test Environment |
| 6 | + |
| 7 | +To run the tests, you will need to have Bats installed. |
| 8 | + |
| 9 | +### Installing Bats |
| 10 | + |
| 11 | +You can install Bats using your system's package manager. |
| 12 | + |
| 13 | +**For Debian/Ubuntu:** |
| 14 | +```bash |
| 15 | +sudo apt-get update |
| 16 | +sudo apt-get install -y bats |
| 17 | +``` |
| 18 | + |
| 19 | +**For macOS (using Homebrew):** |
| 20 | +```bash |
| 21 | +brew install bats-core |
| 22 | +``` |
| 23 | + |
| 24 | +For other systems, please refer to the [official Bats installation guide](https://github.com/bats-core/bats-core#installing-bats-from-source). |
| 25 | + |
| 26 | +After installation, you can verify that Bats is installed correctly by running: |
| 27 | +```bash |
| 28 | +bats --version |
| 29 | +``` |
| 30 | + |
| 31 | +## Running Tests |
| 32 | + |
| 33 | +To run the entire test suite, navigate to the root of the repository and run: |
| 34 | +```bash |
| 35 | +bats tests/ |
| 36 | +``` |
| 37 | + |
| 38 | +This will execute all the `.bats` files in the `tests` directory. |
| 39 | + |
| 40 | +To run a specific test file, you can pass the path to the file as an argument: |
| 41 | +```bash |
| 42 | +bats tests/some_test_file.bats |
| 43 | +``` |
| 44 | + |
| 45 | +## Creating New Tests |
| 46 | + |
| 47 | +To create new tests, you should create a new file in the `tests` directory with a `.bats` extension. It is recommended to create a separate file for each function or group of related functions. For example, tests for the `ollama_generate` function should be in a file named `tests/ollama_generate.bats`. |
| 48 | + |
| 49 | +### Test File Structure |
| 50 | + |
| 51 | +A basic test file looks like this: |
| 52 | + |
| 53 | +```bash |
| 54 | +#!/usr/bin/env bats |
| 55 | + |
| 56 | +# Load the Ollama Bash Lib |
| 57 | +source ./ollama_bash_lib.sh |
| 58 | + |
| 59 | +# A basic test case |
| 60 | +@test "A descriptive name for your test" { |
| 61 | + # Run a command or function |
| 62 | + run some_function_to_test "with some arguments" |
| 63 | + |
| 64 | + # Add assertions to check the output |
| 65 | + [ "$status" -eq 0 ] # Check the exit status |
| 66 | + [ "$output" = "expected output" ] # Check the output string |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### Writing Tests |
| 71 | + |
| 72 | +- **Load the Library:** Make sure to source the `ollama_bash_lib.sh` at the beginning of your test file so that you can call the library functions. |
| 73 | +- **Test Names:** Use descriptive names for your tests in the `@test` directive. |
| 74 | +- **`run` command:** Use the `run` command to execute the function you want to test. This captures the `status` and `output` of the command. |
| 75 | +- **Assertions:** Use assertions to check the results of your test. Bats provides special variables that you can use for assertions: |
| 76 | + - `$status`: The exit status of the command. |
| 77 | + - `$output`: The combined content of stdout and stderr from the command. |
| 78 | + - `$lines`: An array containing each line of the output. |
| 79 | + |
| 80 | +For more advanced testing techniques, please refer to the [official Bats documentation](https://bats-core.readthedocs.io/en/stable/). |
0 commit comments