Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 10db4e3

Browse files
author
Nate McMaster
committed
Add warning when UserSecretsId is empty or missing
Fix incremental build for user secrets target. Add functional test for user secret build target
1 parent efe0cb9 commit 10db4e3

File tree

3 files changed

+131
-5
lines changed

3 files changed

+131
-5
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ nuget.exe
2626
*.sln.ide
2727
project.lock.json
2828
.vs
29+
.vscode/
2930
.build/
30-
.testPublish/
31+
.testPublish/
32+
*.nuget.props
33+
*.nuget.targets

src/Microsoft.Extensions.Configuration.UserSecrets/build/netstandard1.0/Microsoft.Extensions.Configuration.UserSecrets.targets

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Licensed under the Apache License, Version 2.0.
55
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
66

77
<PropertyGroup>
8+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
89
<GeneratedUserSecretsAttributeFile Condition="'$(GeneratedUserSecretsAttributeFile)'==''">$(IntermediateOutputPath)UserSecretsAssemblyInfo$(DefaultLanguageSourceExtension)</GeneratedUserSecretsAttributeFile>
910
<GenerateUserSecretsAttribute Condition="'$(GenerateUserSecretsAttribute)'==''">true</GenerateUserSecretsAttribute>
1011
</PropertyGroup>
@@ -19,11 +20,16 @@ Generates an assembly attribute that has the user secrets id that .AddUserSecret
1920
<Target Name="GenerateUserSecretsAttribute"
2021
BeforeTargets="CoreCompile"
2122
DependsOnTargets="PrepareForBuild;CoreGenerateUserSecretsAttribute"
22-
Condition="'$(GenerateUserSecretsAttribute)'=='true' and '$(UserSecretsId)'!='' and '$(DesignTimeBuild)'!='true'"
23-
Inputs="$(MSBuildAllProjects)"
24-
Outputs="$(GeneratedUserSecretsAttributeFile)" />
23+
Condition="'$(GenerateUserSecretsAttribute)'=='true' and '$(DesignTimeBuild)'!='true'" >
24+
<Warning Text="The assembly attribute 'UserSecretsIdAttribute' could not be generated because the 'UserSecretsId' property was empty or missing. UserSecretsIdAttribute may be required for .AddUserSecrets() to function correctly."
25+
Code="USERSECRETS001"
26+
Condition="'$(UserSecretsId)'==''" />
27+
</Target>
2528

26-
<Target Name="CoreGenerateUserSecretsAttribute">
29+
<Target Name="CoreGenerateUserSecretsAttribute"
30+
Condition="'$(UserSecretsId)'!=''"
31+
Inputs="$(MSBuildAllProjects)"
32+
Outputs="$(GeneratedUserSecretsAttributeFile)" >
2733
<ItemGroup>
2834
<_UserSecretAssemblyAttribute Remove="@(_UserSecretAssemblyAttribute)" />
2935
<_UserSecretAssemblyAttribute Include="Microsoft.Extensions.Configuration.UserSecrets.UserSecretsIdAttribute">
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using Xunit;
8+
9+
namespace Microsoft.Extensions.Configuration.UserSecrets
10+
{
11+
public class MsBuildTargetTest : IDisposable
12+
{
13+
private const string SkipReason = "Not safe to run on CI. MSBuild and SDK not available yet.";
14+
private readonly string _tempDir;
15+
private readonly DirectoryInfo _solutionRoot;
16+
17+
public MsBuildTargetTest()
18+
{
19+
_tempDir = Path.Combine(Path.GetTempPath(), "usersecrettest", Guid.NewGuid().ToString());
20+
Directory.CreateDirectory(_tempDir);
21+
22+
_solutionRoot = new DirectoryInfo(AppContext.BaseDirectory);
23+
while (_solutionRoot != null)
24+
{
25+
if (File.Exists(Path.Combine(_solutionRoot.FullName, "global.json")))
26+
{
27+
break;
28+
}
29+
30+
_solutionRoot = _solutionRoot.Parent;
31+
}
32+
}
33+
34+
[Fact(Skip = SkipReason)]
35+
public void GeneratesAssemblyAttributeFile()
36+
{
37+
if (_solutionRoot == null)
38+
{
39+
Assert.True(false, "Could not identify solution root");
40+
}
41+
var target = Path.Combine(_solutionRoot.FullName, "src", "Microsoft.Extensions.Configuration.UserSecrets", "build", "netstandard1.0", "Microsoft.Extensions.Configuration.UserSecrets.targets");
42+
var testProj = Path.Combine(_tempDir, "test.csproj");
43+
// should represent a 'dotnet new' project
44+
File.WriteAllText(testProj, @"
45+
<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
46+
<Import Project=""$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"" />
47+
<PropertyGroup>
48+
<OutputType>Exe></OutputType>
49+
<UserSecretsId>xyz123</UserSecretsId>
50+
<TargetFrameworks>netcoreapp1.0</TargetFrameworks>
51+
</PropertyGroup>
52+
<ItemGroup>
53+
<Compile Include=""Program.cs""/>
54+
<PackageReference Include=""Microsoft.NETCore.App"">
55+
<Version>1.0.1</Version>
56+
</PackageReference>
57+
<PackageReference Include=""Microsoft.NET.Sdk"">
58+
<Version>1.0.0-*</Version>
59+
<PrivateAssets>All</PrivateAssets>
60+
</PackageReference>
61+
</ItemGroup>
62+
<Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
63+
<ImportGroup Condition=""'$(TargetFramework)'!=''"">
64+
<Import Project=""$(TestTarget)"" Condition=""'$(TestTarget)' != ''""/>
65+
</ImportGroup>
66+
</Project>
67+
");
68+
File.WriteAllText(Path.Combine(_tempDir, "Program.cs"), "public class Program { public static void Main(){}}");
69+
var assemblyInfoFile = Path.Combine(_tempDir, "obj/Debug/netcoreapp1.0/UserSecretsAssemblyInfo.cs");
70+
71+
var restoreInfo = new ProcessStartInfo
72+
{
73+
FileName = "dotnet",
74+
Arguments = $"restore3 \"{testProj}\" -s https://dotnet.myget.org/F/dotnet-core/api/v3/index.json /nologo /v:m",
75+
UseShellExecute = false
76+
};
77+
var restore = Process.Start(restoreInfo);
78+
restore.WaitForExit();
79+
Assert.Equal(0, restore.ExitCode);
80+
81+
Assert.False(File.Exists(assemblyInfoFile));
82+
83+
// TODO actually build a project
84+
var buildInfo = new ProcessStartInfo
85+
{
86+
FileName = "dotnet",
87+
Arguments = $"msbuild \"{testProj}\" /nologo /v:m \"/p:TestTarget={target}\" /p:TargetFramework=netcoreapp1.0 /t:GenerateUserSecretsAttribute",
88+
UseShellExecute = false
89+
};
90+
var build = Process.Start(buildInfo);
91+
build.WaitForExit();
92+
Assert.Equal(0, build.ExitCode);
93+
94+
Assert.True(File.Exists(assemblyInfoFile));
95+
var contents = File.ReadAllText(assemblyInfoFile);
96+
Assert.Contains("[assembly: Microsoft.Extensions.Configuration.UserSecrets.UserSecretsIdAttribute(\"xyz123\")]", contents);
97+
var lastWrite = new FileInfo(assemblyInfoFile).LastWriteTimeUtc;
98+
99+
build = Process.Start(buildInfo);
100+
build.WaitForExit();
101+
Assert.Equal(0, build.ExitCode);
102+
// assert that the target doesn't re-generate assembly file. Important for incremental build.
103+
Assert.Equal(lastWrite, new FileInfo(assemblyInfoFile).LastWriteTimeUtc);
104+
}
105+
106+
public void Dispose()
107+
{
108+
try
109+
{
110+
Directory.Delete(_tempDir, recursive: true);
111+
}
112+
catch
113+
{
114+
}
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)