diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d8df00d5..fba533a4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,7 +5,7 @@ on: branches: - master - develop - - v8 + - v8 paths: - src/** - .github/workflows/** @@ -39,7 +39,6 @@ jobs: with: dotnet-version: | 3.1.x - 5.0.x 6.0.x 7.0.x source-url: https://nuget.pkg.github.com/graphql-dotnet/index.json diff --git a/src/GraphQLParser.ApiTests/GraphQLParser.ApiTests.csproj b/src/GraphQLParser.ApiTests/GraphQLParser.ApiTests.csproj index 3ad1ee81..36734205 100644 --- a/src/GraphQLParser.ApiTests/GraphQLParser.ApiTests.csproj +++ b/src/GraphQLParser.ApiTests/GraphQLParser.ApiTests.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/src/GraphQLParser.Tests/GraphQLParser.Tests.csproj b/src/GraphQLParser.Tests/GraphQLParser.Tests.csproj index c0da9b5d..9e1ffaa2 100644 --- a/src/GraphQLParser.Tests/GraphQLParser.Tests.csproj +++ b/src/GraphQLParser.Tests/GraphQLParser.Tests.csproj @@ -13,7 +13,8 @@ - netcoreapp3.1;net5;net6;net7 + netcoreapp3.1;net6;net7 + $(TargetFrameworks);net462 @@ -27,7 +28,7 @@ - + diff --git a/src/GraphQLParser.Tests/Visitors/ASTVisitorTests.cs b/src/GraphQLParser.Tests/Visitors/ASTVisitorTests.cs index 9f1be0e9..ffe9d87f 100644 --- a/src/GraphQLParser.Tests/Visitors/ASTVisitorTests.cs +++ b/src/GraphQLParser.Tests/Visitors/ASTVisitorTests.cs @@ -23,17 +23,17 @@ public void ASTVisitor_Should_Handle_Null() } [Fact] - public void ASTVisitor_Should_Throw_On_Unknown_Node() + public async Task ASTVisitor_Should_Throw_On_Unknown_Node() { var visitor = new ASTVisitor(); var context = new Context(); - var ex = Should.Throw(() => visitor.VisitAsync(new MySuperNode(), context).GetAwaiter().GetResult()); + var ex = await Should.ThrowAsync(async () => await visitor.VisitAsync(new MySuperNode(), context)); ex.Message.ShouldBe("Unknown node 'MySuperNode'."); } [Fact] - public void ASTVisitor_Should_Pass_CancellationToken() + public async Task ASTVisitor_Should_Pass_CancellationToken() { var document = "scalar JSON".Parse(); var visitor = new MyVisitor(); @@ -41,7 +41,7 @@ public void ASTVisitor_Should_Pass_CancellationToken() var context = new Context { CancellationToken = cts.Token }; context.CancellationToken.ThrowIfCancellationRequested(); - Should.Throw(() => visitor.VisitAsync(document, context).GetAwaiter().GetResult()); + await Should.ThrowAsync(async () => await visitor.VisitAsync(document, context)); } private sealed class MyVisitor : ASTVisitor diff --git a/src/GraphQLParser.Tests/Visitors/CountVisitorTests.cs b/src/GraphQLParser.Tests/Visitors/CountVisitorTests.cs index d36dac64..57ea0024 100644 --- a/src/GraphQLParser.Tests/Visitors/CountVisitorTests.cs +++ b/src/GraphQLParser.Tests/Visitors/CountVisitorTests.cs @@ -38,7 +38,7 @@ public async Task CountVisitor_Should_Count_Nodes(string text, int expectedCount var document = text.Parse(); - await visitor.VisitAsync(document, context).ConfigureAwait(false); + await visitor.VisitAsync(document, context); context.Count.ShouldBe(expectedCount); document.AllNestedCount().ShouldBe(expectedCount); } @@ -55,7 +55,7 @@ public async Task CountVisitor_Should_Count_Zero_Nodes(string text) var document = text.Parse(); - await visitor.VisitAsync(document, context).ConfigureAwait(false); + await visitor.VisitAsync(document, context); context.Count.ShouldBe(0); } } diff --git a/src/GraphQLParser.Tests/Visitors/MaxDepthVisitorTests.cs b/src/GraphQLParser.Tests/Visitors/MaxDepthVisitorTests.cs index 0d7a9ad3..9040fafe 100644 --- a/src/GraphQLParser.Tests/Visitors/MaxDepthVisitorTests.cs +++ b/src/GraphQLParser.Tests/Visitors/MaxDepthVisitorTests.cs @@ -22,7 +22,7 @@ public async Task MaxDepthVisitor_Should_Work(string text, int expectedMaxDepth) var document = text.Parse(); - await visitor.VisitAsync(document, context).ConfigureAwait(false); + await visitor.VisitAsync(document, context); context.MaxDepth.ShouldBe(expectedMaxDepth); document.MaxNestedDepth().ShouldBe(expectedMaxDepth); } diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs index 935d47b9..0fdff1e3 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs @@ -867,7 +867,7 @@ public async Task SDLPrinter_Should_Print_Document( var writer = new StringWriter(); var document = text.Parse(); - await printer.PrintAsync(document, writer).ConfigureAwait(false); + await printer.PrintAsync(document, writer); var actual = writer.ToString(); actual.ShouldBe(expected, $"Test {number} failed"); @@ -924,11 +924,11 @@ public async Task SDLPrinter_Should_Print_BlockStrings(int number, string input, var document = (input + " scalar a").Parse(); var printer = new SDLPrinter(); - await printer.PrintAsync(document, writer).ConfigureAwait(false); + await printer.PrintAsync(document, writer); var renderedOriginal = writer.ToString(); - var lines = renderedOriginal.Split(Environment.NewLine); - var renderedDescription = string.Join(Environment.NewLine, lines.SkipLast(1)); + var lines = renderedOriginal.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); + var renderedDescription = string.Join(Environment.NewLine, lines.Take(lines.Length - 1)); renderedDescription = renderedDescription.Replace("\r\n", "\n"); renderedDescription.ShouldBe(expected); @@ -955,7 +955,7 @@ public async Task SDLPrinter_Should_Print_EscapedStrings(string stringValue) var document = query.Parse(); var printer = new SDLPrinter(); - await printer.PrintAsync(document, writer).ConfigureAwait(false); + await printer.PrintAsync(document, writer); var rendered = writer.ToString(); rendered.ShouldBe(expected); diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs index 18746642..926ccee5 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs @@ -54,7 +54,7 @@ public async Task Printer_Should_Print_Pretty_If_Directives_Skipped( var writer = new StringWriter(); var document = text.Parse(); - await printer.PrintAsync(document, writer).ConfigureAwait(false); + await printer.PrintAsync(document, writer); var actual = writer.ToString(); actual.ShouldBe(expected, $"Test {number} failed"); diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs index a22b58b5..2939c3ff 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs @@ -186,7 +186,7 @@ public async Task Printer_Should_Print_Pretty_If_Definitions_Skipped( var writer = new StringWriter(); var document = text.Parse(); - await printer.PrintAsync(document, writer).ConfigureAwait(false); + await printer.PrintAsync(document, writer); var actual = writer.ToString(); actual.ShouldBe(expected, $"Test {number} failed"); diff --git a/src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs b/src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs index 857e0dce..b3b0277c 100644 --- a/src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs +++ b/src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs @@ -285,7 +285,7 @@ public async Task StructurePrinter_Should_Print_Tree(string text, string expecte { var writer = new StringWriter(); var document = text.Parse(); - await _structPrinter1.PrintAsync(document, writer).ConfigureAwait(false); + await _structPrinter1.PrintAsync(document, writer); var actual = writer.ToString(); actual.ShouldBe(expected); } @@ -304,7 +304,7 @@ public async Task StructurePrinter_Should_Print_Tree_Without_Names(string text, { var writer = new StringWriter(); var document = text.Parse(); - await _structPrinter2.PrintAsync(document, writer).ConfigureAwait(false); + await _structPrinter2.PrintAsync(document, writer); var actual = writer.ToString(); actual.ShouldBe(expected); } @@ -550,7 +550,7 @@ public async Task StructurePrinter_Should_Print_Tree_With_Locations(string text, var writer = new StringWriter(); var document = text.Parse(new ParserOptions { Ignore = option }); - await _structPrinter3.PrintAsync(document, writer).ConfigureAwait(false); + await _structPrinter3.PrintAsync(document, writer); var actual = writer.ToString(); actual.ShouldBe(expected); } @@ -587,7 +587,7 @@ public async Task StructurePrinter_Should_Print_Tree_With_Custom_Indentation(str var document = text.Parse(new ParserOptions { Ignore = option }); var printer = new StructurePrinter(new StructurePrinterOptions { PrintNames = false, IndentSize = indentSize }); - await printer.PrintAsync(document, writer).ConfigureAwait(false); + await printer.PrintAsync(document, writer); var actual = writer.ToString(); actual.ShouldBe(expected); } diff --git a/src/GraphQLParser/Visitors/PrintContextExtensions.cs b/src/GraphQLParser/Visitors/PrintContextExtensions.cs index 96bd40f5..36fe4a94 100644 --- a/src/GraphQLParser/Visitors/PrintContextExtensions.cs +++ b/src/GraphQLParser/Visitors/PrintContextExtensions.cs @@ -25,7 +25,7 @@ public static ValueTask WriteAsync(this TContext context, ROM value) context.IndentPrinted = false; var task = -#if NETSTANDARD2_0 +#if NETSTANDARD2_0 || NET462 // no cancellationToken support on netstandard2.0 context.Writer.WriteAsync(value.ToString()); //ISSUE: allocation - either WriteAsync(value.ToString()) or Write(char value) in a loop #elif NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER diff --git a/src/GraphQLParser/Visitors/SDLPrinterExtensions.cs b/src/GraphQLParser/Visitors/SDLPrinterExtensions.cs index 29385ec0..0033200e 100644 --- a/src/GraphQLParser/Visitors/SDLPrinterExtensions.cs +++ b/src/GraphQLParser/Visitors/SDLPrinterExtensions.cs @@ -24,13 +24,23 @@ public static string Print(this SDLPrinter printer, ASTNode node) public static void Print(this SDLPrinter printer, ASTNode node, StringBuilder stringBuilder) => printer.PrintAsync(node, new StringWriter(stringBuilder), default).AsTask().GetAwaiter().GetResult(); +#if !NET6_0_OR_GREATER + private static readonly Encoding _uTF8NoBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); +#endif + /// /// Prints the specified AST into the specified as a SDL document. /// If no encoding is specified, the document is written in UTF-8 format without a byte order mark. /// public static void Print(this SDLPrinter printer, ASTNode node, MemoryStream memoryStream, Encoding? encoding = null) { - using var streamWriter = new StreamWriter(memoryStream, encoding, -1 /* default */, true); + int bufferSize = -1; +#if !NET6_0_OR_GREATER + encoding ??= _uTF8NoBOM; + if (bufferSize == -1) + bufferSize = 1024; +#endif + using var streamWriter = new StreamWriter(memoryStream, encoding, bufferSize, true); printer.PrintAsync(node, streamWriter, default).AsTask().GetAwaiter().GetResult(); // flush encoder state to stream streamWriter.Flush();