-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add debug flag and logging to scan (#278)
* Add debug flag and logging to scan * Add negative test
- Loading branch information
1 parent
c561944
commit 521aa21
Showing
4 changed files
with
70 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package debug | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
func Log(message string, debug bool) { | ||
if debug { | ||
DebugLogger := log.New(os.Stderr, "DEBUG: ", log.Ldate|log.Ltime) | ||
DebugLogger.Println(color.BlueString(message)) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package debug | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLog(t *testing.T) { | ||
rescueStderr := os.Stderr | ||
r, w, _ := os.Pipe() | ||
os.Stderr = w | ||
|
||
Log("hello", true) | ||
|
||
_ = w.Close() | ||
output, _ := io.ReadAll(r) | ||
os.Stderr = rescueStderr | ||
|
||
assert.Contains(t, string(output), "DEBUG: ") | ||
assert.Contains(t, string(output), "hello\n") | ||
} | ||
|
||
func TestLogDebugDisabled(t *testing.T) { | ||
rescueStderr := os.Stderr | ||
r, w, _ := os.Pipe() | ||
os.Stderr = w | ||
|
||
Log("hello", false) | ||
|
||
_ = w.Close() | ||
output, _ := io.ReadAll(r) | ||
os.Stderr = rescueStderr | ||
|
||
assert.Empty(t, string(output)) | ||
} |
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