-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
108 lines (94 loc) · 2.82 KB
/
error_test.go
File metadata and controls
108 lines (94 loc) · 2.82 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
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
/*
* Copyright 2022-2026 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package services
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tknie/errorrepo"
"github.com/tknie/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func initLog(fileName string) (err error) {
switch log.Log.(type) {
case *zap.SugaredLogger:
return
default:
}
p := os.Getenv("LOGPATH")
if p == "" {
p = "."
}
var name string
if runtime.GOOS == "windows" {
zap.RegisterSink("winfile", newWinFileSink)
// OutputPaths: []string{"stdout", "winfile:///" + filepath.Join(GlobalConfigDir.Path, "info.log.json")},
name = "winfile:///" + p + string(os.PathSeparator) + fileName
} else {
name = "file://" + filepath.ToSlash(p+string(os.PathSeparator)+fileName)
}
fmt.Println("Logging to file", name)
level := zapcore.ErrorLevel
ed := os.Getenv("ENABLE_DEBUG")
if ed == "1" {
level = zapcore.DebugLevel
fmt.Println("Enable debug logging into", name)
log.SetDebugLevel(true)
}
rawJSON := []byte(`{
"level": "error",
"encoding": "console",
"outputPaths": [ "XXX"],
"errorOutputPaths": ["stderr"],
"encoderConfig": {
"messageKey": "message",
"levelKey": "level",
"levelEncoder": "lowercase"
}
}`)
var cfg zap.Config
if err := json.Unmarshal(rawJSON, &cfg); err != nil {
panic(err)
}
cfg.Level.SetLevel(level)
cfg.OutputPaths = []string{name}
logger, err := cfg.Build()
if err != nil {
panic(err)
}
defer logger.Sync()
sugar := logger.Sugar()
sugar.Infof("logger construction succeeded %s", "xx")
log.InitLog(sugar)
return
}
func TestErrors(t *testing.T) {
err := initLog("errors.log")
if err != nil {
fmt.Println("ERROR : ", err)
return
}
assert.Equal(t, "SYS00001: Unknown error ...SYS00001", NewError("SYS00001").Error())
fmt.Println("Test")
assert.Equal(t, "SYS00002: user xxx unknown", NewError("SYS00002", "xxx").Error())
assert.Equal(t, "SYS00003: not implemented", NewError("SYS00003").Error())
assert.Equal(t, "SYS00004: user totot already in list", NewError("SYS00004", "totot").Error())
assert.Equal(t, "SYS00005: user totot not found in list", NewError("SYS00005", "totot").Error())
assert.Equal(t, "SYS00006: given PID file alala is a directory", NewError("SYS00006", "alala").Error())
assert.Equal(t, "SYS00007: given PID file ffff is already available", NewError("SYS00007", "ffff").Error())
assert.Equal(t, "SYS00008: check PID file adada fails: sdsds", NewError("SYS00008", "adada", "sdsds").Error())
assert.Equal(t, "SYS00002: user tom unknown", errorrepo.NewErrorLocale("en", "SYS00002", "tom").Error())
}