-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support to pretty print into a file
Sometimes like during Fuzz testing, stdout is supressed, make it difficult to debug.
- Loading branch information
Showing
6 changed files
with
147 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package pp | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
func init() { | ||
initDefaultWriter() | ||
} | ||
|
||
var defaultWriter io.Writer = os.Stderr | ||
|
||
func initDefaultWriter() { | ||
fpath, ok := os.LookupEnv("PP") | ||
if !ok { | ||
return | ||
} | ||
if fpath == "" { | ||
return | ||
} | ||
stat, err := os.Stat(fpath) | ||
if err != nil && !os.IsNotExist(err) { | ||
return | ||
} | ||
if stat != nil && stat.IsDir() { | ||
return | ||
} | ||
out, err := os.OpenFile(fpath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defaultWriter = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package pp | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"go.llib.dev/testcase/internal/env" | ||
) | ||
|
||
func Test_envPP(t *testing.T) { | ||
defer initDefaultWriter() | ||
|
||
t.Run("when not supplied", func(t *testing.T) { | ||
env.UnsetEnv(t, "PP") | ||
buf := stubDefaultWriter(t) | ||
initDefaultWriter() | ||
PP("OK") | ||
assertNotEmpty(t, buf.Bytes()) | ||
}) | ||
|
||
t.Run("when provided but empty", func(t *testing.T) { | ||
env.SetEnv(t, "PP", "") | ||
buf := stubDefaultWriter(t) | ||
initDefaultWriter() | ||
PP("OK") | ||
assertNotEmpty(t, buf.Bytes()) | ||
}) | ||
|
||
t.Run("when provided but not a valid file path", func(t *testing.T) { | ||
env.SetEnv(t, "PP", ".") | ||
buf := stubDefaultWriter(t) | ||
initDefaultWriter() | ||
PP("OK") | ||
assertNotEmpty(t, buf.Bytes()) | ||
}) | ||
|
||
t.Run("when existing file provided", func(t *testing.T) { | ||
f, err := os.CreateTemp(t.TempDir(), "") | ||
assertNoError(t, err) | ||
env.SetEnv(t, "PP", f.Name()) | ||
buf := stubDefaultWriter(t) | ||
initDefaultWriter() | ||
PP("OK") | ||
assertEmpty(t, buf.Bytes()) | ||
bs, err := io.ReadAll(f) | ||
assertNoError(t, err) | ||
assertNotEmpty(t, bs) | ||
}) | ||
|
||
t.Run("when non existing file provided", func(t *testing.T) { | ||
fpath := filepath.Join(t.TempDir(), "test.txt") | ||
env.SetEnv(t, "PP", fpath) | ||
buf := stubDefaultWriter(t) | ||
initDefaultWriter() | ||
PP("OK") | ||
assertEmpty(t, buf.Bytes()) | ||
bs, err := os.ReadFile(fpath) | ||
assertNoError(t, err) | ||
assertNotEmpty(t, bs) | ||
}) | ||
|
||
} |