Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
###############################################################################
* text=auto

# If we don't pin down the line ending here, the tests that use this depend on
# the value of core.autocrlf.
src/Test.UnitTests.Sarif/TestData/InsertOptionalDataVisitor/InsertOptionalDataVisitor.txt text eol=crlf

###############################################################################
# Set default behavior for command prompt diff.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,16 @@ public void AndroidStudioConverter_ConvertToSarifResult_HintsAreStapledToDescrip

var uut = new AndroidStudioProblem(builder);
Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
Assert.Equal(@"hungry EVIL zombies
string expected = @"hungry EVIL zombies
Possible resolution: comment
Possible resolution: delete", result.Message.Text);
Possible resolution: delete";

// We need to normalize the line endings in the multiline string above,
// because otherwise this test passes or fails depending on the value
// of core.autocrlf in git.
expected = expected.Replace("\r", "");
expected = expected.Replace("\n", Environment.NewLine);
Assert.Equal(expected, result.Message.Text);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public void FortifyUtilities_ParseFormattedContentText_Correct()
And here's a garbage

double line break!";
// We need to normalize the line endings in the multiline string above,
// because otherwise this test passes or fails depending on the value
// of core.autocrlf in git.
expected = expected.Replace("\r", "");
expected = expected.Replace("\n", Environment.NewLine);
string actual = FortifyUtilities.ParseFormattedContentText(content);
Assert.Equal(expected, actual);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Text;

Expand Down Expand Up @@ -35,6 +36,12 @@ public void BuildRule_GeneratesExpectedMarkdown()
command.BuildRule(test.Value, sb);

string expectedMarkdown = resourceExtractor.GetResourceExpectedOutputsText(test.Key);

// We need to normalize the line endings in the multiline string above,
// because otherwise this test passes or fails depending on the value
// of core.autocrlf in git.
expectedMarkdown = expectedMarkdown.Replace("\r", "");
expectedMarkdown = expectedMarkdown.Replace("\n", Environment.NewLine);
sb.ToString().Should().Be(expectedMarkdown);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;

using Microsoft.CodeAnalysis.Sarif.Query;
Expand Down Expand Up @@ -50,6 +51,12 @@ public void QueryCommand_Basics()
RunAndVerifyCount(1, new QueryOptions() { Expression = "RuleId = 'CSCAN0020/0'", InputFilePath = filePath, OutputFilePath = outputFilePath, Minify = false, Force = true });

string expected = s_extractor.GetResourceText("elfie-arriba.CSCAN0020.sarif");
// We need to normalize the line endings in the multiline string above,
// because otherwise this test passes or fails depending on the value
// of core.autocrlf in git.
expected = expected.Replace("\r", "");
expected = expected.Replace("\n", Environment.NewLine);

string actual = File.ReadAllText(outputFilePath);
Assert.Equal(expected, actual);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Test.UnitTests.Sarif/Core/StackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream,
at System.IO.File.Create(String path, Int32 bufferSize)
at Microsoft.CodeAnalysis.Test.UnitTests.Sarif.Core.StackTests.Stack_CreateFromExceptionWithInnerException()";

// We need to normalize the line endings in the multiline string above,
// because otherwise this test passes or fails depending on the value
// of core.autocrlf in git.
stackTraceTemplate = stackTraceTemplate.Replace("\r", "");
stackTraceTemplate = stackTraceTemplate.Replace("\n", Environment.NewLine);

int relativePathLineNumber = 60;
string relativeFilePath = "/_/src/Test.UnitTests.Sarif/Core/StackTests.cs";
var sarifStackWithRelativeFileLocation = Stack.Create(stackTraceTemplate + $" in {relativeFilePath}:line {relativePathLineNumber}");
Expand Down
8 changes: 7 additions & 1 deletion src/Test.UnitTests.Sarif/Core/WebRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void WebRequest_Parse_CreatesExpectedWebRequestObject()
public void WebRequest_Parse_ExtractsBody()
{
// Example from RFC 7230.
const string RequestString =
string RequestString =
@"GET /hello.txt HTTP/1.1
User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
Host: www.example.com
Expand All @@ -54,6 +54,12 @@ This is the body.
Line 2.
";

// We need to normalize the line endings in the multiline string above,
// because otherwise this test passes or fails depending on the value
// of core.autocrlf in git.
RequestString = RequestString.Replace("\r", "");
RequestString = RequestString.Replace("\n", Environment.NewLine);

var webRequest = WebRequest.Parse(RequestString);

webRequest.Method.Should().Be("GET");
Expand Down
7 changes: 6 additions & 1 deletion src/Test.UnitTests.Sarif/Core/WebResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class WebResponseTests
public void WebResponse_Parse_CreatesExpectedWebResponseObject()
{
// Example from RFC 7230.
const string ResponseString =
string ResponseString =
@"HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache
Expand All @@ -30,6 +30,11 @@ public void WebResponse_Parse_CreatesExpectedWebResponseObject()

Hello World!My payload includes a trailing NewLine.
";
// We need to normalize the line endings in the multiline string above,
// because otherwise this test passes or fails depending on the value
// of core.autocrlf in git.
ResponseString = ResponseString.Replace("\r", "");
ResponseString = ResponseString.Replace("\n", Environment.NewLine);

var webResponse = WebResponse.Parse(ResponseString);

Expand Down
7 changes: 7 additions & 0 deletions src/Test.UnitTests.Sarif/RoundTrippingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public void SarifLog_PropertyBagProperties_CanBeRoundTripped()
string testFileName = "PropertyBagComprehensiveValueTypes.sarif";
string sarifLogText = GetInputSarifTextFromResource(testFileName);

// The resource file text value depends on the value of core.autocrlf
// in git when the repo was checked out, but the json serializer does
// its serialization using Environment.NewLine. We need to change one
// of the two to get the test to pass consistently.
sarifLogText = sarifLogText.Replace("\r", "");
sarifLogText = sarifLogText.Replace("\n", Environment.NewLine);

SarifLog log = JsonConvert.DeserializeObject<SarifLog>(sarifLogText);

PropertyBagHolder holder = log.Runs[0].Results[0];
Expand Down
10 changes: 9 additions & 1 deletion src/Test.UnitTests.Sarif/UriConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,15 @@ public void WriteJson_ConvertsUriObjectsToStrings()
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
};

JsonConvert.SerializeObject(testObject, settings).Should().Be(s_testFileText);
// The resource file text value depends on the value of core.autocrlf
// in git when the repo was checked out, but the json serializer does
// its serialization using Environment.NewLine. We need to change one
// of the two to get the test to pass consistently.
var expected = s_testFileText;
expected = expected.Replace("\r", "");
expected = expected.Replace("\n", Environment.NewLine);

JsonConvert.SerializeObject(testObject, settings).Should().Be(expected);
}

[Fact]
Expand Down
7 changes: 7 additions & 0 deletions src/Test.UnitTests.Sarif/Writers/SarifLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ private static void StreamOwnershipHelper(bool closeWriterOnDispose)
{
string expectedText = s_extractor.GetResourceText("SimpleExample.sarif");

// The resource file text value depends on the value of core.autocrlf
// in git when the repo was checked out, but the json serializer does
// its serialization using Environment.NewLine. We need to change one
// of the two to get the test to pass consistently.
expectedText = expectedText.Replace("\r", "");
expectedText = expectedText.Replace("\n", Environment.NewLine);

var memoryStream = new MemoryStream();
var streamWriter = new StreamWriter(memoryStream);

Expand Down
7 changes: 7 additions & 0 deletions src/Test.Utilities.Sarif/FileDiffingUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ protected virtual void RunTest(string inputResourceName,
expectedOutputResourceName = Path.GetFileNameWithoutExtension(expectedOutputResourceName) + SarifConstants.SarifFileExtension;
string expectedSarifText = GetExpectedOutputFileFromResource(expectedOutputResourceName);

// Our expected output files have line endings based on the value of git's core.autocrlf
// config setting, but our actual output files use Environment.NewLine. In order for the
// tests to consistently pass regardless of git settings and host OS, we need to correct
// for the desired behavior here.
expectedSarifText = expectedSarifText.Replace("\r", "");
expectedSarifText = expectedSarifText.Replace("\n", Environment.NewLine);

string actualSarifText = ConstructTestOutputFromInputResource(inputResourceName, parameter);

// The comparison code is shared between this one-input-to-one-output method and the
Expand Down
Loading