github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/cf/net/progress_reader_test.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package net_test
     5  
     6  import (
     7  	"os"
     8  	"time"
     9  
    10  	. "code.cloudfoundry.org/cli/cf/net"
    11  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("ProgressReader", func() {
    17  	var (
    18  		testFile       *os.File
    19  		err            error
    20  		progressReader *ProgressReader
    21  		ui             *terminalfakes.FakeUI
    22  		b              []byte
    23  		fileStat       os.FileInfo
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		ui = new(terminalfakes.FakeUI)
    28  
    29  		testFile, err = os.Open("../../fixtures/test.file")
    30  		Expect(err).NotTo(HaveOccurred())
    31  
    32  		fileStat, err = testFile.Stat()
    33  		Expect(err).NotTo(HaveOccurred())
    34  
    35  		b = make([]byte, 1024)
    36  		progressReader = NewProgressReader(testFile, ui, 1*time.Millisecond)
    37  		progressReader.SetTotalSize(fileStat.Size())
    38  	})
    39  
    40  	It("prints progress while content is being read", func() {
    41  		for {
    42  			time.Sleep(50 * time.Microsecond)
    43  			_, err := progressReader.Read(b)
    44  			if err != nil {
    45  				break
    46  			}
    47  		}
    48  
    49  		Expect(ui.SayCallCount()).To(Equal(1))
    50  		Expect(ui.SayArgsForCall(0)).To(ContainSubstring("\rDone "))
    51  
    52  		Expect(ui.PrintCapturingNoOutputCallCount()).To(BeNumerically(">", 0))
    53  		status, _ := ui.PrintCapturingNoOutputArgsForCall(0)
    54  		Expect(status).To(ContainSubstring("uploaded..."))
    55  		status, _ = ui.PrintCapturingNoOutputArgsForCall(ui.PrintCapturingNoOutputCallCount() - 1)
    56  		Expect(status).To(Equal("\r                             "))
    57  	})
    58  
    59  	It("reads the correct number of bytes", func() {
    60  		bytesRead := 0
    61  
    62  		for {
    63  			n, err := progressReader.Read(b)
    64  			if err != nil {
    65  				break
    66  			}
    67  
    68  			bytesRead += n
    69  		}
    70  
    71  		Expect(int64(bytesRead)).To(Equal(fileStat.Size()))
    72  	})
    73  })