flag_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. package zap
  21. import (
  22. "flag"
  23. "io/ioutil"
  24. "testing"
  25. "go.uber.org/zap/zapcore"
  26. "github.com/stretchr/testify/assert"
  27. )
  28. type flagTestCase struct {
  29. args []string
  30. wantLevel zapcore.Level
  31. wantErr bool
  32. }
  33. func (tc flagTestCase) runImplicitSet(t testing.TB) {
  34. origCommandLine := flag.CommandLine
  35. flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
  36. flag.CommandLine.SetOutput(ioutil.Discard)
  37. defer func() { flag.CommandLine = origCommandLine }()
  38. level := LevelFlag("level", InfoLevel, "")
  39. tc.run(t, flag.CommandLine, level)
  40. }
  41. func (tc flagTestCase) runExplicitSet(t testing.TB) {
  42. var lvl zapcore.Level
  43. set := flag.NewFlagSet("test", flag.ContinueOnError)
  44. set.Var(&lvl, "level", "minimum enabled logging level")
  45. tc.run(t, set, &lvl)
  46. }
  47. func (tc flagTestCase) run(t testing.TB, set *flag.FlagSet, actual *zapcore.Level) {
  48. err := set.Parse(tc.args)
  49. if tc.wantErr {
  50. assert.Error(t, err, "Parse(%v) should fail.", tc.args)
  51. return
  52. }
  53. if assert.NoError(t, err, "Parse(%v) should succeed.", tc.args) {
  54. assert.Equal(t, tc.wantLevel, *actual, "Level mismatch.")
  55. }
  56. }
  57. func TestLevelFlag(t *testing.T) {
  58. tests := []flagTestCase{
  59. {
  60. args: nil,
  61. wantLevel: zapcore.InfoLevel,
  62. },
  63. {
  64. args: []string{"--level", "unknown"},
  65. wantErr: true,
  66. },
  67. {
  68. args: []string{"--level", "error"},
  69. wantLevel: zapcore.ErrorLevel,
  70. },
  71. }
  72. for _, tt := range tests {
  73. tt.runExplicitSet(t)
  74. tt.runImplicitSet(t)
  75. }
  76. }
  77. func TestLevelFlagsAreIndependent(t *testing.T) {
  78. origCommandLine := flag.CommandLine
  79. flag.CommandLine = flag.NewFlagSet("test", flag.ContinueOnError)
  80. flag.CommandLine.SetOutput(ioutil.Discard)
  81. defer func() { flag.CommandLine = origCommandLine }()
  82. // Make sure that these two flags are independent.
  83. fileLevel := LevelFlag("file-level", InfoLevel, "")
  84. consoleLevel := LevelFlag("console-level", InfoLevel, "")
  85. assert.NoError(t, flag.CommandLine.Parse([]string{"-file-level", "debug"}), "Unexpected flag-parsing error.")
  86. assert.Equal(t, InfoLevel, *consoleLevel, "Expected file logging level to remain unchanged.")
  87. assert.Equal(t, DebugLevel, *fileLevel, "Expected console logging level to have changed.")
  88. }