Skip to content

Commit ad3421a

Browse files
authored
Exception messages for file share clients when for emulator connection string (#48484)
* Implement emulator checks * Add tests * Change from recorded tests into unit tests * Fix unit test issues * Use correct connection string
1 parent e7cffcf commit ad3421a

File tree

9 files changed

+40
-0
lines changed

9 files changed

+40
-0
lines changed

sdk/storage/Azure.Storage.Files.Shares/src/ShareClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public ShareClient(
158158
Argument.AssertNotNullOrWhiteSpace(shareName, nameof(shareName));
159159
options ??= new ShareClientOptions();
160160
var conn = StorageConnectionString.Parse(connectionString);
161+
ShareErrors.AssertNotDevelopment(conn, nameof(connectionString));
161162
ShareUriBuilder uriBuilder = new ShareUriBuilder(conn.FileEndpoint) { ShareName = shareName };
162163
_uri = uriBuilder.ToUri();
163164
_accountName = conn.AccountName;

sdk/storage/Azure.Storage.Files.Shares/src/ShareDirectoryClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ public ShareDirectoryClient(
193193
Argument.AssertNotNullOrWhiteSpace(shareName, nameof(shareName));
194194
options ??= new ShareClientOptions();
195195
var conn = StorageConnectionString.Parse(connectionString);
196+
ShareErrors.AssertNotDevelopment(conn, nameof(connectionString));
196197
ShareUriBuilder uriBuilder =
197198
new ShareUriBuilder(conn.FileEndpoint)
198199
{

sdk/storage/Azure.Storage.Files.Shares/src/ShareErrors.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@ public static void AssertAlgorithmSupport(StorageChecksumAlgorithm? algorithm)
3232
throw new ArgumentException($"{nameof(StorageChecksumAlgorithm)} does not support value {Enum.GetName(typeof(StorageChecksumAlgorithm), resolved) ?? ((int)resolved).ToString(CultureInfo.InvariantCulture)}.");
3333
}
3434
}
35+
36+
public static void AssertNotDevelopment(StorageConnectionString conn, string argumentName)
37+
{
38+
if (conn.IsDevStoreAccount)
39+
{
40+
throw new ArgumentException("Connection string for emulator is not valid for Azure File Shares", argumentName);
41+
}
42+
}
3543
}
3644
}

sdk/storage/Azure.Storage.Files.Shares/src/ShareFileClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public ShareFileClient(
203203
Argument.AssertNotNullOrWhiteSpace(filePath, nameof(filePath));
204204
options ??= new ShareClientOptions();
205205
var conn = StorageConnectionString.Parse(connectionString);
206+
ShareErrors.AssertNotDevelopment(conn, nameof(connectionString));
206207
ShareUriBuilder uriBuilder =
207208
new ShareUriBuilder(conn.FileEndpoint)
208209
{

sdk/storage/Azure.Storage.Files.Shares/src/ShareServiceClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public ShareServiceClient(
129129
{
130130
options ??= new ShareClientOptions();
131131
var conn = StorageConnectionString.Parse(connectionString);
132+
ShareErrors.AssertNotDevelopment(conn, nameof(connectionString));
132133
_uri = conn.FileEndpoint;
133134
_accountName = conn.AccountName;
134135
_clientConfiguration = new ShareClientConfiguration(

sdk/storage/Azure.Storage.Files.Shares/tests/DirectoryClientTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ public async Task Ctor_CustomAudience()
198198
Assert.IsNotNull(exists);
199199
}
200200

201+
[Test]
202+
public void Ctor_DevelopmentThrows()
203+
{
204+
var ex = Assert.Throws<ArgumentException>(() => new ShareDirectoryClient("UseDevelopmentStorage=true", "share", "dir"));
205+
Assert.AreEqual("connectionString", ex.ParamName);
206+
}
207+
201208
[RecordedTest]
202209
public async Task Ctor_StorageAccountAudience()
203210
{

sdk/storage/Azure.Storage.Files.Shares/tests/FileClientTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ public async Task Ctor_DefaultAudience()
182182
Assert.IsNotNull(exists);
183183
}
184184

185+
[Test]
186+
public void Ctor_DevelopmentThrows()
187+
{
188+
var ex = Assert.Throws<ArgumentException>(() => new ShareFileClient("UseDevelopmentStorage=true", "share", "dir/file"));
189+
Assert.AreEqual("connectionString", ex.ParamName);
190+
}
191+
185192
[RecordedTest]
186193
public async Task Ctor_CustomAudience()
187194
{

sdk/storage/Azure.Storage.Files.Shares/tests/ServiceClientTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ public void Ctor_AzureSasCredential_VerifyNoSasInUri()
103103
e => e.Message.Contains($"You cannot use {nameof(AzureSasCredential)} when the resource URI also contains a Shared Access Signature"));
104104
}
105105

106+
[Test]
107+
public void Ctor_DevelopmentThrows()
108+
{
109+
var ex = Assert.Throws<ArgumentException>(() => new ShareServiceClient("UseDevelopmentStorage=true"));
110+
Assert.AreEqual("connectionString", ex.ParamName);
111+
}
112+
106113
[RecordedTest]
107114
public async Task GetPropertiesAsync()
108115
{

sdk/storage/Azure.Storage.Files.Shares/tests/ShareClientTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,13 @@ await TestHelper.AssertExpectedExceptionAsync<RequestFailedException>(
303303
e => Assert.AreEqual("InvalidAuthenticationInfo", e.ErrorCode));
304304
}
305305

306+
[Test]
307+
public void Ctor_DevelopmentThrows()
308+
{
309+
var ex = Assert.Throws<ArgumentException>(() => new ShareClient("UseDevelopmentStorage=true", "share"));
310+
Assert.AreEqual("connectionString", ex.ParamName);
311+
}
312+
306313
[RecordedTest]
307314
public void WithSnapshot()
308315
{

0 commit comments

Comments
 (0)