DevLog250305

> Log Date: 250305

# Dev Log | 250305

Project Ideas & Considerations

1. Enhancing the Spotify Playlist Maker

2. Netflix Movie Recommendation Tool


Reflections & Next Steps


Immediate Next Steps


Dev Log: Implementing Tests, CI/CD, and Environment Variable Management

Date: 250305


Overview

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.


1. Adding Tests

Unit Tests in Rust

We implemented unit tests to verify that environment variables are being loaded correctly.

Integration Test for .env Variables

A test file was created at tests/integration_tests.rs:

use dotenv::dotenv;
use std::env;

#[test]
fn test_env_variables() {
    dotenv().ok(); // Load .env file
    
    assert!(env::var("SPOTIFY_CLIENT_ID").is_ok());
    assert!(env::var("SPOTIFY_CLIENT_SECRET").is_ok());
    assert!(env::var("SPOTIFY_REDIRECT_URI").is_ok());
}

Running Tests

To execute tests, we ran:

cargo test

The tests passed, confirming that the .env variables were properly loaded.


2. Setting Up CI/CD with GitHub Actions

Adding a GitHub Actions Workflow

A new workflow file, .github/workflows/rust-ci.yml, was created to automate testing on every push.

GitHub Actions Workflow (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.


3. Managing Environment Variables

Moving .env to the Root Directory

To ensure Rust can properly load environment variables, the .env file was moved:

mv src/.env .env

Updating .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.

Verifying .env in Rust

To check if the .env file was being loaded correctly, values were printed in main.rs:

use dotenv::dotenv;
use std::env;

dotenv().ok();
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.


4. Pushing Updates to GitHub

Commit and Push

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.


Final Results


Next Steps

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.