github.com/jfrog/jfrog-cli-core/v2@v2.51.0/common/progressbar/filesprogressbar_test.go (about)

     1  package progressbar
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/jfrog/jfrog-cli-core/v2/utils/progressbar"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  const terminalWidth = 100
    11  
    12  func TestBuildProgressDescription(t *testing.T) {
    13  	// Set an arbitrary terminal width
    14  	for _, test := range getTestCases() {
    15  		t.Run(test.name, func(t *testing.T) {
    16  			assert.Equal(t, test.expectedDesc, buildProgressDescription(test.prefix, test.path, terminalWidth, test.extraCharsLen))
    17  		})
    18  	}
    19  }
    20  
    21  func getTestCases() []testCase {
    22  	prefix := "  downloading"
    23  	path := "/a/path/to/a/file"
    24  	separator := " | "
    25  
    26  	fullDesc := " " + prefix + separator + path + separator
    27  	emptyPathDesc := " " + prefix + separator + "..." + separator
    28  	shortenedDesc := " " + prefix + separator + "...ggggg/path/to/a/file" + separator
    29  
    30  	widthMinusProgress := terminalWidth - progressbar.ProgressBarWidth*2
    31  	return []testCase{
    32  		{"commonUseCase", prefix, path, 17, fullDesc},
    33  		{"zeroExtraChars", prefix, path, 0, fullDesc},
    34  		{"minDescLength", prefix, path, widthMinusProgress - len(emptyPathDesc), emptyPathDesc},
    35  		{"longPath", prefix, "/a/longggggggggggggggggggggg/path/to/a/file", 17, shortenedDesc},
    36  		{"longPrefix", "longggggggggggggggggggggggggg prefix", path, 17, ""},
    37  		{"manyExtraChars", prefix, path, widthMinusProgress - len(emptyPathDesc) + 1, ""},
    38  	}
    39  }
    40  
    41  type testCase struct {
    42  	name          string
    43  	prefix        string
    44  	path          string
    45  	extraCharsLen int
    46  	expectedDesc  string
    47  }