func TestTraceSetToFile(t *testing.T) { stdOut := bytes.NewBuffer([]byte{}) trace.SetStdout(stdOut) fileutils.TempFile("trace_test", func(file *os.File, err error) { assert.NoError(t, err) file.Write([]byte("pre-existing content")) os.Setenv(trace.CF_TRACE, file.Name()) logger := trace.NewLogger() logger.Print("hello world") file.Seek(0, os.SEEK_SET) result, err := ioutil.ReadAll(file) assert.NoError(t, err) byteString := string(result) assert.Contains(t, byteString, "pre-existing content") assert.Contains(t, byteString, "hello world") result, _ = ioutil.ReadAll(stdOut) assert.Equal(t, string(result), "") }) }
func TestGetRequestWithVerboseFlagEnablesTrace(t *testing.T) { deps := newCurlDependencies() output := bytes.NewBuffer(make([]byte, 1024)) trace.SetStdout(output) runCurlWithInputs(deps, []string{"-v", "/foo"}) trace.Logger.Print("logging enabled") assert.Contains(t, output.String(), "logging enabled") }
func TestTraceSetToTrue(t *testing.T) { stdOut := bytes.NewBuffer([]byte{}) trace.SetStdout(stdOut) os.Setenv(trace.CF_TRACE, "true") logger := trace.NewLogger() logger.Print("hello world") result, _ := ioutil.ReadAll(stdOut) assert.Contains(t, string(result), "hello world") }
func TestTraceSetToInvalidFile(t *testing.T) { stdOut := bytes.NewBuffer([]byte{}) trace.SetStdout(stdOut) fileutils.TempFile("trace_test", func(file *os.File, err error) { assert.NoError(t, err) file.Chmod(0000) os.Setenv(trace.CF_TRACE, file.Name()) logger := trace.NewLogger() logger.Print("hello world") result, _ := ioutil.ReadAll(file) assert.Equal(t, string(result), "") result, _ = ioutil.ReadAll(stdOut) assert.Contains(t, string(result), "hello world") }) }
import ( "bytes" "cf/trace" "github.com/cloudfoundry/gofileutils/fileutils" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "io/ioutil" "os" "runtime" ) var _ = Describe("Testing with ginkgo", func() { It("TestTraceSetToFalse", func() { stdOut := bytes.NewBuffer([]byte{}) trace.SetStdout(stdOut) os.Setenv(trace.CF_TRACE, "false") logger := trace.NewLogger() logger.Print("hello world") result, _ := ioutil.ReadAll(stdOut) Expect(string(result)).To(Equal("")) }) It("TestTraceSetToTrue", func() { stdOut := bytes.NewBuffer([]byte{}) trace.SetStdout(stdOut) os.Setenv(trace.CF_TRACE, "true")
ui := &testterm.FakeUI{} config := testconfig.NewRepository() manifestRepo := &testmanifest.FakeManifestRepository{} repoLocator := api.NewRepositoryLocator(config, map[string]net.Gateway{ "auth": net.NewUAAGateway(), "cloud-controller": net.NewCloudControllerGateway(), "uaa": net.NewUAAGateway(), }) cmdFactory := commands.NewFactory(ui, config, manifestRepo, repoLocator) cmdRunner := &FakeRunner{cmdFactory: cmdFactory} for _, cmdName := range expectedCommandNames { output := bytes.NewBuffer(make([]byte, 1024)) trace.SetStdout(output) trace.EnableTrace() app, err := NewApp(cmdRunner) Expect(err).NotTo(HaveOccurred()) Expect(output.String()).To(ContainSubstring("VERSION:\n" + cf.Version)) app.Run([]string{"", cmdName}) Expect(cmdRunner.cmdName).To(Equal(cmdName)) } }) }) type FakeRunner struct { cmdFactory commands.Factory