-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logging): standardise log and output messages
Add leveled logging across most key components. Add common flags (--verbose --logLevel( to CLI and arguments to the Pact DSL to control log level.
- Loading branch information
Showing
13 changed files
with
206 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,62 @@ | ||
package command | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func init() { | ||
// Set CLI flags to simulate real | ||
os.Args = []string{"version"} | ||
} | ||
|
||
func Test_RootCommand(t *testing.T) { | ||
os.Args = []string{"version"} | ||
err := RootCmd.Execute() | ||
if err != nil { | ||
t.Fatalf("Error: %v", err) | ||
} | ||
|
||
Execute() | ||
} | ||
|
||
func Test_LogLevel(t *testing.T) { | ||
res := captureOutput(func() { | ||
setLogLevel(true, "DEBUG") | ||
log.Println("[DEBUG] this should display") | ||
}) | ||
|
||
if !strings.Contains(res, "[DEBUG] this should display") { | ||
t.Fatalf("Expected log message to contain '[DEBUG] this should display' but got '%s'", res) | ||
} | ||
|
||
res = captureOutput(func() { | ||
setLogLevel(true, "INFO") | ||
log.Print("[DEBUG] this should not display") | ||
}) | ||
|
||
if res != "" { | ||
t.Fatalf("Expected log message to be empty but got '%s'", res) | ||
} | ||
|
||
res = captureOutput(func() { | ||
setLogLevel(false, "INFO") | ||
log.Print("[DEBUG] this should not display") | ||
}) | ||
|
||
if res != "" { | ||
t.Fatalf("Expected log message to be empty but got '%s'", res) | ||
} | ||
} | ||
|
||
func captureOutput(action func()) string { | ||
rescueStderr := os.Stderr | ||
r, w, _ := os.Pipe() | ||
os.Stderr = w | ||
|
||
action() | ||
|
||
w.Close() | ||
out, _ := ioutil.ReadAll(r) | ||
os.Stderr = rescueStderr | ||
|
||
return strings.TrimSpace(string(out)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.