-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain_test.go
More file actions
67 lines (55 loc) · 1.96 KB
/
Copy pathmain_test.go
File metadata and controls
67 lines (55 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"bytes"
"context"
"errors"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestMainFunction tests the main function by invoking it as a separate process.
func TestMainFunction(t *testing.T) {
if os.Getenv("TEST_MAIN_FUNCTION") != "1" {
// This test is run in a subprocess to test main() function
// #nosec G204 - This is a test file executing a controlled test binary
// deepcode ignore CommandInjection/test: This is a test file executing a controlled test binary, it does not deploy in production
//nolint:gosec // Controlled self-execution of the current test binary with fixed arguments.
cmd := exec.CommandContext(context.Background(), os.Args[0], "-test.run=TestMainFunction")
cmd.Env = append(os.Environ(), "TEST_MAIN_FUNCTION=1")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
// We expect the main function to exit with code 1 since no config file is provided
if err != nil {
exitError := &exec.ExitError{}
if errors.As(err, &exitError) {
assert.Equal(t, 1, exitError.ExitCode(), "Expected exit code 1")
}
}
// Verify that some output was produced (help text or error message)
output := stdout.String() + stderr.String()
assert.NotEmpty(t, output, "Expected some output from main function")
return
}
// This is the actual subprocess execution
main()
}
// TestMainExecution tests that main can be called without panicking.
func TestMainExecution(t *testing.T) {
// We can't directly test main() without it calling os.Exit
// but we can test that the command structure is properly set up
// Test that we can get the root command without errors
require.NotPanics(t, func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("main() setup panicked: %v", r)
}
}()
// Just test command creation, not execution
// since execution would call os.Exit
_ = context.Background()
})
}