github.com/thediveo/gons@v0.9.9/reexec/testing/pritipratel_test.go (about)

     1  // Copyright 2019 Harald Albrecht.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testing
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"strings"
    22  
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  )
    26  
    27  // While running function f(), captures f's output to stderr.and returns it.
    28  func capturestdout(f func()) (stderr string) {
    29  	origStderr := os.Stderr
    30  	r, w, _ := os.Pipe()
    31  	defer func() {
    32  		os.Stderr = origStderr
    33  		r.Close()
    34  		w.Close()
    35  	}()
    36  	os.Stderr = w
    37  	// Run function f() only on this thread, as Gomego doesn't like it
    38  	// otherwise. So we have to run the Stdout replacement pipe reader on a
    39  	// separate go routine. When it has read all that was in the pipe, it will
    40  	// set the return value and signal that it's done.
    41  	done := make(chan struct{})
    42  	go func() {
    43  		b, _ := ioutil.ReadAll(r)
    44  		stderr = string(b)
    45  		close(done)
    46  	}()
    47  	f()
    48  	// Shut down the writer end, so the pipe reader knows that capturing
    49  	// stderr is finished, and can retrieve the complete captured output. We
    50  	// wait for the pipe reader to be finally done before returning.
    51  	w.Close()
    52  	<-done
    53  	return
    54  }
    55  
    56  var _ = Describe("stderr processing", func() {
    57  
    58  	It("passes test harness self-test", func() {
    59  		Expect(capturestdout(func() { fmt.Fprint(os.Stderr, "foo") })).To(Equal("foo"))
    60  	})
    61  
    62  	It("correctly passes on normal output", func() {
    63  		Expect(capturestdout(func() {
    64  			pritiPratel(func() {
    65  				fmt.Fprint(os.Stderr, "some test")
    66  			})
    67  		})).To(Equal("some test"))
    68  		Expect(capturestdout(func() {
    69  			pritiPratel(func() {
    70  				fmt.Fprint(os.Stderr, "coverage is meh\ntest\n")
    71  			})
    72  		})).To(Equal("coverage is meh\ntest\n"))
    73  		long := strings.Repeat("abc", 1024)
    74  		Expect(capturestdout(func() {
    75  			pritiPratel(func() {
    76  				fmt.Fprint(os.Stderr, long+"\ntest\n")
    77  			})
    78  		})).To(Equal(long + "\ntest\n"))
    79  	})
    80  
    81  	It("hides unwanted truths about coverage: and testing:", func() {
    82  		Expect(capturestdout(func() {
    83  			pritiPratel(func() {
    84  				fmt.Fprint(os.Stderr, "some test\ncoverage: foo\nbar\ntesting: foo\nbar")
    85  			})
    86  		})).To(Equal("some test\nbar\nbar"))
    87  		long := strings.Repeat("abc", 1024)
    88  		Expect(capturestdout(func() {
    89  			pritiPratel(func() {
    90  				fmt.Fprint(os.Stderr, "some test\ncoverage: "+long+"\nbar\ntesting: foo\nbar")
    91  			})
    92  		})).To(Equal("some test\nbar\nbar"))
    93  	})
    94  
    95  })