diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index b68d29b..835b0aa 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -66,3 +66,8 @@ jobs: with: name: dist path: dist + + - name: 🧹 Check formatting + run: | + go fmt ./... + git diff --exit-code diff --git a/cmd/splashscreen-changer/config_test.go b/cmd/splashscreen-changer/config_test.go new file mode 100644 index 0000000..5bf5ced --- /dev/null +++ b/cmd/splashscreen-changer/config_test.go @@ -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) + } +} diff --git a/cmd/splashscreen-changer/main_test.go b/cmd/splashscreen-changer/main_test.go new file mode 100644 index 0000000..bcccfaf --- /dev/null +++ b/cmd/splashscreen-changer/main_test.go @@ -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()) + } +}