Skip to content

Commit ccfa6f8

Browse files
committed
Enable InternalsVisibleTo for test projects in Package mode
- SqlClient.csproj: Add signed+Package IVT for UnitTests with test key public key - Abstractions.csproj: Add signed+Package IVT for Abstractions.Test - Azure.csproj: Add signed+Package IVT for Azure.Test - UnitTests.csproj: Remove ReferenceType=Project restriction, add conditional ProjectReference/PackageReference, add TestSigningKeyPath signing - Abstractions.Test.csproj: Add conditional references and TestSigningKeyPath - Azure.Test.csproj: Make Azure reference conditional, add TestSigningKeyPath - TestCommon.csproj: Make SqlClient reference conditional, add TestSigningKeyPath - TestUtilities.csproj: Add TestSigningKeyPath signing - build.proj: Declare TestSigningKeyPath property and plumb to Test* targets Relates to AB#45126, AB#45162
1 parent 98683d2 commit ccfa6f8

9 files changed

Lines changed: 108 additions & 24 deletions

File tree

build.proj

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,20 @@
259259
-p:SigningKeyPath="$(SigningKeyPath)"
260260
</SigningKeyPathArgument>
261261

262+
<!--
263+
TestSigningKeyPath
264+
Applies to: Test* targets
265+
Description: Path to the key used to sign test assemblies. Required in Package mode when
266+
SigningKeyPath is set so that InternalsVisibleTo grants from signed driver
267+
assemblies can be fulfilled.
268+
Default value: [blank]
269+
Example: C:\path\to\sqlclient-test-key.snk
270+
-->
271+
<TestSigningKeyPath Condition="'$(TestSigningKeyPath)' == ''" />
272+
<TestSigningKeyPathArgument Condition="'$(TestSigningKeyPath)' != ''">
273+
-p:TestSigningKeyPath="$(TestSigningKeyPath)"
274+
</TestSigningKeyPathArgument>
275+
262276
<!--
263277
TestBlameTimeout
264278
Applies to: Test* targets
@@ -678,10 +692,6 @@
678692
<!-- TestSqlClientUnit: Runs unit tests for SqlClient -->
679693
<Target Name="TestSqlClientUnit">
680694
<PropertyGroup>
681-
<!--
682-
Note: This test exclusively uses project references, so neither ReferenceType nor any
683-
package version arguments are specified in this command.
684-
-->
685695
<LogFilePrefix>SqlClientUnit-$(OS)</LogFilePrefix>
686696
<LogFilePrefix Condition="'$(TestFramework)' != ''">$(LogFilePrefix)-$(TestFramework)</LogFilePrefix>
687697

@@ -692,6 +702,10 @@
692702
$(TestCodeCoverageArgument)
693703
$(TestFiltersArgument)
694704
$(TestFrameworkArgument)
705+
$(ReferenceTypeArgument)
706+
$(TestSigningKeyPathArgument)
707+
$(PackageVersionSqlClientArgument)
708+
$(PackageVersionSqlServerArgument)
695709
--results-directory "$(TestResultsFolderPath)"
696710
--logger:"trx;LogFilePrefix=$(LogFilePrefix)"
697711
</DotnetCommand>
@@ -860,10 +874,6 @@
860874
<!-- TestAbstractions: Runs Microsoft.Data.SqlClient.Extensions.Abstractions.Tests -->
861875
<Target Name="TestAbstractions">
862876
<PropertyGroup>
863-
<!--
864-
Note: This test exclusively uses project references, so neither ReferenceType nor any
865-
package version arguments are specified in this command.
866-
-->
867877
<LogFilePrefix>AbstractionsTests-$(OS)</LogFilePrefix>
868878
<LogFilePrefix Condition="'$(TestFramework)' != ''">$(LogFilePrefix)-$(TestFramework)</LogFilePrefix>
869879

@@ -874,6 +884,9 @@
874884
$(TestCodeCoverageArgument)
875885
$(TestFiltersArgument)
876886
$(TestFrameworkArgument)
887+
$(ReferenceTypeArgument)
888+
$(TestSigningKeyPathArgument)
889+
$(PackageVersionAbstractionsArgument)
877890
--results-directory "$(TestResultsFolderPath)"
878891
--logger:"trx;LogFilePrefix=$(LogFilePrefix)"
879892
</DotnetCommand>
@@ -977,7 +990,9 @@
977990

978991
<!-- Reference type arguments -->
979992
$(ReferenceTypeArgument)
993+
$(TestSigningKeyPathArgument)
980994
$(PackageVersionAbstractionsArgument)
995+
$(PackageVersionAzureArgument)
981996
$(PackageVersionLoggingArgument)
982997
$(PackageVersionSqlClientArgument)
983998
$(PackageVersionSqlServerArgument)

src/Microsoft.Data.SqlClient.Extensions/Abstractions/src/Abstractions.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@
3434

3535
<!-- Strong name signing ============================================= -->
3636
<!-- This is done in Directory.Build.props -->
37+
38+
<!-- Unsigned: expose internals to the test project in any reference mode. -->
3739
<ItemGroup Condition="'$(SigningKeyPath)' == ''">
3840
<InternalsVisibleTo Include="$(AssemblyName).Test" />
3941
</ItemGroup>
4042

43+
<!-- Signed + Package mode: expose internals to the test project signed with the test key. -->
44+
<ItemGroup Condition="'$(SigningKeyPath)' != '' AND '$(ReferenceType)' == 'Package'">
45+
<InternalsVisibleTo Include="$(AssemblyName).Test, PublicKey=00240000048000001401000006020000002400005253413100080000010001003D19684676DA365F331D00CE7BD4B8EF03E74102F39A5681B40622703D68F0298ECACECC723D3FFC1EA9365AF4958578550EA1EBEEC084B0B3757F3762449F5365E872802A4B548056760764FAD062BFEE81ED26183109AD46810E7E6E965419D0A10473680144D20C1BFE1027A5F586CA987523C06F5C126C44EA7D4F51EB023867A9F294315F95775ACEFD2D678186919458DFCCB4DE2E9F53AEFC766C7CBCEC474ED21C1616E5A9414D366D91D121C39F5FE6641295ADC058EF3FB10593BCDE2E82D9F217C2634909EEF496CD53AE78ABBEA572B871D72EBFC5378205950ABA97C7CCC2B9635D96933D5F9C9624D71FF53EE2094CF3A6BD38534D66E414B7" />
46+
</ItemGroup>
47+
4148
<!-- Build Output ==================================================== -->
4249
<PropertyGroup>
4350
<ArtifactPath>$(RepoRoot)artifacts/</ArtifactPath>

src/Microsoft.Data.SqlClient.Extensions/Abstractions/test/Abstractions.Test.csproj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
<IsTestProject>true</IsTestProject>
99
</PropertyGroup>
1010

11+
<!-- Strong name signing ============================================= -->
12+
<!--
13+
When a test signing key is provided, sign this test assembly so InternalsVisibleTo from the
14+
Abstractions project works.
15+
-->
16+
<PropertyGroup Condition="'$(TestSigningKeyPath)' != ''">
17+
<SignAssembly>true</SignAssembly>
18+
<AssemblyOriginatorKeyFile>$(TestSigningKeyPath)</AssemblyOriginatorKeyFile>
19+
</PropertyGroup>
20+
1121
<!-- Compiler Options ================================================ -->
1222
<PropertyGroup>
1323
<ImplicitUsings>enable</ImplicitUsings>
@@ -30,7 +40,10 @@
3040
<!-- References ====================================================== -->
3141
<!-- Test Target Reference -->
3242
<ItemGroup>
33-
<ProjectReference Include="$(RepoRoot)src/Microsoft.Data.SqlClient.Extensions/Abstractions/src/Abstractions.csproj" />
43+
<ProjectReference Include="$(RepoRoot)src/Microsoft.Data.SqlClient.Extensions/Abstractions/src/Abstractions.csproj"
44+
Condition="'$(ReferenceType)' != 'Package'" />
45+
<PackageReference Include="Microsoft.Data.SqlClient.Extensions.Abstractions"
46+
Condition="'$(ReferenceType)' == 'Package'" />
3447
</ItemGroup>
3548

3649
<!-- Other References -->

src/Microsoft.Data.SqlClient.Extensions/Azure/src/Azure.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@
3434

3535
<!-- Strong name signing ============================================= -->
3636
<!-- This is done in Directory.Build.props -->
37+
38+
<!-- Unsigned: expose internals to the test project in any reference mode. -->
3739
<ItemGroup Condition="'$(SigningKeyPath)' == ''">
3840
<InternalsVisibleTo Include="$(AssemblyName).Test" />
3941
</ItemGroup>
4042

43+
<!-- Signed + Package mode: expose internals to the test project signed with the test key. -->
44+
<ItemGroup Condition="'$(SigningKeyPath)' != '' AND '$(ReferenceType)' == 'Package'">
45+
<InternalsVisibleTo Include="$(AssemblyName).Test, PublicKey=00240000048000001401000006020000002400005253413100080000010001003D19684676DA365F331D00CE7BD4B8EF03E74102F39A5681B40622703D68F0298ECACECC723D3FFC1EA9365AF4958578550EA1EBEEC084B0B3757F3762449F5365E872802A4B548056760764FAD062BFEE81ED26183109AD46810E7E6E965419D0A10473680144D20C1BFE1027A5F586CA987523C06F5C126C44EA7D4F51EB023867A9F294315F95775ACEFD2D678186919458DFCCB4DE2E9F53AEFC766C7CBCEC474ED21C1616E5A9414D366D91D121C39F5FE6641295ADC058EF3FB10593BCDE2E82D9F217C2634909EEF496CD53AE78ABBEA572B871D72EBFC5378205950ABA97C7CCC2B9635D96933D5F9C9624D71FF53EE2094CF3A6BD38534D66E414B7" />
46+
</ItemGroup>
47+
4148
<!-- Build Output ==================================================== -->
4249
<PropertyGroup>
4350
<ArtifactPath>$(RepoRoot)artifacts/</ArtifactPath>

src/Microsoft.Data.SqlClient.Extensions/Azure/test/Azure.Test.csproj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,22 @@
4646
<PackageReference Include="Microsoft.Data.SqlClient"
4747
Condition="'$(ReferenceType)' == 'Package'"/>
4848

49-
<ProjectReference Include="$(RepoRoot)src/Microsoft.Data.SqlClient.Extensions/Azure/src/Azure.csproj" />
49+
<ProjectReference Include="$(RepoRoot)src/Microsoft.Data.SqlClient.Extensions/Azure/src/Azure.csproj"
50+
Condition="'$(ReferenceType)' != 'Package'" />
51+
<PackageReference Include="Microsoft.Data.SqlClient.Extensions.Azure"
52+
Condition="'$(ReferenceType)' == 'Package'" />
5053
</ItemGroup>
5154

55+
<!-- Strong name signing ============================================= -->
56+
<!--
57+
When a test signing key is provided, sign this test assembly so InternalsVisibleTo from the
58+
Azure project works.
59+
-->
60+
<PropertyGroup Condition="'$(TestSigningKeyPath)' != ''">
61+
<SignAssembly>true</SignAssembly>
62+
<AssemblyOriginatorKeyFile>$(TestSigningKeyPath)</AssemblyOriginatorKeyFile>
63+
</PropertyGroup>
64+
5265
<!-- References for netfx -->
5366
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
5467
<PackageReference Include="Microsoft.DotNet.XUnitExtensions" />

src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,21 @@
6262

6363
<!-- Strong name signing ============================================= -->
6464
<!-- Strong naming is being done in Directory.Build.props -->
65-
<!-- If we're not signing, we are permitted to expose our internals to tests. -->
65+
66+
<!-- Unsigned: expose internals to UnitTests in any reference mode. -->
6667
<ItemGroup Condition="'$(SigningKeyPath)' == ''">
6768
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
6869
<_Parameter1>UnitTests</_Parameter1>
6970
</AssemblyAttribute>
7071
</ItemGroup>
7172

73+
<!-- Signed + Package mode: expose internals to UnitTests signed with the test key. -->
74+
<ItemGroup Condition="'$(SigningKeyPath)' != '' AND '$(ReferenceType)' == 'Package'">
75+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
76+
<_Parameter1>UnitTests, PublicKey=00240000048000001401000006020000002400005253413100080000010001003D19684676DA365F331D00CE7BD4B8EF03E74102F39A5681B40622703D68F0298ECACECC723D3FFC1EA9365AF4958578550EA1EBEEC084B0B3757F3762449F5365E872802A4B548056760764FAD062BFEE81ED26183109AD46810E7E6E965419D0A10473680144D20C1BFE1027A5F586CA987523C06F5C126C44EA7D4F51EB023867A9F294315F95775ACEFD2D678186919458DFCCB4DE2E9F53AEFC766C7CBCEC474ED21C1616E5A9414D366D91D121C39F5FE6641295ADC058EF3FB10593BCDE2E82D9F217C2634909EEF496CD53AE78ABBEA572B871D72EBFC5378205950ABA97C7CCC2B9635D96933D5F9C9624D71FF53EE2094CF3A6BD38534D66E414B7</_Parameter1>
77+
</AssemblyAttribute>
78+
</ItemGroup>
79+
7280
<!-- Build Output ==================================================== -->
7381
<PropertyGroup>
7482
<ArtifactPath>$(RepoRoot)artifacts/</ArtifactPath>

src/Microsoft.Data.SqlClient/tests/Common/Microsoft.Data.SqlClient.TestCommon.csproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<IsTestProject>false</IsTestProject>
1313
</PropertyGroup>
1414

15+
<!-- Strong name signing ============================================= -->
16+
<!-- Sign with the test key so signed test assemblies can reference this project. -->
17+
<PropertyGroup Condition="'$(TestSigningKeyPath)' != ''">
18+
<SignAssembly>true</SignAssembly>
19+
<AssemblyOriginatorKeyFile>$(TestSigningKeyPath)</AssemblyOriginatorKeyFile>
20+
</PropertyGroup>
21+
1522
<!-- Target Frameworks =============================================== -->
1623
<PropertyGroup>
1724
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
@@ -26,7 +33,10 @@
2633
<!-- References ====================================================== -->
2734
<!-- Test target reference -->
2835
<ItemGroup>
29-
<ProjectReference Include="$(RepoRoot)src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj" />
36+
<ProjectReference Include="$(RepoRoot)src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj"
37+
Condition="'$(ReferenceType)' != 'Package'" />
38+
<PackageReference Include="Microsoft.Data.SqlClient"
39+
Condition="'$(ReferenceType)' == 'Package'" />
3040
</ItemGroup>
3141

3242
<!-- References for netfx -->

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,26 @@
3636
</None>
3737
</ItemGroup>
3838

39+
<!-- Strong name signing ============================================= -->
40+
<!-- When a test signing key is provided, sign UnitTests so IVT from signed SqlClient works. -->
41+
<PropertyGroup Condition="'$(TestSigningKeyPath)' != ''">
42+
<SignAssembly>true</SignAssembly>
43+
<AssemblyOriginatorKeyFile>$(TestSigningKeyPath)</AssemblyOriginatorKeyFile>
44+
</PropertyGroup>
45+
3946
<!-- References ====================================================== -->
4047
<!-- Test target reference -->
4148
<ItemGroup>
42-
<!--
43-
The Unit Test project only supports ReferenceType = Project since it needs access to the
44-
internals of sibling packages.
45-
-->
46-
<ProjectReference Include="$(RepoRoot)src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj" />
47-
<ProjectReference Include="$(RepoRoot)src/Microsoft.SqlServer.Server/Microsoft.SqlServer.Server.csproj" />
49+
<ProjectReference Include="$(RepoRoot)src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj"
50+
Condition="'$(ReferenceType)' != 'Package'" />
51+
<PackageReference Include="Microsoft.Data.SqlClient"
52+
Condition="'$(ReferenceType)' == 'Package'" />
53+
<ProjectReference Include="$(RepoRoot)src/Microsoft.SqlServer.Server/Microsoft.SqlServer.Server.csproj"
54+
Condition="'$(ReferenceType)' != 'Package'" />
55+
<PackageReference Include="Microsoft.SqlServer.Server"
56+
Condition="'$(ReferenceType)' == 'Package'" />
4857
</ItemGroup>
4958

50-
<Target Name="ValidateReferenceType"
51-
BeforeTargets="Restore;PrepareForBuild"
52-
Condition="'$(ReferenceType)' != 'Project'">
53-
<Error Text="Microsoft.Data.SqlClient.UnitTests.csproj only supports ReferenceType=Project." />
54-
</Target>
55-
5659
<!-- References for netframework -->
5760
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
5861
<Reference Include="System.Transactions" />

src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Microsoft.Data.SqlClient.TestUtilities.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
44
</PropertyGroup>
5+
6+
<!-- Strong name signing ============================================= -->
7+
<!-- Sign with the test key so signed test assemblies can reference this project. -->
8+
<PropertyGroup Condition="'$(TestSigningKeyPath)' != ''">
9+
<SignAssembly>true</SignAssembly>
10+
<AssemblyOriginatorKeyFile>$(TestSigningKeyPath)</AssemblyOriginatorKeyFile>
11+
</PropertyGroup>
12+
513
<Target Name="CopyConfig" BeforeTargets="Compile">
614
<Copy SourceFiles="config.default.json" DestinationFiles="config.json" Condition="!Exists('config.json')" />
715
</Target>

0 commit comments

Comments
 (0)