Skip to content

Commit

Permalink
simplifying the testing code. will introduce tapper only when used at…
Browse files Browse the repository at this point in the history
… many places
  • Loading branch information
Amrit Singh committed Oct 4, 2024
1 parent 2d9bf36 commit d1fdfab
Showing 1 changed file with 11 additions and 53 deletions.
64 changes: 11 additions & 53 deletions internal/password/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ func TestGenerate(t *testing.T) {

func TestWrite(t *testing.T) {
t.Run("Must write to stdout when asked to do so", func(t *testing.T) {
tapper := tapStdOut{}
tapper.Start()
// setup for tapping into os.Stdout
rf, wf, err := os.Pipe()
assert.NoError(t, err)
backUp := os.Stdout
os.Stdout = wf
var outbuff bytes.Buffer
passwordLength := 40
count := 1
options := Options{
Expand All @@ -109,10 +113,13 @@ func TestWrite(t *testing.T) {
passwords, err := Generate(&options)
assert.NoError(t, err)
Write(passwords, &options)
output, err := tapper.Flush()
err = wf.Close()
os.Stdout = backUp
assert.NoError(t, err)
_, err = io.Copy(&outbuff, rf)
assert.NoError(t, err)
msg := fmt.Sprintf("Expected password to be of length %d", passwordLength)
assert.Equal(t, passwordLength, len(output), msg)
assert.Equal(t, passwordLength, len(outbuff.String()), msg)
})

t.Run("Must write to file when asked to do so", func(t *testing.T) {
Expand Down Expand Up @@ -166,52 +173,3 @@ func TestWrite(t *testing.T) {
})

}

// tapStdOut provides mechanism to tap into the stdout
// useful for testing only
type tapStdOut struct {
outChan chan string
errChan chan error

writeTo *os.File
stdOutbackup *os.File
}

// Start starts the tapping process and backsup stdout
func (tapper *tapStdOut) Start() error {
tapper.stdOutbackup = os.Stdout
rf, wf, err := os.Pipe()
if err != nil {
return err
}
os.Stdout = wf
tapper.writeTo = wf
tapper.outChan = make(chan string)
tapper.errChan = make(chan error)
go tapper.read(rf)
return nil
}

// read reads from readpipe into channel of tapper
// must be called in a go routine to prevent
// blocking writes to writepipe (such as stdout)
func (tapper *tapStdOut) read(readFrom *os.File) {
var output bytes.Buffer
_, err := io.Copy(&output, readFrom)
tapper.errChan <- err
tapper.outChan <- output.String()
}

// Flush Stops the tapping process, sends the stored output and restores stdout
func (tapper *tapStdOut) Flush() (string, error) {
err := tapper.writeTo.Close()
if err != nil {
return "", err
}
err = <-tapper.errChan
if err != nil {
return "", err
}
os.Stdout = tapper.stdOutbackup
return <-tapper.outChan, nil
}

0 comments on commit d1fdfab

Please sign in to comment.