<commit_before>// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Xunit;
using static Microsoft.NET.TestFramework.Commands.MSBuildTest;
using System.Linq;
using FluentAssertions;
using System.Xml.Linq;
using System.Runtime.Versioning;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System;
using System.Runtime.CompilerServices;

namespace Microsoft.NET.Build.Tests
{
    public class GivenThatWeWantToBuildALibrary
    {
        private TestAssetsManager _testAssetsManager = TestAssetsManager.TestProjectsAssetsManager;

       [Fact]
        public void It_builds_the_library_successfully()
        {
            var testAsset = _testAssetsManager
                .CopyTestAsset("AppWithLibrary")
                .WithSource()
                .Restore(relativePath: "TestLibrary");

            var libraryProjectDirectory = Path.Combine(testAsset.TestRoot, "TestLibrary");

            var buildCommand = new BuildCommand(Stage0MSBuild, libraryProjectDirectory);
            buildCommand
                .Execute()
                .Should()
                .Pass();

            var outputDirectory = buildCommand.GetOutputDirectory("netstandard1.5");

            outputDirectory.Should().OnlyHaveFiles(new[] {
                "TestLibrary.dll",
                "TestLibrary.pdb",
                "TestLibrary.deps.json"
            });
        }

       [Fact]
        public void It_builds_the_library_twice_in_a_row()
        {
            var testAsset = _testAssetsManager
                .CopyTestAsset("AppWithLibrary")
                .WithSource()
                .Restore(relativePath: "TestLibrary");

            var libraryProjectDirectory = Path.Combine(testAsset.TestRoot, "TestLibrary");

            var buildCommand = new BuildCommand(Stage0MSBuild, libraryProjectDirectory);
            buildCommand
                .Execute()
                .Should()
                .Pass();

            buildCommand
                .Execute()
                .Should()
                .Pass();
        }

        [Fact]
        public void All_props_and_targets_add_themselves_to_MSBuildAllTargets()
        {
            List<string> expectedAllProjects = new List<string>();
            string baseIntermediateDirectory = null;

            var allProjectsFromProperty = GetValuesFromTestLibrary("MSBuildAllProjects", getValuesCommand =>
            {
                baseIntermediateDirectory = getValuesCommand.GetBaseIntermediateDirectory().FullName;

                string preprocessedFile = Path.Combine(getValuesCommand.GetOutputDirectory("netstandard1.5").FullName, "preprocessed.xml");

                //  Preprocess the file, and then scan it to find all the project files that were imported.  The preprocessed output
                //  includes comments with long lines of "======", between which is the import statement and then the path to the file
                //  that was imported
                getValuesCommand.Execute("/pp:" + preprocessedFile)
                    .Should()
                    .Pass();

                string previousLine = null;
                bool insideDelimiters = false;
                int lineNumber = 0;
                foreach (string line in File.ReadAllLines(preprocessedFile))
                {
                    lineNumber++;
                    if (line.All(c => c == '=') && line.Length >= 80)
                    {
                        if (insideDelimiters)
                        {
                            if (!previousLine.Trim().Equals("</Import>", StringComparison.OrdinalIgnoreCase))
                            {
                                expectedAllProjects.Add(previousLine);
                            }
                        }
                        insideDelimiters = !insideDelimiters;
                    }

                    previousLine = line;
                }

                File.Delete(preprocessedFile);
            }, valueType: GetValuesCommand.ValueType.Property);

            string dotnetRoot = Path.GetDirectoryName(RepoInfo.DotNetHostPath);
<commit_msg>Fix test when "--" is included in the path<commit_after>// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Xunit;
using static Microsoft.NET.TestFramework.Commands.MSBuildTest;
using System.Linq;
using FluentAssertions;
using System.Xml.Linq;
using System.Runtime.Versioning;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System;
using System.Runtime.CompilerServices;

namespace Microsoft.NET.Build.Tests
{
    public class GivenThatWeWantToBuildALibrary
    {
        private TestAssetsManager _testAssetsManager = TestAssetsManager.TestProjectsAssetsManager;

       [Fact]
        public void It_builds_the_library_successfully()
        {
            var testAsset = _testAssetsManager
                .CopyTestAsset("AppWithLibrary")
                .WithSource()
                .Restore(relativePath: "TestLibrary");

            var libraryProjectDirectory = Path.Combine(testAsset.TestRoot, "TestLibrary");

            var buildCommand = new BuildCommand(Stage0MSBuild, libraryProjectDirectory);
            buildCommand
                .Execute()
                .Should()
                .Pass();

            var outputDirectory = buildCommand.GetOutputDirectory("netstandard1.5");

            outputDirectory.Should().OnlyHaveFiles(new[] {
                "TestLibrary.dll",
                "TestLibrary.pdb",
                "TestLibrary.deps.json"
            });
        }

       [Fact]
        public void It_builds_the_library_twice_in_a_row()
        {
            var testAsset = _testAssetsManager
                .CopyTestAsset("AppWithLibrary")
                .WithSource()
                .Restore(relativePath: "TestLibrary");

            var libraryProjectDirectory = Path.Combine(testAsset.TestRoot, "TestLibrary");

            var buildCommand = new BuildCommand(Stage0MSBuild, libraryProjectDirectory);
            buildCommand
                .Execute()
                .Should()
                .Pass();

            buildCommand
                .Execute()
                .Should()
                .Pass();
        }

        [Fact]
        public void All_props_and_targets_add_themselves_to_MSBuildAllTargets()
        {
            List<string> expectedAllProjects = new List<string>();
            string baseIntermediateDirectory = null;

            var allProjectsFromProperty = GetValuesFromTestLibrary("MSBuildAllProjects", getValuesCommand =>
            {
                baseIntermediateDirectory = getValuesCommand.GetBaseIntermediateDirectory().FullName;

                string preprocessedFile = Path.Combine(getValuesCommand.GetOutputDirectory("netstandard1.5").FullName, "preprocessed.xml");

                //  Preprocess the file, and then scan it to find all the project files that were imported.  The preprocessed output
                //  includes comments with long lines of "======", between which is the import statement and then the path to the file
                //  that was imported
                getValuesCommand.Execute("/pp:" + preprocessedFile)
                    .Should()
                    .Pass();

                string previousLine = null;
                bool insideDelimiters = false;
                int lineNumber = 0;
                foreach (string line in File.ReadAllLines(preprocessedFile))
                {
                    lineNumber++;
                    if (line.All(c => c == '=') && line.Length >= 80)
                    {
                        if (insideDelimiters)
                        {
                            if (!previousLine.Trim().Equals("</Import>", StringComparison.OrdinalIgnoreCase))
                            {
                                //  MSBuild replaces "--" with "__" in the filenames, since a double hyphen isn't allowed in XML comments per the spec.
                                //  This causes problems on the CI machines where the path includes "---".  So convert it back here.
                                previousLine = previousLine.Replace("__", "--");

                                expectedAllProjects.Add(previousLine);
                            }
                        }
                        insideDelimiters = !insideDelimiters;
                    }

                    previousLine = line;
                }

                File.Delete(preprocessedFile);
            }, valueType: GetValuesCommand.ValueType.Property);

            string dotnetRoot = Path.GetDirectoryName(RepoInfo.DotNetHostPath);

{'fraction_non_alphanumeric': 0.0809866841301026, 'fraction_numerical': 0.0019646365422396855, 'mean_word_length': 1.7876483115302708, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2369571', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2089, 'n_words': 493}