| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 | // Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the Apache 2.0 License.// See the LICENSE file in the project root for more information.using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using Xunit;namespace Tests.System.Reactive.Tests{    /// <summary>    /// Verify that main classes and unit tests have a license header    /// in the source files.    /// </summary>    public class LicenseHeaderTest    {        static readonly bool fixHeaders = true;        static readonly string[] lines = {            "// Licensed to the .NET Foundation under one or more agreements.",            "// The .NET Foundation licenses this file to you under the Apache 2.0 License.",            "// See the LICENSE file in the project root for more information.",            ""        };        [Fact]        public void ScanFiles()        {            var dir = Directory.GetCurrentDirectory();            var idx = dir.LastIndexOf("Rx.NET");            Assert.False(idx < 0, $"Could not locate sources directory: {dir}");            var newDir = Path.Combine(dir.Substring(0, idx), "Rx.NET", "Source");            var error = new StringBuilder();            var count = ScanPath(newDir, error);            if (error.Length != 0)            {                Assert.False(true, $"Files with no license header: {count}\r\n{error.ToString()}");            }        }        int ScanPath(string path, StringBuilder error)        {            var count = 0;            foreach (var file in Directory.EnumerateFiles(path, "*.cs", SearchOption.AllDirectories))            {                // exclusions                if (file.Contains("obj/Debug")                     || file.Contains(@"obj\Debug")                    || file.Contains("AssemblyInfo.cs")                    || file.Contains(".Designer.cs")                    || file.Contains(".Generated.cs")                    || file.Contains("Uwp.DeviceRunner")                    || file.Contains(@"obj\Release")                    || file.Contains("obj/Release")                )                {                    continue;                }                // analysis                string content = File.ReadAllText(file);                if (!content.StartsWith(lines[0]))                {                    count++;                    error.Append(file).Append("\r\n");                    if (fixHeaders)                    {                        StringBuilder newContent = new StringBuilder();                        string separator = content.Contains("\r\n") ? "\r\n" : "\n";                        foreach (var s in lines)                        {                            newContent.Append(s).Append(separator);                        }                        newContent.Append(content);                        File.WriteAllText(file, newContent.ToString());                     }                }            }            return count;        }    }}
 |