NuGetGallery icon indicating copy to clipboard operation
NuGetGallery copied to clipboard

Add copilot instructions for Visual Studio

Open erdembayar opened this issue 9 months ago • 0 comments

The latest version of Visual Studio 2022 allows repo-specific Copilot instructions to better guide AI (LLM) generated code to be more compatible with existing code and follow the same coding pattern, improve code quality. I created this instruction based on https://github.com/dotnet/android/blob/main/.github/copilot-instructions.md and our existing repo-specific documentations. I tested it with prompts to add new services and a few unit test mocks, and it seems to work very well.

We could start with one and improve on it later. For example: prompt: Add new service that reject all the packages don't have proper size logo image image image

prompt: Add missing unit test for this code

using System;
using System.IO;
using System.Threading.Tasks;
using Moq;
using NuGet.Packaging;
using NuGet.Services.Entities;
using NuGetGallery.Packaging;
using Xunit;

namespace NuGetGallery
{
    public class PackageUploadServiceFacts
    {
        private readonly Mock<IPackageService> _packageService;
        private readonly Mock<IPackageFileService> _packageFileService;
        private readonly Mock<IEntitiesContext> _entitiesContext;
        private readonly Mock<IReservedNamespaceService> _reservedNamespaceService;
        private readonly Mock<IValidationService> _validationService;
        private readonly Mock<ICoreLicenseFileService> _coreLicenseFileService;
        private readonly Mock<ICoreReadmeFileService> _coreReadmeFileService;
        private readonly Mock<IPackageVulnerabilitiesManagementService> _vulnerabilityService;
        private readonly Mock<IPackageMetadataValidationService> _metadataValidationService;
        private readonly PackageUploadService _target;

        public PackageUploadServiceFacts()
        {
            _packageService = new Mock<IPackageService>();
            _packageFileService = new Mock<IPackageFileService>();
            _entitiesContext = new Mock<IEntitiesContext>();
            _reservedNamespaceService = new Mock<IReservedNamespaceService>();
            _validationService = new Mock<IValidationService>();
            _coreLicenseFileService = new Mock<ICoreLicenseFileService>();
            _coreReadmeFileService = new Mock<ICoreReadmeFileService>();
            _vulnerabilityService = new Mock<IPackageVulnerabilitiesManagementService>();
            _metadataValidationService = new Mock<IPackageMetadataValidationService>();

            _target = new PackageUploadService(
                _packageService.Object,
                _packageFileService.Object,
                _entitiesContext.Object,
                _reservedNamespaceService.Object,
                _validationService.Object,
                _coreLicenseFileService.Object,
                _coreReadmeFileService.Object,
                Mock.Of<IDiagnosticsService>(),
                _vulnerabilityService.Object,
                _metadataValidationService.Object);
        }

        [Fact]
        public async Task ValidateBeforeGeneratePackageAsync_ValidInputs_ReturnsSuccess()
        {
            // Arrange
            var nuGetPackage = new Mock<PackageArchiveReader>(MockBehavior.Strict, Stream.Null, leaveStreamOpen: false);
            var packageMetadata = new PackageMetadata();
            var currentUser = new User();

            _metadataValidationService
                .Setup(m => m.ValidateMetadataBeforeUploadAsync(nuGetPackage.Object, packageMetadata, currentUser))
                .ReturnsAsync(PackageValidationResult.Valid);

            // Act
            var result = await _target.ValidateBeforeGeneratePackageAsync(nuGetPackage.Object, packageMetadata, currentUser);

            // Assert
            Assert.Equal(PackageValidationResultType.Valid, result.Type);
        }

erdembayar avatar Apr 02 '25 00:04 erdembayar