Skip to content

Commit f9a47a7

Browse files
authored
[CI-615] File based device parsing (#196)
* Add file based device parsing * Propagate errors
1 parent b014fdc commit f9a47a7

File tree

3 files changed

+98
-12
lines changed

3 files changed

+98
-12
lines changed

devportalservice/devportalservice.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,6 @@ type APIKeyConnection struct {
166166
PrivateKey string `json:"private_key"`
167167
}
168168

169-
// TestDevice ...
170-
type TestDevice struct {
171-
ID int `json:"id"`
172-
UserID int `json:"user_id"`
173-
// DeviceID is the Apple device UDID
174-
DeviceID string `json:"device_identifier"`
175-
Title string `json:"title"`
176-
CreatedAt time.Time `json:"created_at"`
177-
UpdatedAt time.Time `json:"updated_at"`
178-
DeviceType string `json:"device_type"`
179-
}
180-
181169
// IsEqualUDID compares two UDIDs (stored in the DeviceID field of TestDevice)
182170
func IsEqualUDID(UDID string, otherUDID string) bool {
183171
return normalizeDeviceUDID(UDID) == normalizeDeviceUDID(otherUDID)

devportalservice/testdevice.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package devportalservice
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"time"
8+
9+
"github.com/bitrise-io/go-utils/pathutil"
10+
)
11+
12+
// TestDevice ...
13+
type TestDevice struct {
14+
ID int `json:"id"`
15+
UserID int `json:"user_id"`
16+
// DeviceID is the Apple device UDID
17+
DeviceID string `json:"device_identifier"`
18+
Title string `json:"title"`
19+
CreatedAt time.Time `json:"created_at"`
20+
UpdatedAt time.Time `json:"updated_at"`
21+
DeviceType string `json:"device_type"`
22+
}
23+
24+
// ParseTestDevicesFromFile ...
25+
func ParseTestDevicesFromFile(path string, currentTime time.Time) ([]TestDevice, error) {
26+
absPath, err := pathutil.AbsPath(path)
27+
if err != nil {
28+
return []TestDevice{}, err
29+
}
30+
31+
bytes, err := os.ReadFile(absPath)
32+
if err != nil {
33+
return []TestDevice{}, err
34+
}
35+
36+
fileContent := strings.TrimSpace(string(bytes))
37+
identifiers := strings.Split(fileContent, ",")
38+
39+
var testDevices []TestDevice
40+
for i, identifier := range identifiers {
41+
testDevices = append(testDevices, TestDevice{
42+
DeviceID: identifier,
43+
Title: fmt.Sprintf("Device %d", i+1),
44+
CreatedAt: currentTime,
45+
UpdatedAt: currentTime,
46+
DeviceType: "unknown",
47+
})
48+
}
49+
50+
return testDevices, nil
51+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package devportalservice
2+
3+
import (
4+
"path"
5+
"testing"
6+
"time"
7+
8+
"github.com/bitrise-io/go-utils/fileutil"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestDeviceParsing(t *testing.T) {
14+
content := "00000000–0000000000000001,00000000–0000000000000002,00000000–0000000000000003"
15+
pth := path.Join(t.TempDir(), "devices.txt")
16+
err := fileutil.WriteStringToFile(pth, content)
17+
require.NoError(t, err)
18+
19+
currentTime := time.Now()
20+
got, err := ParseTestDevicesFromFile(pth, currentTime)
21+
require.NoError(t, err)
22+
23+
expected := []TestDevice{
24+
{
25+
DeviceID: "00000000–0000000000000001",
26+
Title: "Device 1",
27+
CreatedAt: currentTime,
28+
UpdatedAt: currentTime,
29+
DeviceType: "unknown",
30+
},
31+
{
32+
DeviceID: "00000000–0000000000000002",
33+
Title: "Device 2",
34+
CreatedAt: currentTime,
35+
UpdatedAt: currentTime,
36+
DeviceType: "unknown",
37+
},
38+
{
39+
DeviceID: "00000000–0000000000000003",
40+
Title: "Device 3",
41+
CreatedAt: currentTime,
42+
UpdatedAt: currentTime,
43+
DeviceType: "unknown",
44+
},
45+
}
46+
assert.Equal(t, expected, got)
47+
}

0 commit comments

Comments
 (0)