Convert NuGetPackageSource from enum to class
This commit is contained in:
parent
394adba776
commit
ee710d3954
|
|
@ -1,9 +1,39 @@
|
|||
namespace AspNetCoreSdkTests
|
||||
using System;
|
||||
|
||||
namespace AspNetCoreSdkTests
|
||||
{
|
||||
public enum NuGetPackageSource
|
||||
public class NuGetPackageSource
|
||||
{
|
||||
None,
|
||||
NuGetOrg,
|
||||
EnvironmentVariable,
|
||||
public static NuGetPackageSource None { get; } = new NuGetPackageSource
|
||||
{
|
||||
Name = nameof(None),
|
||||
SourceArgumentLazy = new Lazy<string>(string.Empty),
|
||||
};
|
||||
|
||||
public static NuGetPackageSource NuGetOrg { get; } = new NuGetPackageSource
|
||||
{
|
||||
Name = nameof(NuGetOrg),
|
||||
SourceArgumentLazy = new Lazy<string>("--source https://api.nuget.org/v3/index.json"),
|
||||
};
|
||||
|
||||
public static NuGetPackageSource EnvironmentVariable { get; } = new NuGetPackageSource
|
||||
{
|
||||
Name = nameof(EnvironmentVariable),
|
||||
SourceArgumentLazy = new Lazy<string>(() => $"--source {GetPackageSourceFromEnvironment()}"),
|
||||
};
|
||||
|
||||
private NuGetPackageSource() { }
|
||||
|
||||
public string Name { get; private set; }
|
||||
public string SourceArgument => SourceArgumentLazy.Value;
|
||||
private Lazy<string> SourceArgumentLazy { get; set; }
|
||||
|
||||
public override string ToString() => Name;
|
||||
|
||||
private static string GetPackageSourceFromEnvironment()
|
||||
{
|
||||
return Environment.GetEnvironmentVariable("NUGET_PACKAGE_SOURCE") ??
|
||||
throw new InvalidOperationException("Environment variable NUGET_PACKAGE_SOURCE is required but not set");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
using AspNetCoreSdkTests.Templates;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AspNetCoreSdkTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class SelfContained
|
||||
{
|
||||
[Test]
|
||||
[TestCaseSource(nameof(RestoreData))]
|
||||
public void Restore(Template template)
|
||||
{
|
||||
CollectionAssert.AreEquivalent(template.ExpectedObjFilesAfterRestore, template.ObjFilesAfterRestore);
|
||||
}
|
||||
|
||||
public static IEnumerable<Template> RestoreData = new[]
|
||||
{
|
||||
Template.GetInstance<ClassLibraryTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
Template.GetInstance<ConsoleApplicationTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
Template.GetInstance<RazorClassLibraryTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
Template.GetInstance<WebTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
Template.GetInstance<RazorTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
Template.GetInstance<MvcTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
Template.GetInstance<AngularTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
Template.GetInstance<ReactTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
Template.GetInstance<ReactReduxTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
Template.GetInstance<WebApiTemplate>(NuGetPackageSource.EnvironmentVariable),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -39,8 +39,7 @@ namespace AspNetCoreSdkTests.Util
|
|||
|
||||
public static string Restore(string workingDirectory, NuGetPackageSource packageSource)
|
||||
{
|
||||
var sourceArgument = GetSourceArgument(packageSource);
|
||||
return RunDotNet($"restore --no-cache{sourceArgument}", workingDirectory, GetEnvironment(workingDirectory));
|
||||
return RunDotNet($"restore --no-cache {packageSource.SourceArgument}", workingDirectory, GetEnvironment(workingDirectory));
|
||||
}
|
||||
|
||||
public static string Build(string workingDirectory)
|
||||
|
|
@ -64,24 +63,6 @@ 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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue