mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2026-01-11 00:04:43 +01:00
Refactor tests to use shared setupFormatFlag and helper function
- Create setupTestCommand helper function to reduce test duplication - Update all format flag tests to use shared setupFormatFlag function - Remove unused enumflag import from test file - Ensures test consistency with production code - All tests continue to pass Co-authored-by: Belphemur <197810+Belphemur@users.noreply.github.com>
This commit is contained in:
@@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/belphemur/CBZOptimizer/v2/pkg/converter"
|
"github.com/belphemur/CBZOptimizer/v2/pkg/converter"
|
||||||
"github.com/belphemur/CBZOptimizer/v2/pkg/converter/constant"
|
"github.com/belphemur/CBZOptimizer/v2/pkg/converter/constant"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/thediveo/enumflag/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MockConverter is a mock implementation of the Converter interface
|
// MockConverter is a mock implementation of the Converter interface
|
||||||
@@ -174,22 +173,17 @@ func TestConvertCbzCommand(t *testing.T) {
|
|||||||
t.Logf("Found %d converted files", len(convertedFiles))
|
t.Logf("Found %d converted files", len(convertedFiles))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestFormatFlagWithSpace tests that the format flag works with space-separated values
|
// setupTestCommand creates a test command with all required flags
|
||||||
func TestFormatFlagWithSpace(t *testing.T) {
|
func setupTestCommand(t *testing.T) (*cobra.Command, func()) {
|
||||||
// Create a temporary directory for testing
|
t.Helper()
|
||||||
tempDir, err := os.MkdirTemp("", "test_format_space")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create temp directory: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
// Mock the converter.Get function
|
// Mock the converter.Get function
|
||||||
originalGet := converter.Get
|
originalGet := converter.Get
|
||||||
converter.Get = func(format constant.ConversionFormat) (converter.Converter, error) {
|
converter.Get = func(format constant.ConversionFormat) (converter.Converter, error) {
|
||||||
return &MockConverter{}, nil
|
return &MockConverter{}, nil
|
||||||
}
|
}
|
||||||
defer func() { converter.Get = originalGet }()
|
cleanup := func() { converter.Get = originalGet }
|
||||||
|
|
||||||
// Set up the command
|
// Set up the command
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "optimize",
|
Use: "optimize",
|
||||||
@@ -202,8 +196,22 @@ func TestFormatFlagWithSpace(t *testing.T) {
|
|||||||
|
|
||||||
// Reset converterType to default before test
|
// Reset converterType to default before test
|
||||||
converterType = constant.WebP
|
converterType = constant.WebP
|
||||||
formatFlag := enumflag.New(&converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive)
|
setupFormatFlag(cmd, &converterType, false)
|
||||||
cmd.Flags().VarP(formatFlag, "format", "f", "Format to convert the images to")
|
|
||||||
|
return cmd, cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestFormatFlagWithSpace tests that the format flag works with space-separated values
|
||||||
|
func TestFormatFlagWithSpace(t *testing.T) {
|
||||||
|
// Create a temporary directory for testing
|
||||||
|
tempDir, err := os.MkdirTemp("", "test_format_space")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create temp directory: %v", err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
|
cmd, cleanup := setupTestCommand(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
// Test with space-separated format flag (--format webp)
|
// Test with space-separated format flag (--format webp)
|
||||||
cmd.ParseFlags([]string{"--format", "webp"})
|
cmd.ParseFlags([]string{"--format", "webp"})
|
||||||
@@ -229,27 +237,8 @@ func TestFormatFlagWithShortForm(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.RemoveAll(tempDir)
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
// Mock the converter.Get function
|
cmd, cleanup := setupTestCommand(t)
|
||||||
originalGet := converter.Get
|
defer cleanup()
|
||||||
converter.Get = func(format constant.ConversionFormat) (converter.Converter, error) {
|
|
||||||
return &MockConverter{}, nil
|
|
||||||
}
|
|
||||||
defer func() { converter.Get = originalGet }()
|
|
||||||
|
|
||||||
// Set up the command
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "optimize",
|
|
||||||
}
|
|
||||||
cmd.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)")
|
|
||||||
cmd.Flags().IntP("parallelism", "n", 1, "Number of chapters to convert in parallel")
|
|
||||||
cmd.Flags().BoolP("override", "o", false, "Override the original CBZ/CBR files")
|
|
||||||
cmd.Flags().BoolP("split", "s", false, "Split long pages into smaller chunks")
|
|
||||||
cmd.Flags().DurationP("timeout", "t", 0, "Maximum time allowed for converting a single chapter")
|
|
||||||
|
|
||||||
// Reset converterType to default before test
|
|
||||||
converterType = constant.WebP
|
|
||||||
formatFlag := enumflag.New(&converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive)
|
|
||||||
cmd.Flags().VarP(formatFlag, "format", "f", "Format to convert the images to")
|
|
||||||
|
|
||||||
// Test with short form and space (-f webp)
|
// Test with short form and space (-f webp)
|
||||||
cmd.ParseFlags([]string{"-f", "webp"})
|
cmd.ParseFlags([]string{"-f", "webp"})
|
||||||
@@ -275,27 +264,8 @@ func TestFormatFlagWithEquals(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.RemoveAll(tempDir)
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
// Mock the converter.Get function
|
cmd, cleanup := setupTestCommand(t)
|
||||||
originalGet := converter.Get
|
defer cleanup()
|
||||||
converter.Get = func(format constant.ConversionFormat) (converter.Converter, error) {
|
|
||||||
return &MockConverter{}, nil
|
|
||||||
}
|
|
||||||
defer func() { converter.Get = originalGet }()
|
|
||||||
|
|
||||||
// Set up the command
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "optimize",
|
|
||||||
}
|
|
||||||
cmd.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)")
|
|
||||||
cmd.Flags().IntP("parallelism", "n", 1, "Number of chapters to convert in parallel")
|
|
||||||
cmd.Flags().BoolP("override", "o", false, "Override the original CBZ/CBR files")
|
|
||||||
cmd.Flags().BoolP("split", "s", false, "Split long pages into smaller chunks")
|
|
||||||
cmd.Flags().DurationP("timeout", "t", 0, "Maximum time allowed for converting a single chapter")
|
|
||||||
|
|
||||||
// Reset converterType to default before test
|
|
||||||
converterType = constant.WebP
|
|
||||||
formatFlag := enumflag.New(&converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive)
|
|
||||||
cmd.Flags().VarP(formatFlag, "format", "f", "Format to convert the images to")
|
|
||||||
|
|
||||||
// Test with equals syntax (--format=webp)
|
// Test with equals syntax (--format=webp)
|
||||||
cmd.ParseFlags([]string{"--format=webp"})
|
cmd.ParseFlags([]string{"--format=webp"})
|
||||||
@@ -321,27 +291,8 @@ func TestFormatFlagDefaultValue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.RemoveAll(tempDir)
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
// Mock the converter.Get function
|
cmd, cleanup := setupTestCommand(t)
|
||||||
originalGet := converter.Get
|
defer cleanup()
|
||||||
converter.Get = func(format constant.ConversionFormat) (converter.Converter, error) {
|
|
||||||
return &MockConverter{}, nil
|
|
||||||
}
|
|
||||||
defer func() { converter.Get = originalGet }()
|
|
||||||
|
|
||||||
// Set up the command
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "optimize",
|
|
||||||
}
|
|
||||||
cmd.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)")
|
|
||||||
cmd.Flags().IntP("parallelism", "n", 1, "Number of chapters to convert in parallel")
|
|
||||||
cmd.Flags().BoolP("override", "o", false, "Override the original CBZ/CBR files")
|
|
||||||
cmd.Flags().BoolP("split", "s", false, "Split long pages into smaller chunks")
|
|
||||||
cmd.Flags().DurationP("timeout", "t", 0, "Maximum time allowed for converting a single chapter")
|
|
||||||
|
|
||||||
// Reset converterType to default before test
|
|
||||||
converterType = constant.DefaultConversion
|
|
||||||
formatFlag := enumflag.New(&converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive)
|
|
||||||
cmd.Flags().VarP(formatFlag, "format", "f", "Format to convert the images to")
|
|
||||||
|
|
||||||
// Don't set format flag - should use default
|
// Don't set format flag - should use default
|
||||||
cmd.ParseFlags([]string{})
|
cmd.ParseFlags([]string{})
|
||||||
@@ -367,31 +318,12 @@ func TestFormatFlagCaseInsensitive(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.RemoveAll(tempDir)
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
// Mock the converter.Get function
|
|
||||||
originalGet := converter.Get
|
|
||||||
converter.Get = func(format constant.ConversionFormat) (converter.Converter, error) {
|
|
||||||
return &MockConverter{}, nil
|
|
||||||
}
|
|
||||||
defer func() { converter.Get = originalGet }()
|
|
||||||
|
|
||||||
testCases := []string{"webp", "WEBP", "WebP", "WeBp"}
|
testCases := []string{"webp", "WEBP", "WebP", "WeBp"}
|
||||||
|
|
||||||
for _, formatValue := range testCases {
|
for _, formatValue := range testCases {
|
||||||
t.Run(formatValue, func(t *testing.T) {
|
t.Run(formatValue, func(t *testing.T) {
|
||||||
// Set up the command
|
cmd, cleanup := setupTestCommand(t)
|
||||||
cmd := &cobra.Command{
|
defer cleanup()
|
||||||
Use: "optimize",
|
|
||||||
}
|
|
||||||
cmd.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)")
|
|
||||||
cmd.Flags().IntP("parallelism", "n", 1, "Number of chapters to convert in parallel")
|
|
||||||
cmd.Flags().BoolP("override", "o", false, "Override the original CBZ/CBR files")
|
|
||||||
cmd.Flags().BoolP("split", "s", false, "Split long pages into smaller chunks")
|
|
||||||
cmd.Flags().DurationP("timeout", "t", 0, "Maximum time allowed for converting a single chapter")
|
|
||||||
|
|
||||||
// Reset converterType to default before test
|
|
||||||
converterType = constant.WebP
|
|
||||||
formatFlag := enumflag.New(&converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive)
|
|
||||||
cmd.Flags().VarP(formatFlag, "format", "f", "Format to convert the images to")
|
|
||||||
|
|
||||||
// Test with different case variations
|
// Test with different case variations
|
||||||
cmd.ParseFlags([]string{"--format", formatValue})
|
cmd.ParseFlags([]string{"--format", formatValue})
|
||||||
|
|||||||
Reference in New Issue
Block a user