Skip to content

Commit

Permalink
Go unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronromeo committed Jan 1, 2024
1 parent db30865 commit 37d10e6
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/unittesting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Unit Testing

on: [push]

jobs:
# python-tests:
# name: Run Python unit tests
# runs-on: ubuntu-latest

# steps:
# - name: Checkout code
# uses: actions/checkout@v2

# - name: Set up Python
# uses: actions/setup-python@v2
# with:
# python-version: 3.x

# - name: Install dependencies
# run: pip install -r requirements.txt

# - name: Run unit tests
# run: pytest
go-tests:
name: Run Go unit tests
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "~1.21" # Use the version of Go in your project

- name: Test with Go
run: go test -json > TestResults.json

- name: Upload Go test results
uses: actions/upload-artifact@v3
with:
name: Go-results
path: TestResults.json
61 changes: 61 additions & 0 deletions system/bin/btconnect/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package btconnect

import (
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -48,3 +49,63 @@ func TestFilesAreDifferent(t *testing.T) {
os.Remove("test1.txt")
os.Remove("test2.txt")
}

func TestHasALSAConfigChanged(t *testing.T) {
service := &Service{}

// Setup test files
testDir, err := os.MkdirTemp(".", "test_data")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testDir)
err = os.MkdirAll(filepath.Join(testDir, "system", "home"), 0755)
if err != nil {
t.Fatal(err)
}

testSystemConfig := filepath.Join(testDir, "system.asoundrc")
testRepoConfig := filepath.Join(filepath.Join(testDir, "system", "home"), ".asoundrc")

os.Setenv("PJ_ALSA_CONFIG", testSystemConfig)
os.Setenv("PJ_PROJECT_ROOT", testDir)

// Test 1: Identical Files
err = os.WriteFile(testSystemConfig, []byte("test config"), 0644)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(testRepoConfig, []byte("test config"), 0644)
if err != nil {
t.Fatal(err)
}
diff, err := service.hasALSAConfigChanged()
if err != nil {
t.Fatal(err)
}
if diff {
t.Errorf("Expected ALSA configs to be identical, but they were reported as different")
}

// Test 2: Different Files
err = os.WriteFile(testRepoConfig, []byte("different config"), 0644)
if err != nil {
t.Fatal(err)
}
diff, err = service.hasALSAConfigChanged()
if err != nil {
t.Fatal(err)
}
if !diff {
t.Errorf("Expected ALSA configs to be different, but they were reported as identical")
}

// Test 3: File Does Not Exist
os.Remove(testRepoConfig)
_, err = service.hasALSAConfigChanged()
if err == nil {
t.Errorf("Expected error for missing file, but got none")
}

// Optionally, add more tests for scenarios with environment variables set
}

0 comments on commit 37d10e6

Please sign in to comment.