Skip to content

Commit d00858c

Browse files
authored
Handle explicit mobile platforms for the CLI builder arg + Add optional testDataSetIndex arg to select (#165)
1 parent 50670ad commit d00858c

5 files changed

Lines changed: 76 additions & 29 deletions

File tree

Assets/Plugins/StreamChat/EditorTools/Builders/BuildTargetPlatform.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public enum BuildTargetPlatform
44
{
55
Standalone,
6-
Mobile
6+
Mobile,
7+
Android,
8+
IOS
79
}
810
}

Assets/Plugins/StreamChat/EditorTools/CommandLineParsers/BuildSettingsCommandLineParser.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class BuildSettingsCommandLineParser : CommandLineParserBase<(BuildSettin
1717
public const string BuildTargetPathArgKey = "-buildTargetPath";
1818

1919
public const string StreamBase64TestDataArgKey = "-streamBase64TestDataSet";
20+
public const string TestDataSetIndexArgKey = "-testDataSetIndex";
2021

2122
protected override (BuildSettings buildSettings, AuthCredentials authCredentials) Parse(
2223
IDictionary<string, string> args)
@@ -53,39 +54,56 @@ protected override (BuildSettings buildSettings, AuthCredentials authCredentials
5354
var scriptingImplementation = GetScriptingImplementation(scriptingBackend);
5455
var targetPath = args[BuildTargetPathArgKey];
5556

56-
var testAuthDataSet = ParseTestAuthDataSetArg(args);
57+
var testAuthDataSet = ParseTestAuthDataSetArg(args, out var optionalTestDataIndex);
5758

5859
return (new BuildSettings(buildTargetGroup, apiCompatibilityLevel, scriptingImplementation, targetPath),
59-
testAuthDataSet.GetAdminData());
60+
testAuthDataSet.GetAdminData(forceIndex: optionalTestDataIndex));
61+
62+
6063
}
6164

62-
public TestAuthDataSet ParseTestAuthDataSetArg(IDictionary<string, string> args)
65+
public TestAuthDataSet ParseTestAuthDataSetArg(IDictionary<string, string> args, out int? forceDataSetIndex)
6366
{
6467
if (!args.ContainsKey(StreamBase64TestDataArgKey))
6568
{
6669
throw new ArgumentException($"Missing argument: `{StreamBase64TestDataArgKey}`");
6770
}
6871

72+
forceDataSetIndex = GetOptionalTestDataIndex();
6973
var rawTestDataSet = GetTestDataSet(args);
7074
var serializer = new NewtonsoftJsonSerializer();
7175
return serializer.Deserialize<TestAuthDataSet>(rawTestDataSet);
76+
77+
int? GetOptionalTestDataIndex()
78+
{
79+
if (!args.TryGetValue(TestDataSetIndexArgKey, out var arg))
80+
{
81+
return default;
82+
}
83+
84+
return int.Parse(arg);
85+
}
7286
}
7387

74-
private BuildTargetGroup GetBuildTargetGroup(BuildTargetPlatform targetPlatform)
88+
private static BuildTargetGroup GetBuildTargetGroup(BuildTargetPlatform targetPlatform)
7589
{
76-
if (targetPlatform == BuildTargetPlatform.Standalone)
90+
switch (targetPlatform)
7791
{
78-
return BuildTargetGroup.Standalone;
79-
}
80-
92+
case BuildTargetPlatform.Standalone: return BuildTargetGroup.Standalone;
93+
case BuildTargetPlatform.Mobile:
8194
#if UNITY_STANDALONE_OSX
82-
return BuildTargetGroup.iOS;
95+
return BuildTargetGroup.iOS;
8396
#else
84-
return BuildTargetGroup.Android;
97+
return BuildTargetGroup.Android;
8598
#endif
99+
case BuildTargetPlatform.Android: return BuildTargetGroup.Android;
100+
case BuildTargetPlatform.IOS: return BuildTargetGroup.iOS;
101+
default:
102+
throw new ArgumentOutOfRangeException(nameof(targetPlatform), targetPlatform, null);
103+
}
86104
}
87105

88-
private ApiCompatibilityLevel GetApiCompatibilityLevel(ApiCompatibility apiCompatibility)
106+
private static ApiCompatibilityLevel GetApiCompatibilityLevel(ApiCompatibility apiCompatibility)
89107
{
90108
#if UNITY_2021
91109
switch (apiCompatibility)
@@ -108,7 +126,7 @@ private ApiCompatibilityLevel GetApiCompatibilityLevel(ApiCompatibility apiCompa
108126
#endif
109127
}
110128

111-
private ScriptingImplementation GetScriptingImplementation(ScriptingBackend scriptingBackend)
129+
private static ScriptingImplementation GetScriptingImplementation(ScriptingBackend scriptingBackend)
112130
{
113131
switch (scriptingBackend)
114132
{
@@ -119,7 +137,7 @@ private ScriptingImplementation GetScriptingImplementation(ScriptingBackend scri
119137
}
120138
}
121139

122-
private string GetTestDataSet(IDictionary<string, string> args)
140+
private static string GetTestDataSet(IDictionary<string, string> args)
123141
{
124142
var decodedBytes = Convert.FromBase64String(args[StreamBase64TestDataArgKey]);
125143
return Encoding.UTF8.GetString(decodedBytes);

Assets/Plugins/StreamChat/EditorTools/TestAuthDataSet.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using StreamChat.Libs.Auth;
5+
using UnityEngine;
56
using Random = UnityEngine.Random;
67

78
namespace StreamChat.EditorTools
@@ -20,11 +21,25 @@ public TestAuthDataSet(IEnumerable<AuthCredentials> testAdminData, AuthCredentia
2021
TestGuestData = testGuestData;
2122
}
2223

23-
public AuthCredentials GetAdminData(string forcedAdminId = null)
24+
public AuthCredentials GetAdminData(string forcedAdminId = default, int? forceIndex = default)
2425
{
25-
return forcedAdminId != null
26-
? TestAdminData.First(_ => _.UserId == forcedAdminId)
27-
: TestAdminData[Random.Range(0, TestAdminData.Length)];
26+
if (!string.IsNullOrEmpty(forcedAdminId))
27+
{
28+
return TestAdminData.First(credentials => credentials.UserId == forcedAdminId);
29+
}
30+
31+
if (forceIndex.HasValue)
32+
{
33+
if (forceIndex < TestAdminData.Length)
34+
{
35+
return TestAdminData[forceIndex.Value];
36+
}
37+
38+
Debug.LogWarning(
39+
$"{nameof(forceIndex)} is out of range -> given: {forceIndex}, max allowed: {TestAdminData.Length - 1}. Using random admin data instead.");
40+
}
41+
42+
return TestAdminData[Random.Range(0, TestAdminData.Length)];
2843
}
2944

3045
public AuthCredentials GetOtherThan(AuthCredentials authCredentials)

Assets/Plugins/StreamChat/Tests/StreamTestClients.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,29 @@ private StreamTestClients()
118118
{
119119
UnityTestRunnerCallbacks.RunFinishedCallback += OnRunFinishedCallback;
120120

121-
var testAuthDataSet = TestUtils.GetTestAuthCredentials();
121+
var testAuthDataSet = TestUtils.GetTestAuthCredentials(out var optionalTestDataIndex);
122122
if (testAuthDataSet.TestAdminData.Length < 3)
123123
{
124-
throw new ArgumentException("At least 3 admin credentials required");
124+
throw new ArgumentException("At least 3 admin credentials are required");
125125
}
126126

127-
var adminData = testAuthDataSet.TestAdminData.OrderBy(_ => Random.value).ToList();
127+
AuthCredentials primaryAdminSet;
128+
if (optionalTestDataIndex.HasValue)
129+
{
130+
primaryAdminSet = testAuthDataSet.TestAdminData[optionalTestDataIndex.Value];
131+
}
132+
else
133+
{
134+
var shuffledSets = testAuthDataSet.TestAdminData.OrderBy(_ => Random.value);
135+
primaryAdminSet = shuffledSets.First();
136+
}
137+
138+
var otherAdminSets = testAuthDataSet.TestAdminData.Except(new[] { primaryAdminSet }).ToArray();
128139

129-
LowLevelClientCredentials = adminData[0];
130-
_stateClientCredentials = adminData[1];
131-
_otherUserCredentials = adminData[2];
132-
_otherUsersCredentials = adminData.Skip(3).ToList();
140+
LowLevelClientCredentials = primaryAdminSet;
141+
_stateClientCredentials = primaryAdminSet;
142+
_otherUserCredentials = otherAdminSets[1];
143+
_otherUsersCredentials = otherAdminSets.Skip(2).ToList();
133144
}
134145

135146
private static async Task<StreamChatClient> ConnectStateClientAsync(StreamChatClient client,

Assets/Plugins/StreamChat/Tests/TestUtils.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ public static void GetTestAuthCredentials(out AuthCredentials guestAuthCredentia
2121
out AuthCredentials userAuthCredentials, out AuthCredentials adminAuthCredentials,
2222
out AuthCredentials otherUserAuthCredentials, string forcedAdminId = null)
2323
{
24-
var testAuthDataSet = GetTestAuthCredentials();
24+
var testAuthDataSet = GetTestAuthCredentials(out var forceDataSetIndex);
2525

2626
guestAuthCredentials = testAuthDataSet.TestGuestData;
2727
userAuthCredentials = testAuthDataSet.TestUserData;
28-
adminAuthCredentials = testAuthDataSet.GetAdminData(forcedAdminId);
28+
adminAuthCredentials = testAuthDataSet.GetAdminData(forcedAdminId, forceDataSetIndex);
2929
otherUserAuthCredentials = testAuthDataSet.GetOtherThan(adminAuthCredentials);
3030
}
3131

32-
public static TestAuthDataSet GetTestAuthCredentials()
32+
public static TestAuthDataSet GetTestAuthCredentials(out int? forceDataSetIndex)
3333
{
34+
forceDataSetIndex = default;
3435
const string TestAuthDataFilePath = "test_auth_data_xSpgxW.txt";
3536

3637
if (Application.isBatchMode)
@@ -40,7 +41,7 @@ public static TestAuthDataSet GetTestAuthCredentials()
4041
var parser = new BuildSettingsCommandLineParser();
4142
var argsDict = parser.GetParsedCommandLineArguments();
4243

43-
var testAuthDataSet = parser.ParseTestAuthDataSetArg(argsDict);
44+
var testAuthDataSet = parser.ParseTestAuthDataSetArg(argsDict, out forceDataSetIndex);
4445

4546
Debug.Log("Data deserialized correctly. Sample: " + testAuthDataSet.TestAdminData[0].UserId);
4647

0 commit comments

Comments
 (0)