From 3530ba3205a97193f0a773635371d5ef4d548237 Mon Sep 17 00:00:00 2001 From: Sean Fausett Date: Fri, 11 Jun 2021 02:32:33 +1200 Subject: [PATCH] [dotnet] Fix Resharper code issues (#7178) * Fix resharper code issues for language usage opportunities * Fix resharper code issues for common practices and code improvements * Fix resharper code issues for potential code quality issues * Fix resharper code issues for redundancies in code * Fix xunit test output * Update changelog * Fix resharper code issues for compiler warnings * Fix resharper code issues for inconsistent naming * Add resharper solution settings file * Fix resharper code issues for potential code quality issues * Fix resharper code issues for redundancies in code * Fix resharper code issues for redundancies in symbol declarations --- CHANGELOG_PENDING.md | 2 + .../CommandExceptionTests.cs | 3 +- .../EventLogWatcherTests.cs | 13 +- .../LocalPulumiCmdTests.cs | 10 +- .../LocalWorkspaceTests.cs | 272 +++++++++--------- .../Serialization/DynamicObjectTests.cs | 2 +- .../GeneralJsonConverterTests.cs | 2 +- .../ProjectRuntimeJsonConverterTests.cs | 14 +- .../ProjectRuntimeYamlConverterTests.cs | 13 +- ...ckSettingsConfigValueJsonConverterTests.cs | 6 +- ...ckSettingsConfigValueYamlConverterTests.cs | 6 +- .../Collections/DictionaryContentsComparer.cs | 16 +- .../Commands/Exceptions/CommandException.cs | 12 +- .../Pulumi.Automation/Commands/IPulumiCmd.cs | 2 +- .../Commands/LocalPulumiCmd.cs | 72 ++--- .../Pulumi.Automation/DestroyOptions.cs | 2 - .../Events/EventLogWatcher.cs | 13 +- .../Exceptions/NoSummaryEventException.cs | 4 +- .../Pulumi.Automation/LocalWorkspace.cs | 36 ++- .../Pulumi.Automation/ProjectBackend.cs | 2 +- .../Pulumi.Automation/ProjectRuntime.cs | 2 +- .../Pulumi.Automation/ProjectRuntimeName.cs | 1 + .../ProjectRuntimeOptions.cs | 2 +- .../Pulumi.Automation/ProjectSettings.cs | 11 +- .../Pulumi.Automation/ProjectTemplate.cs | 4 +- .../ProjectTemplateConfigValue.cs | 2 +- .../Pulumi.Automation/PulumiFn.Inline.cs | 1 - .../PulumiFn.ServiceProvider.cs | 4 +- .../Pulumi.Automation/PulumiFn.TStack.cs | 2 +- sdk/dotnet/Pulumi.Automation/PulumiFn.cs | 22 +- .../Pulumi.Automation/RefreshOptions.cs | 2 - .../Runtime/LanguageRuntimeService.cs | 3 +- .../Serialization/DiagnosticEventModel.cs | 2 +- .../Serialization/EngineEventModel.cs | 2 +- .../Json/SystemObjectJsonConverter.cs | 4 +- .../Serialization/PolicyEventModel.cs | 2 +- .../Serialization/PreludeEventModel.cs | 2 +- .../Serialization/PropertyDiffModel.cs | 2 +- .../ResourceOperationFailedEventModel.cs | 2 +- .../ResourceOutputsEventModel.cs | 2 +- .../Serialization/ResourcePreEventModel.cs | 2 +- .../StandardOutputEngineEventModel.cs | 2 +- .../Serialization/StepEventMetadataModel.cs | 4 +- .../StepEventStateMetadataModel.cs | 2 +- .../Serialization/SummaryEventModel.cs | 4 +- .../ProjectRuntimeOptionsYamlConverter.cs | 15 +- .../Yaml/ProjectRuntimeYamlConverter.cs | 13 +- .../StackSettingsConfigValueYamlConverter.cs | 12 +- sdk/dotnet/Pulumi.Automation/StackSettings.cs | 14 +- sdk/dotnet/Pulumi.Automation/Workspace.cs | 8 +- .../Pulumi.Automation/WorkspaceStack.cs | 53 ++-- sdk/dotnet/Pulumi.Tests/Core/InputTests.cs | 24 +- sdk/dotnet/Pulumi.Tests/Core/OutputTests.cs | 2 +- .../Pulumi.Tests/Core/ResourceArgsTests.cs | 10 +- sdk/dotnet/Pulumi.Tests/Mocks/MocksTests.cs | 31 +- sdk/dotnet/Pulumi.Tests/Mocks/TestStack.cs | 19 +- .../Serialization/BooleanConverterTests.cs | 4 +- .../ComplexTypeConverterTests.cs | 2 +- .../Serialization/EnumConverterTests.cs | 16 +- .../Serialization/InternalPropertyTests.cs | 3 - .../Serialization/ResourceRefPropertyTests.cs | 46 +-- .../Serialization/UnionConverterTests.cs | 2 +- sdk/dotnet/Pulumi.Tests/StackTests.cs | 6 +- sdk/dotnet/Pulumi/Config.cs | 26 +- sdk/dotnet/Pulumi/Config_Exceptions.cs | 7 +- sdk/dotnet/Pulumi/Core/AssetOrArchive.cs | 8 +- sdk/dotnet/Pulumi/Core/InputList.cs | 6 +- sdk/dotnet/Pulumi/Core/InputMap.cs | 6 +- sdk/dotnet/Pulumi/Core/InputUnion.cs | 6 +- sdk/dotnet/Pulumi/Core/Options.cs | 1 + sdk/dotnet/Pulumi/Core/Output.cs | 8 +- sdk/dotnet/Pulumi/Core/Union.cs | 4 +- sdk/dotnet/Pulumi/Core/Urn.cs | 5 +- .../Pulumi/Deployment/Deployment.Runner.cs | 15 +- sdk/dotnet/Pulumi/Deployment/Deployment.cs | 9 +- .../Pulumi/Deployment/Deployment_Invoke.cs | 1 + .../Pulumi/Deployment/Deployment_Prepare.cs | 59 ++-- .../Deployment_ReadOrRegisterResource.cs | 3 +- .../Deployment/Deployment_ReadResource.cs | 7 +- .../Deployment/Deployment_RegisterResource.cs | 10 +- .../Deployment_RegisterResourceOutputs.cs | 3 +- .../Deployment/Deployment_RootResource.cs | 2 +- .../Pulumi/Deployment/Deployment_Run.cs | 4 +- .../Deployment/Deployment_Serialization.cs | 3 +- sdk/dotnet/Pulumi/Deployment/GrpcEngine.cs | 2 +- sdk/dotnet/Pulumi/Deployment/GrpcMonitor.cs | 2 +- sdk/dotnet/Pulumi/Deployment/InvokeOptions.cs | 3 +- .../Pulumi/Exceptions/ResourceException.cs | 4 +- sdk/dotnet/Pulumi/Extensions.cs | 3 +- .../Resources/ComponentResourceOptions.cs | 3 +- .../Pulumi/Resources/CustomResourceOptions.cs | 2 +- .../Resources/DependencyProviderResource.cs | 6 +- sdk/dotnet/Pulumi/Resources/InputArgs.cs | 12 +- sdk/dotnet/Pulumi/Resources/InvokeArgs.cs | 4 - .../Pulumi/Resources/ProviderResource.cs | 12 +- sdk/dotnet/Pulumi/Resources/Resource.cs | 17 +- sdk/dotnet/Pulumi/Resources/ResourceArgs.cs | 4 - .../Pulumi/Resources/ResourceOptions.cs | 6 +- sdk/dotnet/Pulumi/Resources/StackReference.cs | 3 +- sdk/dotnet/Pulumi/Serialization/Converter.cs | 79 +++-- .../Pulumi/Serialization/Deserializer.cs | 10 +- .../Serialization/OutputCompletionSource.cs | 9 +- sdk/dotnet/Pulumi/Serialization/OutputData.cs | 2 +- .../Pulumi/Serialization/ResourcePackages.cs | 11 +- sdk/dotnet/Pulumi/Serialization/Serializer.cs | 11 +- sdk/dotnet/Pulumi/Stack.cs | 11 +- sdk/dotnet/Pulumi/Testing/IMocks.cs | 1 - sdk/dotnet/Pulumi/Testing/MockEngine.cs | 1 - sdk/dotnet/Pulumi/Testing/MockMonitor.cs | 2 +- sdk/dotnet/dotnet.sln.DotSettings | 28 ++ 110 files changed, 624 insertions(+), 687 deletions(-) create mode 100644 sdk/dotnet/dotnet.sln.DotSettings diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 36ed86483..9ad8d6e8b 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -1,5 +1,7 @@ ### Improvements +- [dotnet] Fix Resharper code issues. + [#7178](https://github.com/pulumi/pulumi/pull/7178) - [codegen] - Include properties with an underlying type of string on Go provider instances. diff --git a/sdk/dotnet/Pulumi.Automation.Tests/CommandExceptionTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/CommandExceptionTests.cs index ee0a9a7b9..db7efc368 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/CommandExceptionTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/CommandExceptionTests.cs @@ -80,8 +80,9 @@ namespace Pulumi.Automation.Tests var program = PulumiFn.Create(() => { hitSemaphore = true; + // ReSharper disable once AccessToDisposedClosure semaphore.Wait(); - return new Dictionary() + return new Dictionary { ["test"] = "doesnt matter", }; diff --git a/sdk/dotnet/Pulumi.Automation.Tests/EventLogWatcherTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/EventLogWatcherTests.cs index 502dbbf82..af4b72ebb 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/EventLogWatcherTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/EventLogWatcherTests.cs @@ -1,9 +1,9 @@ // Copyright 2016-2021, Pulumi Corporation +using System; +using System.IO; using System.Threading; using System.Threading.Tasks; -using System.IO; -using System; using Pulumi.Automation.Events; using Xunit; @@ -37,12 +37,7 @@ namespace Pulumi.Automation.Tests [Fact] public async Task PropagatesUserExceptionsToCaller() { - using var fx = new Fixture(); - - fx.Action = ev => - { - throw new MyException(); - }; + using var fx = new Fixture { Action = ev => throw new MyException() }; await fx.Write("{}"); @@ -62,7 +57,7 @@ namespace Pulumi.Automation.Tests private class MyException : Exception { } - private class Fixture : IDisposable + private sealed class Fixture : IDisposable { public int EventCounter; public string LogFileName { get; } diff --git a/sdk/dotnet/Pulumi.Automation.Tests/LocalPulumiCmdTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/LocalPulumiCmdTests.cs index 9184fc60c..62aabffa8 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/LocalPulumiCmdTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/LocalPulumiCmdTests.cs @@ -2,10 +2,10 @@ using System; using System.Collections.Generic; -using System.Threading.Tasks; -using Xunit; -using Pulumi.Automation.Commands; using System.Linq; +using System.Threading.Tasks; +using Pulumi.Automation.Commands; +using Xunit; namespace Pulumi.Automation.Tests { @@ -15,8 +15,8 @@ namespace Pulumi.Automation.Tests public async Task CheckVersionCommand() { var localCmd = new LocalPulumiCmd(); - IDictionary extraEnv = new Dictionary(); - IEnumerable args = new string[]{ "version" }; + var extraEnv = new Dictionary(); + var args = new[] { "version" }; var stdoutLines = new List(); var stderrLines = new List(); diff --git a/sdk/dotnet/Pulumi.Automation.Tests/LocalWorkspaceTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/LocalWorkspaceTests.cs index 553b3df0e..a2dadd1c4 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/LocalWorkspaceTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/LocalWorkspaceTests.cs @@ -1,19 +1,20 @@ // Copyright 2016-2021, Pulumi Corporation -using Semver; using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.IO; using System.Linq; using System.Runtime.CompilerServices; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; -using Pulumi.Automation.Exceptions; +using Microsoft.Extensions.DependencyInjection; using Pulumi.Automation.Commands.Exceptions; using Pulumi.Automation.Events; +using Pulumi.Automation.Exceptions; +using Semver; using Xunit; -using System.Collections.Immutable; -using Microsoft.Extensions.DependencyInjection; namespace Pulumi.Automation.Tests { @@ -124,7 +125,7 @@ namespace Pulumi.Automation.Tests using var workspace = await LocalWorkspace.CreateAsync(new LocalWorkspaceOptions { ProjectSettings = projectSettings, - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -144,7 +145,7 @@ namespace Pulumi.Automation.Tests stacks = await workspace.ListStacksAsync(); var newStack = stacks.FirstOrDefault(s => s.Name == stackName); Assert.NotNull(newStack); - Assert.True(newStack.IsCurrent); + Assert.True(newStack!.IsCurrent); await workspace.SelectStackAsync(stackName); await workspace.RemoveStackAsync(stackName); @@ -166,8 +167,6 @@ namespace Pulumi.Automation.Tests ProjectSettings = projectSettings }); - StackDeployment deployment; - var stackName = $"{RandomStackName()}"; try { @@ -178,7 +177,7 @@ namespace Pulumi.Automation.Tests Assert.Equal(UpdateState.Succeeded, upResult.Summary.Result); Assert.Equal(3, upResult.Outputs.Count); - deployment = await workspace.ExportStackAsync(stackName); + var deployment = await workspace.ExportStackAsync(stackName); Assert.True(deployment.Version > 0); var previewBeforeDestroy = await stack.PreviewAsync(); @@ -214,7 +213,7 @@ namespace Pulumi.Automation.Tests using var workspace = await LocalWorkspace.CreateAsync(new LocalWorkspaceOptions { ProjectSettings = projectSettings, - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -223,12 +222,6 @@ namespace Pulumi.Automation.Tests var stackName = $"{RandomStackName()}"; var stack = await WorkspaceStack.CreateAsync(stackName, workspace); - var config = new Dictionary() - { - ["plain"] = new ConfigValue("abc"), - ["secret"] = new ConfigValue("def", isSecret: true), - }; - var plainKey = NormalizeConfigKey("plain", projectName); var secretKey = NormalizeConfigKey("secret", projectName); @@ -240,7 +233,13 @@ namespace Pulumi.Automation.Tests var values = await stack.GetAllConfigAsync(); Assert.Empty(values); + var config = new Dictionary + { + ["plain"] = new ConfigValue("abc"), + ["secret"] = new ConfigValue("def", isSecret: true), + }; await stack.SetAllConfigAsync(config); + values = await stack.GetAllConfigAsync(); Assert.True(values.TryGetValue(plainKey, out var plainValue)); Assert.Equal("abc", plainValue!.Value); @@ -282,7 +281,7 @@ namespace Pulumi.Automation.Tests using var workspace = await LocalWorkspace.CreateAsync(new LocalWorkspaceOptions { ProjectSettings = projectSettings, - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -320,7 +319,7 @@ namespace Pulumi.Automation.Tests using var workspace = await LocalWorkspace.CreateAsync(new LocalWorkspaceOptions { ProjectSettings = projectSettings, - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -348,19 +347,19 @@ namespace Pulumi.Automation.Tests var workingDir = ResourcePath(Path.Combine("Data", "testproj")); using var stack = await LocalWorkspace.CreateStackAsync(new LocalProgramArgs(stackName, workingDir) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } }); - var config = new Dictionary() - { - ["bar"] = new ConfigValue("abc"), - ["buzz"] = new ConfigValue("secret", isSecret: true), - }; try { + var config = new Dictionary + { + ["bar"] = new ConfigValue("abc"), + ["buzz"] = new ConfigValue("secret", isSecret: true), + }; await stack.SetAllConfigAsync(config); // pulumi up @@ -410,7 +409,7 @@ namespace Pulumi.Automation.Tests { var program = PulumiFn.Create(() => { - var config = new Pulumi.Config(); + var config = new Config(); return new Dictionary { ["exp_static"] = "foo", @@ -424,19 +423,19 @@ namespace Pulumi.Automation.Tests var projectName = "inline_node"; using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } }); - var config = new Dictionary() - { - ["bar"] = new ConfigValue("abc"), - ["buzz"] = new ConfigValue("secret", isSecret: true), - }; try { + var config = new Dictionary + { + ["bar"] = new ConfigValue("abc"), + ["buzz"] = new ConfigValue("secret", isSecret: true), + }; await stack.SetAllConfigAsync(config); // pulumi up @@ -499,20 +498,19 @@ namespace Pulumi.Automation.Tests var projectName = "inline_node"; using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } }); - var config = new Dictionary() - { - ["bar"] = new ConfigValue("abc"), - ["buzz"] = new ConfigValue("secret", isSecret: true), - }; - try { + var config = new Dictionary + { + ["bar"] = new ConfigValue("abc"), + ["buzz"] = new ConfigValue("secret", isSecret: true), + }; await stack.SetAllConfigAsync(config); var initialOutputs = await stack.GetOutputsAsync(); @@ -565,12 +563,10 @@ namespace Pulumi.Automation.Tests public async Task StackReferenceDestroyDiscardsWithTwoInlinePrograms() { var programA = PulumiFn.Create(() => - { - return new Dictionary + new Dictionary { ["exp_static"] = "foo", - }; - }); + }); var programB = PulumiFn.Create(() => { @@ -588,7 +584,7 @@ namespace Pulumi.Automation.Tests var stackA = await SetupStack(projectName, stackNameA, programA, new Dictionary()); - var stackB = await SetupStack(projectName, stackNameB, programB, new Dictionary() + var stackB = await SetupStack(projectName, stackNameB, programB, new Dictionary { ["Ref"] = new ConfigValue(FullyQualifiedStackName(_pulumiOrg, projectName, stackNameA)), }); @@ -645,7 +641,7 @@ namespace Pulumi.Automation.Tests { var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(project, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -661,18 +657,16 @@ namespace Pulumi.Automation.Tests public async Task OutputStreamAndDelegateIsWritten() { var program = PulumiFn.Create(() => - { - return new Dictionary + new Dictionary { ["test"] = "test", - }; - }); + }); var stackName = $"{RandomStackName()}"; var projectName = "inline_output"; using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -680,29 +674,27 @@ namespace Pulumi.Automation.Tests try { - var outputCalled = false; - // pulumi preview - outputCalled = false; - var previewResult = await stack.PreviewAsync(new PreviewOptions { OnStandardOutput = (str) => outputCalled = true }); + var outputCalled = false; + var previewResult = await stack.PreviewAsync(new PreviewOptions { OnStandardOutput = str => outputCalled = true }); Assert.False(string.IsNullOrEmpty(previewResult.StandardOutput)); Assert.True(outputCalled); // pulumi up outputCalled = false; - var upResult = await stack.UpAsync(new UpOptions { OnStandardOutput = (str) => outputCalled = true }); + var upResult = await stack.UpAsync(new UpOptions { OnStandardOutput = str => outputCalled = true }); Assert.False(string.IsNullOrEmpty(upResult.StandardOutput)); Assert.True(outputCalled); // pulumi refresh outputCalled = false; - var refreshResult = await stack.RefreshAsync(new RefreshOptions { OnStandardOutput = (str) => outputCalled = true }); + var refreshResult = await stack.RefreshAsync(new RefreshOptions { OnStandardOutput = str => outputCalled = true }); Assert.False(string.IsNullOrEmpty(refreshResult.StandardOutput)); Assert.True(outputCalled); // pulumi destroy outputCalled = false; - var destroyResult = await stack.DestroyAsync(new DestroyOptions { OnStandardOutput = (str) => outputCalled = true }); + var destroyResult = await stack.DestroyAsync(new DestroyOptions { OnStandardOutput = str => outputCalled = true }); Assert.False(string.IsNullOrEmpty(destroyResult.StandardOutput)); Assert.True(outputCalled); } @@ -716,17 +708,15 @@ namespace Pulumi.Automation.Tests public async Task HandlesEvents() { var program = PulumiFn.Create(() => - { - return new Dictionary + new Dictionary { ["exp_static"] = "foo", - }; - }); + }); var projectName = "event_test"; var stackName = $"inline_events{GetTestSuffix()}"; using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -769,7 +759,7 @@ namespace Pulumi.Automation.Tests { var events = new List(); - var result = await func(new TOptions() { OnEvent = events.Add }, CancellationToken.None); + var result = await func(new TOptions { OnEvent = events.Add }, CancellationToken.None); var seenSummaryEvent = events.Any(@event => @event.SummaryEvent != null); var seenCancelEvent = events.Any(@event => @event.CancelEvent != null); @@ -806,10 +796,10 @@ namespace Pulumi.Automation.Tests config.GetSecretInt32("plainint3"); config.RequireSecretInt32("plainint4"); - config.GetObject("plainobj1"); - config.RequireObject("plainobj2"); - config.GetSecretObject("plainobj3"); - config.RequireSecretObject("plainobj4"); + config.GetObject("plainobj1"); + config.RequireObject("plainobj2"); + config.GetSecretObject("plainobj3"); + config.RequireSecretObject("plainobj4"); config.Get("str1"); config.Require("str2"); @@ -826,59 +816,59 @@ namespace Pulumi.Automation.Tests config.GetSecretInt32("int3"); config.RequireSecretInt32("int4"); - config.GetObject("obj1"); - config.RequireObject("obj2"); - config.GetSecretObject("obj3"); - config.RequireSecretObject("obj4"); + config.GetObject("obj1"); + config.RequireObject("obj2"); + config.GetSecretObject("obj3"); + config.RequireSecretObject("obj4"); }); + var projectName = "inline_dotnet"; var stackName = $"inline_dotnet{GetTestSuffix()}"; using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } }); - var config = new Dictionary() - { - { "plainstr1", new ConfigValue("1") }, - { "plainstr2", new ConfigValue("2") }, - { "plainstr3", new ConfigValue("3") }, - { "plainstr4", new ConfigValue("4") }, - { "plainbool1", new ConfigValue("true") }, - { "plainbool2", new ConfigValue("true") }, - { "plainbool3", new ConfigValue("true") }, - { "plainbool4", new ConfigValue("true") }, - { "plainint1", new ConfigValue("1") }, - { "plainint2", new ConfigValue("2") }, - { "plainint3", new ConfigValue("3") }, - { "plainint4", new ConfigValue("4") }, - { "plainobj1", new ConfigValue("{}") }, - { "plainobj2", new ConfigValue("{}") }, - { "plainobj3", new ConfigValue("{}") }, - { "plainobj4", new ConfigValue("{}") }, - { "str1", new ConfigValue("1", isSecret: true) }, - { "str2", new ConfigValue("2", isSecret: true) }, - { "str3", new ConfigValue("3", isSecret: true) }, - { "str4", new ConfigValue("4", isSecret: true) }, - { "bool1", new ConfigValue("true", isSecret: true) }, - { "bool2", new ConfigValue("true", isSecret: true) }, - { "bool3", new ConfigValue("true", isSecret: true) }, - { "bool4", new ConfigValue("true", isSecret: true) }, - { "int1", new ConfigValue("1", isSecret: true) }, - { "int2", new ConfigValue("2", isSecret: true) }, - { "int3", new ConfigValue("3", isSecret: true) }, - { "int4", new ConfigValue("4", isSecret: true) }, - { "obj1", new ConfigValue("{}", isSecret: true) }, - { "obj2", new ConfigValue("{}", isSecret: true) }, - { "obj3", new ConfigValue("{}", isSecret: true) }, - { "obj4", new ConfigValue("{}", isSecret: true) }, - }; - try { + var config = new Dictionary + { + { "plainstr1", new ConfigValue("1") }, + { "plainstr2", new ConfigValue("2") }, + { "plainstr3", new ConfigValue("3") }, + { "plainstr4", new ConfigValue("4") }, + { "plainbool1", new ConfigValue("true") }, + { "plainbool2", new ConfigValue("true") }, + { "plainbool3", new ConfigValue("true") }, + { "plainbool4", new ConfigValue("true") }, + { "plainint1", new ConfigValue("1") }, + { "plainint2", new ConfigValue("2") }, + { "plainint3", new ConfigValue("3") }, + { "plainint4", new ConfigValue("4") }, + { "plainobj1", new ConfigValue("{}") }, + { "plainobj2", new ConfigValue("{}") }, + { "plainobj3", new ConfigValue("{}") }, + { "plainobj4", new ConfigValue("{}") }, + { "str1", new ConfigValue("1", isSecret: true) }, + { "str2", new ConfigValue("2", isSecret: true) }, + { "str3", new ConfigValue("3", isSecret: true) }, + { "str4", new ConfigValue("4", isSecret: true) }, + { "bool1", new ConfigValue("true", isSecret: true) }, + { "bool2", new ConfigValue("true", isSecret: true) }, + { "bool3", new ConfigValue("true", isSecret: true) }, + { "bool4", new ConfigValue("true", isSecret: true) }, + { "int1", new ConfigValue("1", isSecret: true) }, + { "int2", new ConfigValue("2", isSecret: true) }, + { "int3", new ConfigValue("3", isSecret: true) }, + { "int4", new ConfigValue("4", isSecret: true) }, + { "obj1", new ConfigValue("{}", isSecret: true) }, + { "obj2", new ConfigValue("{}", isSecret: true) }, + { "obj3", new ConfigValue("{}", isSecret: true) }, + { "obj4", new ConfigValue("{}", isSecret: true) }, + }; await stack.SetAllConfigAsync(config); // pulumi preview @@ -895,7 +885,7 @@ namespace Pulumi.Automation.Tests static async Task RunCommand(Func> func, string command) where TOptions : UpdateOptions, new() { - var expectedWarnings = new string[] + var expectedWarnings = new[] { "Configuration 'inline_dotnet:str1' value is a secret; use `GetSecret` instead of `Get`", "Configuration 'inline_dotnet:str2' value is a secret; use `RequireSecret` instead of `Require`", @@ -908,7 +898,7 @@ namespace Pulumi.Automation.Tests }; // These keys should not be in any warning messages. - var unexpectedWarnings = new string[] + var unexpectedWarnings = new[] { "plainstr1", "plainstr2", @@ -938,7 +928,7 @@ namespace Pulumi.Automation.Tests var events = new List(); - var result = await func(new TOptions() + var result = await func(new TOptions { OnEvent = @event => { @@ -976,7 +966,7 @@ namespace Pulumi.Automation.Tests public ValidStack() { - var config = new Pulumi.Config(); + var config = new Config(); this.ExpStatic = Output.Create("foo"); this.ExpConfig = Output.Create(config.Get("bar")!); this.ExpSecret = config.GetSecret("buzz")!; @@ -993,19 +983,19 @@ namespace Pulumi.Automation.Tests var projectName = "inline_tstack_node"; using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } }); - var config = new Dictionary() - { - ["bar"] = new ConfigValue("abc"), - ["buzz"] = new ConfigValue("secret", isSecret: true), - }; try { + var config = new Dictionary + { + ["bar"] = new ConfigValue("abc"), + ["buzz"] = new ConfigValue("secret", isSecret: true), + }; await stack.SetAllConfigAsync(config); // pulumi up @@ -1053,7 +1043,7 @@ namespace Pulumi.Automation.Tests [Fact] public async Task StackLifecycleInlineProgramWithServiceProvider() { - using var provider = new ServiceCollection() + await using var provider = new ServiceCollection() .AddTransient() // must be transient so it is instantiated each time .BuildServiceProvider(); @@ -1064,19 +1054,19 @@ namespace Pulumi.Automation.Tests var projectName = "inline_serviceprovider_node"; using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } }); - var config = new Dictionary() - { - ["bar"] = new ConfigValue("abc"), - ["buzz"] = new ConfigValue("secret", isSecret: true), - }; try { + var config = new Dictionary + { + ["bar"] = new ConfigValue("abc"), + ["buzz"] = new ConfigValue("secret", isSecret: true), + }; await stack.SetAllConfigAsync(config); // pulumi up @@ -1131,7 +1121,7 @@ namespace Pulumi.Automation.Tests using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -1146,7 +1136,7 @@ namespace Pulumi.Automation.Tests () => upTask); } - private class FileNotFoundStack : Pulumi.Stack + private class FileNotFoundStack : Stack { public FileNotFoundStack() { @@ -1164,7 +1154,7 @@ namespace Pulumi.Automation.Tests using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -1182,7 +1172,7 @@ namespace Pulumi.Automation.Tests [Fact] public async Task InlineProgramExceptionPropagatesToCallerWithServiceProvider() { - using var provider = new ServiceCollection() + await using var provider = new ServiceCollection() .AddTransient() // must be transient so it is instantiated each time .BuildServiceProvider(); @@ -1193,7 +1183,7 @@ namespace Pulumi.Automation.Tests using var stack = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectName, stackName, program) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -1225,10 +1215,11 @@ namespace Pulumi.Automation.Tests // the semaphore because we want to alternately stutter // programOne and programTwo so we can assert they aren't // touching eachothers instances - var config = new Pulumi.Config(); + var config = new Config(); Assert.Equal(projectNameOne, Deployment.Instance.ProjectName); Assert.Equal(stackNameOne, Deployment.Instance.StackName); hasReachedSemaphoreOne = true; + // ReSharper disable once AccessToDisposedClosure semaphoreOne.Wait(); Assert.Equal(projectNameOne, Deployment.Instance.ProjectName); Assert.Equal(stackNameOne, Deployment.Instance.StackName); @@ -1245,10 +1236,11 @@ namespace Pulumi.Automation.Tests var programTwo = PulumiFn.Create(() => { - var config = new Pulumi.Config(); + var config = new Config(); Assert.Equal(projectNameTwo, Deployment.Instance.ProjectName); Assert.Equal(stackNameTwo, Deployment.Instance.StackName); hasReachedSemaphoreTwo = true; + // ReSharper disable once AccessToDisposedClosure semaphoreTwo.Wait(); Assert.Equal(projectNameTwo, Deployment.Instance.ProjectName); Assert.Equal(stackNameTwo, Deployment.Instance.StackName); @@ -1262,7 +1254,7 @@ namespace Pulumi.Automation.Tests using var stackOne = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectNameOne, stackNameOne, programOne) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } @@ -1270,19 +1262,19 @@ namespace Pulumi.Automation.Tests using var stackTwo = await LocalWorkspace.CreateStackAsync(new InlineProgramArgs(projectNameTwo, stackNameTwo, programTwo) { - EnvironmentVariables = new Dictionary() + EnvironmentVariables = new Dictionary { ["PULUMI_CONFIG_PASSPHRASE"] = "test", } }); - await stackOne.SetAllConfigAsync(new Dictionary() + await stackOne.SetAllConfigAsync(new Dictionary { ["bar"] = new ConfigValue("1"), ["buzz"] = new ConfigValue("1", isSecret: true), }); - await stackTwo.SetAllConfigAsync(new Dictionary() + await stackTwo.SetAllConfigAsync(new Dictionary { ["bar"] = new ConfigValue("2"), ["buzz"] = new ConfigValue("2", isSecret: true), @@ -1295,7 +1287,7 @@ namespace Pulumi.Automation.Tests await Task.Delay(TimeSpan.FromSeconds(2)); if (upTaskOne.IsFaulted) throw upTaskOne.Exception!; - else if (upTaskOne.IsCompleted) + if (upTaskOne.IsCompleted) throw new Exception("Never hit semaphore in first UP task."); } @@ -1306,7 +1298,7 @@ namespace Pulumi.Automation.Tests await Task.Delay(TimeSpan.FromSeconds(2)); if (upTaskTwo.IsFaulted) throw upTaskTwo.Exception!; - else if (upTaskTwo.IsCompleted) + if (upTaskTwo.IsCompleted) throw new Exception("Never hit semaphore in second UP task."); } @@ -1370,7 +1362,7 @@ namespace Pulumi.Automation.Tests try { - Task.WaitAll(new Task[] { destroyTask, cancelTask }); + Task.WaitAll(destroyTask, cancelTask); } catch (AggregateException) { @@ -1422,8 +1414,8 @@ namespace Pulumi.Automation.Tests var testMinVersion = SemVersion.Parse("2.21.1"); if (errorExpected) { - Action act = () => LocalWorkspace.ValidatePulumiVersion(testMinVersion, currentVersion, optOut); - Assert.Throws(act); + void ValidatePulumiVersion() => LocalWorkspace.ValidatePulumiVersion(testMinVersion, currentVersion, optOut); + Assert.Throws(ValidatePulumiVersion); } else { diff --git a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/DynamicObjectTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/DynamicObjectTests.cs index 9a64dfc6f..d49c6c4bd 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/DynamicObjectTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/DynamicObjectTests.cs @@ -8,7 +8,7 @@ namespace Pulumi.Automation.Tests.Serialization { public class DynamicObjectTests { - private static LocalSerializer _serializer = new LocalSerializer(); + private static readonly LocalSerializer _serializer = new LocalSerializer(); [Fact] public void Dynamic_With_YamlDotNet() diff --git a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/GeneralJsonConverterTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/GeneralJsonConverterTests.cs index 2301ba8f4..25b15e171 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/GeneralJsonConverterTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/GeneralJsonConverterTests.cs @@ -9,7 +9,7 @@ namespace Pulumi.Automation.Tests.Serialization { public class GeneralJsonConverterTests { - private static LocalSerializer _serializer = new LocalSerializer(); + private static readonly LocalSerializer _serializer = new LocalSerializer(); [Fact] public void CanDeserializeConfigValue() diff --git a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/ProjectRuntimeJsonConverterTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/ProjectRuntimeJsonConverterTests.cs index d6dd17d7f..c12899405 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/ProjectRuntimeJsonConverterTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/ProjectRuntimeJsonConverterTests.cs @@ -1,15 +1,21 @@ // Copyright 2016-2021, Pulumi Corporation -using System; using System.Text.Json; using Pulumi.Automation.Serialization; using Xunit; +using Xunit.Abstractions; namespace Pulumi.Automation.Tests.Serialization { public class ProjectRuntimeJsonConverterTests { - private static LocalSerializer _serializer = new LocalSerializer(); + private readonly ITestOutputHelper _output; + private static readonly LocalSerializer _serializer = new LocalSerializer(); + + public ProjectRuntimeJsonConverterTests(ITestOutputHelper output) + { + _output = output; + } [Theory] [InlineData(ProjectRuntimeName.NodeJS)] @@ -71,7 +77,7 @@ namespace Pulumi.Automation.Tests.Serialization var runtime = new ProjectRuntime(ProjectRuntimeName.Dotnet); var json = _serializer.SerializeJson(runtime); - Console.WriteLine(json); + _output.WriteLine(json); using var document = JsonDocument.Parse(json); Assert.NotNull(document); @@ -91,7 +97,7 @@ namespace Pulumi.Automation.Tests.Serialization }; var json = _serializer.SerializeJson(runtime); - Console.WriteLine(json); + _output.WriteLine(json); using var document = JsonDocument.Parse(json); Assert.NotNull(document); diff --git a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/ProjectRuntimeYamlConverterTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/ProjectRuntimeYamlConverterTests.cs index af168b400..6faea5efb 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/ProjectRuntimeYamlConverterTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/ProjectRuntimeYamlConverterTests.cs @@ -4,12 +4,19 @@ using System; using System.Text; using Pulumi.Automation.Serialization; using Xunit; +using Xunit.Abstractions; namespace Pulumi.Automation.Tests.Serialization { public class ProjectRuntimeYamlConverterTests { - private static LocalSerializer _serializer = new LocalSerializer(); + private readonly ITestOutputHelper _output; + private static readonly LocalSerializer _serializer = new LocalSerializer(); + + public ProjectRuntimeYamlConverterTests(ITestOutputHelper output) + { + _output = output; + } [Theory] [InlineData(ProjectRuntimeName.NodeJS)] @@ -67,7 +74,7 @@ runtime: var runtime = new ProjectRuntime(ProjectRuntimeName.Dotnet); var yaml = _serializer.SerializeYaml(runtime); - Console.WriteLine(yaml); + _output.WriteLine(yaml); Assert.Equal("dotnet" + Environment.NewLine, yaml); } @@ -84,7 +91,7 @@ runtime: }; var yaml = _serializer.SerializeYaml(runtime); - Console.WriteLine(yaml); + _output.WriteLine(yaml); var expected = new StringBuilder(); expected.AppendLine("name: dotnet"); diff --git a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/StackSettingsConfigValueJsonConverterTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/StackSettingsConfigValueJsonConverterTests.cs index 30367ef0e..c18e58ae9 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/StackSettingsConfigValueJsonConverterTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/StackSettingsConfigValueJsonConverterTests.cs @@ -9,7 +9,7 @@ namespace Pulumi.Automation.Tests.Serialization { public class StackSettingsConfigValueJsonConverterTests { - private static LocalSerializer _serializer = new LocalSerializer(); + private static readonly LocalSerializer _serializer = new LocalSerializer(); [Fact] public void CanDeserializePlainString() @@ -23,7 +23,7 @@ namespace Pulumi.Automation.Tests.Serialization "; var settings = _serializer.DeserializeJson(json); - Assert.NotNull(settings?.Config); + Assert.NotNull(settings.Config); Assert.True(settings!.Config!.ContainsKey("test")); var value = settings.Config["test"]; @@ -46,7 +46,7 @@ namespace Pulumi.Automation.Tests.Serialization "; var settings = _serializer.DeserializeJson(json); - Assert.NotNull(settings?.Config); + Assert.NotNull(settings.Config); Assert.True(settings!.Config!.ContainsKey("test")); var value = settings.Config["test"]; diff --git a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/StackSettingsConfigValueYamlConverterTests.cs b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/StackSettingsConfigValueYamlConverterTests.cs index 90f1eaada..29304ee25 100644 --- a/sdk/dotnet/Pulumi.Automation.Tests/Serialization/StackSettingsConfigValueYamlConverterTests.cs +++ b/sdk/dotnet/Pulumi.Automation.Tests/Serialization/StackSettingsConfigValueYamlConverterTests.cs @@ -9,7 +9,7 @@ namespace Pulumi.Automation.Tests.Serialization { public class StackSettingsConfigValueYamlConverterTests { - private static LocalSerializer _serializer = new LocalSerializer(); + private static readonly LocalSerializer _serializer = new LocalSerializer(); [Fact] public void CanDeserializePlainString() @@ -20,7 +20,7 @@ config: "; var settings = _serializer.DeserializeYaml(yaml); - Assert.NotNull(settings?.Config); + Assert.NotNull(settings.Config); Assert.True(settings!.Config!.ContainsKey("test")); var value = settings.Config["test"]; @@ -39,7 +39,7 @@ config: "; var settings = _serializer.DeserializeYaml(yaml); - Assert.NotNull(settings?.Config); + Assert.NotNull(settings.Config); Assert.True(settings!.Config!.ContainsKey("test")); var value = settings.Config["test"]; diff --git a/sdk/dotnet/Pulumi.Automation/Collections/DictionaryContentsComparer.cs b/sdk/dotnet/Pulumi.Automation/Collections/DictionaryContentsComparer.cs index 8e0e6d71f..103cd25ce 100644 --- a/sdk/dotnet/Pulumi.Automation/Collections/DictionaryContentsComparer.cs +++ b/sdk/dotnet/Pulumi.Automation/Collections/DictionaryContentsComparer.cs @@ -5,18 +5,18 @@ using System.Collections.Generic; namespace Pulumi.Automation.Collections { /// Compares two dictionaries for equality by content, as F# maps would. - internal sealed class DictionaryContentsComparer : IEqualityComparer> where K : notnull + internal sealed class DictionaryContentsComparer : IEqualityComparer> where TKey : notnull { - private readonly IEqualityComparer _keyComparer; - private readonly IEqualityComparer _valueComparer; + private readonly IEqualityComparer _keyComparer; + private readonly IEqualityComparer _valueComparer; - public DictionaryContentsComparer(IEqualityComparer keyComparer, IEqualityComparer valueComparer) + public DictionaryContentsComparer(IEqualityComparer keyComparer, IEqualityComparer valueComparer) { this._keyComparer = keyComparer; this._valueComparer = valueComparer; } - bool IEqualityComparer>.Equals(IDictionary? x, IDictionary? y) + bool IEqualityComparer>.Equals(IDictionary? x, IDictionary? y) { if (x == null) { @@ -24,7 +24,7 @@ namespace Pulumi.Automation.Collections } if (y == null) { - return x == null; + return false; } if (ReferenceEquals(x, y)) { @@ -34,7 +34,7 @@ namespace Pulumi.Automation.Collections { return false; } - var y2 = new Dictionary(y, this._keyComparer); + var y2 = new Dictionary(y, this._keyComparer); foreach (var pair in x) { if (!y2.ContainsKey(pair.Key)) @@ -50,7 +50,7 @@ namespace Pulumi.Automation.Collections return true; } - int IEqualityComparer>.GetHashCode(IDictionary obj) + int IEqualityComparer>.GetHashCode(IDictionary obj) { return 0; // inefficient but correct } diff --git a/sdk/dotnet/Pulumi.Automation/Commands/Exceptions/CommandException.cs b/sdk/dotnet/Pulumi.Automation/Commands/Exceptions/CommandException.cs index 3c00ecc90..9fb097e1c 100644 --- a/sdk/dotnet/Pulumi.Automation/Commands/Exceptions/CommandException.cs +++ b/sdk/dotnet/Pulumi.Automation/Commands/Exceptions/CommandException.cs @@ -20,14 +20,14 @@ namespace Pulumi.Automation.Commands.Exceptions this.Name = name; } - private static readonly Regex NotFoundRegexPattern = new Regex("no stack named.*found"); - private static readonly Regex AlreadyExistsRegexPattern = new Regex("stack.*already exists"); - private static readonly string ConflictText = "[409] Conflict: Another update is currently in progress."; + private static readonly Regex _notFoundRegexPattern = new Regex("no stack named.*found"); + private static readonly Regex _alreadyExistsRegexPattern = new Regex("stack.*already exists"); + private static readonly string _conflictText = "[409] Conflict: Another update is currently in progress."; internal static CommandException CreateFromResult(CommandResult result) - => NotFoundRegexPattern.IsMatch(result.StandardError) ? new StackNotFoundException(result) - : AlreadyExistsRegexPattern.IsMatch(result.StandardError) ? new StackAlreadyExistsException(result) - : result.StandardError?.IndexOf(ConflictText) >= 0 ? new ConcurrentUpdateException(result) + => _notFoundRegexPattern.IsMatch(result.StandardError) ? new StackNotFoundException(result) + : _alreadyExistsRegexPattern.IsMatch(result.StandardError) ? new StackAlreadyExistsException(result) + : result.StandardError.IndexOf(_conflictText, StringComparison.Ordinal) >= 0 ? new ConcurrentUpdateException(result) : new CommandException(result); } } diff --git a/sdk/dotnet/Pulumi.Automation/Commands/IPulumiCmd.cs b/sdk/dotnet/Pulumi.Automation/Commands/IPulumiCmd.cs index 43aa8b071..b0d0aeae9 100644 --- a/sdk/dotnet/Pulumi.Automation/Commands/IPulumiCmd.cs +++ b/sdk/dotnet/Pulumi.Automation/Commands/IPulumiCmd.cs @@ -11,7 +11,7 @@ namespace Pulumi.Automation.Commands internal interface IPulumiCmd { Task RunAsync( - IEnumerable args, + IList args, string workingDir, IDictionary additionalEnv, Action? onStandardOutput = null, diff --git a/sdk/dotnet/Pulumi.Automation/Commands/LocalPulumiCmd.cs b/sdk/dotnet/Pulumi.Automation/Commands/LocalPulumiCmd.cs index aa605b276..186b554f0 100644 --- a/sdk/dotnet/Pulumi.Automation/Commands/LocalPulumiCmd.cs +++ b/sdk/dotnet/Pulumi.Automation/Commands/LocalPulumiCmd.cs @@ -9,7 +9,6 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; - using CliWrap; using Pulumi.Automation.Commands.Exceptions; using Pulumi.Automation.Events; @@ -20,7 +19,7 @@ namespace Pulumi.Automation.Commands { public async Task RunAsync( - IEnumerable args, + IList args, string workingDir, IDictionary additionalEnv, Action? onStandardOutput = null, @@ -40,14 +39,11 @@ namespace Pulumi.Automation.Commands await eventLogWatcher.Stop(); } } - else - { - return await RunAsyncInner(args, workingDir, additionalEnv, onStandardOutput, onStandardError, eventLogFile: null, cancellationToken); - } + return await RunAsyncInner(args, workingDir, additionalEnv, onStandardOutput, onStandardError, eventLogFile: null, cancellationToken); } private async Task RunAsyncInner( - IEnumerable args, + IList args, string workingDir, IDictionary additionalEnv, Action? onStandardOutput = null, @@ -88,10 +84,7 @@ namespace Pulumi.Automation.Commands { throw CommandException.CreateFromResult(result); } - else - { - return result; - } + return result; } private static IReadOnlyDictionary PulumiEnvironment(IDictionary additionalEnv, bool debugCommands) @@ -108,18 +101,18 @@ namespace Pulumi.Automation.Commands return env; } - private static IEnumerable PulumiArgs(IEnumerable args, EventLogFile? eventLogFile) + private static IList PulumiArgs(IList args, EventLogFile? eventLogFile) { // all commands should be run in non-interactive mode. // this causes commands to fail rather than prompting for input (and thus hanging indefinitely) if (!args.Contains("--non-interactive")) { - args = args.Concat(new[] { "--non-interactive" }); + args = args.Concat(new[] { "--non-interactive" }).ToList(); } if (eventLogFile != null) { - args = args.Concat(new[] { "--event-log", eventLogFile.FilePath }); + args = args.Concat(new[] { "--event-log", eventLogFile.FilePath }).ToList(); } return args; @@ -132,15 +125,12 @@ namespace Pulumi.Automation.Commands { return "event-log"; } - else - { - return alphaNumWord.IsMatch(firstArgument) ? firstArgument : "event-log"; - } + return alphaNumWord.IsMatch(firstArgument) ? firstArgument : "event-log"; } - private class EventLogFile : IDisposable + private sealed class EventLogFile : IDisposable { - private bool _disposedValue; + public string FilePath { get; } public EventLogFile(string command) { @@ -149,37 +139,21 @@ namespace Pulumi.Automation.Commands this.FilePath = Path.Combine(logDir, "eventlog.txt"); } - public string FilePath { get; } - - protected virtual void Dispose(bool disposing) - { - if (!_disposedValue) - { - if (disposing) - { - var dir = Path.GetDirectoryName(this.FilePath); - try - { - Directory.Delete(dir, recursive: true); - } - catch (Exception e) - { - // allow graceful exit if for some reason - // we're not able to delete the directory - // will rely on OS to clean temp directory - // in this case. - Trace.TraceWarning("Ignoring exception during cleanup of {0} folder: {1}", dir, e); - } - } - _disposedValue = true; - } - } - public void Dispose() { - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - Dispose(disposing: true); - GC.SuppressFinalize(this); + var dir = Path.GetDirectoryName(this.FilePath); + try + { + Directory.Delete(dir, recursive: true); + } + catch (Exception e) + { + // allow graceful exit if for some reason + // we're not able to delete the directory + // will rely on OS to clean temp directory + // in this case. + Trace.TraceWarning("Ignoring exception during cleanup of {0} folder: {1}", dir, e); + } } } } diff --git a/sdk/dotnet/Pulumi.Automation/DestroyOptions.cs b/sdk/dotnet/Pulumi.Automation/DestroyOptions.cs index 0fc28a843..0abd54f3a 100644 --- a/sdk/dotnet/Pulumi.Automation/DestroyOptions.cs +++ b/sdk/dotnet/Pulumi.Automation/DestroyOptions.cs @@ -1,7 +1,5 @@ // Copyright 2016-2021, Pulumi Corporation -using System; - namespace Pulumi.Automation { /// diff --git a/sdk/dotnet/Pulumi.Automation/Events/EventLogWatcher.cs b/sdk/dotnet/Pulumi.Automation/Events/EventLogWatcher.cs index 843bc0aa4..fb2724985 100644 --- a/sdk/dotnet/Pulumi.Automation/Events/EventLogWatcher.cs +++ b/sdk/dotnet/Pulumi.Automation/Events/EventLogWatcher.cs @@ -8,14 +8,14 @@ using Pulumi.Automation.Serialization; namespace Pulumi.Automation.Events { - internal class EventLogWatcher : IDisposable + internal sealed class EventLogWatcher : IDisposable { private readonly LocalSerializer _localSerializer = new LocalSerializer(); private readonly Action _onEvent; private const int _pollingIntervalMilliseconds = 100; // We keep track of the last position in the file. - private long _position = 0; + private long _position; public string LogFile { get; } private readonly Task _pollingTask; private readonly CancellationTokenSource _internalCancellationTokenSource = new CancellationTokenSource(); @@ -82,6 +82,7 @@ namespace Pulumi.Automation.Events await ReadEventsOnce(); await Task.Delay(_pollingIntervalMilliseconds, linkedSource.Token); } + // ReSharper disable once FunctionNeverReturns } private async Task ReadEventsOnce() @@ -91,15 +92,11 @@ namespace Pulumi.Automation.Events return; } - using var fs = new FileStream(LogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) - { - Position = this._position - }; + await using var fs = new FileStream(LogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) { Position = this._position }; using var reader = new StreamReader(fs); - string? line; while (reader.Peek() >= 0) { - line = await reader.ReadLineAsync(); + var line = await reader.ReadLineAsync(); this._position = fs.Position; if (!string.IsNullOrWhiteSpace(line)) { diff --git a/sdk/dotnet/Pulumi.Automation/Exceptions/NoSummaryEventException.cs b/sdk/dotnet/Pulumi.Automation/Exceptions/NoSummaryEventException.cs index 74b164a70..e56661da6 100644 --- a/sdk/dotnet/Pulumi.Automation/Exceptions/NoSummaryEventException.cs +++ b/sdk/dotnet/Pulumi.Automation/Exceptions/NoSummaryEventException.cs @@ -1,10 +1,12 @@ // Copyright 2016-2021, Pulumi Corporation +using Pulumi.Automation.Events; + namespace Pulumi.Automation.Exceptions { public sealed class NoSummaryEventException : MissingExpectedEventException { - internal NoSummaryEventException(string? message) : base(nameof(Events.SummaryEvent), message) + internal NoSummaryEventException(string? message) : base(nameof(SummaryEvent), message) { } } diff --git a/sdk/dotnet/Pulumi.Automation/LocalWorkspace.cs b/sdk/dotnet/Pulumi.Automation/LocalWorkspace.cs index fe6b7ab78..52547f7f4 100644 --- a/sdk/dotnet/Pulumi.Automation/LocalWorkspace.cs +++ b/sdk/dotnet/Pulumi.Automation/LocalWorkspace.cs @@ -1,16 +1,16 @@ // Copyright 2016-2021, Pulumi Corporation -using Semver; using System; using System.Collections.Generic; using System.Collections.Immutable; using System.IO; using System.Linq; -using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Pulumi.Automation.Commands; +using Pulumi.Automation.Exceptions; using Pulumi.Automation.Serialization; +using Semver; namespace Pulumi.Automation { @@ -43,9 +43,9 @@ namespace Pulumi.Automation /// public override string? PulumiHome { get; } - private SemVersion? pulumiVersion; + private SemVersion? _pulumiVersion; /// - public override string PulumiVersion => pulumiVersion?.ToString() ?? throw new InvalidOperationException("Failed to get Pulumi version."); + public override string PulumiVersion => _pulumiVersion?.ToString() ?? throw new InvalidOperationException("Failed to get Pulumi version."); /// public override string? SecretsProvider { get; } @@ -358,11 +358,11 @@ namespace Pulumi.Automation !ProjectSettings.Comparer.Equals(projectSettings, existingSettings)) { var path = this.FindSettingsFile(); - throw new Exceptions.ProjectSettingsConflictException(path); + throw new ProjectSettingsConflictException(path); } } - private static readonly string[] SettingsExtensions = new string[] { ".yaml", ".yml", ".json" }; + private static readonly string[] _settingsExtensions = { ".yaml", ".yml", ".json" }; private async Task PopulatePulumiVersionAsync(CancellationToken cancellationToken) { @@ -376,8 +376,8 @@ namespace Pulumi.Automation var skipVersionCheckVar = "PULUMI_AUTOMATION_API_SKIP_VERSION_CHECK"; var hasSkipEnvVar = this.EnvironmentVariables?.ContainsKey(skipVersionCheckVar) ?? false; var optOut = hasSkipEnvVar || Environment.GetEnvironmentVariable(skipVersionCheckVar) != null; - LocalWorkspace.ValidatePulumiVersion(LocalWorkspace._minimumVersion, version, optOut); - this.pulumiVersion = version; + ValidatePulumiVersion(_minimumVersion, version, optOut); + this._pulumiVersion = version; } internal static void ValidatePulumiVersion(SemVersion minVersion, SemVersion currentVersion, bool optOut) @@ -410,12 +410,8 @@ namespace Pulumi.Automation { return this._serializer.DeserializeJson(content); } - else - { - - var model = this._serializer.DeserializeYaml(content); - return model.Convert(); - } + var model = this._serializer.DeserializeYaml(content); + return model.Convert(); } /// @@ -429,7 +425,7 @@ namespace Pulumi.Automation private string FindSettingsFile() { - foreach (var ext in SettingsExtensions) + foreach (var ext in _settingsExtensions) { var testPath = Path.Combine(this.WorkDir, $"Pulumi{ext}"); if (File.Exists(testPath)) @@ -455,7 +451,7 @@ namespace Pulumi.Automation { var settingsName = GetStackSettingsName(stackName); - foreach (var ext in SettingsExtensions) + foreach (var ext in _settingsExtensions) { var isJson = ext == ".json"; var path = Path.Combine(this.WorkDir, $"Pulumi.{settingsName}{ext}"); @@ -475,7 +471,7 @@ namespace Pulumi.Automation var settingsName = GetStackSettingsName(stackName); var foundExt = ".yaml"; - foreach (var ext in SettingsExtensions) + foreach (var ext in _settingsExtensions) { var testPath = Path.Combine(this.WorkDir, $"Pulumi.{settingsName}{ext}"); if (File.Exists(testPath)) @@ -567,7 +563,7 @@ namespace Pulumi.Automation /// public override Task CreateStackAsync(string stackName, CancellationToken cancellationToken) { - var args = new List() + var args = new List { "stack", "init", @@ -614,7 +610,7 @@ namespace Pulumi.Automation var tempFileName = Path.GetTempFileName(); try { - File.WriteAllText(tempFileName, state.Json.GetRawText()); + await File.WriteAllTextAsync(tempFileName, state.Json.GetRawText(), cancellationToken); await this.RunCommandAsync(new[] { "stack", "import", "--file", tempFileName, "--stack", stackName }, cancellationToken).ConfigureAwait(false); } @@ -631,7 +627,7 @@ namespace Pulumi.Automation /// public override Task RemovePluginAsync(string? name = null, string? versionRange = null, PluginKind kind = PluginKind.Resource, CancellationToken cancellationToken = default) { - var args = new List() + var args = new List { "plugin", "rm", diff --git a/sdk/dotnet/Pulumi.Automation/ProjectBackend.cs b/sdk/dotnet/Pulumi.Automation/ProjectBackend.cs index d47e2289c..092f9e21b 100644 --- a/sdk/dotnet/Pulumi.Automation/ProjectBackend.cs +++ b/sdk/dotnet/Pulumi.Automation/ProjectBackend.cs @@ -25,7 +25,7 @@ namespace Pulumi.Automation if (y == null) { - return x == null; + return false; } if (ReferenceEquals(x, y)) diff --git a/sdk/dotnet/Pulumi.Automation/ProjectRuntime.cs b/sdk/dotnet/Pulumi.Automation/ProjectRuntime.cs index 4bfd946e1..9ef5fcba7 100644 --- a/sdk/dotnet/Pulumi.Automation/ProjectRuntime.cs +++ b/sdk/dotnet/Pulumi.Automation/ProjectRuntime.cs @@ -32,7 +32,7 @@ namespace Pulumi.Automation if (y == null) { - return x == null; + return false; } if (ReferenceEquals(x, y)) diff --git a/sdk/dotnet/Pulumi.Automation/ProjectRuntimeName.cs b/sdk/dotnet/Pulumi.Automation/ProjectRuntimeName.cs index 36ca98a1c..ac52f90ab 100644 --- a/sdk/dotnet/Pulumi.Automation/ProjectRuntimeName.cs +++ b/sdk/dotnet/Pulumi.Automation/ProjectRuntimeName.cs @@ -7,6 +7,7 @@ namespace Pulumi.Automation /// public enum ProjectRuntimeName { + // ReSharper disable once InconsistentNaming NodeJS, Go, Python, diff --git a/sdk/dotnet/Pulumi.Automation/ProjectRuntimeOptions.cs b/sdk/dotnet/Pulumi.Automation/ProjectRuntimeOptions.cs index cabed4199..934538260 100644 --- a/sdk/dotnet/Pulumi.Automation/ProjectRuntimeOptions.cs +++ b/sdk/dotnet/Pulumi.Automation/ProjectRuntimeOptions.cs @@ -46,7 +46,7 @@ namespace Pulumi.Automation if (y == null) { - return x == null; + return false; } if (ReferenceEquals(x, y)) diff --git a/sdk/dotnet/Pulumi.Automation/ProjectSettings.cs b/sdk/dotnet/Pulumi.Automation/ProjectSettings.cs index a725e435a..4989436a8 100644 --- a/sdk/dotnet/Pulumi.Automation/ProjectSettings.cs +++ b/sdk/dotnet/Pulumi.Automation/ProjectSettings.cs @@ -48,17 +48,14 @@ namespace Pulumi.Automation { } - internal static ProjectSettings Default(string name) { - var defaultSettings = new ProjectSettings(name, new ProjectRuntime(ProjectRuntimeName.Dotnet)); - defaultSettings.Main = Directory.GetCurrentDirectory(); - return defaultSettings; - } + internal static ProjectSettings Default(string name) => + new ProjectSettings(name, new ProjectRuntime(ProjectRuntimeName.Dotnet)) { Main = Directory.GetCurrentDirectory() }; internal bool IsDefault { get { - return ProjectSettings.Comparer.Equals(this, ProjectSettings.Default(this.Name)); + return Comparer.Equals(this, Default(this.Name)); } } @@ -73,7 +70,7 @@ namespace Pulumi.Automation if (y == null) { - return x == null; + return false; } if (ReferenceEquals(x, y)) diff --git a/sdk/dotnet/Pulumi.Automation/ProjectTemplate.cs b/sdk/dotnet/Pulumi.Automation/ProjectTemplate.cs index 60d882fcd..35860fbb0 100644 --- a/sdk/dotnet/Pulumi.Automation/ProjectTemplate.cs +++ b/sdk/dotnet/Pulumi.Automation/ProjectTemplate.cs @@ -24,7 +24,7 @@ namespace Pulumi.Automation private sealed class ProjectTemplateComparer : IEqualityComparer { - private IEqualityComparer> _configComparer = + private readonly IEqualityComparer> _configComparer = new DictionaryContentsComparer( EqualityComparer.Default, ProjectTemplateConfigValue.Comparer); @@ -38,7 +38,7 @@ namespace Pulumi.Automation if (y == null) { - return x == null; + return false; } if (ReferenceEquals(x, y)) diff --git a/sdk/dotnet/Pulumi.Automation/ProjectTemplateConfigValue.cs b/sdk/dotnet/Pulumi.Automation/ProjectTemplateConfigValue.cs index 4b609ca88..940eaf994 100644 --- a/sdk/dotnet/Pulumi.Automation/ProjectTemplateConfigValue.cs +++ b/sdk/dotnet/Pulumi.Automation/ProjectTemplateConfigValue.cs @@ -29,7 +29,7 @@ namespace Pulumi.Automation if (y == null) { - return x == null; + return false; } if (ReferenceEquals(x, y)) diff --git a/sdk/dotnet/Pulumi.Automation/PulumiFn.Inline.cs b/sdk/dotnet/Pulumi.Automation/PulumiFn.Inline.cs index a4dee895d..4f5e5f05a 100644 --- a/sdk/dotnet/Pulumi.Automation/PulumiFn.Inline.cs +++ b/sdk/dotnet/Pulumi.Automation/PulumiFn.Inline.cs @@ -1,6 +1,5 @@ // Copyright 2016-2021, Pulumi Corporation - using System; using System.Collections.Generic; using System.Runtime.ExceptionServices; diff --git a/sdk/dotnet/Pulumi.Automation/PulumiFn.ServiceProvider.cs b/sdk/dotnet/Pulumi.Automation/PulumiFn.ServiceProvider.cs index 6ce0b3423..06de0b864 100644 --- a/sdk/dotnet/Pulumi.Automation/PulumiFn.ServiceProvider.cs +++ b/sdk/dotnet/Pulumi.Automation/PulumiFn.ServiceProvider.cs @@ -23,7 +23,7 @@ namespace Pulumi.Automation if (stackType is null) throw new ArgumentNullException(nameof(stackType)); - var pulumiStackType = typeof(Pulumi.Stack); + var pulumiStackType = typeof(Stack); if (!pulumiStackType.IsAssignableFrom(stackType) || pulumiStackType == stackType) throw new ArgumentException($"Provided stack type must derive from {pulumiStackType.FullName}.", nameof(stackType)); @@ -42,7 +42,7 @@ namespace Pulumi.Automation if (this._serviceProvider is null) throw new ArgumentNullException(nameof(this._serviceProvider), $"The provided service provider was null by the time this {nameof(PulumiFn)} was invoked."); - return this._serviceProvider.GetService(this._stackType) as Pulumi.Stack + return this._serviceProvider.GetService(this._stackType) as Stack ?? throw new ApplicationException( $"Failed to resolve instance of type {this._stackType.FullName} from service provider. Register the type with the service provider before this {nameof(PulumiFn)} is invoked."); } diff --git a/sdk/dotnet/Pulumi.Automation/PulumiFn.TStack.cs b/sdk/dotnet/Pulumi.Automation/PulumiFn.TStack.cs index 50876e9a5..8b1db498f 100644 --- a/sdk/dotnet/Pulumi.Automation/PulumiFn.TStack.cs +++ b/sdk/dotnet/Pulumi.Automation/PulumiFn.TStack.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace Pulumi.Automation { - internal class PulumiFn : PulumiFn where TStack : Pulumi.Stack + internal class PulumiFn : PulumiFn where TStack : Stack { private readonly Func _stackFactory; diff --git a/sdk/dotnet/Pulumi.Automation/PulumiFn.cs b/sdk/dotnet/Pulumi.Automation/PulumiFn.cs index 0b283f9e9..1b00d81bf 100644 --- a/sdk/dotnet/Pulumi.Automation/PulumiFn.cs +++ b/sdk/dotnet/Pulumi.Automation/PulumiFn.cs @@ -40,13 +40,13 @@ namespace Pulumi.Automation /// An asynchronous pulumi program that takes in a . public static PulumiFn Create(Func program) { - Func>> wrapper = async cancellationToken => + async Task> Wrapper(CancellationToken cancellationToken) { await program(cancellationToken).ConfigureAwait(false); return ImmutableDictionary.Empty; - }; + } - return new PulumiFnInline(wrapper); + return new PulumiFnInline(Wrapper); } /// @@ -55,13 +55,13 @@ namespace Pulumi.Automation /// An asynchronous pulumi program. public static PulumiFn Create(Func program) { - Func>> wrapper = async cancellationToken => + async Task> Wrapper(CancellationToken cancellationToken) { await program().ConfigureAwait(false); return ImmutableDictionary.Empty; - }; + } - return new PulumiFnInline(wrapper); + return new PulumiFnInline(Wrapper); } /// @@ -70,13 +70,13 @@ namespace Pulumi.Automation /// A pulumi program that returns an output. public static PulumiFn Create(Func> program) { - Func>> wrapper = cancellationToken => + Task> Wrapper(CancellationToken cancellationToken) { var output = program(); return Task.FromResult(output); - }; + } - return new PulumiFnInline(wrapper); + return new PulumiFnInline(Wrapper); } /// @@ -91,7 +91,7 @@ namespace Pulumi.Automation /// /// The type. public static PulumiFn Create() - where TStack : Pulumi.Stack, new() + where TStack : Stack, new() => new PulumiFn(() => new TStack()); /// @@ -104,7 +104,7 @@ namespace Pulumi.Automation /// The type. /// The service provider that will be used to resolve an instance of . public static PulumiFn Create(IServiceProvider serviceProvider) - where TStack : Pulumi.Stack + where TStack : Stack => new PulumiFnServiceProvider(serviceProvider, typeof(TStack)); /// diff --git a/sdk/dotnet/Pulumi.Automation/RefreshOptions.cs b/sdk/dotnet/Pulumi.Automation/RefreshOptions.cs index 51f886155..713c57dbf 100644 --- a/sdk/dotnet/Pulumi.Automation/RefreshOptions.cs +++ b/sdk/dotnet/Pulumi.Automation/RefreshOptions.cs @@ -1,7 +1,5 @@ // Copyright 2016-2021, Pulumi Corporation -using System; - namespace Pulumi.Automation { /// diff --git a/sdk/dotnet/Pulumi.Automation/Runtime/LanguageRuntimeService.cs b/sdk/dotnet/Pulumi.Automation/Runtime/LanguageRuntimeService.cs index 345f3df5d..7b96294f6 100644 --- a/sdk/dotnet/Pulumi.Automation/Runtime/LanguageRuntimeService.cs +++ b/sdk/dotnet/Pulumi.Automation/Runtime/LanguageRuntimeService.cs @@ -1,7 +1,5 @@ // Copyright 2016-2021, Pulumi Corporation -using System; -using System.Collections.Generic; using System.Linq; using System.Runtime.ExceptionServices; using System.Threading; @@ -56,6 +54,7 @@ namespace Pulumi.Automation this._callerContext.ExceptionDispatchInfo = await Deployment.RunInlineAsync( settings, + // ReSharper disable once AccessToDisposedClosure runner => this._callerContext.Program.InvokeAsync(runner, cts.Token)) .ConfigureAwait(false); diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/DiagnosticEventModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/DiagnosticEventModel.cs index 6bb666f70..69e8c7e01 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/DiagnosticEventModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/DiagnosticEventModel.cs @@ -1,7 +1,7 @@ // Copyright 2016-2021, Pulumi Corporation -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/EngineEventModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/EngineEventModel.cs index cf9a94d44..8ebc48080 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/EngineEventModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/EngineEventModel.cs @@ -1,7 +1,7 @@ // Copyright 2016-2021, Pulumi Corporation -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/Json/SystemObjectJsonConverter.cs b/sdk/dotnet/Pulumi.Automation/Serialization/Json/SystemObjectJsonConverter.cs index a4a94f663..4c7f3a76d 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/Json/SystemObjectJsonConverter.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/Json/SystemObjectJsonConverter.cs @@ -23,7 +23,7 @@ namespace Pulumi.Automation.Serialization.Json if (reader.TokenType == JsonTokenType.Number) { - if (reader.TryGetInt64(out long l)) + if (reader.TryGetInt64(out var l)) { return l; } @@ -33,7 +33,7 @@ namespace Pulumi.Automation.Serialization.Json if (reader.TokenType == JsonTokenType.String) { - if (reader.TryGetDateTime(out DateTime datetime)) + if (reader.TryGetDateTime(out var datetime)) { return datetime; } diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/PolicyEventModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/PolicyEventModel.cs index e83ce279f..2e0637425 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/PolicyEventModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/PolicyEventModel.cs @@ -1,7 +1,7 @@ // Copyright 2016-2021, Pulumi Corporation -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/PreludeEventModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/PreludeEventModel.cs index f2ab5e249..6f49fa39c 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/PreludeEventModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/PreludeEventModel.cs @@ -1,8 +1,8 @@ // Copyright 2016-2021, Pulumi Corporation using System.Collections.Generic; -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/PropertyDiffModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/PropertyDiffModel.cs index ca1eb875b..132a077f7 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/PropertyDiffModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/PropertyDiffModel.cs @@ -1,7 +1,7 @@ // Copyright 2016-2021, Pulumi Corporation -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/ResourceOperationFailedEventModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/ResourceOperationFailedEventModel.cs index d51cc2a82..28bf742b9 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/ResourceOperationFailedEventModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/ResourceOperationFailedEventModel.cs @@ -1,7 +1,7 @@ // Copyright 2016-2021, Pulumi Corporation -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/ResourceOutputsEventModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/ResourceOutputsEventModel.cs index a0950c377..62342034b 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/ResourceOutputsEventModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/ResourceOutputsEventModel.cs @@ -1,7 +1,7 @@ // Copyright 2016-2021, Pulumi Corporation -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/ResourcePreEventModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/ResourcePreEventModel.cs index fbb3e31a5..7ced0e92b 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/ResourcePreEventModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/ResourcePreEventModel.cs @@ -1,7 +1,7 @@ // Copyright 2016-2021, Pulumi Corporation -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/StandardOutputEngineEventModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/StandardOutputEngineEventModel.cs index 4316ffe85..298abb44c 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/StandardOutputEngineEventModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/StandardOutputEngineEventModel.cs @@ -1,7 +1,7 @@ // Copyright 2016-2021, Pulumi Corporation -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/StepEventMetadataModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/StepEventMetadataModel.cs index 3899edde1..338bcd5c1 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/StepEventMetadataModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/StepEventMetadataModel.cs @@ -1,9 +1,9 @@ // Copyright 2016-2021, Pulumi Corporation using System.Collections.Generic; -using Pulumi.Automation.Serialization.Json; -using Pulumi.Automation.Events; using System.Linq; +using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/StepEventStateMetadataModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/StepEventStateMetadataModel.cs index cc620393a..a0bf892af 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/StepEventStateMetadataModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/StepEventStateMetadataModel.cs @@ -1,8 +1,8 @@ // Copyright 2016-2021, Pulumi Corporation using System.Collections.Generic; -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/SummaryEventModel.cs b/sdk/dotnet/Pulumi.Automation/Serialization/SummaryEventModel.cs index 1984b2c5e..1881ecb3b 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/SummaryEventModel.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/SummaryEventModel.cs @@ -1,8 +1,8 @@ // Copyright 2016-2021, Pulumi Corporation using System.Collections.Generic; -using Pulumi.Automation.Serialization.Json; using Pulumi.Automation.Events; +using Pulumi.Automation.Serialization.Json; // NOTE: The classes in this file are intended to align with the serialized // JSON types defined and versioned in sdk/go/common/apitype/events.go @@ -22,7 +22,9 @@ namespace Pulumi.Automation.Serialization new SummaryEvent( this.MaybeCorrupt, this.DurationSeconds, + // ReSharper disable once ConstantNullCoalescingCondition this.ResourceChanges ?? new Dictionary(), + // ReSharper disable once ConstantNullCoalescingCondition this.PolicyPacks ?? new Dictionary()); } } diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/ProjectRuntimeOptionsYamlConverter.cs b/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/ProjectRuntimeOptionsYamlConverter.cs index 21afd135c..6a742ccb7 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/ProjectRuntimeOptionsYamlConverter.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/ProjectRuntimeOptionsYamlConverter.cs @@ -11,9 +11,9 @@ namespace Pulumi.Automation.Serialization.Yaml { internal class ProjectRuntimeOptionsYamlConverter : IYamlTypeConverter { - private static readonly Type Type = typeof(ProjectRuntimeOptions); - private static readonly List PropertyNames = typeof(ProjectRuntimeOptions).GetProperties().Select(x => x.Name).ToList(); - private static readonly Dictionary> Readers = + private static readonly Type _type = typeof(ProjectRuntimeOptions); + private static readonly List _propertyNames = typeof(ProjectRuntimeOptions).GetProperties().Select(x => x.Name).ToList(); + private static readonly Dictionary> _readers = new Dictionary>(StringComparer.OrdinalIgnoreCase) { [nameof(ProjectRuntimeOptions.Binary)] = (x, p, t) => x.Value, @@ -21,22 +21,21 @@ namespace Pulumi.Automation.Serialization.Yaml [nameof(ProjectRuntimeOptions.VirtualEnv)] = (x, p, t) => x.Value, }; - public bool Accepts(Type type) - => type == Type; + public bool Accepts(Type type) => type == _type; - public object? ReadYaml(IParser parser, Type type) + public object ReadYaml(IParser parser, Type type) { if (!parser.TryConsume(out _)) throw new YamlException($"Unable to deserialize [{type.FullName}]. Expecting object."); - var values = PropertyNames.ToDictionary(x => x, x => (object?)null, StringComparer.OrdinalIgnoreCase); + var values = _propertyNames.ToDictionary(x => x, x => (object?)null, StringComparer.OrdinalIgnoreCase); do { if (!parser.TryConsume(out var propertyNameScalar)) throw new YamlException($"Unable to deserialize [{type.FullName}]. Expecting a property name."); - if (!Readers.TryGetValue(propertyNameScalar.Value, out var readerFunc)) + if (!_readers.TryGetValue(propertyNameScalar.Value, out var readerFunc)) throw new YamlException($"Unable to deserialize [{type.FullName}]. Invalid property [{propertyNameScalar.Value}]."); if (!parser.TryConsume(out var propertyValueScalar)) diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/ProjectRuntimeYamlConverter.cs b/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/ProjectRuntimeYamlConverter.cs index 9394826e6..e1ad83f1c 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/ProjectRuntimeYamlConverter.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/ProjectRuntimeYamlConverter.cs @@ -9,15 +9,14 @@ namespace Pulumi.Automation.Serialization.Yaml { internal class ProjectRuntimeYamlConverter : IYamlTypeConverter { - private static readonly Type Type = typeof(ProjectRuntime); - private static readonly Type OptionsType = typeof(ProjectRuntimeOptions); + private static readonly Type _type = typeof(ProjectRuntime); + private static readonly Type _optionsType = typeof(ProjectRuntimeOptions); private readonly IYamlTypeConverter _optionsConverter = new ProjectRuntimeOptionsYamlConverter(); - public bool Accepts(Type type) - => type == Type; + public bool Accepts(Type type) => type == _type; - public object? ReadYaml(IParser parser, Type type) + public object ReadYaml(IParser parser, Type type) { if (parser.TryConsume(out var nameValueScalar)) { @@ -51,7 +50,7 @@ namespace Pulumi.Automation.Serialization.Yaml if (!parser.Accept(out _)) throw new YamlException($"Unable to deserialize [{type.FullName}]. Runtime options property should be an object."); - var runtimeOptionsObj = this._optionsConverter.ReadYaml(parser, OptionsType); + var runtimeOptionsObj = this._optionsConverter.ReadYaml(parser, _optionsType); if (!(runtimeOptionsObj is ProjectRuntimeOptions runtimeOptions)) throw new YamlException("There was an issue deserializing the runtime options object."); @@ -87,7 +86,7 @@ namespace Pulumi.Automation.Serialization.Yaml emitter.Emit(new Scalar(runtime.Name.ToString().ToLower())); emitter.Emit(new Scalar("options")); - this._optionsConverter.WriteYaml(emitter, runtime.Options, OptionsType); + this._optionsConverter.WriteYaml(emitter, runtime.Options, _optionsType); emitter.Emit(new MappingEnd()); } diff --git a/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/StackSettingsConfigValueYamlConverter.cs b/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/StackSettingsConfigValueYamlConverter.cs index d760d83ca..603f81278 100644 --- a/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/StackSettingsConfigValueYamlConverter.cs +++ b/sdk/dotnet/Pulumi.Automation/Serialization/Yaml/StackSettingsConfigValueYamlConverter.cs @@ -9,12 +9,11 @@ namespace Pulumi.Automation.Serialization.Yaml { internal class StackSettingsConfigValueYamlConverter : IYamlTypeConverter { - private static readonly Type Type = typeof(StackSettingsConfigValue); + private static readonly Type _type = typeof(StackSettingsConfigValue); - public bool Accepts(Type type) - => type == Type; + public bool Accepts(Type type) => type == _type; - public object? ReadYaml(IParser parser, Type type) + public object ReadYaml(IParser parser, Type type) { // check if plain string if (parser.Accept(out var stringValue)) @@ -43,10 +42,7 @@ namespace Pulumi.Automation.Serialization.Yaml parser.MoveNext(); return new StackSettingsConfigValue(securePropertyValue.Value, true); } - else - { - throw new NotSupportedException("Automation API does not currently support deserializing complex objects from stack settings."); - } + throw new NotSupportedException("Automation API does not currently support deserializing complex objects from stack settings."); } public void WriteYaml(IEmitter emitter, object? value, Type type) diff --git a/sdk/dotnet/Pulumi.Automation/StackSettings.cs b/sdk/dotnet/Pulumi.Automation/StackSettings.cs index 0146d29b1..0dd0d4f0f 100644 --- a/sdk/dotnet/Pulumi.Automation/StackSettings.cs +++ b/sdk/dotnet/Pulumi.Automation/StackSettings.cs @@ -1,6 +1,8 @@ // Copyright 2016-2021, Pulumi Corporation using System.Collections.Generic; +using System.Text.Json.Serialization; +using YamlDotNet.Serialization; namespace Pulumi.Automation { @@ -9,8 +11,8 @@ namespace Pulumi.Automation /// /// This stack's secrets provider. /// - [YamlDotNet.Serialization.YamlMember(Alias="secretsprovider")] - [System.Text.Json.Serialization.JsonPropertyName("secretsprovider")] + [YamlMember(Alias="secretsprovider")] + [JsonPropertyName("secretsprovider")] public string? SecretsProvider { get; set; } @@ -18,8 +20,8 @@ namespace Pulumi.Automation /// This is the KMS-encrypted ciphertext for the data key used for secrets /// encryption. Only used for cloud-based secrets providers. /// - [YamlDotNet.Serialization.YamlMember(Alias="encryptedkey")] - [System.Text.Json.Serialization.JsonPropertyName("encryptedkey")] + [YamlMember(Alias="encryptedkey")] + [JsonPropertyName("encryptedkey")] public string? EncryptedKey { get; set; } @@ -27,8 +29,8 @@ namespace Pulumi.Automation /// This is this stack's base64 encoded encryption salt. Only used for /// passphrase-based secrets providers. /// - [YamlDotNet.Serialization.YamlMember(Alias="encryptionsalt")] - [System.Text.Json.Serialization.JsonPropertyName("encryptionsalt")] + [YamlMember(Alias="encryptionsalt")] + [JsonPropertyName("encryptionsalt")] public string? EncryptionSalt { get; set; } diff --git a/sdk/dotnet/Pulumi.Automation/Workspace.cs b/sdk/dotnet/Pulumi.Automation/Workspace.cs index be86dbdf6..ec4a3dad2 100644 --- a/sdk/dotnet/Pulumi.Automation/Workspace.cs +++ b/sdk/dotnet/Pulumi.Automation/Workspace.cs @@ -9,6 +9,8 @@ using System.Threading.Tasks; using Pulumi.Automation.Commands; using Pulumi.Automation.Commands.Exceptions; using Pulumi.Automation.Events; +// ReSharper disable UnusedMemberInSuper.Global +// ReSharper disable VirtualMemberNeverOverridden.Global namespace Pulumi.Automation { @@ -276,7 +278,7 @@ namespace Pulumi.Automation internal async Task RunStackCommandAsync( string stackName, - IEnumerable args, + IList args, Action? onStandardOutput, Action? onStandardError, Action? onEngineEvent, @@ -291,12 +293,12 @@ namespace Pulumi.Automation } internal Task RunCommandAsync( - IEnumerable args, + IList args, CancellationToken cancellationToken) => this.RunCommandAsync(args, onStandardOutput: null, onStandardError: null, onEngineEvent: null, cancellationToken); internal Task RunCommandAsync( - IEnumerable args, + IList args, Action? onStandardOutput, Action? onStandardError, Action? onEngineEvent, diff --git a/sdk/dotnet/Pulumi.Automation/WorkspaceStack.cs b/sdk/dotnet/Pulumi.Automation/WorkspaceStack.cs index 88670fc40..4a0ffc000 100644 --- a/sdk/dotnet/Pulumi.Automation/WorkspaceStack.cs +++ b/sdk/dotnet/Pulumi.Automation/WorkspaceStack.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.IO; using System.Linq; using System.Net; using System.Runtime.ExceptionServices; @@ -117,30 +116,23 @@ namespace Pulumi.Automation this.Name = name; this.Workspace = workspace; - switch (mode) + this._readyTask = mode switch { - case WorkspaceStackInitMode.Create: - this._readyTask = workspace.CreateStackAsync(name, cancellationToken); - break; - case WorkspaceStackInitMode.Select: - this._readyTask = workspace.SelectStackAsync(name, cancellationToken); - break; - case WorkspaceStackInitMode.CreateOrSelect: - this._readyTask = Task.Run(async () => + WorkspaceStackInitMode.Create => workspace.CreateStackAsync(name, cancellationToken), + WorkspaceStackInitMode.Select => workspace.SelectStackAsync(name, cancellationToken), + WorkspaceStackInitMode.CreateOrSelect => Task.Run(async () => + { + try { - try - { - await workspace.CreateStackAsync(name, cancellationToken).ConfigureAwait(false); - } - catch (StackAlreadyExistsException) - { - await workspace.SelectStackAsync(name, cancellationToken).ConfigureAwait(false); - } - }); - break; - default: - throw new InvalidOperationException($"Unexpected Stack creation mode: {mode}"); - } + await workspace.CreateStackAsync(name, cancellationToken).ConfigureAwait(false); + } + catch (StackAlreadyExistsException) + { + await workspace.SelectStackAsync(name, cancellationToken).ConfigureAwait(false); + } + }), + _ => throw new InvalidOperationException($"Unexpected Stack creation mode: {mode}") + }; } /// @@ -211,7 +203,7 @@ namespace Pulumi.Automation { var execKind = ExecKind.Local; var program = this.Workspace.Program; - var args = new List() + var args = new List { "up", "--yes", @@ -313,7 +305,7 @@ namespace Pulumi.Automation { var execKind = ExecKind.Local; var program = this.Workspace.Program; - var args = new List() { "preview" }; + var args = new List { "preview" }; if (options != null) { @@ -423,7 +415,7 @@ namespace Pulumi.Automation RefreshOptions? options = null, CancellationToken cancellationToken = default) { - var args = new List() + var args = new List { "refresh", "--yes", @@ -478,7 +470,7 @@ namespace Pulumi.Automation DestroyOptions? options = null, CancellationToken cancellationToken = default) { - var args = new List() + var args = new List { "destroy", "--yes", @@ -539,7 +531,7 @@ namespace Pulumi.Automation HistoryOptions? options = null, CancellationToken cancellationToken = default) { - var args = new List() + var args = new List { "stack", "history", @@ -616,14 +608,13 @@ namespace Pulumi.Automation } private async Task RunCommandAsync( - IEnumerable args, + IList args, Action? onStandardOutput, Action? onStandardError, Action? onEngineEvent, CancellationToken cancellationToken) { - var argsList = args.ToList(); - argsList.AddRange(new List() { "--stack", this.Name }); + args = args.Concat(new[] { "--stack", this.Name }).ToList(); return await this.Workspace.RunStackCommandAsync(this.Name, args, onStandardOutput, onStandardError, onEngineEvent, cancellationToken); } diff --git a/sdk/dotnet/Pulumi.Tests/Core/InputTests.cs b/sdk/dotnet/Pulumi.Tests/Core/InputTests.cs index 02d7695fd..e519d65a9 100644 --- a/sdk/dotnet/Pulumi.Tests/Core/InputTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Core/InputTests.cs @@ -1,11 +1,7 @@ // Copyright 2016-2019, Pulumi Corporation -using System.Collections.Generic; using System.Collections.Immutable; -using System.Runtime.CompilerServices; -using System.Threading; using System.Threading.Tasks; -using Pulumi.Serialization; using Xunit; namespace Pulumi.Tests.Core @@ -16,14 +12,18 @@ namespace Pulumi.Tests.Core public Task MergeInputMaps() => RunInPreview(async () => { - var map1 = new InputMap(); - map1.Add("K1", "V1"); - map1.Add("K2", Output.Create("V2")); - map1.Add("K3", Output.Create("V3_wrong")); + var map1 = new InputMap + { + { "K1", "V1" }, + { "K2", Output.Create("V2") }, + { "K3", Output.Create("V3_wrong") } + }; - var map2 = new InputMap(); - map2.Add("K3", Output.Create("V3")); - map2.Add("K4", "V4"); + var map2 = new InputMap + { + { "K3", Output.Create("V3") }, + { "K4", "V4" } + }; var result = InputMap.Merge(map1, map2); @@ -31,7 +31,7 @@ namespace Pulumi.Tests.Core var data = await result.ToOutput().DataTask.ConfigureAwait(false); Assert.True(data.IsKnown); Assert.Equal(4, data.Value.Count); - for (int i = 1; i <=4; i++) + for (var i = 1; i <=4; i++) Assert.True(data.Value.Contains($"K{i}", $"V{i}")); // Check that the input maps haven't changed diff --git a/sdk/dotnet/Pulumi.Tests/Core/OutputTests.cs b/sdk/dotnet/Pulumi.Tests/Core/OutputTests.cs index 5e913b926..78a063c6b 100644 --- a/sdk/dotnet/Pulumi.Tests/Core/OutputTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Core/OutputTests.cs @@ -8,7 +8,7 @@ using Xunit; namespace Pulumi.Tests.Core { - public partial class OutputTests : PulumiTest + public class OutputTests : PulumiTest { private static Output CreateOutput(T value, bool isKnown, bool isSecret = false) => new Output(Task.FromResult(OutputData.Create( diff --git a/sdk/dotnet/Pulumi.Tests/Core/ResourceArgsTests.cs b/sdk/dotnet/Pulumi.Tests/Core/ResourceArgsTests.cs index 648eff201..9a3493585 100644 --- a/sdk/dotnet/Pulumi.Tests/Core/ResourceArgsTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Core/ResourceArgsTests.cs @@ -2,7 +2,6 @@ using System.Collections.Immutable; using System.Threading.Tasks; -using Pulumi.Serialization; using Xunit; namespace Pulumi.Tests.Core @@ -17,7 +16,8 @@ namespace Pulumi.Tests.Core [Input("array")] private InputList _array = null!; public InputList Array { - get => _array ?? (_array = new InputList()); + // ReSharper disable once ConstantNullCoalescingCondition + get => _array ??= new InputList(); set => _array = value; } } @@ -90,14 +90,16 @@ namespace Pulumi.Tests.Core [Input("array", json: true)] private InputList _array = null!; public InputList Array { - get => _array ?? (_array = new InputList()); + // ReSharper disable once ConstantNullCoalescingCondition + get => _array ??= new InputList(); set => _array = value; } [Input("map", json: true)] private InputMap _map = null!; public InputMap Map { - get => _map ?? (_map = new InputMap()); + // ReSharper disable once ConstantNullCoalescingCondition + get => _map ??= new InputMap(); set => _map = value; } } diff --git a/sdk/dotnet/Pulumi.Tests/Mocks/MocksTests.cs b/sdk/dotnet/Pulumi.Tests/Mocks/MocksTests.cs index bec86d5fd..6200573a6 100644 --- a/sdk/dotnet/Pulumi.Tests/Mocks/MocksTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Mocks/MocksTests.cs @@ -17,23 +17,16 @@ namespace Pulumi.Tests.Mocks return Task.FromResult(args); } - public Task<(string? id, object state)> NewResourceAsync(MockResourceArgs args) - { - switch (args.Type) + public Task<(string? id, object state)> NewResourceAsync(MockResourceArgs args) => + args.Type switch { - case "aws:ec2/instance:Instance": - return Task.FromResult<(string?, object)>(("i-1234567890abcdef0", new Dictionary { - { "publicIp", "203.0.113.12" }, - })); - case "pkg:index:MyCustom": - return Task.FromResult<(string?, object)>((args.Name + "_id", args.Inputs)); - default: - throw new Exception($"Unknown resource {args.Type}"); - } - } + "aws:ec2/instance:Instance" => Task.FromResult<(string?, object)>(("i-1234567890abcdef0", new Dictionary { { "publicIp", "203.0.113.12" }, })), + "pkg:index:MyCustom" => Task.FromResult<(string?, object)>((args.Name + "_id", args.Inputs)), + _ => throw new Exception($"Unknown resource {args.Type}") + }; } - public partial class MocksTests + public class MocksTests { [Fact] public async Task TestCustom() @@ -43,7 +36,7 @@ namespace Pulumi.Tests.Mocks var instance = resources.OfType().FirstOrDefault(); Assert.NotNull(instance); - var ip = await instance.PublicIp.GetValueAsync(); + var ip = await instance!.PublicIp.GetValueAsync(); Assert.Equal("203.0.113.12", ip); } @@ -52,10 +45,10 @@ namespace Pulumi.Tests.Mocks { var resources = await Testing.RunAsync(); - var mycustom = resources.OfType().FirstOrDefault(); - Assert.NotNull(mycustom); + var myCustom = resources.OfType().FirstOrDefault(); + Assert.NotNull(myCustom); - var instance = await mycustom.Instance.GetValueAsync(); + var instance = await myCustom!.Instance.GetValueAsync(); Assert.IsType(instance); var ip = await instance.PublicIp.GetValueAsync(); @@ -70,7 +63,7 @@ namespace Pulumi.Tests.Mocks var stack = resources.OfType().FirstOrDefault(); Assert.NotNull(stack); - var ip = await stack.PublicIp.GetValueAsync(); + var ip = await stack!.PublicIp.GetValueAsync(); Assert.Equal("203.0.113.12", ip); } } diff --git a/sdk/dotnet/Pulumi.Tests/Mocks/TestStack.cs b/sdk/dotnet/Pulumi.Tests/Mocks/TestStack.cs index e536b5151..a857a68e6 100644 --- a/sdk/dotnet/Pulumi.Tests/Mocks/TestStack.cs +++ b/sdk/dotnet/Pulumi.Tests/Mocks/TestStack.cs @@ -3,33 +3,33 @@ namespace Pulumi.Tests.Mocks { [ResourceType("aws:ec2/instance:Instance", null)] - public partial class Instance : Pulumi.CustomResource + public class Instance : CustomResource { [Output("publicIp")] public Output PublicIp { get; private set; } = null!; public Instance(string name, InstanceArgs args, CustomResourceOptions? options = null) - : base("aws:ec2/instance:Instance", name, args ?? new InstanceArgs(), options) + : base("aws:ec2/instance:Instance", name, args, options) { } } - public sealed class InstanceArgs : Pulumi.ResourceArgs + public sealed class InstanceArgs : ResourceArgs { } - public partial class MyCustom : Pulumi.CustomResource + public class MyCustom : CustomResource { [Output("instance")] public Output Instance { get; private set; } = null!; public MyCustom(string name, MyCustomArgs args, CustomResourceOptions? options = null) - : base("pkg:index:MyCustom", name, args ?? new MyCustomArgs(), options) + : base("pkg:index:MyCustom", name, args, options) { } } - public sealed class MyCustomArgs : Pulumi.ResourceArgs + public sealed class MyCustomArgs : ResourceArgs { [Input("instance")] public Input? Instance { get; set; } @@ -38,15 +38,12 @@ namespace Pulumi.Tests.Mocks public class MyStack : Stack { [Output("publicIp")] - public Output PublicIp { get; private set; } = null!; + public Output PublicIp { get; private set; } public MyStack() { var myInstance = new Instance("instance", new InstanceArgs()); - var myCustom = new MyCustom("mycustom", new MyCustomArgs - { - Instance = myInstance, - }); + new MyCustom("mycustom", new MyCustomArgs { Instance = myInstance }); this.PublicIp = myInstance.PublicIp; } } diff --git a/sdk/dotnet/Pulumi.Tests/Serialization/BooleanConverterTests.cs b/sdk/dotnet/Pulumi.Tests/Serialization/BooleanConverterTests.cs index 1e35fe792..bb4085479 100644 --- a/sdk/dotnet/Pulumi.Tests/Serialization/BooleanConverterTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Serialization/BooleanConverterTests.cs @@ -52,7 +52,7 @@ namespace Pulumi.Tests.Serialization { Assert.Throws(() => { - var data = Converter.ConvertValue("", new Value { StringValue = "" }); + Converter.ConvertValue("", new Value { StringValue = "" }); }); } @@ -94,7 +94,7 @@ namespace Pulumi.Tests.Serialization { Assert.Throws(() => { - var data = Converter.ConvertValue("", new Value { StringValue = "" }); + Converter.ConvertValue("", new Value { StringValue = "" }); }); } diff --git a/sdk/dotnet/Pulumi.Tests/Serialization/ComplexTypeConverterTests.cs b/sdk/dotnet/Pulumi.Tests/Serialization/ComplexTypeConverterTests.cs index f4a1a5f21..4e7341ef6 100644 --- a/sdk/dotnet/Pulumi.Tests/Serialization/ComplexTypeConverterTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Serialization/ComplexTypeConverterTests.cs @@ -35,7 +35,7 @@ namespace Pulumi.Tests.Serialization public override bool Equals(object? obj) => obj is ContainerColor other && Equals(other); public bool Equals(ContainerColor other) => string.Equals(_value, other._value, StringComparison.Ordinal); - public override int GetHashCode() => _value?.GetHashCode() ?? 0; + public override int GetHashCode() => _value.GetHashCode(); public override string ToString() => _value; } diff --git a/sdk/dotnet/Pulumi.Tests/Serialization/EnumConverterTests.cs b/sdk/dotnet/Pulumi.Tests/Serialization/EnumConverterTests.cs index 0f39bdc3e..76d9de9e6 100644 --- a/sdk/dotnet/Pulumi.Tests/Serialization/EnumConverterTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Serialization/EnumConverterTests.cs @@ -3,9 +3,12 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.Threading.Tasks; +using Google.Protobuf.WellKnownTypes; using Pulumi.Serialization; using Xunit; +using Type = System.Type; namespace Pulumi.Tests.Serialization { @@ -35,7 +38,7 @@ namespace Pulumi.Tests.Serialization public bool Equals(ContainerColor other) => string.Equals(_value, other._value, StringComparison.Ordinal); [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => _value?.GetHashCode() ?? 0; + public override int GetHashCode() => _value.GetHashCode(); public override string ToString() => _value; } @@ -60,12 +63,13 @@ namespace Pulumi.Tests.Serialization [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object? obj) => obj is ContainerBrightness other && Equals(other); + // ReSharper disable once CompareOfFloatsByEqualityOperator public bool Equals(ContainerBrightness other) => _value == other._value; [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() => _value.GetHashCode(); - public override string ToString() => _value.ToString(); + public override string ToString() => _value.ToString(CultureInfo.InvariantCulture); } public enum ContainerSize @@ -196,14 +200,14 @@ namespace Pulumi.Tests.Serialization public static IEnumerable EnumsWithUnconvertibleValues() => new[] { - new object[] { typeof(ContainerColor), new Google.Protobuf.WellKnownTypes.Value { NumberValue = 1.0 } }, - new object[] { typeof(ContainerBrightness), new Google.Protobuf.WellKnownTypes.Value { StringValue = "hello" } }, - new object[] { typeof(ContainerSize), new Google.Protobuf.WellKnownTypes.Value { StringValue = "hello" } }, + new object[] { typeof(ContainerColor), new Value { NumberValue = 1.0 } }, + new object[] { typeof(ContainerBrightness), new Value { StringValue = "hello" } }, + new object[] { typeof(ContainerSize), new Value { StringValue = "hello" } }, }; [Theory] [MemberData(nameof(EnumsWithUnconvertibleValues))] - public void ConvertingUnconvertibleValuesThrows(Type targetType, Google.Protobuf.WellKnownTypes.Value value) + public void ConvertingUnconvertibleValuesThrows(Type targetType, Value value) { Assert.Throws(() => { diff --git a/sdk/dotnet/Pulumi.Tests/Serialization/InternalPropertyTests.cs b/sdk/dotnet/Pulumi.Tests/Serialization/InternalPropertyTests.cs index 26d96b828..3e7a9cdd1 100644 --- a/sdk/dotnet/Pulumi.Tests/Serialization/InternalPropertyTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Serialization/InternalPropertyTests.cs @@ -1,9 +1,6 @@ // Copyright 2016-2019, Pulumi Corporation -using System; -using System.Collections.Generic; using System.Collections.Immutable; -using System.Threading.Tasks; using Google.Protobuf.WellKnownTypes; using Pulumi.Serialization; using Xunit; diff --git a/sdk/dotnet/Pulumi.Tests/Serialization/ResourceRefPropertyTests.cs b/sdk/dotnet/Pulumi.Tests/Serialization/ResourceRefPropertyTests.cs index d369cc306..53318b813 100644 --- a/sdk/dotnet/Pulumi.Tests/Serialization/ResourceRefPropertyTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Serialization/ResourceRefPropertyTests.cs @@ -14,7 +14,7 @@ namespace Pulumi.Tests.Serialization { public class ResourceRefPropertyTests : ConverterTests { - public sealed class MyArgs : Pulumi.ResourceArgs + public sealed class MyArgs : ResourceArgs { } @@ -52,18 +52,18 @@ namespace Pulumi.Tests.Serialization { public MyStack() { - var customResource = new MyCustomResource("test", null, null); - var componentResource = new MyComponentResource("test", null, null); + new MyCustomResource("test", null); + new MyComponentResource("test", null); } } class MyMocks : IMocks { - bool isPreview; + private readonly bool _isPreview; public MyMocks(bool isPreview) { - this.isPreview = isPreview; + this._isPreview = isPreview; } public Task CallAsync(MockCallArgs args) @@ -77,10 +77,10 @@ namespace Pulumi.Tests.Serialization { case "test:index:resource": case "test:missing:resource": - return Task.FromResult<(string?, object)>((this.isPreview ? null : "id", new Dictionary {})); + return Task.FromResult<(string?, object)>((this._isPreview ? null : "id", new Dictionary())); case "test:index:component": case "test:missing:component": - return Task.FromResult<(string?, object)>((null, new Dictionary {})); + return Task.FromResult<(string?, object)>((null, new Dictionary())); default: throw new Exception($"Unknown resource {args.Type}"); } @@ -129,7 +129,7 @@ namespace Pulumi.Tests.Serialization var res = resources.OfType().FirstOrDefault(); Assert.NotNull(res); - var urn = (await res.Urn.DataTask).Value; + var urn = (await res!.Urn.DataTask).Value; var id = (await res.Id.DataTask).Value; var v = await SerializeToValueAsync(res); @@ -145,7 +145,7 @@ namespace Pulumi.Tests.Serialization var res = resources.OfType().FirstOrDefault(); Assert.NotNull(res); - var id = await SerializeToValueAsync(res.Id); + var id = await SerializeToValueAsync(res!.Id); var v = await SerializeToValueAsync(res, false); Assert.Equal(id, v); @@ -154,11 +154,11 @@ namespace Pulumi.Tests.Serialization public class DeserializeCustomResourceStack : Stack { [Output("values")] - public Output> Values { get; private set; } = null!; + public Output> Values { get; private set; } public DeserializeCustomResourceStack() { - var res = new MyCustomResource("test", null, null); + var res = new MyCustomResource("test", null); var urn = res.Urn.DataTask.Result.Value; var id = res.Id.DataTask.Result.Value; @@ -184,7 +184,7 @@ namespace Pulumi.Tests.Serialization var stack = resources.OfType().FirstOrDefault(); Assert.NotNull(stack); - var values = (await stack.Values.DataTask).Value; + var values = (await stack!.Values.DataTask).Value; Assert.Equal(values["expectedUrn"], values["actualUrn"]); Assert.Equal(values["expectedId"], values["actualId"]); } @@ -192,11 +192,11 @@ namespace Pulumi.Tests.Serialization public class DeserializeMissingCustomResourceStack : Stack { [Output("values")] - public Output> Values { get; private set; } = null!; + public Output> Values { get; private set; } public DeserializeMissingCustomResourceStack() { - var res = new MissingCustomResource("test", null, null); + var res = new MissingCustomResource("test", null); var urn = res.Urn.DataTask.Result.Value; @@ -219,7 +219,7 @@ namespace Pulumi.Tests.Serialization var stack = resources.OfType().FirstOrDefault(); Assert.NotNull(stack); - var values = (await stack.Values.DataTask).Value; + var values = (await stack!.Values.DataTask).Value; Assert.Equal(values["expectedUrn"], values["actualUrn"]); } @@ -232,7 +232,7 @@ namespace Pulumi.Tests.Serialization var res = resources.OfType().FirstOrDefault(); Assert.NotNull(res); - var urn = (await res.Urn.DataTask).Value; + var urn = (await res!.Urn.DataTask).Value; var v = await SerializeToValueAsync(res); Assert.Equal(CreateComponentResourceReference(urn), v); @@ -247,7 +247,7 @@ namespace Pulumi.Tests.Serialization var res = resources.OfType().FirstOrDefault(); Assert.NotNull(res); - var urn = await SerializeToValueAsync(res.Urn); + var urn = await SerializeToValueAsync(res!.Urn); var v = await SerializeToValueAsync(res, false); Assert.Equal(urn, v); @@ -256,11 +256,11 @@ namespace Pulumi.Tests.Serialization public class DeserializeComponentResourceStack : Stack { [Output("values")] - public Output> Values { get; private set; } = null!; + public Output> Values { get; private set; } public DeserializeComponentResourceStack() { - var res = new MyComponentResource("test", null, null); + var res = new MyComponentResource("test", null); var urn = res.Urn.DataTask.Result.Value; @@ -283,18 +283,18 @@ namespace Pulumi.Tests.Serialization var stack = resources.OfType().FirstOrDefault(); Assert.NotNull(stack); - var values = (await stack.Values.DataTask).Value; + var values = (await stack!.Values.DataTask).Value; Assert.Equal(values["expectedUrn"], values["actualUrn"]); } public class DeserializeMissingComponentResourceStack : Stack { [Output("values")] - public Output> Values { get; private set; } = null!; + public Output> Values { get; private set; } public DeserializeMissingComponentResourceStack() { - var res = new MissingComponentResource("test", null, null); + var res = new MissingComponentResource("test", null); var urn = res.Urn.DataTask.Result.Value; @@ -317,7 +317,7 @@ namespace Pulumi.Tests.Serialization var stack = resources.OfType().FirstOrDefault(); Assert.NotNull(stack); - var values = (await stack.Values.DataTask).Value; + var values = (await stack!.Values.DataTask).Value; Assert.Equal(values["expectedUrn"], values["actualUrn"]); } } diff --git a/sdk/dotnet/Pulumi.Tests/Serialization/UnionConverterTests.cs b/sdk/dotnet/Pulumi.Tests/Serialization/UnionConverterTests.cs index ac4fbc23b..ee1faecd3 100644 --- a/sdk/dotnet/Pulumi.Tests/Serialization/UnionConverterTests.cs +++ b/sdk/dotnet/Pulumi.Tests/Serialization/UnionConverterTests.cs @@ -50,7 +50,7 @@ namespace Pulumi.Tests.Serialization { Assert.Throws(() => { - var data = Converter.ConvertValue>("", new Value { BoolValue = true }); + Converter.ConvertValue>("", new Value { BoolValue = true }); }); } } diff --git a/sdk/dotnet/Pulumi.Tests/StackTests.cs b/sdk/dotnet/Pulumi.Tests/StackTests.cs index fd504e9c7..a77e38b8c 100644 --- a/sdk/dotnet/Pulumi.Tests/StackTests.cs +++ b/sdk/dotnet/Pulumi.Tests/StackTests.cs @@ -38,7 +38,7 @@ namespace Pulumi.Tests private class NullOutputStack : Stack { [Output("foo")] - public Output? Foo { get; } + public Output? Foo { get; } = null; } [Fact] @@ -46,7 +46,7 @@ namespace Pulumi.Tests { try { - var (stack, outputs) = await Run(); + await Run(); } catch (RunException ex) { @@ -73,7 +73,7 @@ namespace Pulumi.Tests { try { - var (stack, outputs) = await Run(); + await Run(); } catch (RunException ex) { diff --git a/sdk/dotnet/Pulumi/Config.cs b/sdk/dotnet/Pulumi/Config.cs index 6d4110b98..65b0672ff 100644 --- a/sdk/dotnet/Pulumi/Config.cs +++ b/sdk/dotnet/Pulumi/Config.cs @@ -1,7 +1,6 @@ // Copyright 2016-2019, Pulumi Corporation using System; -using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Text.Json; @@ -32,14 +31,10 @@ namespace Pulumi /// public Config(string? name = null) { - if (name == null) - { - name = Deployment.Instance.ProjectName; - } - + name ??= Deployment.Instance.ProjectName; if (name.EndsWith(":config", StringComparison.Ordinal)) { - name = name[0..^":config".Length]; + name = name[..^":config".Length]; } _name = name; @@ -84,9 +79,13 @@ namespace Pulumi private bool? GetBooleanImpl(string key, string? use = null, [CallerMemberName] string? insteadOf = null) { var v = GetImpl(key, use, insteadOf); - return v == null ? default(bool?) : - v == "true" ? true : - v == "false" ? false : throw new ConfigTypeException(FullKey(key), v, nameof(Boolean)); + return v switch + { + null => default(bool?), + "true" => true, + "false" => false, + _ => throw new ConfigTypeException(FullKey(key), v, nameof(Boolean)) + }; } /// @@ -155,8 +154,7 @@ namespace Pulumi /// /// Loads an optional configuration value, as an object, by its key, marking it as a secret /// or null if it doesn't exist. This works by taking the value associated with and passing it to . + /// name="key"/> and passing it to . /// public Output? GetSecretObject(string key) { @@ -238,8 +236,8 @@ namespace Pulumi /// /// Loads a configuration value as a JSON string and deserializes the JSON into a JavaScript /// object, marking it as a secret. If it doesn't exist, or the configuration value cannot - /// be converted using . an error is thrown. + /// be converted using , + /// an error is thrown. /// public Output RequireSecretObject(string key) => Output.CreateSecret(RequireObjectImpl(key)); diff --git a/sdk/dotnet/Pulumi/Config_Exceptions.cs b/sdk/dotnet/Pulumi/Config_Exceptions.cs index 931ded788..aa2ec345e 100644 --- a/sdk/dotnet/Pulumi/Config_Exceptions.cs +++ b/sdk/dotnet/Pulumi/Config_Exceptions.cs @@ -11,12 +11,7 @@ namespace Pulumi /// private class ConfigTypeException : RunException { - public ConfigTypeException(string key, object? v, string expectedType) - : this(key, v, expectedType, innerException: null) - { - } - - public ConfigTypeException(string key, object? v, string expectedType, Exception? innerException) + public ConfigTypeException(string key, object? v, string expectedType, Exception? innerException = null) : base($"Configuration '{key}' value '{v}' is not a valid {expectedType}", innerException) { } diff --git a/sdk/dotnet/Pulumi/Core/AssetOrArchive.cs b/sdk/dotnet/Pulumi/Core/AssetOrArchive.cs index cc0508e77..4fe60325c 100644 --- a/sdk/dotnet/Pulumi/Core/AssetOrArchive.cs +++ b/sdk/dotnet/Pulumi/Core/AssetOrArchive.cs @@ -1,5 +1,7 @@ // Copyright 2016-2019, Pulumi Corporation +using System; + namespace Pulumi { /// @@ -13,9 +15,9 @@ namespace Pulumi private protected AssetOrArchive(string sigKey, string propName, object value) { - SigKey = sigKey ?? throw new System.ArgumentNullException(nameof(sigKey)); - PropName = propName ?? throw new System.ArgumentNullException(nameof(propName)); - Value = value ?? throw new System.ArgumentNullException(nameof(value)); + SigKey = sigKey ?? throw new ArgumentNullException(nameof(sigKey)); + PropName = propName ?? throw new ArgumentNullException(nameof(propName)); + Value = value ?? throw new ArgumentNullException(nameof(value)); } } } diff --git a/sdk/dotnet/Pulumi/Core/InputList.cs b/sdk/dotnet/Pulumi/Core/InputList.cs index 0dac7a303..180060388 100644 --- a/sdk/dotnet/Pulumi/Core/InputList.cs +++ b/sdk/dotnet/Pulumi/Core/InputList.cs @@ -127,13 +127,13 @@ namespace Pulumi #region construct from Output of some list type. public static implicit operator InputList(Output values) - => values.Apply(a => ImmutableArray.CreateRange(a)); + => values.Apply(ImmutableArray.CreateRange); public static implicit operator InputList(Output> values) - => values.Apply(a => ImmutableArray.CreateRange(a)); + => values.Apply(ImmutableArray.CreateRange); public static implicit operator InputList(Output> values) - => values.Apply(a => ImmutableArray.CreateRange(a)); + => values.Apply(ImmutableArray.CreateRange); public static implicit operator InputList(Output> values) => new InputList(values); diff --git a/sdk/dotnet/Pulumi/Core/InputMap.cs b/sdk/dotnet/Pulumi/Core/InputMap.cs index 920d2eb13..6068a51f8 100644 --- a/sdk/dotnet/Pulumi/Core/InputMap.cs +++ b/sdk/dotnet/Pulumi/Core/InputMap.cs @@ -13,7 +13,7 @@ namespace Pulumi /// . The individual values are themselves s. i.e. /// the individual values can be concrete values or s. /// - /// differs from a normal in that it is + /// differs from a normal in that it is /// itself an . For example, a that accepts an /// will accept not just a dictionary but an /// of a dictionary as well. This is important for cases where the @@ -96,10 +96,10 @@ namespace Pulumi => Output.Create(values); public static implicit operator InputMap(Output> values) - => values.Apply(d => ImmutableDictionary.CreateRange(d)); + => values.Apply(ImmutableDictionary.CreateRange); public static implicit operator InputMap(Output> values) - => values.Apply(d => ImmutableDictionary.CreateRange(d)); + => values.Apply(ImmutableDictionary.CreateRange); public static implicit operator InputMap(Output> values) => new InputMap(values); diff --git a/sdk/dotnet/Pulumi/Core/InputUnion.cs b/sdk/dotnet/Pulumi/Core/InputUnion.cs index 962c1874b..9ed084c61 100644 --- a/sdk/dotnet/Pulumi/Core/InputUnion.cs +++ b/sdk/dotnet/Pulumi/Core/InputUnion.cs @@ -1,7 +1,5 @@ // Copyright 2016-2019, Pulumi Corporation -using System; - namespace Pulumi { /// @@ -29,10 +27,10 @@ namespace Pulumi => Output.Create(value); public static implicit operator InputUnion(Output value) - => new InputUnion(value.Apply(v => Union.FromT0(v))); + => new InputUnion(value.Apply(Union.FromT0)); public static implicit operator InputUnion(Output value) - => new InputUnion(value.Apply(v => Union.FromT1(v))); + => new InputUnion(value.Apply(Union.FromT1)); #endregion } diff --git a/sdk/dotnet/Pulumi/Core/Options.cs b/sdk/dotnet/Pulumi/Core/Options.cs index 62de2fcbe..c7439b055 100644 --- a/sdk/dotnet/Pulumi/Core/Options.cs +++ b/sdk/dotnet/Pulumi/Core/Options.cs @@ -1,5 +1,6 @@ // Copyright 2016-2019, Pulumi Corporation +// ReSharper disable NotAccessedField.Global namespace Pulumi { internal class Options diff --git a/sdk/dotnet/Pulumi/Core/Output.cs b/sdk/dotnet/Pulumi/Core/Output.cs index 19deb57be..62ae13491 100644 --- a/sdk/dotnet/Pulumi/Core/Output.cs +++ b/sdk/dotnet/Pulumi/Core/Output.cs @@ -192,19 +192,19 @@ namespace Pulumi } /// - /// for more details. + /// for more details. /// public Output Apply(Func func) => Apply(t => Output.Create(func(t))); /// - /// for more details. + /// for more details. /// public Output Apply(Func> func) => Apply(t => Output.Create(func(t))); /// - /// for more details. + /// for more details. /// public Output Apply(Func?> func) => Apply(t => func(t).ToOutput()); @@ -303,6 +303,7 @@ namespace Pulumi var isKnown = true; var isSecret = false; +#pragma warning disable 8601 Update(await GetData(item1).ConfigureAwait(false), ref tuple.Item1); Update(await GetData(item2).ConfigureAwait(false), ref tuple.Item2); Update(await GetData(item3).ConfigureAwait(false), ref tuple.Item3); @@ -311,6 +312,7 @@ namespace Pulumi Update(await GetData(item6).ConfigureAwait(false), ref tuple.Item6); Update(await GetData(item7).ConfigureAwait(false), ref tuple.Item7); Update(await GetData(item8).ConfigureAwait(false), ref tuple.Item8); +#pragma warning restore 8601 return OutputData.Create(resources.ToImmutable(), tuple, isKnown, isSecret); diff --git a/sdk/dotnet/Pulumi/Core/Union.cs b/sdk/dotnet/Pulumi/Core/Union.cs index face471eb..cad8fd7a5 100644 --- a/sdk/dotnet/Pulumi/Core/Union.cs +++ b/sdk/dotnet/Pulumi/Core/Union.cs @@ -3,6 +3,8 @@ using System; using OneOf; +// ReSharper disable PossiblyImpureMethodCallOnReadonlyVariable + namespace Pulumi { /// @@ -26,7 +28,7 @@ namespace Pulumi /// or a can be represented as Output<int, string>. The version of this is . /// - public struct Union : IEquatable>, IUnion + public readonly struct Union : IEquatable>, IUnion { private readonly OneOf _data; diff --git a/sdk/dotnet/Pulumi/Core/Urn.cs b/sdk/dotnet/Pulumi/Core/Urn.cs index 31145b1ad..fa412696f 100644 --- a/sdk/dotnet/Pulumi/Core/Urn.cs +++ b/sdk/dotnet/Pulumi/Core/Urn.cs @@ -66,10 +66,7 @@ namespace Pulumi if (childName!.StartsWith(parentName, StringComparison.Ordinal)) { aliasName = parentAlias.ToOutput().Apply(parentAliasUrn => - { - return parentAliasUrn.Substring(parentAliasUrn.LastIndexOf("::", StringComparison.Ordinal) + 2) - + childName.Substring(parentName.Length); - }); + parentAliasUrn.Substring(parentAliasUrn.LastIndexOf("::", StringComparison.Ordinal) + 2) + childName.Substring(parentName.Length)); } var urn = Create( diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment.Runner.cs b/sdk/dotnet/Pulumi/Deployment/Deployment.Runner.cs index 92b6f0a96..1beba8aad 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment.Runner.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment.Runner.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Reflection; using System.Threading; using System.Threading.Tasks; @@ -29,7 +30,7 @@ namespace Pulumi public Runner(IDeploymentInternal deployment) => _deployment = deployment; - public Task RunAsync(IServiceProvider serviceProvider) where TStack : Stack + Task IRunner.RunAsync(IServiceProvider serviceProvider) { if (serviceProvider == null) { @@ -40,8 +41,7 @@ namespace Pulumi ?? throw new ApplicationException($"Failed to resolve instance of type {typeof(TStack)} from service provider. Register the type with the service provider before calling {nameof(RunAsync)}.")); } - public Task RunAsync() where TStack : Stack, new() - => RunAsync(() => new TStack()); + Task IRunner.RunAsync() => RunAsync(() => new TStack()); public Task RunAsync(Func stackFactory) where TStack : Stack { @@ -60,7 +60,7 @@ namespace Pulumi return WhileRunningAsync(); } - public Task RunAsync(Func>> func, StackOptions? options) + Task IRunner.RunAsync(Func>> func, StackOptions? options) { var stack = new Stack(func, options); RegisterTask("User program code.", stack.Outputs.DataTask); @@ -214,10 +214,9 @@ namespace Pulumi } else { - var location = System.Reflection.Assembly.GetEntryAssembly()?.Location; - await _deployment.Logger.ErrorAsync( - $@"Running program '{location}' failed with an unhandled exception: -{exception.ToString()}").ConfigureAwait(false); + var location = Assembly.GetEntryAssembly()?.Location; + await _deployment.Logger.ErrorAsync($@"Running program '{location}' failed with an unhandled exception: +{exception}").ConfigureAwait(false); } _deployment.Serilogger.Debug("Wrote last error. Returning from program."); diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment.cs b/sdk/dotnet/Pulumi/Deployment/Deployment.cs index 28353f876..6bf51d4f2 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment.cs @@ -7,6 +7,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Pulumi.Testing; +using Pulumirpc; using Serilog; using Serilog.Events; @@ -30,7 +31,7 @@ namespace Pulumi /// /// Importantly: Cloud resources cannot be created outside of the lambda passed to any of the /// overloads. Because cloud Resource construction is - /// inherently asynchronous, the result of this function is a which should + /// inherently asynchronous, the result of this function is a which should /// then be returned or awaited. This will ensure that any problems that are encountered during /// the running of the program are properly reported. Failure to do this may lead to the /// program ending early before all resources are properly registered. @@ -38,7 +39,7 @@ namespace Pulumi public sealed partial class Deployment : IDeploymentInternal { private static readonly object _instanceLock = new object(); - private static AsyncLocal _instance = new AsyncLocal(); + private static readonly AsyncLocal _instance = new AsyncLocal(); /// /// The current running deployment instance. This is only available from inside the function @@ -101,6 +102,7 @@ namespace Pulumi private Deployment() { + // ReSharper disable UnusedVariable var monitor = Environment.GetEnvironmentVariable("PULUMI_MONITOR"); var engine = Environment.GetEnvironmentVariable("PULUMI_ENGINE"); var project = Environment.GetEnvironmentVariable("PULUMI_PROJECT"); @@ -121,6 +123,7 @@ namespace Pulumi { throw new InvalidOperationException("Program run without the Pulumi engine available; re-run using the `pulumi` CLI"); } + // ReSharper restore UnusedVariable _isDryRun = dryRunValue; _stackName = stack; @@ -193,7 +196,7 @@ namespace Pulumi { if (!this._featureSupport.ContainsKey(feature)) { - var request = new Pulumirpc.SupportsFeatureRequest {Id = feature }; + var request = new SupportsFeatureRequest {Id = feature }; var response = await this.Monitor.SupportsFeatureAsync(request).ConfigureAwait(false); this._featureSupport[feature] = response.HasSupport; } diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment_Invoke.cs b/sdk/dotnet/Pulumi/Deployment/Deployment_Invoke.cs index 12afed229..6f42044a5 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment_Invoke.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment_Invoke.cs @@ -36,6 +36,7 @@ namespace Pulumi Log.Debug(label); // Be resilient to misbehaving callers. + // ReSharper disable once ConstantNullCoalescingCondition args ??= InvokeArgs.Empty; // Wait for all values to be available, and then perform the RPC. diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment_Prepare.cs b/sdk/dotnet/Pulumi/Deployment/Deployment_Prepare.cs index 2bb48f59a..4170da0d1 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment_Prepare.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment_Prepare.cs @@ -1,6 +1,5 @@ // Copyright 2016-2021, Pulumi Corporation -using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; @@ -40,7 +39,7 @@ namespace Pulumi // Wait for the parent to complete. // If no parent was provided, parent to the root resource. LogExcessive($"Getting parent urn: t={type}, name={name}, custom={custom}, remote={remote}"); - var parentURN = options.Parent != null + var parentUrn = options.Parent != null ? await options.Parent.Urn.GetValueAsync().ConfigureAwait(false) : await GetRootResourceAsync(type).ConfigureAwait(false); LogExcessive($"Got parent urn: t={type}, name={name}, custom={custom}, remote={remote}"); @@ -53,25 +52,21 @@ namespace Pulumi } var providerRefs = new Dictionary(); - if (remote) + if (remote && options is ComponentResourceOptions componentOpts) { - var componentOpts = options as ComponentResourceOptions; - if (componentOpts != null) + // If only the Provider opt is set, move it to the Providers list for further processing. + if (componentOpts.Provider != null && componentOpts.Providers.Count == 0) { - // If only the Provider opt is set, move it to the Providers list for further processing. - if (componentOpts.Provider != null && componentOpts.Providers.Count == 0) - { - componentOpts.Providers.Add(componentOpts.Provider); - componentOpts.Provider = null; - } + componentOpts.Providers.Add(componentOpts.Provider); + componentOpts.Provider = null; + } - foreach (var provider in componentOpts.Providers) + foreach (var provider in componentOpts.Providers) + { + var pref = await ProviderResource.RegisterAsync(provider).ConfigureAwait(false); + if (pref != null) { - var pref = await ProviderResource.RegisterAsync(provider).ConfigureAwait(false); - if (pref != null) - { - providerRefs.Add(provider.Package, pref); - } + providerRefs.Add(provider.Package, pref); } } } @@ -82,16 +77,16 @@ namespace Pulumi // The list of all dependencies (implicit or explicit). var allDirectDependencies = new HashSet(explicitDirectDependencies); - var allDirectDependencyURNs = await GetAllTransitivelyReferencedCustomResourceURNsAsync(explicitDirectDependencies).ConfigureAwait(false); - var propertyToDirectDependencyURNs = new Dictionary>(); + var allDirectDependencyUrns = await GetAllTransitivelyReferencedCustomResourceUrnsAsync(explicitDirectDependencies).ConfigureAwait(false); + var propertyToDirectDependencyUrns = new Dictionary>(); foreach (var (propertyName, directDependencies) in propertyToDirectDependencies) { allDirectDependencies.AddRange(directDependencies); - var urns = await GetAllTransitivelyReferencedCustomResourceURNsAsync(directDependencies).ConfigureAwait(false); - allDirectDependencyURNs.AddRange(urns); - propertyToDirectDependencyURNs[propertyName] = urns; + var urns = await GetAllTransitivelyReferencedCustomResourceUrnsAsync(directDependencies).ConfigureAwait(false); + allDirectDependencyUrns.AddRange(urns); + propertyToDirectDependencyUrns[propertyName] = urns; } // Wait for all aliases. Note that we use 'res._aliases' instead of 'options.aliases' as @@ -111,11 +106,11 @@ namespace Pulumi return new PrepareResult( serializedProps, - parentURN ?? "", + parentUrn ?? "", providerRef ?? "", providerRefs, - allDirectDependencyURNs, - propertyToDirectDependencyURNs, + allDirectDependencyUrns, + propertyToDirectDependencyUrns, aliases); void LogExcessive(string message) @@ -128,7 +123,7 @@ namespace Pulumi private static Task> GatherExplicitDependenciesAsync(InputList resources) => resources.ToOutput().GetValueAsync(); - private static async Task> GetAllTransitivelyReferencedCustomResourceURNsAsync( + private static async Task> GetAllTransitivelyReferencedCustomResourceUrnsAsync( HashSet resources) { // Go through 'resources', but transitively walk through **Component** resources, @@ -190,24 +185,24 @@ namespace Pulumi } } - private struct PrepareResult + private readonly struct PrepareResult { public readonly Struct SerializedProps; public readonly string ParentUrn; public readonly string ProviderRef; public readonly Dictionary ProviderRefs; - public readonly HashSet AllDirectDependencyURNs; - public readonly Dictionary> PropertyToDirectDependencyURNs; + public readonly HashSet AllDirectDependencyUrns; + public readonly Dictionary> PropertyToDirectDependencyUrns; public readonly List Aliases; - public PrepareResult(Struct serializedProps, string parentUrn, string providerRef, Dictionary providerRefs, HashSet allDirectDependencyURNs, Dictionary> propertyToDirectDependencyURNs, List aliases) + public PrepareResult(Struct serializedProps, string parentUrn, string providerRef, Dictionary providerRefs, HashSet allDirectDependencyUrns, Dictionary> propertyToDirectDependencyUrns, List aliases) { SerializedProps = serializedProps; ParentUrn = parentUrn; ProviderRef = providerRef; ProviderRefs = providerRefs; - AllDirectDependencyURNs = allDirectDependencyURNs; - PropertyToDirectDependencyURNs = propertyToDirectDependencyURNs; + AllDirectDependencyUrns = allDirectDependencyUrns; + PropertyToDirectDependencyUrns = propertyToDirectDependencyUrns; Aliases = aliases; } } diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment_ReadOrRegisterResource.cs b/sdk/dotnet/Pulumi/Deployment/Deployment_ReadOrRegisterResource.cs index 9e24a6e7a..3b166c5d3 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment_ReadOrRegisterResource.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment_ReadOrRegisterResource.cs @@ -90,8 +90,9 @@ namespace Pulumi resource, remote, newDependency, args, options).ConfigureAwait(false); completionSources[Constants.UrnPropertyName].SetStringValue(response.urn, isKnown: true); - if (resource is CustomResource customResource) + if (resource is CustomResource) { + // ReSharper disable once ConstantNullCoalescingCondition var id = response.id ?? ""; completionSources[Constants.IdPropertyName].SetStringValue(id, isKnown: id != ""); } diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment_ReadResource.cs b/sdk/dotnet/Pulumi/Deployment/Deployment_ReadResource.cs index 4ba7e798b..6c6629f04 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment_ReadResource.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment_ReadResource.cs @@ -1,10 +1,8 @@ // Copyright 2016-2019, Pulumi Corporation -using System; using System.Collections.Immutable; using System.Threading.Tasks; using Google.Protobuf.WellKnownTypes; -using Pulumi.Serialization; using Pulumirpc; namespace Pulumi @@ -22,7 +20,6 @@ namespace Pulumi var prepareResult = await this.PrepareResourceAsync( label, resource, custom: true, remote: false, args, options).ConfigureAwait(false); - var serializer = new Serializer(_excessiveDebugOutput); Log.Debug($"ReadResource RPC prepared: id={id}, t={type}, name={name}" + (_excessiveDebugOutput ? $", obj={prepareResult.SerializedProps}" : "")); @@ -35,12 +32,12 @@ namespace Pulumi Parent = prepareResult.ParentUrn, Provider = prepareResult.ProviderRef, Properties = prepareResult.SerializedProps, - Version = options?.Version ?? "", + Version = options.Version ?? "", AcceptSecrets = true, AcceptResources = !_disableResourceReferences, }; - request.Dependencies.AddRange(prepareResult.AllDirectDependencyURNs); + request.Dependencies.AddRange(prepareResult.AllDirectDependencyUrns); // Now run the operation, serializing the invocation if necessary. var response = await this.Monitor.ReadResourceAsync(resource, request); diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment_RegisterResource.cs b/sdk/dotnet/Pulumi/Deployment/Deployment_RegisterResource.cs index e4df0fd22..ec30d056b 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment_RegisterResource.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment_RegisterResource.cs @@ -54,12 +54,12 @@ namespace Pulumi request.Provider = prepareResult.ProviderRef; request.Providers.Add(prepareResult.ProviderRefs); request.Aliases.AddRange(prepareResult.Aliases); - request.Dependencies.AddRange(prepareResult.AllDirectDependencyURNs); + request.Dependencies.AddRange(prepareResult.AllDirectDependencyUrns); - foreach (var (key, resourceURNs) in prepareResult.PropertyToDirectDependencyURNs) + foreach (var (key, resourceUrns) in prepareResult.PropertyToDirectDependencyUrns) { var deps = new RegisterResourceRequest.Types.PropertyDependencies(); - deps.Urns.AddRange(resourceURNs); + deps.Urns.AddRange(resourceUrns); request.PropertyDependencies.Add(key, deps); } } @@ -70,7 +70,7 @@ namespace Pulumi var customOpts = options as CustomResourceOptions; var deleteBeforeReplace = customOpts?.DeleteBeforeReplace; - var request = new RegisterResourceRequest() + var request = new RegisterResourceRequest { Type = type, Name = name, @@ -113,7 +113,7 @@ namespace Pulumi // Simply put, we simply convert our ticks to the integral number of nanoseconds // corresponding to it. Since each tick is 100ns, this can trivialy be done just by // appending "00" to it. - return timeSpan.Value.Ticks.ToString() + "00ns"; + return timeSpan.Value.Ticks + "00ns"; } } } diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment_RegisterResourceOutputs.cs b/sdk/dotnet/Pulumi/Deployment/Deployment_RegisterResourceOutputs.cs index 26a1a6210..0c56a5d53 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment_RegisterResourceOutputs.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment_RegisterResourceOutputs.cs @@ -1,7 +1,6 @@ // Copyright 2016-2019, Pulumi Corporation using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using Google.Protobuf; using Pulumirpc; @@ -22,7 +21,7 @@ namespace Pulumi private async Task RegisterResourceOutputsAsync( Resource resource, Output> outputs) { - var opLabel = $"monitor.registerResourceOutputs(...)"; + var opLabel = "monitor.registerResourceOutputs(...)"; // The registration could very well still be taking place, so we will need to wait for its URN. // Additionally, the output properties might have come from other resources, so we must await those too. diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment_RootResource.cs b/sdk/dotnet/Pulumi/Deployment/Deployment_RootResource.cs index 9213459d7..f9d900914 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment_RootResource.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment_RootResource.cs @@ -9,7 +9,7 @@ namespace Pulumi public partial class Deployment { private Task? _rootResource; - private object _rootResourceLock = new object(); + private readonly object _rootResourceLock = new object(); /// /// Returns a root resource URN that will automatically become the default parent of all diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment_Run.cs b/sdk/dotnet/Pulumi/Deployment/Deployment_Run.cs index 2e26f319d..17034c51b 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment_Run.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment_Run.cs @@ -12,7 +12,7 @@ namespace Pulumi public partial class Deployment { /// - /// for more details. + /// for more details. /// /// Callback that creates stack resources. public static Task RunAsync(Action action) @@ -174,7 +174,7 @@ namespace Pulumi return engine.Errors.Count switch { 1 => throw new RunException(engine.Errors.Single()), - int v when v > 1 => throw new AggregateException(engine.Errors.Select(e => new RunException(e))), + var v when v > 1 => throw new AggregateException(engine.Errors.Select(e => new RunException(e))), _ => monitor.Resources.ToImmutableArray() }; } diff --git a/sdk/dotnet/Pulumi/Deployment/Deployment_Serialization.cs b/sdk/dotnet/Pulumi/Deployment/Deployment_Serialization.cs index 4c01eac6d..f657cc982 100644 --- a/sdk/dotnet/Pulumi/Deployment/Deployment_Serialization.cs +++ b/sdk/dotnet/Pulumi/Deployment/Deployment_Serialization.cs @@ -1,7 +1,6 @@ // Copyright 2016-2019, Pulumi Corporation using System; -using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Threading.Tasks; @@ -65,7 +64,7 @@ namespace Pulumi propertyToDependentResources.ToImmutable()); } - private struct SerializationResult + private readonly struct SerializationResult { public readonly Struct Serialized; public readonly ImmutableDictionary> PropertyToDependentResources; diff --git a/sdk/dotnet/Pulumi/Deployment/GrpcEngine.cs b/sdk/dotnet/Pulumi/Deployment/GrpcEngine.cs index 061654d7b..330015520 100644 --- a/sdk/dotnet/Pulumi/Deployment/GrpcEngine.cs +++ b/sdk/dotnet/Pulumi/Deployment/GrpcEngine.cs @@ -1,7 +1,7 @@ // Copyright 2016-2020, Pulumi Corporation -using System.Threading.Tasks; using System.Collections.Generic; +using System.Threading.Tasks; using Grpc.Core; using Pulumirpc; diff --git a/sdk/dotnet/Pulumi/Deployment/GrpcMonitor.cs b/sdk/dotnet/Pulumi/Deployment/GrpcMonitor.cs index 69060821e..6d5d3adc2 100644 --- a/sdk/dotnet/Pulumi/Deployment/GrpcMonitor.cs +++ b/sdk/dotnet/Pulumi/Deployment/GrpcMonitor.cs @@ -1,7 +1,7 @@ // Copyright 2016-2020, Pulumi Corporation -using System.Threading.Tasks; using System.Collections.Generic; +using System.Threading.Tasks; using Grpc.Core; using Pulumirpc; diff --git a/sdk/dotnet/Pulumi/Deployment/InvokeOptions.cs b/sdk/dotnet/Pulumi/Deployment/InvokeOptions.cs index f005ee176..2797b17f7 100644 --- a/sdk/dotnet/Pulumi/Deployment/InvokeOptions.cs +++ b/sdk/dotnet/Pulumi/Deployment/InvokeOptions.cs @@ -3,8 +3,7 @@ namespace Pulumi { /// - /// Options to help control the behavior of . + /// Options to help control the behavior of . /// public class InvokeOptions { diff --git a/sdk/dotnet/Pulumi/Exceptions/ResourceException.cs b/sdk/dotnet/Pulumi/Exceptions/ResourceException.cs index c6b98dd56..f38137dd2 100644 --- a/sdk/dotnet/Pulumi/Exceptions/ResourceException.cs +++ b/sdk/dotnet/Pulumi/Exceptions/ResourceException.cs @@ -12,8 +12,8 @@ namespace Pulumi /// public class ResourceException : Exception { - internal readonly Resource? Resource; - internal readonly bool HideStack; + internal Resource? Resource { get; } + internal bool HideStack { get; } public ResourceException(string message, Resource? resource, bool hideStack = false) : base(message) { diff --git a/sdk/dotnet/Pulumi/Extensions.cs b/sdk/dotnet/Pulumi/Extensions.cs index 3da6820bc..4fc485f9a 100644 --- a/sdk/dotnet/Pulumi/Extensions.cs +++ b/sdk/dotnet/Pulumi/Extensions.cs @@ -21,7 +21,7 @@ namespace Pulumi return result; } - public static void Deconstruct(this KeyValuePair pair, out K key, out V value) + public static void Deconstruct(this KeyValuePair pair, out TKey key, out TValue value) { key = pair.Key; value = pair.Value; @@ -43,6 +43,7 @@ namespace Pulumi { _ = response.ContinueWith(t => { + // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault switch (t.Status) { default: throw new InvalidOperationException("Task was not complete: " + t.Status); diff --git a/sdk/dotnet/Pulumi/Resources/ComponentResourceOptions.cs b/sdk/dotnet/Pulumi/Resources/ComponentResourceOptions.cs index 044bebbeb..96d9a7ada 100644 --- a/sdk/dotnet/Pulumi/Resources/ComponentResourceOptions.cs +++ b/sdk/dotnet/Pulumi/Resources/ComponentResourceOptions.cs @@ -1,6 +1,5 @@ // Copyright 2016-2019, Pulumi Corporation -using System; using System.Collections.Generic; namespace Pulumi @@ -20,7 +19,7 @@ namespace Pulumi /// public List Providers { - get => _providers ?? (_providers = new List()); + get => _providers ??= new List(); set => _providers = value; } diff --git a/sdk/dotnet/Pulumi/Resources/CustomResourceOptions.cs b/sdk/dotnet/Pulumi/Resources/CustomResourceOptions.cs index f020fa7da..936ae62e8 100644 --- a/sdk/dotnet/Pulumi/Resources/CustomResourceOptions.cs +++ b/sdk/dotnet/Pulumi/Resources/CustomResourceOptions.cs @@ -26,7 +26,7 @@ namespace Pulumi /// public List AdditionalSecretOutputs { - get => _additionalSecretOutputs ?? (_additionalSecretOutputs = new List()); + get => _additionalSecretOutputs ??= new List(); set => _additionalSecretOutputs = value; } diff --git a/sdk/dotnet/Pulumi/Resources/DependencyProviderResource.cs b/sdk/dotnet/Pulumi/Resources/DependencyProviderResource.cs index 245514b86..820b3bbfc 100644 --- a/sdk/dotnet/Pulumi/Resources/DependencyProviderResource.cs +++ b/sdk/dotnet/Pulumi/Resources/DependencyProviderResource.cs @@ -16,13 +16,13 @@ namespace Pulumi public DependencyProviderResource(string reference) : base(package: "", name: "", args: ResourceArgs.Empty, dependency: true) { - int lastSep = reference.LastIndexOf("::", StringComparison.Ordinal); + var lastSep = reference.LastIndexOf("::", StringComparison.Ordinal); if (lastSep == -1) { throw new ArgumentException($"Expected \"::\" in provider reference ${reference}."); } - string urn = reference.Substring(0, lastSep); - string id = reference.Substring(lastSep + 2); + var urn = reference.Substring(0, lastSep); + var id = reference.Substring(lastSep + 2); var resources = ImmutableHashSet.Create(this); diff --git a/sdk/dotnet/Pulumi/Resources/InputArgs.cs b/sdk/dotnet/Pulumi/Resources/InputArgs.cs index 01761525c..ff6895895 100644 --- a/sdk/dotnet/Pulumi/Resources/InputArgs.cs +++ b/sdk/dotnet/Pulumi/Resources/InputArgs.cs @@ -35,14 +35,15 @@ namespace Pulumi var all = fieldQuery.Concat(propQuery).ToList(); - foreach (var (attr, memberName, memberType, getValue) in all) + foreach (var (_, memberName, memberType, _) in all) { var fullName = $"[Input] {this.GetType().FullName}.{memberName}"; + // ReSharper disable once VirtualMemberCallInConstructor ValidateMember(memberType, fullName); } _inputInfos = all.Select(t => - new InputInfo(t.attr, t.memberName, t.memberType, t.getValue)).ToImmutableArray(); + new InputInfo(t.attr!, t.memberName, t.memberType, t.getValue)).ToImmutableArray(); } internal virtual async Task> ToDictionaryAsync() @@ -80,14 +81,15 @@ namespace Pulumi return JsonFormatter.Default.Format(value); } - private struct InputInfo + private readonly struct InputInfo { public readonly InputAttribute Attribute; + // ReSharper disable once NotAccessedField.Local public readonly Type MemberType; public readonly string MemberName; - public Func GetValue; + public readonly Func GetValue; - public InputInfo(InputAttribute attribute, string memberName, Type memberType, Func getValue) : this() + public InputInfo(InputAttribute attribute, string memberName, Type memberType, Func getValue) : this() { Attribute = attribute; MemberName = memberName; diff --git a/sdk/dotnet/Pulumi/Resources/InvokeArgs.cs b/sdk/dotnet/Pulumi/Resources/InvokeArgs.cs index 7b9d5ce71..1b352ef57 100644 --- a/sdk/dotnet/Pulumi/Resources/InvokeArgs.cs +++ b/sdk/dotnet/Pulumi/Resources/InvokeArgs.cs @@ -11,10 +11,6 @@ namespace Pulumi { public static readonly InvokeArgs Empty = new EmptyInvokeArgs(); - protected InvokeArgs() - { - } - private protected override void ValidateMember(Type memberType, string fullName) { if (typeof(IInput).IsAssignableFrom(memberType)) diff --git a/sdk/dotnet/Pulumi/Resources/ProviderResource.cs b/sdk/dotnet/Pulumi/Resources/ProviderResource.cs index 66bf76c14..e037c9342 100644 --- a/sdk/dotnet/Pulumi/Resources/ProviderResource.cs +++ b/sdk/dotnet/Pulumi/Resources/ProviderResource.cs @@ -12,7 +12,7 @@ namespace Pulumi /// public class ProviderResource : CustomResource { - internal readonly string Package; + internal string Package { get; } private string? _registrationId; @@ -53,14 +53,14 @@ namespace Pulumi if (provider._registrationId == null) { - var providerURN = await provider.Urn.GetValueAsync().ConfigureAwait(false); - var providerID = await provider.Id.GetValueAsync().ConfigureAwait(false); - if (string.IsNullOrEmpty(providerID)) + var providerUrn = await provider.Urn.GetValueAsync().ConfigureAwait(false); + var providerId = await provider.Id.GetValueAsync().ConfigureAwait(false); + if (string.IsNullOrEmpty(providerId)) { - providerID = Constants.UnknownValue; + providerId = Constants.UnknownValue; } - provider._registrationId = $"{providerURN}::{providerID}"; + provider._registrationId = $"{providerUrn}::{providerId}"; } return provider._registrationId; diff --git a/sdk/dotnet/Pulumi/Resources/Resource.cs b/sdk/dotnet/Pulumi/Resources/Resource.cs index fd170cf09..0adb223d2 100644 --- a/sdk/dotnet/Pulumi/Resources/Resource.cs +++ b/sdk/dotnet/Pulumi/Resources/Resource.cs @@ -16,11 +16,6 @@ namespace Pulumi private readonly string _type; private readonly string _name; - /// - /// The optional parent of this resource. - /// - private readonly Resource? _parentResource; - /// /// The child resources of this resource. We use these (only from a ComponentResource) to /// allow code to dependOn a ComponentResource and have that effectively mean that it is @@ -56,7 +51,7 @@ namespace Pulumi /// ever need to reference the urn of a component resource. So it's acceptable if that sort /// of pattern failed in practice. /// - internal readonly HashSet ChildResources = new HashSet(); + internal HashSet ChildResources { get; } = new HashSet(); /// /// Urn is the stable logical URN used to distinctly address a resource, both before and @@ -187,14 +182,13 @@ namespace Pulumi if (options.Parent != null) { - this._parentResource = options.Parent; - lock (this._parentResource.ChildResources) + var parentResource = options.Parent; + lock (parentResource.ChildResources) { - this._parentResource.ChildResources.Add(this); + parentResource.ChildResources.Add(this); } - if (options.Protect == null) - options.Protect = options.Parent._protect; + options.Protect ??= options.Parent._protect; // Make a copy of the aliases array, and add to it any implicit aliases inherited from its parent options.Aliases = options.Aliases.ToList(); @@ -331,7 +325,6 @@ $"Only specify one of '{nameof(Alias.Parent)}', '{nameof(Alias.ParentUrn)}' or ' if (value != null) { ThrowAliasPropertyConflict(name); - return; } } diff --git a/sdk/dotnet/Pulumi/Resources/ResourceArgs.cs b/sdk/dotnet/Pulumi/Resources/ResourceArgs.cs index 00bfe4f5b..0129f6cda 100644 --- a/sdk/dotnet/Pulumi/Resources/ResourceArgs.cs +++ b/sdk/dotnet/Pulumi/Resources/ResourceArgs.cs @@ -11,10 +11,6 @@ namespace Pulumi { public static readonly ResourceArgs Empty = new EmptyResourceArgs(); - protected ResourceArgs() - { - } - private protected override void ValidateMember(Type memberType, string fullName) { // No validation. A member may or may not be IInput. diff --git a/sdk/dotnet/Pulumi/Resources/ResourceOptions.cs b/sdk/dotnet/Pulumi/Resources/ResourceOptions.cs index 34b887ebc..c88e077df 100644 --- a/sdk/dotnet/Pulumi/Resources/ResourceOptions.cs +++ b/sdk/dotnet/Pulumi/Resources/ResourceOptions.cs @@ -26,7 +26,7 @@ namespace Pulumi /// public InputList DependsOn { - get => _dependsOn ?? (_dependsOn = new InputList()); + get => _dependsOn ??= new InputList(); set => _dependsOn = value; } @@ -42,7 +42,7 @@ namespace Pulumi /// public List IgnoreChanges { - get => _ignoreChanges ?? (_ignoreChanges = new List()); + get => _ignoreChanges ??= new List(); set => _ignoreChanges = value; } @@ -78,7 +78,7 @@ namespace Pulumi /// public List ResourceTransformations { - get => _resourceTransformations ?? (_resourceTransformations = new List()); + get => _resourceTransformations ??= new List(); set => _resourceTransformations = value; } diff --git a/sdk/dotnet/Pulumi/Resources/StackReference.cs b/sdk/dotnet/Pulumi/Resources/StackReference.cs index fa096b238..6f925403c 100644 --- a/sdk/dotnet/Pulumi/Resources/StackReference.cs +++ b/sdk/dotnet/Pulumi/Resources/StackReference.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Threading.Tasks; -using Pulumi.Serialization; namespace Pulumi { @@ -152,6 +151,6 @@ namespace Pulumi /// The name of the stack to reference. /// [Input("name", required: true)] - public Input? Name { get; set; } = null!; + public Input? Name { get; set; } } } diff --git a/sdk/dotnet/Pulumi/Serialization/Converter.cs b/sdk/dotnet/Pulumi/Serialization/Converter.cs index cff68690e..edeb21eee 100644 --- a/sdk/dotnet/Pulumi/Serialization/Converter.cs +++ b/sdk/dotnet/Pulumi/Serialization/Converter.cs @@ -8,6 +8,10 @@ using System.Linq; using System.Reflection; using System.Text.Json; using Google.Protobuf.WellKnownTypes; +using Enum = System.Enum; +using Type = System.Type; + +// ReSharper disable TailRecursiveCall namespace Pulumi.Serialization { @@ -19,15 +23,15 @@ namespace Pulumi.Serialization return new OutputData(ImmutableHashSet.Empty, (T)data!, isKnown, isSecret); } - public static OutputData ConvertValue(string context, Value value, System.Type targetType) + public static OutputData ConvertValue(string context, Value value, Type targetType) { return ConvertValue(context, value, targetType, ImmutableHashSet.Empty); } public static OutputData ConvertValue( - string context, Value value, System.Type targetType, ImmutableHashSet resources) + string context, Value value, Type targetType, ImmutableHashSet resources) { - CheckTargetType(context, targetType, new HashSet()); + CheckTargetType(context, targetType, new HashSet()); var (deserialized, isKnown, isSecret) = Deserializer.Deserialize(value); var converted = ConvertObject(context, deserialized, targetType); @@ -35,7 +39,7 @@ namespace Pulumi.Serialization return new OutputData(resources, converted, isKnown, isSecret); } - private static object? ConvertObject(string context, object? val, System.Type targetType) + private static object? ConvertObject(string context, object? val, Type targetType) { var (result, exception) = TryConvertObject(context, val, targetType); if (exception != null) @@ -44,7 +48,7 @@ namespace Pulumi.Serialization return result; } - private static (object?, InvalidOperationException?) TryConvertObject(string context, object? val, System.Type targetType) + private static (object?, InvalidOperationException?) TryConvertObject(string context, object? val, Type targetType) { var targetIsNullable = targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Nullable<>); @@ -116,7 +120,7 @@ namespace Pulumi.Serialization if (exception != null || value is null) return (null, exception); - return (System.Enum.ToObject(targetType, value), null); + return (Enum.ToObject(targetType, value), null); } if (targetType.IsValueType && targetType.GetCustomAttribute() != null) @@ -136,7 +140,7 @@ namespace Pulumi.Serialization return (null, new InvalidOperationException( $"Expected target type {targetType.FullName} to have a constructor with a single {valType.FullName} parameter.")); } - return (enumTypeConstructor.Invoke(new object[] { val }), null); + return (enumTypeConstructor.Invoke(new[] { val }), null); } if (targetType.IsConstructedGenericType) @@ -154,14 +158,14 @@ namespace Pulumi.Serialization $"Unexpected generic target type {targetType.FullName} when deserializing {context}"); } - if (targetType.GetCustomAttribute() == null) + if (targetType.GetCustomAttribute() == null) return (null, new InvalidOperationException( $"Unexpected target type {targetType.FullName} when deserializing {context}")); var constructor = GetPropertyConstructor(targetType); if (constructor == null) return (null, new InvalidOperationException( - $"Expected target type {targetType.FullName} to have [{nameof(Pulumi.OutputConstructorAttribute)}] constructor when deserializing {context}")); + $"Expected target type {targetType.FullName} to have [{nameof(OutputConstructorAttribute)}] constructor when deserializing {context}")); var (dictionary, tempException) = TryEnsureType>(context, val); if (tempException != null) @@ -191,20 +195,18 @@ namespace Pulumi.Serialization private static (object?, InvalidOperationException?) TryConvertJsonElement( string context, object val) { - using (var stream = new MemoryStream()) + using var stream = new MemoryStream(); + using (var writer = new Utf8JsonWriter(stream)) { - using (var writer = new Utf8JsonWriter(stream)) - { - var exception = TryWriteJson(context, writer, val); - if (exception != null) - return (null, exception); - } - - stream.Position = 0; - var document = JsonDocument.Parse(stream); - var element = document.RootElement; - return (element, null); + var exception = TryWriteJson(context, writer, val); + if (exception != null) + return (null, exception); } + + stream.Position = 0; + var document = JsonDocument.Parse(stream); + var element = document.RootElement; + return (element, null); } private static InvalidOperationException? TryWriteJson(string context, Utf8JsonWriter writer, object? val) @@ -252,7 +254,7 @@ namespace Pulumi.Serialization private static (T, InvalidOperationException?) TryEnsureType(string context, object val) => val is T t ? (t, null) : (default(T)!, new InvalidOperationException($"Expected {typeof(T).FullName} but got {val.GetType().FullName} deserializing {context}")); - private static (object?, InvalidOperationException?) TryConvertOneOf(string context, object val, System.Type oneOfType) + private static (object?, InvalidOperationException?) TryConvertOneOf(string context, object val, Type oneOfType) { var firstType = oneOfType.GenericTypeArguments[0]; var secondType = oneOfType.GenericTypeArguments[1]; @@ -275,14 +277,14 @@ namespace Pulumi.Serialization } private static (object?, InvalidOperationException?) TryConvertArray( - string fieldName, object val, System.Type targetType) + string fieldName, object val, Type targetType) { if (!(val is ImmutableArray array)) return (null, new InvalidOperationException( $"Expected {typeof(ImmutableArray).FullName} but got {val.GetType().FullName} deserializing {fieldName}")); var builder = - typeof(ImmutableArray).GetMethod(nameof(ImmutableArray.CreateBuilder), Array.Empty())! + typeof(ImmutableArray).GetMethod(nameof(ImmutableArray.CreateBuilder), Array.Empty())! .MakeGenericMethod(targetType.GenericTypeArguments) .Invoke(obj: null, parameters: null)!; @@ -303,7 +305,7 @@ namespace Pulumi.Serialization } private static (object?, InvalidOperationException?) TryConvertDictionary( - string fieldName, object val, System.Type targetType) + string fieldName, object val, Type targetType) { if (!(val is ImmutableDictionary dictionary)) return (null, new InvalidOperationException( @@ -319,7 +321,7 @@ namespace Pulumi.Serialization $"Unexpected type {targetType.FullName} when deserializing {fieldName}. ImmutableDictionary's TKey type was not {typeof(string).FullName}")); var builder = - typeof(ImmutableDictionary).GetMethod(nameof(ImmutableDictionary.CreateBuilder), Array.Empty())! + typeof(ImmutableDictionary).GetMethod(nameof(ImmutableDictionary.CreateBuilder), Array.Empty())! .MakeGenericMethod(targetType.GenericTypeArguments) .Invoke(obj: null, parameters: null)!; @@ -341,7 +343,7 @@ namespace Pulumi.Serialization return (builderToImmutable.Invoke(builder, null), null); } - public static void CheckTargetType(string context, System.Type targetType, HashSet seenTypes) + public static void CheckTargetType(string context, Type targetType, HashSet seenTypes) { // types can be recursive. So only dive into a type if it's the first time we're seeing it. if (!seenTypes.Add(targetType)) @@ -398,7 +400,7 @@ namespace Pulumi.Serialization throw new InvalidOperationException( $"{targetType.FullName} had [{nameof(EnumTypeAttribute)}], but did not contain constructor with a single String or Double parameter."); - static bool CheckEnumType(System.Type targetType, System.Type underlyingType) + static bool CheckEnumType(Type targetType, Type underlyingType) { var constructor = targetType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { underlyingType }, null); if (constructor == null) @@ -424,39 +426,34 @@ namespace Pulumi.Serialization CheckTargetType(context, targetType.GenericTypeArguments.Single(), seenTypes); return; } - else if (targetType.GetGenericTypeDefinition() == typeof(Union<,>)) + if (targetType.GetGenericTypeDefinition() == typeof(Union<,>)) { CheckTargetType(context, targetType.GenericTypeArguments[0], seenTypes); CheckTargetType(context, targetType.GenericTypeArguments[1], seenTypes); return; } - else if (targetType.GetGenericTypeDefinition() == typeof(ImmutableArray<>)) + if (targetType.GetGenericTypeDefinition() == typeof(ImmutableArray<>)) { CheckTargetType(context, targetType.GenericTypeArguments.Single(), seenTypes); return; } - else if (targetType.GetGenericTypeDefinition() == typeof(ImmutableDictionary<,>)) + if (targetType.GetGenericTypeDefinition() == typeof(ImmutableDictionary<,>)) { var dictTypeArgs = targetType.GenericTypeArguments; if (dictTypeArgs[0] != typeof(string)) { - throw new InvalidOperationException( -$@"{context} contains invalid type {targetType.FullName}: + throw new InvalidOperationException($@"{context} contains invalid type {targetType.FullName}: The only allowed ImmutableDictionary 'TKey' type is 'String'."); } CheckTargetType(context, dictTypeArgs[1], seenTypes); return; } - else - { - throw new InvalidOperationException( -$@"{context} contains invalid type {targetType.FullName}: + throw new InvalidOperationException($@"{context} contains invalid type {targetType.FullName}: The only generic types allowed are ImmutableArray<...> and ImmutableDictionary"); - } } - var propertyTypeAttribute = (Attribute?)targetType.GetCustomAttribute(); + var propertyTypeAttribute = targetType.GetCustomAttribute(); if (propertyTypeAttribute == null) { throw new InvalidOperationException( @@ -479,8 +476,8 @@ $@"{targetType.FullName} had [{nameof(OutputTypeAttribute)}], but did not contai } } - private static ConstructorInfo GetPropertyConstructor(System.Type outputTypeArg) + private static ConstructorInfo? GetPropertyConstructor(Type outputTypeArg) => outputTypeArg.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).FirstOrDefault( - c => c.GetCustomAttributes() != null); + c => c.GetCustomAttribute() != null); } } diff --git a/sdk/dotnet/Pulumi/Serialization/Deserializer.cs b/sdk/dotnet/Pulumi/Serialization/Deserializer.cs index 1098c0b15..607deb62c 100644 --- a/sdk/dotnet/Pulumi/Serialization/Deserializer.cs +++ b/sdk/dotnet/Pulumi/Serialization/Deserializer.cs @@ -26,7 +26,7 @@ namespace Pulumi.Serialization { return new OutputData(ImmutableHashSet.Empty, (T)(object)assetOrArchive, isKnown: true, isSecret); } - else if (TryDeserializeResource(value, out var resource)) + if (TryDeserializeResource(value, out var resource)) { return new OutputData(ImmutableHashSet.Empty, (T)(object)resource, isKnown: true, isSecret); } @@ -155,7 +155,7 @@ namespace Pulumi.Serialization assetOrArchive = DeserializeAsset(value); return true; } - else if (sig == Constants.SpecialArchiveSig) + if (sig == Constants.SpecialArchiveSig) { assetOrArchive = DeserializeArchive(value); return true; @@ -217,14 +217,12 @@ namespace Pulumi.Serialization return false; } - string? urn; - if (!TryGetStringValue(value.StructValue.Fields, Constants.ResourceUrnName, out urn)) + if (!TryGetStringValue(value.StructValue.Fields, Constants.ResourceUrnName, out var urn)) { throw new InvalidOperationException("Value was marked as a Resource, but did not conform to required shape."); } - string? version; - if (!TryGetStringValue(value.StructValue.Fields, Constants.ResourceVersionName, out version)) { + if (!TryGetStringValue(value.StructValue.Fields, Constants.ResourceVersionName, out var version)) { version = ""; } diff --git a/sdk/dotnet/Pulumi/Serialization/OutputCompletionSource.cs b/sdk/dotnet/Pulumi/Serialization/OutputCompletionSource.cs index f00113b04..2d091ec76 100644 --- a/sdk/dotnet/Pulumi/Serialization/OutputCompletionSource.cs +++ b/sdk/dotnet/Pulumi/Serialization/OutputCompletionSource.cs @@ -35,7 +35,7 @@ namespace Pulumi.Serialization Output = new Output(_taskCompletionSource.Task); } - public System.Type TargetType => typeof(T); + public Type TargetType => typeof(T); IOutput IOutputCompletionSource.Output => Output; @@ -59,9 +59,6 @@ namespace Pulumi.Serialization { public static ImmutableDictionary InitializeOutputs(Resource resource) { - var name = resource.GetResourceName(); - var type = resource.GetResourceType(); - var query = from property in resource.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance) let attr = property.GetCustomAttribute() where attr != null @@ -89,9 +86,9 @@ namespace Pulumi.Serialization var ocsType = typeof(OutputCompletionSource<>).MakeGenericType(outputTypeArg); var ocsContructor = ocsType.GetConstructors().Single(); - var completionSource = (IOutputCompletionSource)ocsContructor.Invoke(new[] { resource }); + var completionSource = (IOutputCompletionSource)ocsContructor.Invoke(new object?[] { resource }); - setMethod.Invoke(resource, new[] { completionSource.Output }); + setMethod.Invoke(resource, new object?[] { completionSource.Output }); var outputName = attrName ?? prop.Name; result.Add(outputName, completionSource); diff --git a/sdk/dotnet/Pulumi/Serialization/OutputData.cs b/sdk/dotnet/Pulumi/Serialization/OutputData.cs index f2efeb3fc..b2d332070 100644 --- a/sdk/dotnet/Pulumi/Serialization/OutputData.cs +++ b/sdk/dotnet/Pulumi/Serialization/OutputData.cs @@ -13,7 +13,7 @@ namespace Pulumi.Serialization => (isKnown && data.IsKnown, isSecret || data.IsSecret); } - internal struct OutputData + internal readonly struct OutputData { public readonly ImmutableHashSet Resources; public readonly X Value; diff --git a/sdk/dotnet/Pulumi/Serialization/ResourcePackages.cs b/sdk/dotnet/Pulumi/Serialization/ResourcePackages.cs index b711131ce..e4c27cc42 100644 --- a/sdk/dotnet/Pulumi/Serialization/ResourcePackages.cs +++ b/sdk/dotnet/Pulumi/Serialization/ResourcePackages.cs @@ -12,9 +12,8 @@ namespace Pulumi { internal static class ResourcePackages { - private static Lazy>> ResourceTypes - = new Lazy>>( - () => DiscoverResourceTypes()); + private static readonly Lazy>> _resourceTypes + = new Lazy>>(DiscoverResourceTypes); internal static bool TryConstruct(string type, string version, string urn, [NotNullWhen(true)] out Resource? resource) { @@ -29,7 +28,7 @@ namespace Pulumi var constructorInfo = resourceType.GetConstructors().Single(c => c.GetParameters().Length == 3); var resourceOptions = typeof(CustomResource).IsAssignableFrom(resourceType) ? - (ResourceOptions)new CustomResourceOptions { Urn = urn } : + new CustomResourceOptions { Urn = urn } : (ResourceOptions)new ComponentResourceOptions { Urn = urn }; resource = (Resource)constructorInfo.Invoke(new[] { urnName, (object?)null, resourceOptions }); @@ -39,7 +38,7 @@ namespace Pulumi internal static bool TryGetResourceType(string name, string? version, [NotNullWhen(true)] out Type? type) { var minimalVersion = !string.IsNullOrEmpty(version) ? SemVersion.Parse(version) : new SemVersion(0); - var yes = ResourceTypes.Value.TryGetValue(name, out var types); + var yes = _resourceTypes.Value.TryGetValue(name, out var types); if (!yes) { type = null; @@ -93,7 +92,7 @@ namespace Pulumi while (assembliesToCheck.Any()) { - Assembly assemblyToCheck = assembliesToCheck.Dequeue(); + var assemblyToCheck = assembliesToCheck.Dequeue(); if (yieldedAssemblies.Contains(assemblyToCheck.FullName!)) continue; diff --git a/sdk/dotnet/Pulumi/Serialization/Serializer.cs b/sdk/dotnet/Pulumi/Serialization/Serializer.cs index 261bcd4e5..d1568a65a 100644 --- a/sdk/dotnet/Pulumi/Serialization/Serializer.cs +++ b/sdk/dotnet/Pulumi/Serialization/Serializer.cs @@ -9,10 +9,11 @@ using System.Reflection; using System.Text.Json; using System.Threading.Tasks; using Google.Protobuf.WellKnownTypes; +using Enum = System.Enum; namespace Pulumi.Serialization { - internal struct Serializer + internal readonly struct Serializer { public readonly HashSet DependentResources; @@ -222,7 +223,7 @@ $"Tasks are not allowed inside ResourceArgs. Please wrap your Task in an Output: if (prop is IList list) return await SerializeListAsync(ctx, list, keepResources).ConfigureAwait(false); - if (prop is System.Enum e && e.GetTypeCode() == TypeCode.Int32) + if (prop is Enum e && e.GetTypeCode() == TypeCode.Int32) { return (int)prop; } @@ -230,12 +231,12 @@ $"Tasks are not allowed inside ResourceArgs. Please wrap your Task in an Output: var propType = prop.GetType(); if (propType.IsValueType && propType.GetCustomAttribute() != null) { - MethodInfo? mi = propType.GetMethod("op_Explicit", BindingFlags.Public | BindingFlags.Static, null, new[] { propType }, null); + var mi = propType.GetMethod("op_Explicit", BindingFlags.Public | BindingFlags.Static, null, new[] { propType }, null); if (mi == null || (mi.ReturnType != typeof(string) && mi.ReturnType != typeof(double))) { throw new InvalidOperationException($"Expected {propType.FullName} to have an explicit conversion operator to String or Double.\n\t{ctx}"); } - return mi.Invoke(null, new object[] { prop }); + return mi.Invoke(null, new[] { prop }); } throw new InvalidOperationException($"{propType.FullName} is not a supported argument type.\n\t{ctx}"); @@ -374,7 +375,7 @@ $"Tasks are not allowed inside ResourceArgs. Please wrap your Task in an Output: double d => Value.ForNumber(d), bool b => Value.ForBool(b), string s => Value.ForString(s), - ImmutableArray list => Value.ForList(list.Select(v => CreateValue(v)).ToArray()), + ImmutableArray list => Value.ForList(list.Select(CreateValue).ToArray()), ImmutableDictionary dict => Value.ForStruct(CreateStruct(dict)), _ => throw new InvalidOperationException("Unsupported value when converting to protobuf: " + value.GetType().FullName), }; diff --git a/sdk/dotnet/Pulumi/Stack.cs b/sdk/dotnet/Pulumi/Stack.cs index 133977051..cad0022aa 100644 --- a/sdk/dotnet/Pulumi/Stack.cs +++ b/sdk/dotnet/Pulumi/Stack.cs @@ -6,7 +6,6 @@ using System.Collections.Immutable; using System.Linq; using System.Reflection; using System.Threading.Tasks; -using Pulumi.Serialization; namespace Pulumi { @@ -31,7 +30,7 @@ namespace Pulumi /// may look a bit confusing and may incorrectly look like something that could be removed /// without changing semantics. /// - internal static readonly Resource? Root = null; + internal static Resource? Root { get; } = null; /// /// is the type name that should be used to construct @@ -44,7 +43,7 @@ namespace Pulumi /// /// The outputs of this stack, if the init callback exited normally. /// - internal Output> Outputs = + internal Output> Outputs { get; private set; } = Output.Create>(ImmutableDictionary.Empty); /// @@ -113,12 +112,10 @@ namespace Pulumi this.RegisterOutputs(this.Outputs); } - private async Task> RunInitAsync(Func>> init) + private static async Task> RunInitAsync(Func>> init) { var dictionary = await init().ConfigureAwait(false); - return dictionary == null - ? ImmutableDictionary.Empty - : dictionary.ToImmutableDictionary(); + return dictionary.ToImmutableDictionary(); } private static ComponentResourceOptions? ConvertOptions(StackOptions? options) diff --git a/sdk/dotnet/Pulumi/Testing/IMocks.cs b/sdk/dotnet/Pulumi/Testing/IMocks.cs index bf571cf94..78bc95f67 100644 --- a/sdk/dotnet/Pulumi/Testing/IMocks.cs +++ b/sdk/dotnet/Pulumi/Testing/IMocks.cs @@ -1,6 +1,5 @@ // Copyright 2016-2020, Pulumi Corporation -using System.Collections.Generic; using System.Collections.Immutable; using System.Threading.Tasks; diff --git a/sdk/dotnet/Pulumi/Testing/MockEngine.cs b/sdk/dotnet/Pulumi/Testing/MockEngine.cs index d3fe773e3..028d5e2c2 100644 --- a/sdk/dotnet/Pulumi/Testing/MockEngine.cs +++ b/sdk/dotnet/Pulumi/Testing/MockEngine.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Pulumi.Serialization; using Pulumirpc; namespace Pulumi.Testing diff --git a/sdk/dotnet/Pulumi/Testing/MockMonitor.cs b/sdk/dotnet/Pulumi/Testing/MockMonitor.cs index 819ea58c8..3de00e5cd 100644 --- a/sdk/dotnet/Pulumi/Testing/MockMonitor.cs +++ b/sdk/dotnet/Pulumi/Testing/MockMonitor.cs @@ -151,7 +151,7 @@ namespace Pulumi.Testing var parentType = qualifiedType.Split("$").First(); type = parentType + "$" + type; } - return "urn:pulumi:" + string.Join("::", new[] { Deployment.Instance.StackName, Deployment.Instance.ProjectName, type, name }); + return "urn:pulumi:" + string.Join("::", Deployment.Instance.StackName, Deployment.Instance.ProjectName, type, name); } private static ImmutableDictionary ToDictionary(Struct s) diff --git a/sdk/dotnet/dotnet.sln.DotSettings b/sdk/dotnet/dotnet.sln.DotSettings new file mode 100644 index 000000000..246ce1791 --- /dev/null +++ b/sdk/dotnet/dotnet.sln.DotSettings @@ -0,0 +1,28 @@ + + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + DO_NOT_SHOW + \ No newline at end of file