Skip to content

Commit 44f5cd5

Browse files
committed
Allow the Config to receive a Logger instance, if needed
1 parent 54ff70c commit 44f5cd5

File tree

6 files changed

+186
-76
lines changed

6 files changed

+186
-76
lines changed

config.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,16 @@ type Config struct {
7272
// typenames, etc)
7373
Focus *Focus
7474

75+
// Logger is an instance of the LoggerCLI, already created
76+
// by the lib user.
77+
//
78+
// Note that by using this field, the value of the LogFlags
79+
// field will be ignored
80+
Logger LoggerCLI
81+
7582
// LogFlags controls the logging configuration of the parser.
76-
// It can be set using the constants LogTrace, LogDebug and
77-
// LogJSON.
83+
// It can be set using the constants LogTrace, LogDebug, LogJSON,
84+
// etc. If the Logger field is set, this field will be ignored.
7885
//
7986
// Note that you can use a bitwise-AND operator to combine
8087
// multiple flags
@@ -83,7 +90,7 @@ type Config struct {
8390

8491
// packagesLoadConfig returns the configuration struct that is used to
8592
// call the packages.Load function
86-
func packagesLoadConfig(config Config, log LoggerCLI) *packages.Config {
93+
func packagesLoadConfig(config Config) *packages.Config {
8794
if config.Fset == nil {
8895
config.Fset = token.NewFileSet()
8996
}
@@ -99,7 +106,7 @@ func packagesLoadConfig(config Config, log LoggerCLI) *packages.Config {
99106
// Constant values, don't exposed
100107
Mode: packagesConfigMode,
101108
Logf: func(format string, args ...interface{}) {
102-
log.Debug(format, args...)
109+
config.Logger.Debug(format, args...)
103110
},
104111

105112
// Not used

config_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99

1010
func TestConfig(t *testing.T) {
1111
t.Run("If not provided, FileSet should be created", func(t *testing.T) {
12-
config := Config{Fset: nil}
13-
packagesConfig := packagesLoadConfig(config, loggerCLI.New(true, LogDebug|LogTrace))
12+
config := Config{Fset: nil, Logger: loggerCLI.New(true, LogDebug|LogTrace)}
13+
packagesConfig := packagesLoadConfig(config)
1414
if packagesConfig.Fset == nil {
1515
t.Fatal("Fset was expected to be not nil")
1616
}
1717
})
1818
t.Run("The PackagesMode should be equal to the defined constant", func(t *testing.T) {
19-
packagesConfig := packagesLoadConfig(Config{}, loggerCLI.New(true, LogDebug|LogTrace))
19+
packagesConfig := packagesLoadConfig(Config{Logger: loggerCLI.New(true, LogDebug|LogTrace)})
2020
if packagesConfig.Mode != packagesConfigMode {
2121
t.Fatal("The mode is not set correctly")
2222
}
@@ -37,14 +37,14 @@ func TestConfig(t *testing.T) {
3737
return mock
3838
}
3939

40-
packagesConfig := packagesLoadConfig(Config{}, mock)
40+
packagesConfig := packagesLoadConfig(Config{Logger: mock})
4141
packagesConfig.Logf(logMsg, logArgs...)
4242
if debugCalls != 1 {
4343
t.Fatalf("LogCLI Debug was expected to be called one time")
4444
}
4545
})
4646
t.Run("PackagesConfig Context, ParseFile and Overlay should be always nil", func(t *testing.T) {
47-
packagesConfig := packagesLoadConfig(Config{}, loggerCLI.New(true, LogDebug|LogTrace))
47+
packagesConfig := packagesLoadConfig(Config{Logger: loggerCLI.New(true, LogDebug|LogTrace)})
4848
if packagesConfig.Context != nil {
4949
t.Fatalf("Context was expected to be nil")
5050
}
@@ -62,8 +62,9 @@ func TestConfig(t *testing.T) {
6262
Env: []string{"a", "b", "c"},
6363
BuildFlags: []string{"1", "2", "3"},
6464
Fset: token.NewFileSet(),
65+
Logger: loggerCLI.New(true, LogDebug|LogTrace),
6566
}
66-
packagesConfig := packagesLoadConfig(config, loggerCLI.New(true, LogDebug|LogTrace))
67+
packagesConfig := packagesLoadConfig(config)
6768
if packagesConfig.Tests != config.Tests {
6869
t.Fatalf("PackagesConfig.Tests was expected to be equal to config.Tests")
6970
}

new.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ type GoParser struct {
5757
// might be matched by multiple patterns: in general it is not possible
5858
// to determine which packages correspond to which patterns.
5959
func NewGoParser(pattern string, config Config) (*GoParser, error) {
60-
logger := loggerCLI.New(config.LogFlags&LogJSON != 0, config.LogFlags&(^LogJSON))
60+
logger := config.Logger
61+
if logger == nil {
62+
logger = loggerCLI.New(config.LogFlags&LogJSON != 0, config.LogFlags&(^LogJSON))
63+
}
6164
printFinalConfig(pattern, config, logger)
62-
packagesLoadConfig := packagesLoadConfig(config, logger)
65+
66+
config.Logger = logger
67+
packagesLoadConfig := packagesLoadConfig(config)
6368
pkgs, e := packages.Load(packagesLoadConfig, pattern)
6469
if e != nil {
6570
return nil, e

new_test.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package parser
22

33
import (
4+
"github.com/mathbalduino/go-log/loggerCLI"
45
"go/token"
56
"testing"
67
)
@@ -24,7 +25,30 @@ func TestNewGoParser(t *testing.T) {
2425
t.Fatalf("GoParser expected to be not nil")
2526
}
2627
})
27-
t.Run("The returned GoParser pkgs, focus, log and fileSet should be filled", func(t *testing.T) {
28+
t.Run("The returned GoParser pkgs, focus, log (given by the user) and fileSet fields should be filled", func(t *testing.T) {
29+
logger := loggerCLI.New(false, 0)
30+
config := Config{Focus: &Focus{}, Fset: token.NewFileSet(), Logger: logger}
31+
p, _ := NewGoParser("--inexistentPackage--", config)
32+
if p == nil {
33+
t.Fatalf("GoParser expected to be not nil")
34+
}
35+
if p.pkgs == nil {
36+
t.Fatalf("GoParser.pkgs expected to be not nil")
37+
}
38+
if p.focus != config.Focus {
39+
t.Fatalf("GoParser.focus expected to be equal to config.Focus")
40+
}
41+
if p.logger == nil {
42+
t.Fatalf("GoParser.log expected to not be nil")
43+
}
44+
if p.logger != logger {
45+
t.Fatalf("GoParser.log expected to not be nil")
46+
}
47+
if p.fileSet != config.Fset {
48+
t.Fatalf("GoParser.fileSet expected to be equal to config.Fset")
49+
}
50+
})
51+
t.Run("The returned GoParser pkgs, focus, log (dynamically created) and fileSet fields should be filled", func(t *testing.T) {
2852
config := Config{Focus: &Focus{}, Fset: token.NewFileSet()}
2953
p, _ := NewGoParser("--inexistentPackage--", config)
3054
if p == nil {

printFinalConfig.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
// printFinalConfig will print, as a Debug log, the final configuration of the parser
9-
func printFinalConfig(pattern string, config Config, log LoggerCLI) {
9+
func printFinalConfig(pattern string, config Config, logger LoggerCLI) {
1010
focus := nilFocusStr
1111
if config.Focus != nil {
1212
packagePath, filePath, typeName := "nil", "nil", "nil"
@@ -33,8 +33,15 @@ func printFinalConfig(pattern string, config Config, log LoggerCLI) {
3333
dir = config.Dir
3434
}
3535

36-
logFlags := "-"
37-
if config.LogFlags != 0 {
36+
loggerMsg := nilLoggerStr
37+
if config.Logger != nil {
38+
loggerMsg = notNilLoggerStr
39+
}
40+
41+
logFlags := emptyLogFlagsStr
42+
if config.Logger != nil {
43+
logFlags = ignoredLogFlagsStr
44+
} else if config.LogFlags != 0 {
3845
if config.LogFlags&LogJSON != 0 {
3946
logFlags = "LogJSON | "
4047
}
@@ -59,14 +66,15 @@ func printFinalConfig(pattern string, config Config, log LoggerCLI) {
5966
logFlags = strings.TrimSuffix(logFlags, " | ")
6067
}
6168

62-
log.Debug(finalConfigTemplate,
69+
logger.Debug(finalConfigTemplate,
6370
pattern,
6471
config.Tests,
6572
dir,
6673
config.Env,
6774
config.BuildFlags,
6875
focus,
6976
fset,
77+
loggerMsg,
7078
logFlags,
7179
)
7280
}
@@ -75,6 +83,10 @@ const emptyDirStr = "./"
7583
const nilFsetStr = "Using the FileSet of the library"
7684
const notNilFsetStr = "Using the FileSet provided by the client"
7785
const nilFocusStr = "Focus not defined (will not skip anything)"
86+
const nilLoggerStr = "Logger not defined (will be created using LogFlags)"
87+
const notNilLoggerStr = "Using the Logger instance provided by the client"
88+
const emptyLogFlagsStr = "unset"
89+
const ignoredLogFlagsStr = "Ignored (Logger field is set)"
7890

7991
const finalConfigTemplate = `New GoParser created. Final configuration:
8092
Pattern: %s
@@ -85,6 +97,7 @@ Config: {
8597
BuildFlags: %v
8698
Focus: %s
8799
Fset: %s
100+
Logger: %s
88101
LogFlags: %s
89102
}`
90103

0 commit comments

Comments
 (0)