diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..df9dfe6f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,96 @@ +; This is the default for the codeline. +root = true + +[*] +indent_style = space +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.{htm,html,js,ts,tsx,css,sass,scss,less,svg,vue}] +indent_size = 2 + +[*.{xml,config,*proj,nuspec,props,resx,targets,yml,tasks}] +indent_size = 2 + +[*.json] +indent_size = 2 + +[*.{ps1,psm1}] +indent_size = 4 + +[*.sh] +indent_size = 4 +end_of_line = lf + +[*.cs] +indent_size = 4 + +# .NET Coding Conventions + +# Organize usings +dotnet_sort_system_directives_first = true:warning + +# this. preferences +dotnet_style_qualification_for_field = false:warning +dotnet_style_qualification_for_property = false:warning +dotnet_style_qualification_for_method = false:warning +dotnet_style_qualification_for_event = false:warning + +# Language keywords vs BCL type preferences +dotnet_style_predefined_type_for_locals_parameters_members = true:warning +dotnet_style_predefined_type_for_member_access = true:warning + +# Modifier preferences +dotnet_style_require_accessibility_modifiers = always:warning +dotnet_style_readonly_field = true:warning + +# Naming Conventions + +# Style Definitions +dotnet_naming_style.pascal_case_style.capitalization = pascal_case + +# Use PascalCase for constant fields +dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields +dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style +dotnet_naming_symbols.constant_fields.applicable_kinds = field +dotnet_naming_symbols.constant_fields.applicable_accessibilities = * +dotnet_naming_symbols.constant_fields.required_modifiers = const + +# C# Code Style Rules + +# var preferences +csharp_style_var_for_built_in_types = true:warning +csharp_style_var_when_type_is_apparent = true:warning +csharp_style_var_elsewhere = true:warning + +# Modifier preferences +csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async + +# New line preferences +csharp_new_line_before_open_brace = all +csharp_new_line_before_else = true +csharp_new_line_before_catch = true +csharp_new_line_before_finally = true + +# Indentation preferences +csharp_indent_case_contents = true +csharp_indent_switch_labels = true + +# Space preferences +csharp_space_after_cast = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_parentheses = false +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_around_binary_operators = before_and_after +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false + +# Wrapping preferences +csharp_preserve_single_line_statements = true +csharp_preserve_single_line_blocks = true diff --git a/BaGet.sln b/BaGet.sln index b4879934..8891bf1b 100644 --- a/BaGet.sln +++ b/BaGet.sln @@ -19,9 +19,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{26A0B557-53F EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{C237857D-AD8E-4C52-974F-6A8155BB0C18}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BaGet.Protocol", "src\BaGet.Protocol\BaGet.Protocol.csproj", "{A2D23427-9278-4D52-B31F-759212252832}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.Protocol", "src\BaGet.Protocol\BaGet.Protocol.csproj", "{A2D23427-9278-4D52-B31F-759212252832}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BaGet.Protocol.Tests", "tests\BaGet.Protocol.Tests\BaGet.Protocol.Tests.csproj", "{AC764A9A-9EAF-422B-9223-D3290C3CFD79}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.Protocol.Tests", "tests\BaGet.Protocol.Tests\BaGet.Protocol.Tests.csproj", "{AC764A9A-9EAF-422B-9223-D3290C3CFD79}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0B44364D-952B-497A-82E0-C9AAE94E0369}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/BaGet.Azure/Extensions/ServiceCollectionExtensions.cs b/src/BaGet.Azure/Extensions/ServiceCollectionExtensions.cs index fd62e396..0dc84efe 100644 --- a/src/BaGet.Azure/Extensions/ServiceCollectionExtensions.cs +++ b/src/BaGet.Azure/Extensions/ServiceCollectionExtensions.cs @@ -1,6 +1,5 @@ -using BaGet.Azure.Configuration; +using BaGet.Azure.Configuration; using BaGet.Azure.Search; -using BaGet.Core.Services; using Microsoft.Azure.Search; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; diff --git a/src/BaGet.Core/Extensions/PackageArchiveReaderExtensions.cs b/src/BaGet.Core/Extensions/PackageArchiveReaderExtensions.cs index 98349150..f98aecec 100644 --- a/src/BaGet.Core/Extensions/PackageArchiveReaderExtensions.cs +++ b/src/BaGet.Core/Extensions/PackageArchiveReaderExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/src/BaGet.Core/Extensions/StreamExtensions.cs b/src/BaGet.Core/Extensions/StreamExtensions.cs index 82cf6e17..28994a6e 100644 --- a/src/BaGet.Core/Extensions/StreamExtensions.cs +++ b/src/BaGet.Core/Extensions/StreamExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Linq; using System.Security.Cryptography; diff --git a/src/BaGet.Core/Services/ApiKeyAuthenticationService.cs b/src/BaGet.Core/Services/ApiKeyAuthenticationService.cs index 44895708..8dcddb67 100644 --- a/src/BaGet.Core/Services/ApiKeyAuthenticationService.cs +++ b/src/BaGet.Core/Services/ApiKeyAuthenticationService.cs @@ -1,6 +1,4 @@ using System; -using System.Security.Cryptography; -using System.Text; using System.Threading.Tasks; using BaGet.Core.Configuration; using Microsoft.Extensions.Options; @@ -23,10 +21,9 @@ public ApiKeyAuthenticationService(IOptionsSnapshot options) private bool Authenticate(string apiKey) { // No authentication is necessary if there is no required API key. - if (_apiKey == null) - return true; + if (_apiKey == null) return true; return _apiKey == apiKey; } } -} \ No newline at end of file +} diff --git a/src/BaGet.Core/Services/DatabaseSearchService.cs b/src/BaGet.Core/Services/DatabaseSearchService.cs index a02e2df8..63ece1c1 100644 --- a/src/BaGet.Core/Services/DatabaseSearchService.cs +++ b/src/BaGet.Core/Services/DatabaseSearchService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -28,6 +28,9 @@ public async Task> SearchAsync(string query, int ski search = search.Where(p => p.Id.ToLower().Contains(query)); } + // This query MUST fetch all versions for each returned package, otherwise this could return + // incorrect results for a package's latest version. This query first finds package ids, + // then it returns all versions for those package ids. var packages = await _context.Packages .Where(p => search.Select(p2 => p2.Id) diff --git a/src/BaGet.Tools.AzureSearchImporter/Entities/IndexerContextFactory.cs b/src/BaGet.Tools.AzureSearchImporter/Entities/IndexerContextFactory.cs index da35befd..6b740940 100644 --- a/src/BaGet.Tools.AzureSearchImporter/Entities/IndexerContextFactory.cs +++ b/src/BaGet.Tools.AzureSearchImporter/Entities/IndexerContextFactory.cs @@ -1,9 +1,9 @@ -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; namespace BaGet.Tools.AzureSearchImporter.Entities { - class IndexerContextFactory : IDesignTimeDbContextFactory + public class IndexerContextFactory : IDesignTimeDbContextFactory { public const string ConnectionString = "Data Source=indexer.db"; diff --git a/src/BaGet.Tools.AzureSearchImporter/Initializer.cs b/src/BaGet.Tools.AzureSearchImporter/Initializer.cs index 733be192..0794c857 100644 --- a/src/BaGet.Tools.AzureSearchImporter/Initializer.cs +++ b/src/BaGet.Tools.AzureSearchImporter/Initializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Threading.Tasks; using BaGet.Azure.Search; diff --git a/src/BaGet.Tools.AzureSearchImporter/Program.cs b/src/BaGet.Tools.AzureSearchImporter/Program.cs index 7f93280f..ab9ac012 100644 --- a/src/BaGet.Tools.AzureSearchImporter/Program.cs +++ b/src/BaGet.Tools.AzureSearchImporter/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading.Tasks; using BaGet.Azure.Extensions; using BaGet.Core.Configuration; @@ -12,7 +12,7 @@ namespace BaGet.Tools.AzureSearchImporter { - class Program + public class Program { public static void Main(string[] args) => MainAsync(args) @@ -22,7 +22,7 @@ public static void Main(string[] args) private async static Task MainAsync(string[] args) { // Parse the skip from arguments. - int skip = 0; + var skip = 0; if (args.Length > 0) { diff --git a/src/BaGet/Extensions/ServiceCollectionExtensions.cs b/src/BaGet/Extensions/ServiceCollectionExtensions.cs index a135eb54..5e3a6ef2 100644 --- a/src/BaGet/Extensions/ServiceCollectionExtensions.cs +++ b/src/BaGet/Extensions/ServiceCollectionExtensions.cs @@ -1,7 +1,6 @@ -using System; +using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.IO; using System.Net; using System.Net.Http; using System.Reflection; diff --git a/src/BaGet/Routes.cs b/src/BaGet/Routes.cs index bcb0332a..2de7ca73 100644 --- a/src/BaGet/Routes.cs +++ b/src/BaGet/Routes.cs @@ -1,4 +1,4 @@ -namespace BaGet +namespace BaGet { public class Routes { @@ -16,6 +16,5 @@ public class Routes public const string PackageDownloadManifestRouteName = "package-download-manifest"; public const string PackageDownloadReadmeRouteName = "package-download-readme"; public const string SymbolDownloadRouteName = "symbol-download"; - } } diff --git a/tests/BaGet.Core.Tests/Services/FileStorageServiceTests.cs b/tests/BaGet.Core.Tests/Services/FileStorageServiceTests.cs index 3984b947..f2d58961 100644 --- a/tests/BaGet.Core.Tests/Services/FileStorageServiceTests.cs +++ b/tests/BaGet.Core.Tests/Services/FileStorageServiceTests.cs @@ -1,7 +1,6 @@ -using System; +using System; using System.IO; using System.Text; -using System.Threading; using System.Threading.Tasks; using BaGet.Core.Configuration; using BaGet.Core.Services; diff --git a/tests/BaGet.Core.Tests/Services/PackageDeletionServiceTests.cs b/tests/BaGet.Core.Tests/Services/PackageDeletionServiceTests.cs index 51ef11c9..78e954ef 100644 --- a/tests/BaGet.Core.Tests/Services/PackageDeletionServiceTests.cs +++ b/tests/BaGet.Core.Tests/Services/PackageDeletionServiceTests.cs @@ -1,4 +1,4 @@ -using System.Threading; +using System.Threading; using System.Threading.Tasks; using BaGet.Core.Configuration; using BaGet.Core.Services; diff --git a/tests/BaGet.Protocol.Tests/PackageContentClientTests.cs b/tests/BaGet.Protocol.Tests/PackageContentClientTests.cs index b3dd0693..0f277469 100644 --- a/tests/BaGet.Protocol.Tests/PackageContentClientTests.cs +++ b/tests/BaGet.Protocol.Tests/PackageContentClientTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net.Http; using System.Threading.Tasks; using Xunit; diff --git a/tests/BaGet.Tests/NuGetClientIntegrationTest.cs b/tests/BaGet.Tests/NuGetClientIntegrationTest.cs index 81dbb550..a0c89d8e 100644 --- a/tests/BaGet.Tests/NuGetClientIntegrationTest.cs +++ b/tests/BaGet.Tests/NuGetClientIntegrationTest.cs @@ -1,17 +1,16 @@ using System; -using System.Linq; -using Xunit.Abstractions; -using Microsoft.Extensions.Logging; -using Microsoft.AspNetCore.TestHost; -using Xunit; -using System.Threading.Tasks; using System.Collections.Generic; -using NuGet.Protocol.Core.Types; -using NuGet.Protocol; -using NuGet.Configuration; -using BaGet.Tests.Support; -using System.Net.Http; using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using BaGet.Tests.Support; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Logging; +using NuGet.Configuration; +using NuGet.Protocol; +using NuGet.Protocol.Core.Types; +using Xunit; +using Xunit.Abstractions; namespace BaGet.Tests { @@ -22,17 +21,17 @@ public class NuGetClientIntegrationTest : IDisposable { protected readonly ITestOutputHelper Helper; private readonly TestServer server; - readonly string IndexUrlString = "v3/index.json"; - SourceRepository _sourceRepository; - private SourceCacheContext _cacheContext; - HttpSourceResource _httpSource; + private readonly string IndexUrlString = "v3/index.json"; + private SourceRepository _sourceRepository; + private readonly SourceCacheContext _cacheContext; + private HttpSourceResource _httpSource; private HttpClient _httpClient; - string indexUrl; + private readonly string indexUrl; public NuGetClientIntegrationTest(ITestOutputHelper helper) { Helper = helper ?? throw new ArgumentNullException(nameof(helper)); - server = TestServerBuilder.Create().TraceToTestOutputHelper(Helper,LogLevel.Error).Build(); + server = TestServerBuilder.Create().TraceToTestOutputHelper(Helper, LogLevel.Error).Build(); var providers = new List>(); providers.AddRange(Repository.Provider.GetCoreV3()); providers.Add(new Lazy(() => new PackageMetadataResourceV3Provider())); @@ -40,16 +39,15 @@ public NuGetClientIntegrationTest(ITestOutputHelper helper) providers.Insert(0, new Lazy(() => new HttpSourceResourceProviderTestHost(_httpClient))); indexUrl = new Uri(server.BaseAddress, IndexUrlString).AbsoluteUri; - PackageSource packageSource = new PackageSource(indexUrl); + var packageSource = new PackageSource(indexUrl); _sourceRepository = new SourceRepository(packageSource, providers); - _cacheContext = new SourceCacheContext() { NoCache = true, MaxAge=new DateTimeOffset(), DirectDownload=true }; + _cacheContext = new SourceCacheContext() { NoCache = true, MaxAge = new DateTimeOffset(), DirectDownload = true }; _httpSource = _sourceRepository.GetResource(); Assert.IsType(_httpSource.HttpSource); } public void Dispose() { - if(server != null) - server.Dispose(); + server?.Dispose(); } [Fact] @@ -94,6 +92,5 @@ public async Task IndexIncludesAtLeastOneSearchAutocompleteServiceEntry() var indexResource = await _sourceRepository.GetResourceAsync(); Assert.NotEmpty(indexResource.GetServiceEntries("SearchAutocompleteService")); } - } -} \ No newline at end of file +} diff --git a/tests/BaGet.Tests/PackageControllerTest.cs b/tests/BaGet.Tests/PackageControllerTest.cs index 3d001ea3..a991331a 100644 --- a/tests/BaGet.Tests/PackageControllerTest.cs +++ b/tests/BaGet.Tests/PackageControllerTest.cs @@ -1,17 +1,15 @@ using System; using System.Threading.Tasks; -using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.Logging; using Xunit; using Xunit.Abstractions; namespace BaGet.Tests { - public class PackageControllerTest + public class PackageControllerTest { protected readonly ITestOutputHelper Helper; - - readonly string IndexUrlFormatString = "v3/package/{0}/index.json"; + private readonly string IndexUrlFormatString = "v3/package/{0}/index.json"; public PackageControllerTest(ITestOutputHelper helper) { @@ -23,10 +21,10 @@ public PackageControllerTest(ITestOutputHelper helper) [InlineData("id02")] public async Task AskEmptyServerForNotExistingPackageID(string packageID) { - using (TestServer server = TestServerBuilder.Create().TraceToTestOutputHelper(Helper,LogLevel.Error).Build()) + using (var server = TestServerBuilder.Create().TraceToTestOutputHelper(Helper, LogLevel.Error).Build()) { //Ask Empty Storage for a not existings ID - var response = await server.CreateClient().GetAsync(string.Format(IndexUrlFormatString, packageID)); + var response = await server.CreateClient().GetAsync(string.Format(IndexUrlFormatString, packageID)); Assert.Equal(System.Net.HttpStatusCode.NotFound, response.StatusCode); } } diff --git a/tests/BaGet.Tests/TestServerBuilder.cs b/tests/BaGet.Tests/TestServerBuilder.cs index 24b031fd..53bdc744 100644 --- a/tests/BaGet.Tests/TestServerBuilder.cs +++ b/tests/BaGet.Tests/TestServerBuilder.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using BaGet.Core.Configuration; @@ -78,10 +78,10 @@ public static TestServerBuilder Create() private TestServerBuilder UseEmptyTempFolder() { Configuration.Add(DatabaseTypeKey, DatabaseType.Sqlite.ToString()); - string uniqueTempFolder = Path.Combine(Path.GetTempPath(), System.Guid.NewGuid().ToString("N")); + var uniqueTempFolder = Path.Combine(Path.GetTempPath(), System.Guid.NewGuid().ToString("N")); Directory.CreateDirectory(uniqueTempFolder); - string resolvedSqliteFile = Path.Combine(uniqueTempFolder, "BaGet.db"); - string storageFolderPath = Path.Combine(uniqueTempFolder, DefaultPackagesFolderName); + var resolvedSqliteFile = Path.Combine(uniqueTempFolder, "BaGet.db"); + var storageFolderPath = Path.Combine(uniqueTempFolder, DefaultPackagesFolderName); Configuration.Add(ConnectionStringKey, string.Format("Data Source={0}", resolvedSqliteFile)); Configuration.Add(StorageTypeKey, StorageType.FileSystem.ToString()); Configuration.Add(FileSystemStoragePathKey, storageFolderPath); @@ -92,8 +92,8 @@ private TestServerBuilder UseEmptyTempFolder() public TestServer Build() { - IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().AddInMemoryCollection(Configuration); - IWebHostBuilder hostBuilder = new WebHostBuilder() + var configurationBuilder = new ConfigurationBuilder().AddInMemoryCollection(Configuration); + var hostBuilder = new WebHostBuilder() .UseConfiguration(configurationBuilder.Build()) .UseStartup(); @@ -106,7 +106,7 @@ public TestServer Build() }); } - TestServer server = new TestServer(hostBuilder); + var server = new TestServer(hostBuilder); //Ensure that the Database is created, we use the same feature like inside the Startup in case of Env.IsDevelopment (EF-Migrations) var scopeFactory = server.Host.Services.GetRequiredService(); diff --git a/tests/BaGet.Tests/XunitLogger.cs b/tests/BaGet.Tests/XunitLogger.cs index 5b54f623..ec2de7b4 100644 --- a/tests/BaGet.Tests/XunitLogger.cs +++ b/tests/BaGet.Tests/XunitLogger.cs @@ -1,12 +1,10 @@ -using System; +using System; using Microsoft.Extensions.Logging; using Xunit.Abstractions; namespace BaGet.Tests { - //https://stackoverflow.com/questions/46169169/net-core-2-0-configurelogging-xunit-test - - + // https://stackoverflow.com/questions/46169169/net-core-2-0-configurelogging-xunit-test public class XunitLogger : ILogger { private readonly ITestOutputHelper _testOutputHelper; diff --git a/tests/BaGet.Tests/XunitLoggerProvider.cs b/tests/BaGet.Tests/XunitLoggerProvider.cs index 9a4d99cd..2f9ac48e 100644 --- a/tests/BaGet.Tests/XunitLoggerProvider.cs +++ b/tests/BaGet.Tests/XunitLoggerProvider.cs @@ -1,11 +1,10 @@ -using System; +using System; using Microsoft.Extensions.Logging; using Xunit.Abstractions; namespace BaGet.Tests { - //https://stackoverflow.com/questions/46169169/net-core-2-0-configurelogging-xunit-test - + // https://stackoverflow.com/questions/46169169/net-core-2-0-configurelogging-xunit-test public class XunitLoggerProvider : ILoggerProvider { private readonly ITestOutputHelper _testOutputHelper;