github.com/nektos/act@v0.2.83/cmd/execute_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  // Helper function to test main with different os.Args
    10  func testMain(args []string) (exitCode int) {
    11  	// Save original os.Args and defer restoring it
    12  	origArgs := os.Args
    13  	defer func() { os.Args = origArgs }()
    14  
    15  	// Save original os.Exit and defer restoring it
    16  	defer func() { exitFunc = os.Exit }()
    17  
    18  	// Mock os.Exit
    19  	fakeExit := func(code int) {
    20  		exitCode = code
    21  	}
    22  	exitFunc = fakeExit
    23  
    24  	// Mock os.Args
    25  	os.Args = args
    26  
    27  	// Run the main function
    28  	Execute(context.Background(), "")
    29  
    30  	return exitCode
    31  }
    32  
    33  func TestMainHelp(t *testing.T) {
    34  	exitCode := testMain([]string{"cmd", "--help"})
    35  	if exitCode != 0 {
    36  		t.Errorf("Expected exit code 0, got %d", exitCode)
    37  	}
    38  }
    39  
    40  func TestMainNoArgsError(t *testing.T) {
    41  	exitCode := testMain([]string{"cmd"})
    42  	if exitCode != 1 {
    43  		t.Errorf("Expected exit code 1, got %d", exitCode)
    44  	}
    45  }