Skip to content

Commit

Permalink
Silence log output when not verbose
Browse files Browse the repository at this point in the history
Set the default log package output to iotuil.Discard during tests if the
`-v` flag isn't set. If we are verbose, then apply the filter according
to the TF_LOG env variable.
  • Loading branch information
jbardin committed Aug 1, 2016
1 parent 8c4b4ed commit 1af7ee8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
16 changes: 16 additions & 0 deletions command/command_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package command

import (
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"

"github.com/hashicorp/go-getter"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/helper/logging"
"github.com/hashicorp/terraform/terraform"
)

Expand All @@ -27,6 +30,19 @@ func init() {
}
}

func TestMain(m *testing.M) {
flag.Parse()
if testing.Verbose() {
// if we're verbose, use the logging requested by TF_LOG
logging.SetOutput()
} else {
// otherwise silence all logs
log.SetOutput(ioutil.Discard)
}

os.Exit(m.Run())
}

func tempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "tf")
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package config

import (
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/logging"
)

// This is the directory where our test fixtures are.
const fixtureDir = "./test-fixtures"

func TestMain(m *testing.M) {
flag.Parse()
if testing.Verbose() {
// if we're verbose, use the logging requested by TF_LOG
logging.SetOutput()
} else {
// otherwise silence all logs
log.SetOutput(ioutil.Discard)
}

os.Exit(m.Run())
}

func TestConfigCopy(t *testing.T) {
c := testConfig(t, "copy-basic")
rOrig := c.Resources[0]
Expand Down
19 changes: 19 additions & 0 deletions dag/dag_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package dag

import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
"strings"
"sync"
"testing"

"github.com/hashicorp/terraform/helper/logging"
)

func TestMain(m *testing.M) {
flag.Parse()
if testing.Verbose() {
// if we're verbose, use the logging requested by TF_LOG
logging.SetOutput()
} else {
// otherwise silence all logs
log.SetOutput(ioutil.Discard)
}

os.Exit(m.Run())
}

func TestAcyclicGraphRoot(t *testing.T) {
var g AcyclicGraph
g.Add(1)
Expand Down
16 changes: 16 additions & 0 deletions helper/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ func LogOutput() (logOutput io.Writer, err error) {
return
}

// SetOutput checks for a log destination with LogOutput, and calls
// log.SetOutput with the result. If LogOutput returns nil, SetOutput uses
// ioutil.Discard. Any error from LogOutout is fatal.
func SetOutput() {
out, err := LogOutput()
if err != nil {
log.Fatal(err)
}

if out == nil {
out = ioutil.Discard
}

log.SetOutput(out)
}

// LogLevel returns the current log level string based the environment vars
func LogLevel() string {
envLevel := os.Getenv(EnvLog)
Expand Down
16 changes: 16 additions & 0 deletions terraform/terraform_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package terraform

import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -13,11 +15,25 @@ import (
"github.com/hashicorp/go-getter"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/helper/logging"
)

// This is the directory where our test fixtures are.
const fixtureDir = "./test-fixtures"

func TestMain(m *testing.M) {
flag.Parse()
if testing.Verbose() {
// if we're verbose, use the logging requested by TF_LOG
logging.SetOutput()
} else {
// otherwise silence all logs
log.SetOutput(ioutil.Discard)
}

os.Exit(m.Run())
}

func tempDir(t *testing.T) string {
dir, err := ioutil.TempDir("", "tf")
if err != nil {
Expand Down

0 comments on commit 1af7ee8

Please sign in to comment.