1// Copyright (c) 2016 Uber Technologies, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21package zap
22
23import (
24	"flag"
25	"io/ioutil"
26	"testing"
27
28	"go.uber.org/zap/zapcore"
29
30	"github.com/stretchr/testify/assert"
31)
32
33type flagTestCase struct {
34	args      []string
35	wantLevel zapcore.Level
36	wantErr   bool
37}
38
39func (tc flagTestCase) runImplicitSet(t testing.TB) {
40	origCommandLine := flag.CommandLine
41	flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
42	flag.CommandLine.SetOutput(ioutil.Discard)
43	defer func() { flag.CommandLine = origCommandLine }()
44
45	level := LevelFlag("level", InfoLevel, "")
46	tc.run(t, flag.CommandLine, level)
47}
48
49func (tc flagTestCase) runExplicitSet(t testing.TB) {
50	var lvl zapcore.Level
51	set := flag.NewFlagSet("test", flag.ContinueOnError)
52	set.Var(&lvl, "level", "minimum enabled logging level")
53	tc.run(t, set, &lvl)
54}
55
56func (tc flagTestCase) run(t testing.TB, set *flag.FlagSet, actual *zapcore.Level) {
57	err := set.Parse(tc.args)
58	if tc.wantErr {
59		assert.Error(t, err, "Parse(%v) should fail.", tc.args)
60		return
61	}
62	if assert.NoError(t, err, "Parse(%v) should succeed.", tc.args) {
63		assert.Equal(t, tc.wantLevel, *actual, "Level mismatch.")
64	}
65}
66
67func TestLevelFlag(t *testing.T) {
68	tests := []flagTestCase{
69		{
70			args:      nil,
71			wantLevel: zapcore.InfoLevel,
72		},
73		{
74			args:    []string{"--level", "unknown"},
75			wantErr: true,
76		},
77		{
78			args:      []string{"--level", "error"},
79			wantLevel: zapcore.ErrorLevel,
80		},
81	}
82
83	for _, tt := range tests {
84		tt.runExplicitSet(t)
85		tt.runImplicitSet(t)
86	}
87}
88
89func TestLevelFlagsAreIndependent(t *testing.T) {
90	origCommandLine := flag.CommandLine
91	flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
92	flag.CommandLine.SetOutput(ioutil.Discard)
93	defer func() { flag.CommandLine = origCommandLine }()
94
95	// Make sure that these two flags are independent.
96	fileLevel := LevelFlag("file-level", InfoLevel, "")
97	consoleLevel := LevelFlag("console-level", InfoLevel, "")
98
99	assert.NoError(t, flag.CommandLine.Parse([]string{"-file-level", "debug"}), "Unexpected flag-parsing error.")
100	assert.Equal(t, InfoLevel, *consoleLevel, "Expected file logging level to remain unchanged.")
101	assert.Equal(t, DebugLevel, *fileLevel, "Expected console logging level to have changed.")
102}
103