Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests #11

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ jobs:
with:
name: dist
path: dist

- name: 🧹 Check formatting
run: |
go fmt ./...
git diff --exit-code
225 changes: 225 additions & 0 deletions cmd/splashscreen-changer/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package main

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

func TestLoadConfig(t *testing.T) {
// Create necessary directories
tmpDir := os.TempDir()
sourceDirPath := filepath.Join(tmpDir, "source")
if err := os.MkdirAll(sourceDirPath, os.ModePerm); err != nil {
t.Fatalf("Failed to create source directory: %v", err)
}
defer func() {
if err := os.RemoveAll(sourceDirPath); err != nil {
t.Errorf("Failed to remove source directory: %v", err)
}
}()
destinationDirPath := filepath.Join(tmpDir, "destination")
if err := os.MkdirAll(filepath.Join(destinationDirPath, "EasyAntiCheat"), os.ModePerm); err != nil {
t.Fatalf("Failed to create destination directory: %v", err)
}
defer func() {
if err := os.RemoveAll(destinationDirPath); err != nil {
t.Fatalf("Failed to remove destination directory: %v", err)
}
}()

// Create a temporary config file
configContent := `
source:
path: {{ .Source.Path }}
recursive: true
destination:
path: {{ .Destination.Path }}
width: 1024
height: 768
`
configContent = strings.TrimSpace(configContent)
configContent = strings.ReplaceAll(configContent, "\t", " ")
configContent = strings.ReplaceAll(configContent, "{{ .Source.Path }}", sourceDirPath)
configContent = strings.ReplaceAll(configContent, "{{ .Destination.Path }}", destinationDirPath)

tmpFile, err := os.CreateTemp("", "config-*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())

if _, err := tmpFile.Write([]byte(configContent)); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
if err := tmpFile.Close(); err != nil {
t.Fatalf("Failed to close temp file: %v", err)
}

// Load the config
config, err := LoadConfig(tmpFile.Name())
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}

// Check the loaded config values
if config.Source.Path != filepath.Join(tmpDir, "source") {
t.Errorf("Expected source path to be '%s', got '%s'", filepath.Join(tmpDir, "source"), config.Source.Path)
}
if !config.Source.Recursive {
t.Errorf("Expected source recursive to be true, got false")
}
if config.Destination.Path != filepath.Join(tmpDir, "destination") {
t.Errorf("Expected destination path to be '%s', got '%s'", filepath.Join(tmpDir, "destination"), config.Destination.Path)
}
if config.Destination.Width != 1024 {
t.Errorf("Expected destination width to be 1024, got %d", config.Destination.Width)
}
if config.Destination.Height != 768 {
t.Errorf("Expected destination height to be 768, got %d", config.Destination.Height)
}
}

func TestLoadConfigWithDefaults(t *testing.T) {
// Create necessary directories
tmpDir := os.TempDir()
sourceDirPath := filepath.Join(tmpDir, "source")
os.MkdirAll(sourceDirPath, os.ModePerm)
defer os.RemoveAll(sourceDirPath)
destinationDirPath := filepath.Join(tmpDir, "destination")
os.MkdirAll(filepath.Join(destinationDirPath, "EasyAntiCheat"), os.ModePerm)
defer os.RemoveAll(destinationDirPath)

// Create a temporary config file
configContent := `
source:
path: {{ .Source.Path }}
destination:
path: {{ .Destination.Path }}
`
configContent = strings.TrimSpace(configContent)
configContent = strings.ReplaceAll(configContent, "\t", " ")
configContent = strings.ReplaceAll(configContent, "{{ .Source.Path }}", sourceDirPath)
configContent = strings.ReplaceAll(configContent, "{{ .Destination.Path }}", destinationDirPath)

tmpFile, err := os.CreateTemp("", "config-*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())

if _, err := tmpFile.Write([]byte(configContent)); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
if err := tmpFile.Close(); err != nil {
t.Fatalf("Failed to close temp file: %v", err)
}

// Load the config
config, err := LoadConfig(tmpFile.Name())
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}

// Check the loaded config values
if config.Source.Path != filepath.Join(tmpDir, "source") {
t.Errorf("Expected source path to be '%s', got '%s'", filepath.Join(tmpDir, "source"), config.Source.Path)
}
if config.Source.Recursive {
t.Errorf("Expected source recursive to be false, got true")
}
if config.Destination.Path != filepath.Join(tmpDir, "destination") {
t.Errorf("Expected destination path to be '%s', got '%s'", filepath.Join(tmpDir, "destination"), config.Destination.Path)
}
if config.Destination.Width != 800 {
t.Errorf("Expected destination width to be 800, got %d", config.Destination.Width)
}
if config.Destination.Height != 450 {
t.Errorf("Expected destination height to be 450, got %d", config.Destination.Height)
}
}

func TestLoadConfigWithEnvOverrides(t *testing.T) {
// Create necessary directories
tmpDir := os.TempDir()
envSourceDirPath := filepath.Join(tmpDir, "env", "source")
os.MkdirAll(envSourceDirPath, os.ModePerm)
defer os.RemoveAll(envSourceDirPath)
envDestinationDirPath := filepath.Join(tmpDir, "env", "destination")
os.MkdirAll(filepath.Join(envDestinationDirPath, "EasyAntiCheat"), os.ModePerm)
defer os.RemoveAll(envDestinationDirPath)
configSourceDirPath := filepath.Join(tmpDir, "source")
os.MkdirAll(configSourceDirPath, os.ModePerm)
defer os.RemoveAll(configSourceDirPath)
configDestinationDirPath := filepath.Join(tmpDir, "destination")
os.MkdirAll(filepath.Join(configDestinationDirPath, "EasyAntiCheat"), os.ModePerm)
defer os.RemoveAll(configDestinationDirPath)

// Set environment variables
customEnv := map[string]string{
"SOURCE_PATH": envSourceDirPath,
"SOURCE_RECURSIVE": "true",
"DESTINATION_PATH": envDestinationDirPath,
"DESTINATION_WIDTH": "1280",
"DESTINATION_HEIGHT": "720",
}
for key, value := range customEnv {
os.Setenv(key, value)
}
defer func() {
for key := range customEnv {
os.Unsetenv(key)
}
}()

// Create a temporary config file
configContent := `
source:
path: {{ .Source.Path }}
recursive: false
destination:
path: {{ .Destination.Path }}
width: 1024
height: 768
`
configContent = strings.TrimSpace(configContent)
configContent = strings.ReplaceAll(configContent, "\t", " ")
configContent = strings.ReplaceAll(configContent, "{{ .Source.Path }}", configSourceDirPath)
configContent = strings.ReplaceAll(configContent, "{{ .Destination.Path }}", configDestinationDirPath)
tmpFile, err := os.CreateTemp("", "config-*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())

if _, err := tmpFile.Write([]byte(configContent)); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
if err := tmpFile.Close(); err != nil {
t.Fatalf("Failed to close temp file: %v", err)
}

// Load the config
config, err := LoadConfig(tmpFile.Name())
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}

// Check the loaded config values
if config.Source.Path != envSourceDirPath {
t.Errorf("Expected source path to be '%s', got '%s'", envSourceDirPath, config.Source.Path)
}
if !config.Source.Recursive {
t.Errorf("Expected source recursive to be true, got false")
}
if config.Destination.Path != envDestinationDirPath {
t.Errorf("Expected destination path to be '%s', got '%s'", envDestinationDirPath, config.Destination.Path)
}
if config.Destination.Width != 1280 {
t.Errorf("Expected destination width to be 1280, got %d", config.Destination.Width)
}
if config.Destination.Height != 720 {
t.Errorf("Expected destination height to be 720, got %d", config.Destination.Height)
}
}
112 changes: 112 additions & 0 deletions cmd/splashscreen-changer/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

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

// Test listPNGFiles function
func TestListPNGFiles(t *testing.T) {
// Create a temporary directory
tmpDir := os.TempDir()
tmpDir = filepath.Join(tmpDir, "listPNGFiles")
os.Mkdir(tmpDir, os.ModePerm)

// Create some test PNG files
file1, _ := os.Create(filepath.Join(tmpDir, "test1.png"))
file1.Close()
file2, _ := os.Create(filepath.Join(tmpDir, "test2.png"))
file2.Close()

// Create a non-PNG file
file3, _ := os.Create(filepath.Join(tmpDir, "test3.txt"))
file3.Close()

// Test non-recursive listing
files, err := listPNGFiles(tmpDir, false)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if len(files) != 2 {
t.Fatalf("Expected 2 PNG files, got %d", len(files))
}

// Test recursive listing
subDir := filepath.Join(tmpDir, "subdir")
os.Mkdir(subDir, 0755)
file4, _ := os.Create(filepath.Join(subDir, "test4.png"))
file4.Close()

files, err = listPNGFiles(tmpDir, true)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if len(files) != 3 {
t.Fatalf("Expected 3 PNG files, got %d", len(files))
}
}

// Test pickRandomFile function
func TestPickRandomFile(t *testing.T) {
files := []string{"file1.png", "file2.png", "file3.png"}
pickedFile, err := pickRandomFile(files)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if pickedFile == "" {
t.Fatalf("Expected a file to be picked, got an empty string")
}

// Test with empty list
files = []string{}
_, err = pickRandomFile(files)
if err == nil {
t.Fatalf("Expected an error, got nil")
}
}

// Test cropToAspectRatio function
func TestCropToAspectRatio(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 100, 200))
croppedImg := cropToAspectRatio(img, 50, 50)

if croppedImg.Bounds().Dx() != 50 || croppedImg.Bounds().Dy() != 50 {
t.Fatalf("Expected cropped image to be 50x50, got %dx%d", croppedImg.Bounds().Dx(), croppedImg.Bounds().Dy())
}
}

// Test resizePNGFile function
func TestResizePNGFile(t *testing.T) {
// Create a temporary directory
tempDir := os.TempDir()

// Create a test PNG file
srcPath := filepath.Join(tempDir, "src.png")
destPath := filepath.Join(tempDir, "dest.png")

img := image.NewRGBA(image.Rect(0, 0, 100, 200))
f, _ := os.Create(srcPath)
png.Encode(f, img)
f.Close()

err := resizePNGFile(srcPath, destPath, 50, 50)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}

// Check if the destination file exists
if _, err := os.Stat(destPath); os.IsNotExist(err) {
t.Fatalf("Expected destination file to exist, but it does not")
}

// Check if the destination file has the correct dimensions
destFile, _ := os.Open(destPath)
defer destFile.Close()
destImg, _, _ := image.Decode(destFile)
if destImg.Bounds().Dx() != 50 || destImg.Bounds().Dy() != 50 {
t.Fatalf("Expected resized image to be 50x50, got %dx%d", destImg.Bounds().Dx(), destImg.Bounds().Dy())
}
}