Skip to content

Commit 6283da5

Browse files
Validate that the variables are populated (Azure#34790)
1 parent 33b8b7e commit 6283da5

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

sdk/core/Azure.Core.TestFramework/src/TestRecording.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ internal async Task InitializeProxySettingsAsync()
6565
catch (RequestFailedException ex)
6666
when (ex.Status == 404)
6767
{
68+
// We don't throw the exception here because Playback only tests that are testing the
69+
// recording infrastructure itself will not have session records.
6870
MismatchException = new TestRecordingMismatchException(ex.Message, ex);
6971
return;
7072
}
@@ -180,6 +182,7 @@ public TestRandom Random
180182
_random = new TestRandom(Mode, seed);
181183
break;
182184
case RecordedTestMode.Playback:
185+
ValidateVariables();
183186
_random = new TestRandom(Mode, int.Parse(Variables[RandomSeedVariableKey]));
184187
break;
185188
default:
@@ -325,17 +328,25 @@ public string GetVariable(string variableName, string defaultValue, Func<string,
325328
case RecordedTestMode.Live:
326329
return defaultValue;
327330
case RecordedTestMode.Playback:
328-
if (Variables.Count == 0)
329-
{
330-
throw new TestRecordingMismatchException("The recording contains no variables.");
331-
}
331+
ValidateVariables();
332332
Variables.TryGetValue(variableName, out string value);
333333
return value;
334334
default:
335335
throw new ArgumentOutOfRangeException();
336336
}
337337
}
338338

339+
private void ValidateVariables()
340+
{
341+
if (Variables.Count == 0)
342+
{
343+
throw new TestRecordingMismatchException(
344+
"The record session does not exist or is missing the Variables section. If the test is " +
345+
"attributed with 'RecordedTest', it will be recorded automatically. Otherwise, set the " +
346+
"RecordedTestMode to 'Record' and attempt to record the test.");
347+
}
348+
}
349+
339350
public void SetVariable(string variableName, string value, Func<string, string> sanitizer = default)
340351
{
341352
switch (Mode)

0 commit comments

Comments
 (0)