Replace NuGetConfig with NuGetPackageSource

This commit is contained in:
Mike Harder 2018-05-02 15:33:50 -07:00
parent 823baabb8f
commit 394adba776
9 changed files with 63 additions and 63 deletions

View File

@ -15,16 +15,4 @@
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
<ItemGroup>
<None Update="NuGetConfig\NuGet.DotNetCore.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="NuGetConfig\NuGet.NuGetOrg.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="NuGetConfig\NuGet.Empty.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -7,7 +7,7 @@ using System.Net;
namespace AspNetCoreSdkTests
{
[TestFixture]
public class TemplateTests
public class FrameworkDependent
{
[Test]
[TestCaseSource(nameof(RestoreData))]
@ -48,19 +48,19 @@ namespace AspNetCoreSdkTests
public static IEnumerable<Template> RestoreData = new[]
{
Template.GetInstance<ClassLibraryTemplate>(NuGetConfig.Empty),
Template.GetInstance<ConsoleApplicationTemplate>(NuGetConfig.Empty),
Template.GetInstance<ClassLibraryTemplate>(NuGetPackageSource.None),
Template.GetInstance<ConsoleApplicationTemplate>(NuGetPackageSource.None),
// Offline restore currently not supported for RazorClassLibrary template (https://github.com/aspnet/Universe/issues/1123)
Template.GetInstance<RazorClassLibraryTemplate>(NuGetConfig.NuGetOrg),
Template.GetInstance<RazorClassLibraryTemplate>(NuGetPackageSource.NuGetOrg),
Template.GetInstance<WebTemplate>(NuGetConfig.Empty),
Template.GetInstance<RazorTemplate>(NuGetConfig.Empty),
Template.GetInstance<MvcTemplate>(NuGetConfig.Empty),
Template.GetInstance<AngularTemplate>(NuGetConfig.Empty),
Template.GetInstance<ReactTemplate>(NuGetConfig.Empty),
Template.GetInstance<ReactReduxTemplate>(NuGetConfig.Empty),
Template.GetInstance<WebApiTemplate>(NuGetConfig.Empty),
Template.GetInstance<WebTemplate>(NuGetPackageSource.None),
Template.GetInstance<RazorTemplate>(NuGetPackageSource.None),
Template.GetInstance<MvcTemplate>(NuGetPackageSource.None),
Template.GetInstance<AngularTemplate>(NuGetPackageSource.None),
Template.GetInstance<ReactTemplate>(NuGetPackageSource.None),
Template.GetInstance<ReactReduxTemplate>(NuGetPackageSource.None),
Template.GetInstance<WebApiTemplate>(NuGetPackageSource.None),
};
public static IEnumerable<Template> BuildData => RestoreData;

View File

@ -1,9 +0,0 @@
namespace AspNetCoreSdkTests
{
public enum NuGetConfig
{
Empty,
DotNetCore,
NuGetOrg
}
}

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="DotNetCore" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
</packageSources>
</configuration>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
</packageSources>
</configuration>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>

View File

@ -0,0 +1,9 @@
namespace AspNetCoreSdkTests
{
public enum NuGetPackageSource
{
None,
NuGetOrg,
EnvironmentVariable,
}
}

View File

@ -21,11 +21,11 @@ namespace AspNetCoreSdkTests.Templates
ServerCertificateCustomValidationCallback = (m, c, ch, p) => true
});
private static ConcurrentDictionary<(Type, NuGetConfig), Template> _templates = new ConcurrentDictionary<(Type, NuGetConfig), Template>();
private static ConcurrentDictionary<(Type, NuGetPackageSource), Template> _templates = new ConcurrentDictionary<(Type, NuGetPackageSource), Template>();
public static T GetInstance<T>(NuGetConfig nuGetConfig) where T : Template, new()
public static T GetInstance<T>(NuGetPackageSource nuGetPackageSource) where T : Template, new()
{
return (T)_templates.GetOrAdd((typeof(T), nuGetConfig), (k) => new T() { NuGetConfig = nuGetConfig });
return (T)_templates.GetOrAdd((typeof(T), nuGetPackageSource), (k) => new T() { NuGetPackageSource = nuGetPackageSource });
}
private Lazy<IEnumerable<string>> _objFilesAfterRestore;
@ -34,7 +34,7 @@ namespace AspNetCoreSdkTests.Templates
private Lazy<(HttpResponseMessage Http, HttpResponseMessage Https)> _httpResponsesAfterRun;
private Lazy<(HttpResponseMessage Http, HttpResponseMessage Https)> _httpResponsesAfterExec;
public NuGetConfig NuGetConfig { get; private set; }
public NuGetPackageSource NuGetPackageSource { get; private set; }
protected Template()
{
@ -54,9 +54,9 @@ namespace AspNetCoreSdkTests.Templates
GetHttpResponsesAfterExec, LazyThreadSafetyMode.ExecutionAndPublication);
}
public override string ToString() => $"{Name},{NuGetConfig}";
public override string ToString() => $"{Name},{NuGetPackageSource}";
private string TempDir => Path.Combine(AssemblySetUp.TempDir, Name, NuGetConfig.ToString());
private string TempDir => Path.Combine(AssemblySetUp.TempDir, Name, NuGetPackageSource.ToString());
public abstract string Name { get; }
public abstract TemplateType Type { get; }
@ -89,8 +89,8 @@ namespace AspNetCoreSdkTests.Templates
{
Directory.CreateDirectory(TempDir);
DotNetUtil.New(Name, TempDir);
DotNetUtil.Restore(TempDir, NuGetConfig);
return IOUtil.GetFiles(System.IO.Path.Combine(TempDir, "obj"));
DotNetUtil.Restore(TempDir, NuGetPackageSource);
return IOUtil.GetFiles(Path.Combine(TempDir, "obj"));
}
private (IEnumerable<string> ObjFiles, IEnumerable<string> BinFiles) GetFilesAfterBuild()
@ -99,7 +99,7 @@ namespace AspNetCoreSdkTests.Templates
_ = ObjFilesAfterRestore;
DotNetUtil.Build(TempDir);
return (IOUtil.GetFiles(System.IO.Path.Combine(TempDir, "obj")), IOUtil.GetFiles(System.IO.Path.Combine(TempDir, "bin")));
return (IOUtil.GetFiles(Path.Combine(TempDir, "obj")), IOUtil.GetFiles(Path.Combine(TempDir, "bin")));
}
private IEnumerable<string> GetFilesAfterPublish()
@ -108,7 +108,7 @@ namespace AspNetCoreSdkTests.Templates
_ = BinFilesAfterBuild;
DotNetUtil.Publish(TempDir);
return IOUtil.GetFiles(System.IO.Path.Combine(TempDir, DotNetUtil.PublishOutput));
return IOUtil.GetFiles(Path.Combine(TempDir, DotNetUtil.PublishOutput));
}
private (HttpResponseMessage Http, HttpResponseMessage Https) GetHttpResponsesAfterRun()

View File

@ -8,6 +8,15 @@ namespace AspNetCoreSdkTests.Util
{
internal static class DotNetUtil
{
private const string _clearPackageSourcesNuGetConfig =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<clear />
</packageSources>
</configuration>
";
// Bind to dynamic port 0 to avoid port conflicts during parallel tests
private const string _urls = "--urls http://127.0.0.1:0;https://127.0.0.1:0";
@ -22,13 +31,16 @@ namespace AspNetCoreSdkTests.Util
public static string New(string template, string workingDirectory)
{
// Clear all packages sources by default. May be overridden by NuGetPackageSource parameter.
File.WriteAllText(Path.Combine(workingDirectory, "NuGet.config"), _clearPackageSourcesNuGetConfig);
return RunDotNet($"new {template} --name {template} --output . --no-restore", workingDirectory, GetEnvironment(workingDirectory));
}
public static string Restore(string workingDirectory, NuGetConfig config)
public static string Restore(string workingDirectory, NuGetPackageSource packageSource)
{
var configPath = Path.GetFullPath(Path.Combine("NuGetConfig", $"NuGet.{config}.config"));
return RunDotNet($"restore --no-cache --configfile {configPath}", workingDirectory, GetEnvironment(workingDirectory));
var sourceArgument = GetSourceArgument(packageSource);
return RunDotNet($"restore --no-cache{sourceArgument}", workingDirectory, GetEnvironment(workingDirectory));
}
public static string Build(string workingDirectory)
@ -52,6 +64,24 @@ namespace AspNetCoreSdkTests.Util
return RunDotNet($"publish --no-build -o {PublishOutput}", workingDirectory, GetEnvironment(workingDirectory));
}
private static string GetSourceArgument(NuGetPackageSource packageSource)
{
switch (packageSource)
{
case NuGetPackageSource.None:
return string.Empty;
case NuGetPackageSource.NuGetOrg:
return " --source https://api.nuget.org/v3/index.json";
case NuGetPackageSource.EnvironmentVariable:
var env = Environment.GetEnvironmentVariable("NUGET_PACKAGE_SOURCE");
return string.IsNullOrEmpty(env) ?
throw new InvalidOperationException("Environment variable NUGET_PACKAGE_SOURCE is required but not set") :
$" --source env";
default:
throw new ArgumentException("Invalid value", nameof(packageSource));
}
}
private static string RunDotNet(string arguments, string workingDirectory,
IEnumerable<KeyValuePair<string, string>> environment = null, bool throwOnError = true)
{