This log documents improvements made to the Spotify Playlist Maker Rust project by adding automated tests, setting up GitHub Actions for CI/CD, and properly managing environment variables. These updates enhance project reliability, security, and maintainability.
We implemented unit tests to verify that environment variables are being loaded correctly.
.env
VariablesA test file was created at
tests/integration_tests.rs
:
use dotenv::dotenv;
use std::env;
#[test]
fn test_env_variables() {
.ok(); // Load .env file
dotenv()
assert!(env::var("SPOTIFY_CLIENT_ID").is_ok());
assert!(env::var("SPOTIFY_CLIENT_SECRET").is_ok());
assert!(env::var("SPOTIFY_REDIRECT_URI").is_ok());
}
To execute tests, we ran:
cargo test
The tests passed, confirming that the .env
variables
were properly loaded.
A new workflow file, .github/workflows/rust-ci.yml
, was
created to automate testing on every push.
rust-ci.yml
)name: Rust CI/CD Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Install dependencies
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
This workflow ensures that every push and pull request to
main
is automatically tested.
.env
to the Root DirectoryTo ensure Rust can properly load environment variables, the
.env
file was moved:
mv src/.env .env
.gitignore
To prevent .env
from being committed to GitHub, the
.gitignore
file was updated:
# Ignore environment variables (API keys, secrets)
.env
This ensures sensitive credentials remain secure.
.env
in
RustTo check if the .env
file was being loaded correctly,
values were printed in main.rs
:
use dotenv::dotenv;
use std::env;
.ok();
dotenv()println!("SPOTIFY_CLIENT_ID: {:?}", env::var("SPOTIFY_CLIENT_ID"));
println!("SPOTIFY_CLIENT_SECRET: {:?}", env::var("SPOTIFY_CLIENT_SECRET"));
println!("SPOTIFY_REDIRECT_URI: {:?}", env::var("SPOTIFY_REDIRECT_URI"));
Running cargo run
confirmed that the values were
correctly loaded.
After verifying that everything worked, the changes were committed and pushed:
git add .gitignore .github/workflows/rust-ci.yml tests/
git commit -m "Added GitHub Actions CI/CD and integration tests"
git push origin main
GitHub Actions automatically ran the tests upon push, confirming that the pipeline is functional.
With these improvements, the project now has automated testing, secure environment management, and a reliable CI/CD pipeline, ensuring stability as new features are added.