github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/testing/test/testdata/output-funcs/dotnet-extras/Tests/TestHelpers.cs (about)

     1  // Copyright 2016-2021, Pulumi Corporation
     2  
     3  using System;
     4  using System.Collections.Concurrent;
     5  using System.Collections.Generic;
     6  using System.Collections.Immutable;
     7  using System.Linq;
     8  using System.Threading.Tasks;
     9  
    10  using Pulumi;
    11  using Pulumi.Testing;
    12  using static Pulumi.Utilities.OutputUtilities;
    13  
    14  namespace Pulumi.Mypkg
    15  {
    16      public static class TestHelpers
    17      {
    18          public static async Task<(T Result, IEnumerable<string> Deps)> Run<T>(
    19              IMocks mocks, Func<Output<T>> outputGenerator, TestOptions? options = null)
    20          {
    21              options = options ?? new TestOptions();
    22              options.ProjectName = HelperStack.RegisterBuilderAsProjectName(outputGenerator);
    23              var resources = await Deployment.TestAsync<HelperStack>(mocks, options);
    24              var stack = resources.Where(x => x is HelperStack).First() as HelperStack;
    25              if (stack != null)
    26              {
    27                  var result = await GetValueAsync(stack.Result);
    28                  if (result is T)
    29                  {
    30                      var deps = await GetDependenciesAsync(stack.Result);
    31                      var urns = new List<string>();
    32                      foreach (var dep in deps)
    33                      {
    34                          var urn = await GetValueAsync(dep.Urn);
    35                          urns.Add(urn);
    36                      }
    37                      return (Result: (T)result, Deps: urns);
    38                  }
    39                  else
    40                  {
    41                      throw new Exception($"The output did not resolve to the correct type: {result}");
    42                  }
    43              }
    44              else
    45              {
    46                  throw new Exception("Did not find stack");
    47              }
    48          }
    49  
    50          public static Output<T> Out<T>(T x)
    51          {
    52              return Output.Create<T>(x);
    53          }
    54  
    55  
    56          public class HelperStack : Stack
    57          {
    58              private static ConcurrentDictionary<string,Func<Output<object?>>> registry =
    59                  new ConcurrentDictionary<string,Func<Output<object?>>>();
    60  
    61              [Output]
    62              public Output<object?> Result { get; private set; }
    63  
    64              public HelperStack()
    65              {
    66                  Console.WriteLine(Deployment.Instance.ProjectName);
    67                  Func<Output<object?>>? outputBuilder;
    68                  if (!registry.TryGetValue(Deployment.Instance.ProjectName, out outputBuilder))
    69                  {
    70                      throw new Exception("Incorrect use of HelperStack");
    71                  }
    72                  this.Result = outputBuilder.Invoke();
    73              }
    74  
    75              public static string RegisterBuilderAsProjectName<T>(Func<Output<T>> builder)
    76              {
    77                  var projectName = Guid.NewGuid().ToString();
    78                  if (!registry.TryAdd(projectName, () => builder().Apply(x => (object?)x)))
    79                  {
    80                      throw new Exception("Impossible");
    81                  }
    82                  return projectName;
    83              }
    84          }
    85      }
    86  }