github.com/jfrog/build-info-go@v1.9.26/utils/pythonutils/utils_test.go (about) 1 package pythonutils 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 gofrogcmd "github.com/jfrog/gofrog/io" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestGetMultilineCaptureOutputPattern(t *testing.T) { 13 tests := []struct { 14 name string 15 text string 16 startCapturePattern string 17 captureGroupPattern string 18 endCapturePattern string 19 expectedCapture string 20 }{ 21 { 22 name: "Using cached - single line captures", 23 startCapturePattern: startUsingCachedPattern, 24 captureGroupPattern: usingCacheCaptureGroup, 25 endCapturePattern: endPattern, 26 text: ` 27 Looking in indexes: 28 ***localhost:8081/artifactory/api/pypi/cli-pipenv-pypi-virtual-1698829624/simple 29 30 Collecting pexpect==4.8.0 (from -r /tmp/pipenv-qzun2hd3-requirements/pipenv-o_899oue-hashed-reqs.txt (line 1)) 31 32 Using cached http://localhost:8081/artifactory/api/pypi/cli-pipenv-pypi-virtual-1698829624/packages/packages/39/7b/88dbb785881c28a102619d46423cb853b46dbccc70d3ac362d99773a78ce/pexpect-4.8.0-py2.py3-none-any.whl (59 kB)`, 33 expectedCapture: `pexpect-4.8.0-py2.py3-none-any.whl`, 34 }, 35 { 36 name: "Using cached - multi line captures", 37 startCapturePattern: startUsingCachedPattern, 38 captureGroupPattern: usingCacheCaptureGroup, 39 endCapturePattern: endPattern, 40 text: ` 41 Looking in indexes: 42 ***localhost:8081/artifactory/api/pypi/cli-pipenv-pypi-virtual-16 43 98829624/simple 44 45 Collecting pexpect==4.8.0 (from -r 46 /tmp/pipenv-qzun2hd3-requirements/pipenv-o_899oue-hashed-reqs.txt (line 1)) 47 48 Using cached 49 http://localhost:8081/artifactory/api/pypi/cli-pipenv-pypi-virtual-1698829624/pa 50 ckages/packages/39/7b/88dbb785881c28a102619d46423cb853b46dbccc70d3ac362d99773a78 51 ce/pexpect-4.8.0-py2.py3-none-any.whl (59 kB)`, 52 expectedCapture: `pexpect-4.8.0-py2.py3-none-any.whl`, 53 }, 54 { 55 name: "Downloading - single line captures", 56 startCapturePattern: startDownloadingPattern, 57 captureGroupPattern: downloadingCaptureGroup, 58 endCapturePattern: endPattern, 59 text: ` Preparing metadata (pyproject.toml): finished with status 'done' 60 Collecting PyYAML==5.1.2 (from jfrog-python-example==1.0) 61 Downloading http://localhost:8081/artifactory/api/pypi/cli-pypi-virtual-1698829558/packages/packages/e3/e8/b3212641ee2718d556df0f23f78de8303f068fe29cdaa7a91018849582fe/PyYAML-5.1.2.tar.gz (265 kB) 62 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 265.0/265.0 kB 364.4 MB/s eta 0:00:00 63 Installing build dependencies: started`, 64 expectedCapture: `PyYAML-5.1.2.tar.gz`, 65 }, 66 { 67 name: "Downloading - multi line captures", 68 startCapturePattern: startDownloadingPattern, 69 captureGroupPattern: downloadingCaptureGroup, 70 endCapturePattern: endPattern, 71 text: ` Preparing metadata (pyproject.toml): finished with status 'done' 72 Collecting PyYAML==5.1.2 (from jfrog-python-example==1.0) 73 Downloading http://localhost:8081/artifactory/api/pypi/cli-pypi-virtual-1698 74 829558/packages/packages/e3/e8/b3212641ee2718d556df0f23f78de8303f068fe29cdaa7a91018849 75 582fe/PyYAML-5.1.2.tar.gz (265 kB) 76 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 265.0/265.0 kB 364.4 MB/s eta 0:00:00 77 Installing build dependencies: started`, 78 expectedCapture: `PyYAML-5.1.2.tar.gz`, 79 }, 80 { 81 name: "Downloading - multi line captures", 82 startCapturePattern: startDownloadingPattern, 83 captureGroupPattern: downloadingCaptureGroup, 84 endCapturePattern: endPattern, 85 text: ` Preparing metadata (pyproject.toml): finished with status 'done' 86 Collecting PyYAML==5.1.2 (from jfrog-python-example==1.0) 87 Downloading http://localhost:8081/artifactory/api/pypi/cli-pypi-virtual-1698 88 829558/packages/packages/e3/e8/b3212641ee2718d556df0f23f78de8303f068fe29cdaa7a91018849 89 582fe/PyYAML-5.1.2%2Bsp1.tar.gz (265 kB) 90 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 265.0/265.0 kB 364.4 MB/s eta 0:00:00 91 Installing build dependencies: started`, 92 expectedCapture: `PyYAML-5.1.2+sp1.tar.gz`, 93 }, 94 } 95 96 for _, testCase := range tests { 97 t.Run(testCase.name, func(t *testing.T) { 98 aggFunc, captures := getCapturesFromTest(testCase.expectedCapture) 99 runDummyTextStream(t, testCase.text, getMultilineSplitCaptureOutputPattern( 100 testCase.startCapturePattern, 101 testCase.captureGroupPattern, 102 testCase.endCapturePattern, 103 aggFunc, 104 )) 105 assert.Len(t, (*captures), 1, fmt.Sprintf("Expected 1 captured group, got size: %d", len(*captures))) 106 assert.Equal(t, testCase.expectedCapture, (*captures)[0], fmt.Sprintf("Expected capture group: %s, got: %s", testCase.expectedCapture, (*captures)[0])) 107 }) 108 } 109 } 110 111 func getCapturesFromTest(expectedCaptures ...string) (func(pattern *gofrogcmd.CmdOutputPattern) (string, error), *[]string) { 112 captures := []string{} 113 aggFunc := func(pattern *gofrogcmd.CmdOutputPattern) (string, error) { 114 captured := extractFileNameFromRegexCaptureGroup(pattern) 115 for _, expectedCapture := range expectedCaptures { 116 if expectedCapture == captured { 117 captures = append(captures, expectedCapture) 118 } 119 } 120 return pattern.Line, nil 121 } 122 return aggFunc, &captures 123 } 124 125 func runDummyTextStream(t *testing.T, txt string, parsers []*gofrogcmd.CmdOutputPattern) { 126 // tokenize the text to be represented line by line to simulate expected cmd log output 127 lines := strings.Split(txt, "\n") 128 // iterate over the lines to simulate line text stream 129 for _, line := range lines { 130 for _, parser := range parsers { 131 // check if the line matches the regexp of the parser 132 if parser.RegExp.MatchString(line) { 133 parser.MatchedResults = parser.RegExp.FindStringSubmatch(line) 134 parser.Line = line 135 // execute the parser function 136 _, scannerError := parser.ExecFunc(parser) 137 assert.NoError(t, scannerError) 138 } 139 } 140 } 141 }