Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.

Commit e4ee488

Browse files
committed
tests: Add test for exit.go
Replace os.Exit() call in exit() with a call to function variable exitFunc that is assigned to os.Exit() In the test assign exitFunc to a test function to help test exit() with 0 and more atexit functions. Fixes #402 Signed-off-by: Archana Shinde <[email protected]>
1 parent 362cd19 commit e4ee488

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

exit.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import "os"
1818

1919
var atexitFuncs []func()
2020

21+
var exitFunc = os.Exit
22+
2123
// atexit registers a function f that will be run when exit is called. The
2224
// handlers so registered will be called the in reverse order of their
2325
// registration.
@@ -31,5 +33,5 @@ func exit(status int) {
3133
f := atexitFuncs[i]
3234
f()
3335
}
34-
os.Exit(status)
36+
exitFunc(status)
3537
}

exit_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2017 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"os"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
var testFoo string
25+
26+
func testFunc() {
27+
testFoo = "bar"
28+
}
29+
30+
func TestExit(t *testing.T) {
31+
assert := assert.New(t)
32+
33+
var testExitStatus int
34+
exitFunc = func(status int) {
35+
testExitStatus = status
36+
}
37+
38+
defer func() {
39+
exitFunc = os.Exit
40+
}()
41+
42+
// test with no atexit functions added.
43+
exit(1)
44+
assert.Equal(testExitStatus, 1)
45+
46+
// test with a function added to the atexit list.
47+
atexit(testFunc)
48+
exit(0)
49+
assert.Equal(testFoo, "bar")
50+
assert.Equal(testExitStatus, 0)
51+
}

0 commit comments

Comments
 (0)