Skip to content

Commit cb239c9

Browse files
authored
Treat xcresulttool stdout and stderr separately (#248)
* Treat xcresulttool stdout and stderr separatly * Print the first lines of xcresulttool to the build log when parsing fails * Print first 10 lines * Fix failing curl command in e2e tests * Fix all curl commands * Fix curl commands
1 parent 92b3f4e commit cb239c9

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

bitrise.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ workflows:
370370
if [ -z "${BITRISE_PERMANENT_DOWNLOAD_URL_MAP}" ]; then
371371
echo "BITRISE_PERMANENT_DOWNLOAD_URL_MAP is empty"
372372
exit 1
373-
elif ! curl --head --silent --fail "${BITRISE_PERMANENT_DOWNLOAD_URL_MAP}" > /dev/null; then
373+
elif ! curl --head --silent --fail -H "Accept: text/html" "${BITRISE_PERMANENT_DOWNLOAD_URL_MAP}" > /dev/null; then
374374
echo "BITRISE_PERMANENT_DOWNLOAD_URL_MAP is invalid"
375375
exit 1
376376
fi
@@ -395,7 +395,7 @@ step_bundles:
395395
if [ -z "${BITRISE_PUBLIC_INSTALL_PAGE_URL}" ]; then
396396
echo "BITRISE_PUBLIC_INSTALL_PAGE_URL is empty"
397397
exit 1
398-
elif ! curl --head --silent --fail "${BITRISE_PUBLIC_INSTALL_PAGE_URL}" > /dev/null; then
398+
elif ! curl --head --silent --fail -H "Accept: text/html" "${BITRISE_PUBLIC_INSTALL_PAGE_URL}" > /dev/null; then
399399
echo "BITRISE_PUBLIC_INSTALL_PAGE_URL is invalid"
400400
exit 1
401401
fi
@@ -404,7 +404,7 @@ step_bundles:
404404
if [ -z "${BITRISE_PERMANENT_DOWNLOAD_URL_MAP}" ]; then
405405
echo "BITRISE_PERMANENT_DOWNLOAD_URL_MAP is empty"
406406
exit 1
407-
elif ! curl --head --silent --fail "${BITRISE_PERMANENT_DOWNLOAD_URL_MAP}" > /dev/null; then
407+
elif ! curl --head --silent --fail -H "Accept: text/html" "${BITRISE_PERMANENT_DOWNLOAD_URL_MAP}" > /dev/null; then
408408
echo "BITRISE_PERMANENT_DOWNLOAD_URL_MAP is invalid"
409409
exit 1
410410
fi
@@ -413,7 +413,7 @@ step_bundles:
413413
if [ -z "${BITRISE_ARTIFACT_DETAILS_PAGE_URL}" ]; then
414414
echo "BITRISE_ARTIFACT_DETAILS_PAGE_URL is empty"
415415
exit 1
416-
elif ! curl --head --silent --fail "${BITRISE_ARTIFACT_DETAILS_PAGE_URL}" > /dev/null; then
416+
elif ! curl --head --silent --fail -H "Accept: text/html" "${BITRISE_ARTIFACT_DETAILS_PAGE_URL}" > /dev/null; then
417417
echo "BITRISE_ARTIFACT_DETAILS_PAGE_URL is invalid"
418418
exit 1
419419
fi

test/converters/xcresult3/xcresulttool.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package xcresult3
22

33
import (
4+
"bufio"
5+
"bytes"
46
"encoding/json"
57
"fmt"
8+
"io"
9+
"os"
610
"os/exec"
711
"regexp"
812
"strconv"
13+
"strings"
914

1015
"github.com/bitrise-io/go-utils/command"
1116
"github.com/bitrise-io/go-utils/errorutil"
17+
command2 "github.com/bitrise-io/go-utils/v2/command"
18+
"github.com/bitrise-io/go-utils/v2/env"
19+
"github.com/bitrise-io/go-utils/v2/log"
1220
)
1321

1422
func isXcresulttoolAvailable() bool {
@@ -64,6 +72,9 @@ func supportsNewExtractionMethods() (bool, error) {
6472

6573
// xcresulttoolGet performs xcrun xcresulttool get with --id flag defined if id provided and marshals the output into v.
6674
func xcresulttoolGet(xcresultPth, id string, useLegacyFlag bool, v interface{}) error {
75+
commandFactory := command2.NewFactory(env.NewRepository())
76+
logger := log.NewLogger()
77+
6778
args := []string{"xcresulttool", "get"}
6879

6980
supportsNewMethod, err := supportsNewExtractionMethods()
@@ -87,20 +98,52 @@ func xcresulttoolGet(xcresultPth, id string, useLegacyFlag bool, v interface{})
8798
args = append(args, "--id", id)
8899
}
89100

90-
cmd := command.New("xcrun", args...)
91-
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
92-
if err != nil {
101+
var outBuffer, errBuffer, combinedBuffer bytes.Buffer
102+
outWriter := io.MultiWriter(&outBuffer, &combinedBuffer)
103+
errWriter := io.MultiWriter(&errBuffer, &combinedBuffer)
104+
105+
cmd := commandFactory.Create("xcrun", args, &command2.Opts{
106+
Stdout: outWriter,
107+
Stderr: errWriter,
108+
Stdin: nil,
109+
Env: os.Environ(),
110+
Dir: "",
111+
ErrorFinder: nil,
112+
})
113+
if err := cmd.Run(); err != nil {
93114
if errorutil.IsExitStatusError(err) {
94-
return fmt.Errorf("%s failed: %s", cmd.PrintableCommandArgs(), out)
115+
return fmt.Errorf("%s failed: %s", cmd.PrintableCommandArgs(), combinedBuffer.String())
95116
}
96117
return fmt.Errorf("%s failed: %s", cmd.PrintableCommandArgs(), err)
97118
}
98-
if err := json.Unmarshal([]byte(out), v); err != nil {
119+
if stdErr := errBuffer.String(); stdErr != "" {
120+
logger.Warnf("%s: %s", cmd.PrintableCommandArgs(), stdErr)
121+
}
122+
123+
stdout := outBuffer.Bytes()
124+
if err := json.Unmarshal(stdout, v); err != nil {
125+
logger.Warnf("Failed to parse %s command output, first lines:\n%s", cmd.PrintableCommandArgs(), firstLines(string(stdout), 10))
99126
return err
100127
}
101128
return nil
102129
}
103130

131+
func firstLines(out string, count int) string {
132+
if count < 1 {
133+
return ""
134+
}
135+
136+
var lines []string
137+
scanner := bufio.NewScanner(strings.NewReader(out))
138+
for scanner.Scan() {
139+
lines = append(lines, scanner.Text())
140+
if len(lines) >= count {
141+
break
142+
}
143+
}
144+
return strings.Join(lines, "\n")
145+
}
146+
104147
// xcresulttoolExport exports a file with the given id at the given output path.
105148
func xcresulttoolExport(xcresultPth, id, outputPth string, useLegacyFlag bool) error {
106149
args := []string{"xcresulttool", "export"}

0 commit comments

Comments
 (0)