Skip to content

Commit 0b8c3a2

Browse files
godreitothszabi
andauthored
Switch to bufio.Reader.Readline (#234)
Co-authored-by: Szabolcs Toth <szabolcs.toth@bitrise.io>
1 parent e23ed8a commit 0b8c3a2

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

xcodebuild/show_build_settings.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package xcodebuild
22

33
import (
44
"bufio"
5+
"bytes"
56
"fmt"
7+
"io"
68
"path/filepath"
79
"strings"
810

@@ -97,21 +99,35 @@ func (c ShowBuildSettingsCommandModel) PrintableCmd() string {
9799
func parseBuildSettings(out string) (serialized.Object, error) {
98100
settings := serialized.Object{}
99101

100-
scanner := bufio.NewScanner(strings.NewReader(out))
101-
for scanner.Scan() {
102-
line := strings.TrimSpace(scanner.Text())
102+
reader := bufio.NewReader(strings.NewReader(out))
103+
var buffer bytes.Buffer
103104

104-
if split := strings.Split(line, "="); len(split) > 1 {
105-
key := strings.TrimSpace(split[0])
106-
value := strings.TrimSpace(strings.Join(split[1:], "="))
107-
value = strings.Trim(value, `"`)
105+
for {
106+
b, isPrefix, err := reader.ReadLine()
107+
if err == io.EOF {
108+
break
109+
} else if err != nil {
110+
return nil, err
111+
}
112+
113+
lineFragment := string(b)
114+
buffer.WriteString(lineFragment)
115+
116+
// isPrefix is set to false once a full line has been read
117+
if isPrefix == false {
118+
line := strings.TrimSpace(buffer.String())
108119

109-
settings[key] = value
120+
if split := strings.Split(line, "="); len(split) > 1 {
121+
key := strings.TrimSpace(split[0])
122+
value := strings.TrimSpace(strings.Join(split[1:], "="))
123+
value = strings.Trim(value, `"`)
124+
125+
settings[key] = value
126+
}
127+
128+
buffer.Reset()
110129
}
111130
}
112-
if err := scanner.Err(); err != nil {
113-
return nil, err
114-
}
115131

116132
return settings, nil
117133
}

xcodebuild/show_build_settings_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package xcodebuild
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/bitrise-io/go-xcode/xcodeproject/serialized"
@@ -74,6 +75,28 @@ func Test_parseShowBuildSettingsOutput(t *testing.T) {
7475
}
7576
}
7677

78+
func TestReadLongBuildSettingsLine(t *testing.T) {
79+
// The default line limit for a bufio.Scanner is 65000 characters, and this fie contains a line longer than that
80+
buildSettingsWithLongLine, err := os.ReadFile("./testdata/buildSettingsWithLongLine.txt")
81+
require.NoError(t, err)
82+
83+
got, err := parseBuildSettings(string(buildSettingsWithLongLine))
84+
require.NoError(t, err)
85+
86+
// Reading the same single long line value from a file, so it does not hurt test readability
87+
expectedSingleLongLine, err := os.ReadFile("./testdata/expectedSingleLongLine.txt")
88+
require.NoError(t, err)
89+
90+
want := serialized.Object{
91+
"ACTION": "build",
92+
"AD_HOC_CODE_SIGNING_ALLOWED": "NO",
93+
"REALLY_LONG_LINE": string(expectedSingleLongLine),
94+
"ALTERNATE_GROUP": "staff",
95+
"BUILD_STYLE": "fast",
96+
}
97+
require.Equal(t, want, got)
98+
}
99+
77100
const testBuildSettingsOut = `Build settings for action build and target sample-apps-osx-10-12:
78101
VERSION_INFO_STRING = "@(#)PROGRAM:sample-apps-osx-10-12 PROJECT:sample-apps-osx-10-12-"
79102
AVAILABLE_PLATFORMS = appletvos appletvsimulator iphoneos iphonesimulator macosx watchos watchsimulator

xcodebuild/testdata/buildSettingsWithLongLine.txt

Lines changed: 5 additions & 0 deletions
Large diffs are not rendered by default.

xcodebuild/testdata/expectedSingleLongLine.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)