From 243ac5bfdb21c39ad4a42ab6729e8462cb3a3877 Mon Sep 17 00:00:00 2001 From: Tim de Pater Date: Sat, 28 Sep 2024 09:31:42 +0200 Subject: [PATCH] Fix testsuite --- main_test.go | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/main_test.go b/main_test.go index 6d4da6a..8a5877b 100644 --- a/main_test.go +++ b/main_test.go @@ -1,11 +1,44 @@ package main import ( + "flag" "os" "testing" ) -func TestBasicWorking(t *testing.T) { - os.Args = []string{"./videotranscoder", "-help"} - main() +func TestParseCliArguments(t *testing.T) { + // Save original command-line arguments and defer their restoration + oldArgs := os.Args + defer func() { os.Args = oldArgs }() + + // Simulate command-line arguments + os.Args = []string{"cmd", "-source", "/some/source", "-target", "/some/target"} + flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) // Reset flags + + // Run the function and recover from os.Exit + exitCode := runTestWithoutExit(t, func() { + parseCliArguments() // You can call main() here if needed. + }) + + if exitCode != 0 { + t.Errorf("Unexpected exit code: %d", exitCode) + } +} + +func runTestWithoutExit(t *testing.T, f func()) (exitCode int) { + // Capture calls to os.Exit + defer func() { + if r := recover(); r != nil { + if code, ok := r.(int); ok { + exitCode = code + } else { + t.Errorf("Unexpected panic: %v", r) + } + } + }() + + // Run the function that might call os.Exit() + f() + + return 0 // No exit, return 0 as the default "exit code" }