-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexit_test.go
113 lines (83 loc) · 2.5 KB
/
exit_test.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-FileCopyrightText: 2025 The Cipher Host Team <[email protected]>
//
// SPDX-License-Identifier: MIT
package cmdkit_test
import (
"errors"
"flag"
"os"
"os/exec"
"testing"
"go.cipher.host/cmdkit"
)
func TestExit_NilError(t *testing.T) {
t.Parallel()
if os.Getenv("TEST_EXIT_NILERROR") == "1" {
cmdkit.Exit(nil)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestExit_NilError") //nolint:gosec // this is not a security issue
cmd.Env = append(os.Environ(), "TEST_EXIT_NILERROR=1")
var exitErr *exec.ExitError
if err := cmd.Run(); err != nil {
if errors.As(err, &exitErr) && !exitErr.Success() {
return
}
t.Fatalf("Exit() process ran with err = %v, want exit status 0", err)
}
}
func TestExit_ErrHelp(t *testing.T) {
t.Parallel()
if os.Getenv("TEST_EXIT_ERRHELP") == "1" {
cmdkit.Exit(flag.ErrHelp)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestExit_ErrHelp") //nolint:gosec // this is not a security issue
cmd.Env = append(os.Environ(), "TEST_EXIT_ERRHELP=1")
var exitErr *exec.ExitError
if err := cmd.Run(); err != nil {
if errors.As(err, &exitErr) && !exitErr.Success() {
return
}
t.Fatalf("Exit() process ran with err = %v, want exit status 0", err)
}
}
func TestExit_ExitError(t *testing.T) {
t.Parallel()
if os.Getenv("TEST_EXIT_EXITERROR") == "1" {
testErr := cmdkit.NewExitError(errGeneric, 1)
cmdkit.Exit(testErr)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestExit_ExitError") //nolint:gosec // this is not a security issue
cmd.Env = append(os.Environ(), "TEST_EXIT_EXITERROR=1")
var exitErr *exec.ExitError
if err := cmd.Run(); err != nil {
if errors.As(err, &exitErr) && !exitErr.Success() {
return
}
t.Fatalf("Exit() process ran with err = %v, want exit status 0", err)
}
if exitErr.ExitCode() != 1 {
t.Fatalf("Exit() process exited with code %d, want 1", exitErr.ExitCode())
}
}
func TestExit_GenericError(t *testing.T) {
t.Parallel()
if os.Getenv("TEST_EXIT_GENERICERROR") == "1" {
cmdkit.Exit(errGeneric)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestExit_GenericError") //nolint:gosec // this is not a security issue
cmd.Env = append(os.Environ(), "TEST_EXIT_GENERICERROR=1")
var exitErr *exec.ExitError
if err := cmd.Run(); err != nil {
if errors.As(err, &exitErr) && !exitErr.Success() {
return
}
t.Fatalf("Exit() process ran with err = %v, want exit status 0", err)
}
if exitErr.ExitCode() != 1 {
t.Fatalf("Exit() process exited with code %d, want 1", exitErr.ExitCode())
}
}