// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. #nullable disable using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; using NuGet.Configuration; using NuGet.Frameworks; using NuGet.Packaging.Core; using NuGet.Packaging.Signing; using NuGet.Versioning; namespace NuGet.Test.Utility { public class SimpleTestPackageContext { public SimpleTestPackageContext(string packageId) : this() { Id = packageId; } public SimpleTestPackageContext(string packageId, string version) : this(packageId) { Version = version; } public SimpleTestPackageContext(PackageIdentity identity) : this(identity.Id, identity.Version.ToString()) { } public SimpleTestPackageContext() { } public string Id { get; set; } = "packageA"; public string Version { get; set; } = "1.0.0"; public string VersionOverride { get; set; } public string MinClientVersion { get; set; } public List Dependencies { get; set; } = new List(); public Dictionary> PerFrameworkDependencies { get; set; } = new(); public Dictionary FrameworkReferences { get; set; } = new Dictionary(); public string Include { get; set; } = string.Empty; public string Exclude { get; set; } = string.Empty; // Used by the parent project public string PrivateAssets { get; set; } = string.Empty; public string Aliases { get; set; } = string.Empty; public List> Files { get; set; } = new List>(); public XDocument Nuspec { get; set; } public List PackageTypes { get; set; } = new List(); public PackageType PackageType { get; set; } public string NoWarn { get; set; } public bool UseDefaultRuntimeAssemblies { get; set; } = true; public ITimestampProvider PrimaryTimestampProvider { get; set; } public ITimestampProvider CounterTimestampProvider { get; set; } public bool IsPrimarySigned { get; set; } public bool IsRepositoryCounterSigned { get; set; } public X509Certificate2 PrimarySignatureCertificate { get; set; } public X509Certificate2 RepositoryCountersignatureCertificate { get; set; } public Uri V3ServiceIndexUrl { get; set; } public IReadOnlyList PackageOwners { get; set; } /// /// runtime.json /// public string RuntimeJson { get; set; } public bool IsSymbolPackage { get; set; } public bool IsSnupkgPackage { get; set; } public PackageIdentity Identity => new PackageIdentity(Id, NuGetVersion.Parse(Version)); public string PackageName { get { string dotExtension = !IsSymbolPackage ? NuGetConstants.PackageExtension : (IsSnupkgPackage ? NuGetConstants.SnupkgExtension : NuGetConstants.SymbolsExtension); return $"{Id}.{Version}{dotExtension}"; } } /// /// Add a file to the zip. Ex: lib/net45/a.dll /// 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(path, bytes)); } /// /// Creates a ZipArchive and writes it to a stream. /// public async Task CreateAsStreamAsync() { var stream = new MemoryStream(); await SimpleTestPackageUtility.CreatePackageAsync(stream, this); return stream; } /// /// Creates a file and writes to it. /// /// The directory for the new file. /// The file name. /// A object. public async Task CreateAsFileAsync(TestDirectory testDirectory, string fileName) { var packageFile = new FileInfo(Path.Combine(testDirectory, fileName)); using (var readStream = await CreateAsStreamAsync()) using (var writeStream = packageFile.OpenWrite()) { readStream.CopyTo(writeStream); } return packageFile; } public new string ToString() { return $"{Id}.{Version}"; } } }