Beispiel #1
0
        protected override Task AssertOnAllResponses(Action <TResponse> assert)
        {
            if (!this.ExpectIsValid)
            {
                return(base.AssertOnAllResponses(assert));
            }

            return(base.AssertOnAllResponses((r) =>
            {
                if (TestClient.Configuration.RunIntegrationTests && !r.IsValid && r.ApiCall.OriginalException != null &&
                    !(r.ApiCall.OriginalException is ElasticsearchClientException))
                {
                    ExceptionDispatchInfo.Capture(r.ApiCall.OriginalException.Demystify()).Throw();
                    return;
                }

                using (var scope = new AssertionScope())
                {
                    assert(r);
                    var failures = scope.Discard();
                    if (failures.Length <= 0)
                    {
                        return;
                    }

                    var failure = failures[0];
                    scope.AddReportable("Failure", failure);
                    scope.AddReportable("DebugInformation", r.DebugInformation);
                    scope.FailWith($@"{{Failure}}
Response Under Test:
{{DebugInformation}}");
                }
            }));
        }
Beispiel #2
0
        private static void AreEqualImpl(
            ScreenshotInfo expected,
            Rectangle expectedRect,
            ScreenshotInfo actual,
            Bitmap actualBitmap,
            Rectangle actualRect,
            double expectedToActualScale,
            PixelTolerance tolerance,
            [CallerLineNumber] int line = 0)
        {
            using var assertionScope = new AssertionScope($"{expected.StepName}<=={actual}");
            assertionScope.AddReportable("expectedRect", expectedRect.ToString());
            assertionScope.AddReportable("actualRect", actualRect.ToString());
            assertionScope.AddReportable("expectedToActualScale", expectedToActualScale.ToString(NumberFormatInfo.InvariantInfo));

            var(areEqual, context) = EqualityCheck(expected, expectedRect, actual, actualBitmap, actualRect, expectedToActualScale, tolerance, line);

            if (areEqual)
            {
                Console.WriteLine(context.ToString());
            }
            else
            {
                assertionScope.FailWithText(context);
            }
        }
        private static AssertionScope StartAssertionScope(ProcessHelper processHelper)
        {
            var scope = new AssertionScope();

            scope.AddReportable("Standard output", processHelper.StandardOutput);
            scope.AddReportable("Error output", processHelper.ErrorOutput);
            scope.AddReportable("Exit code", processHelper.Process.ExitCode.ToString());

            return(scope);
        }
        public void AssertEquality(EquivalencyValidationContext context)
        {
            using var scope = new AssertionScope();
            scope.AddReportable("configuration", config.ToString());

            scope.BecauseOf(context.Because, context.BecauseArgs);

            AssertEqualityUsing(context);

            if (context.Tracer != null)
            {
                scope.AddReportable("trace", context.Tracer.ToString());
            }
        }
Beispiel #5
0
        public void AssertEquality(Comparands comparands, EquivalencyValidationContext context)
        {
            using var scope = new AssertionScope();

            scope.AssumeSingleCaller();
            scope.AddReportable("configuration", context.Options.ToString());
            scope.BecauseOf(context.Reason);

            RecursivelyAssertEquality(comparands, context);

            if (context.TraceWriter is not null)
            {
                scope.AddReportable("trace", context.TraceWriter.ToString());
            }
        }
Beispiel #6
0
        public AndConstraint <ServiceCollectionAssertions> BeRegistered(Type serviceType, Type implementationType, ServiceLifetime lifetime, string because = "", params object[] becauseArgs)
        {
            AssertionScope scope = Execute.Assertion;

            scope.AddReportable("lifetime", lifetime.ToString().ToLowerInvariant());
            scope
            .BecauseOf(because, becauseArgs)
            .WithExpectation("Expected {context:service collection} to contain a {lifetime} registration for {0} implemented by {1}{reason}, ", serviceType, implementationType)
            // Match service and lifetime
            .Given(() => Subject.Where(d => d.ServiceType == serviceType))
            .ForCondition(reg => reg.Any())
            .FailWith("but the service was not found.")
            .Then
            .ForCondition(reg => reg.Any(d => d.Lifetime == lifetime))
            .FailWith("but found {0}.", reg => reg.Select(d => d.Lifetime).Distinct())
            .Then
            .Given(reg => reg.FirstOrDefault(d => d.Lifetime == lifetime))
            .ForCondition(r =>
                          // Match implementation, instance or factory
                          r.ImplementationType == implementationType ||
                          r.ImplementationInstance != null && implementationType.IsInstanceOfType(r.ImplementationInstance) ||
                          r.ImplementationFactory != null
                          )
            .FailWith("but it does not.")
            ;

            return(new AndConstraint <ServiceCollectionAssertions>(this));
        }
        private void RunWithDiagnosticAnnotations(string bicepText, Func <IDiagnostic, bool> filterFunc, OnCompileErrors onCompileErrors, Action <IEnumerable <IDiagnostic> > assertAction)
        {
            using (AssertionScope scope = new AssertionScope())
            {
                scope.AddReportable("bicep code", bicepText);

                var result = CompilationHelper.Compile(bicepText);
                result.Should().NotHaveDiagnosticsWithCodes(new[] { LinterAnalyzer.LinterRuleInternalError }, "There should never be linter LinterRuleInternalError errors");

                if (onCompileErrors == OnCompileErrors.Fail)
                {
                    var compileErrors = result.Diagnostics.Where(d => d.Level == DiagnosticLevel.Error);
                    DiagnosticAssertions.DoWithDiagnosticAnnotations(
                        result.Compilation.SourceFileGrouping.EntryPoint,
                        compileErrors,
                        diags => diags.Should().HaveCount(0));
                }

                IDiagnostic[] diagnosticsMatchingCode = result.Diagnostics.Where(filterFunc).ToArray();
                DiagnosticAssertions.DoWithDiagnosticAnnotations(
                    result.Compilation.SourceFileGrouping.EntryPoint,
                    result.Diagnostics.Where(filterFunc),
                    assertAction);
            }
        }
Beispiel #8
0
        public async Task Working()
        {
            using var helper = await StartConsole(enableProfiler : true);

            var processInfo = ProcessInfo.GetProcessInfo(helper.Process.Id);

            processInfo.Should().NotBeNull();

            using var console = ConsoleHelper.Redirect();

            var result = ProcessBasicCheck.Run(processInfo, MockRegistryService(Array.Empty <string>(), ProfilerPath));

            using var scope = new AssertionScope();
            scope.AddReportable("Output", console.Output);

            result.Should().BeTrue();

            console.Output.Should().NotContainAny(
                ProfilerNotLoaded,
                TracerNotLoaded,
                "DD_DOTNET_TRACER_HOME",
                CorProfilerKey,
                CorEnableKey,
                CorProfilerPathKey,
                CorProfilerPath32Key,
                CorProfilerPath64Key);
        }
Beispiel #9
0
        public void EmptyCommand()
        {
            bool callbackInvoked = false;

            Program.CallbackForTests = (_, _, _) =>
            {
                callbackInvoked = true;
            };

            // CI visibility mode checks if there's a running agent
            using var agent = EnableCiVisibilityMode ? new MockTracerAgent(TcpPortProvider.GetOpenPort()) : null;

            var agentUrl = $"http://localhost:{agent?.Port ?? 1111}";

            // dd-env is an argument for the target application and therefore shouldn't set the DD_ENV variable
            var commandLine = $"{CommandPrefix} --tracer-home dummyFolder --agent-url {agentUrl}";

            using var console = ConsoleHelper.Redirect();

            var exitCode = Program.Main(commandLine.Split(' '));

            using var scope = new AssertionScope();

            scope.AddReportable("output", console.Output);

            exitCode.Should().Be(1);
            callbackInvoked.Should().BeFalse();
            console.Output.Should().Contain("Error: Missing command");
        }
Beispiel #10
0
        public void When_message_starts_with_two_single_braces_they_should_be_replaced_with_context()
        {
            // Arrange
            var scope = new AssertionScope();

            scope.AddReportable("SomeKey", "SomeValue");
            scope.AddReportable("AnotherKey", "AnotherValue");

            AssertionScope.Current.FailWith("{SomeKey}{AnotherKey}");

            // Act
            Action act = scope.Dispose;

            // Assert
            act.Should().ThrowExactly <XunitException>()
            .WithMessage("*SomeValue*AnotherValue*");
        }
        public void Message_should_have_reportable_values_appended_at_the_end()
        {
            // Arrange
            var scope = new AssertionScope();

            scope.AddReportable("SomeKey", "SomeValue");
            scope.AddReportable("AnotherKey", "AnotherValue");

            AssertionScope.Current.FailWith("{SomeKey}{AnotherKey}");

            // Act
            Action act = scope.Dispose;

            // Assert
            act.Should().ThrowExactly <XunitException>()
            .WithMessage("*With SomeKey:\nSomeValue\nWith AnotherKey:\nAnotherValue");
        }
Beispiel #12
0
        /// <summary>
        /// Prints the entire program syntax with line numbers if a test fails in the given assertion scope.
        /// </summary>
        public static AssertionScope WithFullSource(this AssertionScope assertionScope, BicepFile bicepFile)
        {
            assertionScope.AddReportable(
                "source",
                () => PrintHelper.PrintFullSource(bicepFile, 1, true));

            return(assertionScope);
        }
        public void Message_should_have_named_placeholders_be_replaced_by_reportable_values()
        {
            // Arrange
            var scope = new AssertionScope();

            scope.AddReportable("SomeKey", "SomeValue");
            scope.AddReportable("AnotherKey", "AnotherValue");

            AssertionScope.Current.FailWith("{SomeKey}{AnotherKey}");

            // Act
            Action act = scope.Dispose;

            // Assert
            act.Should().ThrowExactly <XunitException>()
            .WithMessage("SomeValueAnotherValue*");
        }
Beispiel #14
0
        public void AssertEquality(EquivalencyValidationContext context)
        {
            using (var scope = new AssertionScope())
            {
                scope.AddReportable("configuration", config.ToString());
                scope.AddNonReportable("objects", new CyclicReferenceDetector(config.CyclicReferenceHandling));

                scope.BecauseOf(context.Because, context.BecauseArgs);

                AssertEqualityUsing(context);

                if (context.Tracer != null)
                {
                    scope.AddReportable("trace", context.Tracer.ToString());
                }
            }
        }
        public static AssertionScope WithAnnotatedSource(AssertionScope assertionScope, BicepFile bicepFile, string contextName, IEnumerable <PrintHelper.Annotation> annotations)
        {
            assertionScope.AddReportable(
                contextName,
                () => PrintHelper.PrintWithAnnotations(bicepFile, annotations, 1, true));

            return(assertionScope);
        }
        public static AssertionScope WithAnnotatedSource(AssertionScope assertionScope, BicepFile bicepFile, string contextName, IEnumerable <PrintHelper.Annotation> annotations)
        {
            // TODO: figure out how to set this only on failure, rather than always calculating it
            assertionScope.AddReportable(
                contextName,
                PrintHelper.PrintWithAnnotations(bicepFile, annotations, 1, true));

            return(assertionScope);
        }
Beispiel #17
0
        public void Run()
        {
            string command   = null;
            string arguments = null;
            Dictionary <string, string> environmentVariables = null;
            bool callbackInvoked = false;

            Program.CallbackForTests = (c, a, e) =>
            {
                command              = c;
                arguments            = a;
                environmentVariables = e;
                callbackInvoked      = true;
            };

            // CI visibility mode checks if there's a running agent
            using var agent = EnableCiVisibilityMode ? new MockTracerAgent(TcpPortProvider.GetOpenPort()) : null;

            var agentUrl = $"http://localhost:{agent?.Port ?? 1111}";

            var commandLine = $"{CommandPrefix} test.exe --dd-env TestEnv --dd-service TestService --dd-version TestVersion --tracer-home TestTracerHome --agent-url {agentUrl} --set-env VAR1=A --set-env VAR2=B";

            using var console = ConsoleHelper.Redirect();

            var exitCode = Program.Main(commandLine.Split(' '));

            using var scope = new AssertionScope();

            scope.AddReportable("output", console.Output);

            exitCode.Should().Be(0);
            callbackInvoked.Should().BeTrue();

            command.Should().Be("test.exe");
            arguments.Should().BeNullOrEmpty();
            environmentVariables.Should().NotBeNull();

            environmentVariables.Should().Contain("DD_ENV", "TestEnv");
            environmentVariables.Should().Contain("DD_SERVICE", "TestService");
            environmentVariables.Should().Contain("DD_VERSION", "TestVersion");
            environmentVariables.Should().Contain("DD_DOTNET_TRACER_HOME", "TestTracerHome");
            environmentVariables.Should().Contain("DD_TRACE_AGENT_URL", agentUrl);
            environmentVariables.Should().Contain("VAR1", "A");
            environmentVariables.Should().Contain("VAR2", "B");

            if (EnableCiVisibilityMode)
            {
                environmentVariables.Should().Contain("DD_CIVISIBILITY_ENABLED", "1");
            }
            else
            {
                environmentVariables.Should().NotContainKey("DD_CIVISIBILITY_ENABLED");
            }
        }
        public void AssertEquality(EquivalencyValidationContext context)
        {
            using (var scope = new AssertionScope())
            {
                scope.AddReportable("configuration", config.ToString());
                scope.AddNonReportable("objects", new ObjectTracker(config.CyclicReferenceHandling));

                scope.BecauseOf(context.Reason, context.ReasonArgs);

                AssertEqualityUsing(context);
            }
        }
        public void AssertEquality(EquivalencyValidationContext context)
        {
            using (var scope = new AssertionScope())
            {
                scope.AddReportable("configuration", config.ToString());
                scope.AddNonReportable("objects", new ObjectTracker(config.CyclicReferenceHandling));

                scope.BecauseOf(context.Reason, context.ReasonArgs);

                AssertEqualityUsing(context);
            }
        }
Beispiel #20
0
        public static void DoWithDiagnosticAnnotations(SyntaxTree syntaxTree, IEnumerable <Diagnostic> diagnostics, Action <IEnumerable <Diagnostic> > action)
        {
            using (var scope = new AssertionScope())
            {
                scope.AddReportable("diagnostics", PrintHelper.PrintWithAnnotations(
                                        syntaxTree,
                                        diagnostics.Select(x => new PrintHelper.Annotation(x.Span, $"[{x.Code} ({x.Level})] {x.Message}")),
                                        1,
                                        true));

                action(diagnostics);
            }
        }
Beispiel #21
0
        private static IAssertionScope CreateAssertionScopeWithContext(SyntaxTree syntaxTree, Hover?hover, IPositionable requestedPosition)
        {
            var assertionScope = new AssertionScope();

            // TODO: figure out how to set this only on failure, rather than always calculating it
            assertionScope.AddReportable(
                "hover context",
                PrintHelper.PrintWithAnnotations(syntaxTree, new [] {
                new PrintHelper.Annotation(requestedPosition.Span, "cursor position"),
            }, 1, true));

            return(assertionScope);
        }
        private ConsoleHelper()
        {
            _output = new StringBuilder();

            _originalConsole    = AnsiConsole.Console;
            _originalTextWriter = Console.Out;

            _assertionScope = new AssertionScope();
            _assertionScope.AddReportable("output", () => _output.ToString());

            AnsiConsole.Console = AnsiConsole.Create(new AnsiConsoleSettings {
                Out = new RedirectedOutput(_output)
            });
            Console.SetOut(new StringWriter(_output));
        }
Beispiel #23
0
        public static AndConstraint <GenericCollectionAssertions <T> > NotContainEquivalentOf <T>(
            this GenericCollectionAssertions <T> assert,
            T expectation,
            Func <EquivalencyAssertionOptions <T>, EquivalencyAssertionOptions <T> > config,
            string because = "",
            params object[] becauseArgs)
        {
            if (ReferenceEquals(assert.Subject, null))
            {
                return(new AndConstraint <GenericCollectionAssertions <T> >(assert));
            }

            IEquivalencyAssertionOptions options     = config(AssertionOptions.CloneDefaults <T>());
            IEnumerable <object>         actualItems = assert.Subject.Cast <object>();

            using (var scope = new AssertionScope())
            {
                scope.AddReportable("configuration", options.ToString());

                foreach (var actualItem in actualItems)
                {
                    var context = new EquivalencyValidationContext
                    {
                        Subject         = actualItem,
                        Expectation     = expectation,
                        CompileTimeType = typeof(T),
                        Because         = because,
                        BecauseArgs     = becauseArgs,
                        Tracer          = options.TraceWriter,
                    };

                    var equivalencyValidator = new EquivalencyValidator(options);
                    equivalencyValidator.AssertEquality(context);

                    var failures = scope.Discard();
                    if (!failures.Any())
                    {
                        Execute.Assertion
                        .BecauseOf(because, becauseArgs)
                        .FailWith("Expected {context:collection} {0} to not contain equivalent of {1}.", assert.Subject, expectation);
                        break;
                    }
                }
            }

            return(new AndConstraint <GenericCollectionAssertions <T> >(assert));
        }
Beispiel #24
0
        private void CompileAndTest(string bicepText, string[] expectedFoundMessages)
        {
            using (var scope = new AssertionScope())
            {
                scope.AddReportable("bicepText", bicepText);

                var result        = CompilationHelper.Compile(bicepText);
                var semanticModel = result.Compilation.GetEntrypointSemanticModel();

                // Look for an output - that's what we'll use in the test
                var output = result.BicepFile.ProgramSyntax.Children.OfType <OutputDeclarationSyntax>()
                             .Should().HaveCount(1, "Each testcase should contain a single output with an expression to test")
                             .And.Subject.First();

                var secrets = FindPossibleSecretsVisitor.FindPossibleSecrets(semanticModel, output.Value);
                secrets.Select(s => s.FoundMessage).Should().BeEquivalentTo(expectedFoundMessages);
            }
        }
        public void Deferred_reportable_values_should_not_be_calculated_in_absence_of_failures()
        {
            // Arrange
            var scope = new AssertionScope();
            var deferredValueInvoked = false;

            scope.AddReportable("MyKey", () =>
            {
                deferredValueInvoked = true;

                return("MyValue");
            });

            // Act
            scope.Dispose();

            // Assert
            deferredValueInvoked.Should().BeFalse();
        }
        public void When_using_a_deferred_reportable_value_it_is_not_calculated_if_there_are_no_failures()
        {
            // Arrange
            var scope = new AssertionScope();
            var deferredValueInvoked = false;

            scope.AddReportable("MyKey", () =>
            {
                deferredValueInvoked = true;

                return("MyValue");
            });

            // Act
            scope.Dispose();

            // Assert
            deferredValueInvoked.Should().BeFalse();
        }
Beispiel #27
0
        public async Task String_segments_do_not_return_completions()
        {
            var fileWithCursors = @"
var completeString = |'he|llo'|
var interpolatedString = |'abc${|true}|de|f${|false}|gh|i'|
var multilineString = |'''|
hel|lo
'''|
";
            var bicepFile       = fileWithCursors.Replace("|", "");
            var syntaxTree      = SyntaxTree.Create(new Uri("file:///main.bicep"), bicepFile);

            var cursors = new List <int>();

            for (var i = 0; i < fileWithCursors.Length; i++)
            {
                if (fileWithCursors[i] == '|')
                {
                    cursors.Add(i - cursors.Count);
                }
            }

            using var client = await IntegrationTestHelper.StartServerWithTextAsync(bicepFile, syntaxTree.FileUri, resourceTypeProvider : TypeProvider);

            foreach (var cursor in cursors)
            {
                using var assertionScope = new AssertionScope();
                assertionScope.AddReportable(
                    "completion context",
                    PrintHelper.PrintWithAnnotations(syntaxTree, new [] {
                    new PrintHelper.Annotation(new TextSpan(cursor, 0), "cursor position"),
                }, 1, true));

                var completions = await client.RequestCompletion(new CompletionParams
                {
                    TextDocument = new TextDocumentIdentifier(syntaxTree.FileUri),
                    Position     = TextCoordinateConverter.GetPosition(syntaxTree.LineStarts, cursor),
                });

                completions.Should().BeEmpty();
            }
        }
        public virtual string GenerateAndTestSource(string generatedCodeName, string inputSource, string?outputPath = null, bool isExe = false)
        {
            var outputFileName = GetOutputFileName(outputPath, generatedCodeName, IsVB);

            using var assertionScope = new AssertionScope(outputFileName);
            assertionScope.AddReportable("OutputFileName", outputFileName);

            Compilation cliRootCompilation = GetCliRootCompilation(inputSource, isExe)
                                             ?? throw new InvalidOperationException();

            var requestedSyntaxTree = Generate(cliRootCompilation, out var outputCompilation, out var generationDiagnostics)
                                      .Where(x => x.compilationName.Contains($"{generatedCodeName}.generated"))
                                      .Select(x => x.syntaxTree)
                                      .FirstOrDefault();

            ReportDiagnostics(generatedCodeName, generationDiagnostics, outputCompilation);
            OutputIfRequested(outputFileName, requestedSyntaxTree);

            return(requestedSyntaxTree is null
                    ? "Compilation is null"
                    : requestedSyntaxTree.ToString());
        public void When_message_starts_with_single_braces_they_should_be_replaced_with_context()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var scope = new AssertionScope();

            scope.AddReportable("MyKey", "MyValue");

            AssertionScope.Current.FailWith("{MyKey}");

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = scope.Dispose;

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.Should().ThrowExactly <XunitException>()
            .WithMessage("*MyValue*");
        }
Beispiel #30
0
        public void AdditionalArguments()
        {
            string command   = null;
            string arguments = null;
            Dictionary <string, string> environmentVariables = null;
            bool callbackInvoked = false;

            Program.CallbackForTests = (c, a, e) =>
            {
                command              = c;
                arguments            = a;
                environmentVariables = e;
                callbackInvoked      = true;
            };

            // CI visibility mode checks if there's a running agent
            using var agent = EnableCiVisibilityMode ? new MockTracerAgent(TcpPortProvider.GetOpenPort()) : null;

            var agentUrl = $"http://localhost:{agent?.Port ?? 1111}";

            // dd-env is an argument for the target application and therefore shouldn't set the DD_ENV variable
            var commandLine = $"{CommandPrefix} --tracer-home dummyFolder --agent-url {agentUrl} -- test.exe --dd-env test";

            using var console = ConsoleHelper.Redirect();

            var exitCode = Program.Main(commandLine.Split(' '));

            using var scope = new AssertionScope();

            scope.AddReportable("output", console.Output);

            exitCode.Should().Be(0);
            callbackInvoked.Should().BeTrue();

            command.Should().Be("test.exe");
            arguments.Should().Be("--dd-env test");
            environmentVariables.Should().NotContainKey("DD_ENV");
        }
        public void Message_should_have_named_placeholder_be_replaced_by_defered_reportable_value()
        {
            // Arrange
            var scope = new AssertionScope();
            var deferredValueInvoked = false;

            scope.AddReportable("MyKey", () =>
            {
                deferredValueInvoked = true;

                return("MyValue");
            });

            AssertionScope.Current.FailWith("{MyKey}");

            // Act
            Action act = scope.Dispose;

            // Assert
            act.Should().ThrowExactly <XunitException>()
            .WithMessage("MyValue*\n\nWith MyKey:\nMyValue\n");
            deferredValueInvoked.Should().BeTrue();
        }