2017-09-22 16:56:25 -07:00
|
|
|
// Copyright (c) .NET Foundation. All rights reserved.
|
2016-01-20 14:58:50 -08:00
|
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
|
|
2025-11-20 21:13:26 +10:30
|
|
|
#nullable disable
|
|
|
|
|
|
2016-03-02 12:59:12 -08:00
|
|
|
using System;
|
2016-01-20 14:58:50 -08:00
|
|
|
using System.Collections.Generic;
|
2017-09-22 16:56:25 -07:00
|
|
|
using System.IO;
|
2018-01-23 18:21:07 -06:00
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2016-03-02 12:59:12 -08:00
|
|
|
using System.Text;
|
2018-04-13 17:17:47 -07:00
|
|
|
using System.Threading.Tasks;
|
2016-03-02 12:59:12 -08:00
|
|
|
using System.Xml.Linq;
|
2019-11-21 17:00:37 -08:00
|
|
|
using NuGet.Configuration;
|
2019-03-27 21:44:39 -07:00
|
|
|
using NuGet.Frameworks;
|
2016-01-20 14:58:50 -08:00
|
|
|
using NuGet.Packaging.Core;
|
2017-09-22 16:56:25 -07:00
|
|
|
using NuGet.Packaging.Signing;
|
2016-01-20 14:58:50 -08:00
|
|
|
using NuGet.Versioning;
|
|
|
|
|
|
|
|
|
|
namespace NuGet.Test.Utility
|
|
|
|
|
{
|
|
|
|
|
public class SimpleTestPackageContext
|
|
|
|
|
{
|
2016-03-03 10:58:39 -08:00
|
|
|
public SimpleTestPackageContext(string packageId)
|
2019-11-21 17:00:37 -08:00
|
|
|
: this()
|
2016-03-03 10:58:39 -08:00
|
|
|
{
|
|
|
|
|
Id = packageId;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 15:49:56 -07:00
|
|
|
public SimpleTestPackageContext(string packageId, string version)
|
2019-11-21 17:00:37 -08:00
|
|
|
: this(packageId)
|
2016-05-20 15:49:56 -07:00
|
|
|
{
|
|
|
|
|
Version = version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SimpleTestPackageContext(PackageIdentity identity)
|
2018-01-18 12:52:07 -08:00
|
|
|
: this(identity.Id, identity.Version.ToString())
|
2016-05-20 15:49:56 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 10:58:39 -08:00
|
|
|
public SimpleTestPackageContext()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 14:58:50 -08:00
|
|
|
public string Id { get; set; } = "packageA";
|
|
|
|
|
public string Version { get; set; } = "1.0.0";
|
2022-02-28 15:02:11 -08:00
|
|
|
public string VersionOverride { get; set; }
|
2016-05-10 12:34:26 -07:00
|
|
|
public string MinClientVersion { get; set; }
|
2016-01-20 14:58:50 -08:00
|
|
|
public List<SimpleTestPackageContext> Dependencies { get; set; } = new List<SimpleTestPackageContext>();
|
2021-06-08 15:49:39 -07:00
|
|
|
public Dictionary<NuGetFramework, List<SimpleTestPackageContext>> PerFrameworkDependencies { get; set; } = new();
|
2019-03-27 21:44:39 -07:00
|
|
|
public Dictionary<NuGetFramework, string[]> FrameworkReferences { get; set; } = new Dictionary<NuGetFramework, string[]>();
|
2016-01-20 14:58:50 -08:00
|
|
|
public string Include { get; set; } = string.Empty;
|
|
|
|
|
public string Exclude { get; set; } = string.Empty;
|
2016-09-15 02:03:49 -07:00
|
|
|
// Used by the parent project
|
|
|
|
|
public string PrivateAssets { get; set; } = string.Empty;
|
2020-04-23 12:07:53 -07:00
|
|
|
public string Aliases { get; set; } = string.Empty;
|
2016-03-02 12:59:12 -08:00
|
|
|
public List<KeyValuePair<string, byte[]>> Files { get; set; } = new List<KeyValuePair<string, byte[]>>();
|
|
|
|
|
public XDocument Nuspec { get; set; }
|
2016-06-08 17:15:49 -07:00
|
|
|
public List<PackageType> PackageTypes { get; set; } = new List<PackageType>();
|
2017-04-19 10:26:33 -07:00
|
|
|
public PackageType PackageType { get; set; }
|
2017-06-12 14:18:24 -07:00
|
|
|
public string NoWarn { get; set; }
|
2016-01-20 14:58:50 -08:00
|
|
|
|
2018-01-18 12:52:07 -08:00
|
|
|
public bool UseDefaultRuntimeAssemblies { get; set; } = true;
|
|
|
|
|
|
2018-04-17 15:13:47 -07:00
|
|
|
public ITimestampProvider PrimaryTimestampProvider { get; set; }
|
|
|
|
|
public ITimestampProvider CounterTimestampProvider { get; set; }
|
2018-04-13 17:17:47 -07:00
|
|
|
public bool IsPrimarySigned { get; set; }
|
|
|
|
|
public bool IsRepositoryCounterSigned { get; set; }
|
2018-03-14 14:52:35 -07:00
|
|
|
public X509Certificate2 PrimarySignatureCertificate { get; set; }
|
|
|
|
|
public X509Certificate2 RepositoryCountersignatureCertificate { get; set; }
|
|
|
|
|
public Uri V3ServiceIndexUrl { get; set; }
|
|
|
|
|
public IReadOnlyList<string> PackageOwners { get; set; }
|
2017-09-22 16:56:25 -07:00
|
|
|
|
2016-01-25 18:36:56 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// runtime.json
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string RuntimeJson { get; set; }
|
|
|
|
|
|
2017-03-06 10:09:46 -08:00
|
|
|
public bool IsSymbolPackage { get; set; }
|
2019-11-21 17:00:37 -08:00
|
|
|
public bool IsSnupkgPackage { get; set; }
|
2017-03-06 10:09:46 -08:00
|
|
|
|
2017-04-19 10:26:33 -07:00
|
|
|
public PackageIdentity Identity => new PackageIdentity(Id, NuGetVersion.Parse(Version));
|
2016-03-02 12:59:12 -08:00
|
|
|
|
2019-11-21 17:00:37 -08:00
|
|
|
public string PackageName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string dotExtension = !IsSymbolPackage ? NuGetConstants.PackageExtension : (IsSnupkgPackage ? NuGetConstants.SnupkgExtension : NuGetConstants.SymbolsExtension);
|
|
|
|
|
return $"{Id}.{Version}{dotExtension}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-23 18:21:07 -06:00
|
|
|
|
2016-03-02 12:59:12 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Add a file to the zip. Ex: lib/net45/a.dll
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void AddFile(string path)
|
|
|
|
|
{
|
|
|
|
|
AddFile(path, new byte[] { 0 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddFile(string path, string content)
|
|
|
|
|
{
|
|
|
|
|
AddFile(path, Encoding.UTF8.GetBytes(content));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddFile(string path, byte[] bytes)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(path) || path.StartsWith("/") || path.IndexOf('\\') > -1)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(nameof(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Files.Add(new KeyValuePair<string, byte[]>(path, bytes));
|
|
|
|
|
}
|
2017-09-22 16:56:25 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a ZipArchive and writes it to a stream.
|
|
|
|
|
/// </summary>
|
2018-04-13 17:17:47 -07:00
|
|
|
public async Task<MemoryStream> CreateAsStreamAsync()
|
2017-09-22 16:56:25 -07:00
|
|
|
{
|
|
|
|
|
var stream = new MemoryStream();
|
2018-04-13 17:17:47 -07:00
|
|
|
await SimpleTestPackageUtility.CreatePackageAsync(stream, this);
|
2017-09-22 16:56:25 -07:00
|
|
|
return stream;
|
|
|
|
|
}
|
2018-01-12 17:51:49 -08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a file and writes to it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="testDirectory">The directory for the new file.</param>
|
|
|
|
|
/// <param name="fileName">The file name.</param>
|
|
|
|
|
/// <returns>A <see cref="FileInfo" /> object.</returns>
|
2018-04-13 17:17:47 -07:00
|
|
|
public async Task<FileInfo> CreateAsFileAsync(TestDirectory testDirectory, string fileName)
|
2018-01-12 17:51:49 -08:00
|
|
|
{
|
|
|
|
|
var packageFile = new FileInfo(Path.Combine(testDirectory, fileName));
|
|
|
|
|
|
2018-04-13 17:17:47 -07:00
|
|
|
using (var readStream = await CreateAsStreamAsync())
|
2018-01-12 17:51:49 -08:00
|
|
|
using (var writeStream = packageFile.OpenWrite())
|
|
|
|
|
{
|
|
|
|
|
readStream.CopyTo(writeStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return packageFile;
|
|
|
|
|
}
|
2018-06-13 17:28:24 -07:00
|
|
|
|
|
|
|
|
public new string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{Id}.{Version}";
|
|
|
|
|
}
|
2016-01-20 14:58:50 -08:00
|
|
|
}
|
2019-11-21 17:00:37 -08:00
|
|
|
}
|