@@ -6,11 +6,14 @@ package logs
6
6
import (
7
7
"bytes"
8
8
"errors"
9
+ "github.com/etix/mirrorbits/core"
9
10
"github.com/etix/mirrorbits/filesystem"
10
11
"github.com/etix/mirrorbits/mirrors"
11
12
"github.com/etix/mirrorbits/network"
13
+ "github.com/op/go-logging"
12
14
"io/ioutil"
13
15
"os"
16
+ "reflect"
14
17
"strings"
15
18
"testing"
16
19
)
@@ -48,6 +51,96 @@ func TestDownloadsLogger_Close(t *testing.T) {
48
51
}
49
52
}
50
53
54
+ func TestIsTerminal (t * testing.T ) {
55
+ stat , _ := os .Stdout .Stat ()
56
+ if (stat .Mode () & os .ModeCharDevice ) == 0 {
57
+ t .Skip ("Cannot test without a valid terminal" )
58
+ }
59
+
60
+ if ! isTerminal (os .Stdout ) {
61
+ t .Fatalf ("The current terminal is supposed to support colors" )
62
+ }
63
+
64
+ f , err := ioutil .TempFile ("" , "mirrorbits-tests" )
65
+ if err != nil {
66
+ t .Errorf ("Unable to create a temporary file: %s" , err .Error ())
67
+ return
68
+ }
69
+ defer os .Remove (f .Name ())
70
+
71
+ if isTerminal (f ) {
72
+ t .Fatalf ("The given file cannot be a terminal" )
73
+ }
74
+ }
75
+
76
+ func TestReloadRuntimeLogs (t * testing.T ) {
77
+ rlogger .f = nil
78
+
79
+ ReloadRuntimeLogs ()
80
+
81
+ if rlogger .f == nil {
82
+ t .Fatalf ("The logger output must be setup" )
83
+ }
84
+ if rlogger .f != os .Stderr {
85
+ t .Fatalf ("The logger output is expected to be Stderr" )
86
+ }
87
+ if logging .GetLevel ("main" ) != logging .INFO {
88
+ t .Fatalf ("Log level is supposed to be INFO by default" )
89
+ }
90
+
91
+ ptr := reflect .ValueOf (rlogger .f ).Pointer ()
92
+ ReloadRuntimeLogs ()
93
+ if reflect .ValueOf (rlogger .f ).Pointer () != ptr {
94
+ t .Fatalf ("The logger must not be reloaded when writing on Stderr" )
95
+ }
96
+
97
+ /* */
98
+ core .RunLog = "/"
99
+ ReloadRuntimeLogs ()
100
+ if rlogger .f != os .Stderr {
101
+ t .Fatalf ("Opening an invalid file must fallback to Stderr" )
102
+ }
103
+
104
+ /* */
105
+ f , err := ioutil .TempFile ("" , "mirrorbits-tests" )
106
+ if err != nil {
107
+ t .Errorf ("Unable to create a temporary file: %s" , err .Error ())
108
+ return
109
+ }
110
+ defer os .Remove (f .Name ())
111
+
112
+ core .RunLog = f .Name ()
113
+ core .Debug = true
114
+
115
+ ReloadRuntimeLogs ()
116
+
117
+ if logging .GetLevel ("main" ) != logging .DEBUG {
118
+ t .Fatalf ("Log level is supposed to be DEBUG" )
119
+ }
120
+
121
+ if rlogger .f == os .Stderr {
122
+ t .Fatalf ("The output is expected to be a file, not Stderr" )
123
+ }
124
+
125
+ /* */
126
+ testString := "Testing42"
127
+ log .Error (testString )
128
+
129
+ buf , _ := ioutil .ReadAll (f )
130
+ if ! strings .Contains (string (buf ), testString ) {
131
+ t .Fatalf ("The log doesn't contain the string %s" , testString )
132
+ }
133
+
134
+ /* */
135
+ core .RunLog = ""
136
+ ReloadRuntimeLogs ()
137
+
138
+ if rlogger .f != os .Stderr {
139
+ t .Fatalf ("The output is expected to be Stderr" )
140
+ }
141
+
142
+ }
143
+
51
144
func TestOpenLogFile (t * testing.T ) {
52
145
path , err := ioutil .TempDir ("" , "mirrorbits-tests" )
53
146
if err != nil {
0 commit comments