<commit_before>using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using System.Xml.Linq;
using WixSharp.Bootstrapper;
using WixSharp.CommonTasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

#pragma warning disable MethodDocumentationHeader // The method must have a documentation header.

namespace WixSharp.Test
{
    public class IssueFixesTest : WixLocator
    {
        /// <summary>
        /// Fixes the issue 803.
        /// </summary>
        [Fact]
        [Description("Issue #803")]
        public void Fix_Issue_803()
        {
            // ensure all types that expose their properties for serialization with [XML] have these properties public
            // "SqlDb" and "root" are serializable but not assignable by user
            var classesWithFaultsFields =
                    typeof(Project).Assembly.GetTypes()
                        .SelectMany(t => t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                                          .Where(p => p.Name != "SqlDb")
                                          .Where(p => p.GetCustomAttribute<WixSharp.XmlAttribute>() != null)
                                          .Select(x => $"{x.DeclaringType.Name}.{x.Name}"))
                .Concat(
                    typeof(Project).Assembly.GetTypes()
                        .Where(t => t != typeof(Error))
                        .SelectMany(t => t.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                                          .Where(p => p.Name != "root")
                                          .Where(p => p.GetCustomAttribute<WixSharp.XmlAttribute>() != null)
                                          .Select(x => $"{x.DeclaringType.Name}.{x.Name}")))
                .ToArray();

            Assert.Empty(classesWithFaultsFields);
        }

        [Fact]
        [Description("Issue #1114")]
        public void Fix_Issue_1114()
        {
            var project = new Project("MyProductSetup",
                new Dir(@"%ProgramFiles%\MyCompany\MyProduct\MyApp",
                        new Dir(
                            // new Feature("extra"),
                            @"Logs")));

            project.PreserveTempFiles = true;
            project.WixSourceGenerated += document =>
            {
                var xml = document.ToString();
            };
            project.BuildWxs();
        }

        [Fact]
        [Description("Issue #1171")]
        public void Fix_Issue_1171()
        {
            var tempFile = System.IO.Path.GetTempFileName();
            try
            {
                System.IO.File.WriteAllLines(tempFile, new[]
                {
                    "Windows Registry Editor Version 5.00",
                    "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Test]",
                    "\"New Value #1\"=dword:00000001",
                    "\"New Value #2\"=hex(b):02,00,00,00,00,00,00,00",
                });
                var values = WixSharp.RegFileImporter.ImportFrom(tempFile);
            }
            finally
            {
                tempFile.DeleteIfExists();
            }
        }

        [Fact]
        [Description("Issue #1132")]
        public void Fix_Issue_1132()
        {
            var i = 0;

            var project = new Project("CableScheduler",
                new Dir(@"%ProgramFiles%\CADbloke\CableScheduler",
                new Files($"{Environment.CurrentDirectory}\\*.*", f => i++ < 1)),

                new Dir(@"%ProgramMenu%\CADbloke\CableScheduler",
                        new ExeFileShortcut("Cable Scheduler", $"[INSTALLDIR]Test.exe", "")));

            project.ResolveWildCards(pruneEmptyDirectories: true);

            var wsx = project.BuildWxs();
            var xml = System.IO.File.ReadAllText(wsx);

            Assert.True(xml.Contains("<Shortcut Id="));
        }

        [Fact]
        [Description("Issue #67")]
        public void Fix_Issue_67()
        {
            var project = new Project("MyProduct",
                              new Dir("%ProgramFiles%"));

            project.LicenceFile = "license.rtf";

            var file = project.BuildWxs();
        }

        [Fact]
        [Description("Issue #182")]
        public void Fix_Issue_182()
        {
            void assert(Platform? projPlatform, bool? is64Reg1, bool? is64Reg2, Action<XDocument, XElement, XElement, XElement> assertHandler)
            {
                var proj = new Project("MyProduct",
                                       new Dir(@"%ProgramFiles%\My Company\My Product",
                                           new File("setup.cs")));

                if (projPlatform != null)
                    proj.Platform = projPlatform;

                var regval = new RegValue(RegistryHive.LocalMachine, @"Software\test", "foo_value", "bar") { Win64 = is64Reg1 };
                proj.AddRegValue(regval);

                var regkey = new RegKey(null, RegistryHive.LocalMachine, @"Software\My Company\My Product", new RegValue("Message", "Hello"))
                {
                    Win64 = is64Reg2
                };
                proj.AddRegKey(regkey);

                proj.WixSourceGenerated += doc =>
                {
                    var cpmponents = doc.FindAll("Component");
                    var reg = cpmponents.FirstOrDefault(x => x.HasAttribute("Id", "Registry.1"));
                    var reg2 = cpmponents.FirstOrDefault(x => x.HasAttribute("Id", "Registry.2"));
                    var file = cpmponents.FirstOrDefault(x => x.HasAttribute("Id", value => value.StartsWith("Component.setup.cs")));
                    assertHandler(doc, reg, reg2, file);
                };

                proj.BuildMsiCmd();
            }

            assert(projPlatform: null, is64Reg1: true, is64Reg2: true,
                   (doc, reg, reg2, file) =>
                   {
                       Assert.True(reg.HasAttribute("Win64", "yes"));
                       Assert.True(reg2.HasAttribute("Win64", "yes"));
                       Assert.False(file.HasAttribute("Win64", "yes"));
                   });

            assert(Platform.x64, is64Reg1: null, is64Reg2: null,
                   (doc, reg, reg2, file) =>
                   {
                       Assert.True(reg.HasAttribute("Win64", "yes"));
                       Assert.True(reg2.HasAttribute("Win64", "yes"));
                       Assert.True(file.HasAttribute("Win64", "yes"));
                   });

            assert(Platform.x64, is64Reg1: false, is64Reg2: false,
                   (doc, reg, reg2, file) =>
                   {
                       Assert.False(reg.HasAttribute("Win64", "yes"));
                       Assert.False(reg2.HasAttribute("Win64", "yes"));
                       Assert.True(file.HasAttribute("Win64", "yes"));
                   });
            assert(Platform.x64, is64Reg1: false, is64Reg2: false,
                   (doc, reg, reg2, file) =>
                   {
                       Assert.False(reg.HasAttribute("Win64", "yes"));
                       Assert.False(reg2.HasAttribute("Win64", "yes"));
                       Assert.True(file.HasAttribute("Win64", "yes"));
                   });
        }

        [Fact]
        [Description("Issue #995")]
        public void Fix_Issue_995()
        {
            Compiler.AutoGeneration.IgnoreWildCardEmptyDirectories = true;

            try
            {
                var project =
                    new Project("myproduct",
                        new Dir("%Desktop%"),
                        new Dir("%ProgramFiles%"));

                project.BuildWxs();
            }<commit_msg>- fixed #182 u-test syntax; was failing on AppVeyor but was OK locally<commit_after>using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using System.Xml.Linq;
using WixSharp.Bootstrapper;
using WixSharp.CommonTasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

#pragma warning disable MethodDocumentationHeader // The method must have a documentation header.

namespace WixSharp.Test
{
    public class IssueFixesTest : WixLocator
    {
        /// <summary>
        /// Fixes the issue 803.
        /// </summary>
        [Fact]
        [Description("Issue #803")]
        public void Fix_Issue_803()
        {
            // ensure all types that expose their properties for serialization with [XML] have these properties public
            // "SqlDb" and "root" are serializable but not assignable by user
            var classesWithFaultsFields =
                    typeof(Project).Assembly.GetTypes()
                        .SelectMany(t => t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                                          .Where(p => p.Name != "SqlDb")
                                          .Where(p => p.GetCustomAttribute<WixSharp.XmlAttribute>() != null)
                                          .Select(x => $"{x.DeclaringType.Name}.{x.Name}"))
                .Concat(
                    typeof(Project).Assembly.GetTypes()
                        .Where(t => t != typeof(Error))
                        .SelectMany(t => t.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                                          .Where(p => p.Name != "root")
                                          .Where(p => p.GetCustomAttribute<WixSharp.XmlAttribute>() != null)
                                          .Select(x => $"{x.DeclaringType.Name}.{x.Name}")))
                .ToArray();

            Assert.Empty(classesWithFaultsFields);
        }

        [Fact]
        [Description("Issue #1114")]
        public void Fix_Issue_1114()
        {
            var project = new Project("MyProductSetup",
                new Dir(@"%ProgramFiles%\MyCompany\MyProduct\MyApp",
                        new Dir(
                            // new Feature("extra"),
                            @"Logs")));

            project.PreserveTempFiles = true;
            project.WixSourceGenerated += document =>
            {
                var xml = document.ToString();
            };
            project.BuildWxs();
        }

        [Fact]
        [Description("Issue #1171")]
        public void Fix_Issue_1171()
        {
            var tempFile = System.IO.Path.GetTempFileName();
            try
            {
                System.IO.File.WriteAllLines(tempFile, new[]
                {
                    "Windows Registry Editor Version 5.00",
                    "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Test]",
                    "\"New Value #1\"=dword:00000001",
                    "\"New Value #2\"=hex(b):02,00,00,00,00,00,00,00",
                });
                var values = WixSharp.RegFileImporter.ImportFrom(tempFile);
            }
            finally
            {
                tempFile.DeleteIfExists();
            }
        }

        [Fact]
        [Description("Issue #1132")]
        public void Fix_Issue_1132()
        {
            var i = 0;

            var project = new Project("CableScheduler",
                new Dir(@"%ProgramFiles%\CADbloke\CableScheduler",
                new Files($"{Environment.CurrentDirectory}\\*.*", f => i++ < 1)),

                new Dir(@"%ProgramMenu%\CADbloke\CableScheduler",
                        new ExeFileShortcut("Cable Scheduler", $"[INSTALLDIR]Test.exe", "")));

            project.ResolveWildCards(pruneEmptyDirectories: true);

            var wsx = project.BuildWxs();
            var xml = System.IO.File.ReadAllText(wsx);

            Assert.True(xml.Contains("<Shortcut Id="));
        }

        [Fact]
        [Description("Issue #67")]
        public void Fix_Issue_67()
        {
            var project = new Project("MyProduct",
                              new Dir("%ProgramFiles%"));

            project.LicenceFile = "license.rtf";

            var file = project.BuildWxs();
        }

        [Fact]
        [Description("Issue #182")]
        public void Fix_Issue_182()
        {
            void assert(Platform? projPlatform, bool? is64Reg1, bool? is64Reg2, Action<XDocument, XElement, XElement, XElement> assertHandler)
            {
                var proj = new Project("MyProduct",
                                       new Dir(@"%ProgramFiles%\My Company\My Product",
                                           new File("setup.cs")));

                if (projPlatform != null)
                    proj.Platform = projPlatform;

                var regval = new RegValue(RegistryHive.LocalMachine, @"Software\test", "foo_value", "bar") { Win64 = is64Reg1 };
                proj.AddRegValue(regval);

                var regkey = new RegKey(null, RegistryHive.LocalMachine, @"Software\My Company\My Product", new RegValue("Message", "Hello"))
                {
                    Win64 = is64Reg2
                };
                proj.AddRegKey(regkey);

                proj.WixSourceGenerated += doc =>
                {
                    var cpmponents = doc.FindAll("Component");
                    var reg = cpmponents.FirstOrDefault(x => x.HasAttribute("Id", "Registry.1"));
                    var reg2 = cpmponents.FirstOrDefault(x => x.HasAttribute("Id", "Registry.2"));
                    var file = cpmponents.FirstOrDefault(x => x.HasAttribute("Id", value => value.StartsWith("Component.setup.cs")));
                    assertHandler(doc, reg, reg2, file);
                };

                proj.BuildMsiCmd();
            }

            assert(projPlatform: null, is64Reg1: true, is64Reg2: true,
                   (doc, reg, reg2, file) =>
                   {
                       Assert.True(reg.HasAttribute("Win64", "yes"));
                       Assert.True(reg2.HasAttribute("Win64", "yes"));
                       Assert.False(file.HasAttribute("Win64", "yes"));
                   });

            assert(projPlatform: Platform.x64, is64Reg1: null, is64Reg2: null,
                   (doc, reg, reg2, file) =>
                   {
                       Assert.True(reg.HasAttribute("Win64", "yes"));
                       Assert.True(reg2.HasAttribute("Win64", "yes"));
                       Assert.True(file.HasAttribute("Win64", "yes"));
                   });

            assert(projPlatform: Platform.x64, is64Reg1: false, is64Reg2: false,
                   (doc, reg, reg2, file) =>
                   {
                       Assert.False(reg.HasAttribute("Win64", "yes"));
                       Assert.False(reg2.HasAttribute("Win64", "yes"));
                       Assert.True(file.HasAttribute("Win64", "yes"));
                   });
        }

        [Fact]
        [Description("Issue #995")]
        public void Fix_Issue_995()
        {
            Compiler.AutoGeneration.IgnoreWildCardEmptyDirectories = true;

            try
            {
                var project =
                    new Project("myproduct",
                        new Dir("%Desktop%"),
                        new Dir("%ProgramFiles%"));

                project.BuildWxs();
            }
{'fraction_non_alphanumeric': 0.12005474809359316, 'fraction_numerical': 0.02026982988985205, 'mean_word_length': 1.4371029224904701, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3094567', 'n_tokens_mistral': 4232, 'n_tokens_neox': 3803, 'n_words': 761}