Beispiel #1
0
    public void ShouldOptOutOfXsdThroughMSBuildProperty()
    {
        File.WriteAllText(xmlPath, @"
<Weavers>
  <TestWeaver />
</Weavers>
");

        var weavers = new[] { new WeaverEntry {
                                  AssemblyPath = @"something\TestWeaver.Fody.dll"
                              } };

        var configFiles = ConfigFileFinder.FindWeaverConfigFiles(Guid.NewGuid().ToString(), testDir, new Mock <BuildLogger>().Object).ToArray();

        ConfigFileFinder.EnsureSchemaIsUpToDate(testDir, weavers, false);

        Assert.Single(configFiles);
        Assert.Equal(xmlPath, configFiles[0].FilePath);

        Assert.False(File.Exists(xsdPath));

        var xml = XDocumentEx.Load(xmlPath);

        Assert.NotNull(xml.Root);
        Assert.Null(xml.Root.Attribute(schemaInstanceNamespace + "noNamespaceSchemaLocation"));
    }
Beispiel #2
0
    public void XmlConfigShouldOverrideMSBuildPropertyForXsdGeneration()
    {
        File.WriteAllText(xmlPath, @"
<Weavers GenerateXsd=""true"">
  <TestWeaver />
</Weavers>
");

        var weavers = new[] { new WeaverEntry {
                                  AssemblyPath = @"something\TestWeaver.Fody.dll"
                              } };

        var configs = ConfigFileFinder.FindWeaverConfigFiles(Guid.NewGuid().ToString(), testDir, new Mock <BuildLogger>().Object).ToArray();

        ConfigFileFinder.EnsureSchemaIsUpToDate(testDir, weavers, false);

        Assert.Single(configs);
        Assert.Equal(xmlPath, configs[0].FilePath);

        Assert.True(File.Exists(xsdPath));

        var xml = XDocumentEx.Load(xmlPath);

        Assert.NotNull(xml.Root);
        Assert.Equal("FodyWeavers.xsd", xml.Root.Attribute(schemaInstanceNamespace + "noNamespaceSchemaLocation")?.Value);
    }
Beispiel #3
0
    void Inner()
    {
        ValidateSolutionPath();
        ValidateProjectPath();
        ValidateAssemblyPath();

        ConfigFiles = ConfigFileFinder.FindWeaverConfigFiles(SolutionDirectory, ProjectDirectory, Logger).ToList();

        if (!ConfigFiles.Any())
        {
            ConfigFiles = new List <WeaverConfigFile>
            {
                ConfigFileFinder.GenerateDefault(ProjectDirectory, Weavers, GenerateXsd)
            };
            Logger.LogWarning($"Could not find a FodyWeavers.xml file at the project level ({ProjectDirectory}). A default file has been created. Please review the file and add it to your project.");
        }

        ConfigEntries = ConfigFileFinder.ParseWeaverConfigEntries(ConfigFiles);

        var extraEntries = ConfigEntries.Values
                           .Where(entry => !entry.ConfigFile.IsGlobal && !Weavers.Any(weaver => string.Equals(weaver.ElementName, entry.ElementName)))
                           .ToArray();

        const string missingWeaversHelp = "Add the desired weavers via their nuget package.";

        if (extraEntries.Any())
        {
            throw new WeavingException($"No weavers found for the configuration entries {string.Join(", ", extraEntries.Select(e => e.ElementName))}. " + missingWeaversHelp);
        }

        if (Weavers.Count == 0)
        {
            throw new WeavingException("No weavers found. " + missingWeaversHelp);
        }

        foreach (var weaver in Weavers)
        {
            if (ConfigEntries.TryGetValue(weaver.ElementName, out var config))
            {
                weaver.Element        = config.Content;
                weaver.ExecutionOrder = config.ExecutionOrder;
            }
            else
            {
                Logger.LogWarning($"No configuration entry found for the installed weaver {weaver.ElementName}. This weaver will be skipped. You may want to add this weaver to your FodyWeavers.xml");
            }
        }

        ConfigFileFinder.EnsureSchemaIsUpToDate(SolutionDirectory, ProjectDirectory, Weavers, GenerateXsd);

        Weavers = Weavers
                  .Where(weaver => weaver.Element != null)
                  .OrderBy(weaver => weaver.ExecutionOrder)
                  .ToList();

        lock (mutex)
        {
            ExecuteInOwnAssemblyLoadContext();
        }
    }
Beispiel #4
0
    public void ShouldNotCreateXsd_OnlySolutionWideConfig()
    {
        // Deliberately not writing the file in the project dir.
        if (File.Exists(xmlPath))
        {
            File.Delete(xmlPath);
        }
        File.WriteAllText(slnXmlPath, @"
<Weavers>
  <TestWeaver />
</Weavers>
");

        var weavers = new[]
        {
            new WeaverEntry
            {
                AssemblyPath = @"something\TestWeaver.Fody.dll"
            }
        };

        var configFiles = ConfigFileFinder.FindWeaverConfigFiles(slnDir, testDir, new MockBuildLogger()).ToArray();

        ConfigFileFinder.EnsureSchemaIsUpToDate(slnDir, testDir, weavers, true);

        Assert.Single(configFiles);
        Assert.Equal(slnXmlPath, configFiles[0].FilePath);

        Assert.False(File.Exists(slnXsdPath));

        var xml = XDocumentEx.Load(slnXmlPath);

        Assert.NotNull(xml.Root);
        Assert.Null(xml.Root.Attribute(schemaInstanceNamespace + "noNamespaceSchemaLocation"));
    }
Beispiel #5
0
    public void ShouldOptOutOfXsd()
    {
        File.WriteAllText(xmlPath, @"
<Weavers GenerateXsd=""false"">
  <TestWeaver />
</Weavers>
");

        var weavers = new[]
        {
            new WeaverEntry
            {
                AssemblyPath = @"something\TestWeaver.Fody.dll"
            }
        };

        var configFiles = ConfigFileFinder.FindWeaverConfigFiles(slnDir, testDir, new MockBuildLogger()).ToArray();

        ConfigFileFinder.EnsureSchemaIsUpToDate(slnDir, testDir, weavers, true);

        Assert.Single(configFiles);
        Assert.Equal(xmlPath, configFiles[0].FilePath);

        Assert.False(File.Exists(xsdPath));

        var xml = XDocumentEx.Load(xmlPath);

        Assert.NotNull(xml.Root);
        Assert.Null(xml.Root.Attribute(schemaInstanceNamespace + "noNamespaceSchemaLocation"));
    }
Beispiel #6
0
    public void NotFound()
    {
        var logger = new MockBuildLogger();

        var configFiles = ConfigFileFinder.FindWeaverConfigFiles(null, Environment.CurrentDirectory, Environment.CurrentDirectory, logger);

        Assert.Empty(configFiles);
    }
    public void NotFound()
    {
        var logger = new Mock <BuildLogger>(MockBehavior.Loose).Object;

        var configFiles = ConfigFileFinder.FindWeaverConfigFiles(Environment.CurrentDirectory, Environment.CurrentDirectory, logger);

        Assert.Empty(configFiles);
    }
Beispiel #8
0
    public bool ReadShouldVerifyAssembly(out List <string> ignoreCodes)
    {
        var weaverConfigs = ConfigFileFinder.FindWeaverConfigFiles(SolutionDirectory, ProjectDirectory, Logger).ToList();

        ignoreCodes = ExtractVerifyIgnoreCodesConfigs(weaverConfigs).ToList();

        if (DefineConstants.Any(x => x == "FodyVerifyAssembly"))
        {
            return(true);
        }

        return(ExtractVerifyAssemblyFromConfigs(weaverConfigs));
    }
Beispiel #9
0
    public void ShouldCreateXsd()
    {
        File.WriteAllText(xmlPath, @"
<Weavers>
  <TestWeaver />
</Weavers>
");

        File.WriteAllText(Path.Combine(testDir, "WeaverWithSchema.Fody.xcf"), @"
<xs:complexType xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
  <xs:attribute name=""TestAttribute"" type=""xs:string"" />
</xs:complexType>
");

        var weavers = new[]
        {
            new WeaverEntry {
                AssemblyPath = @"something\TestWeaver.Fody.dll"
            },
            new WeaverEntry {
                AssemblyPath = Path.Combine(testDir, "WeaverWithSchema.Fody.dll")
            }
        };

        var configFiles = ConfigFileFinder.FindWeaverConfigFiles(Guid.NewGuid().ToString(), testDir, new Mock <BuildLogger>().Object).ToArray();

        ConfigFileFinder.EnsureSchemaIsUpToDate(testDir, weavers, true);

        Assert.Single(configFiles);
        Assert.False(configFiles[0].IsGlobal);
        Assert.Equal(xmlPath, configFiles[0].FilePath);

        Assert.True(File.Exists(xsdPath));

        var xml = XDocumentEx.Load(xmlPath);

        Assert.NotNull(xml.Root);
        Assert.Equal("FodyWeavers.xsd", xml.Root.Attribute(schemaInstanceNamespace + "noNamespaceSchemaLocation")?.Value);

        var xsd = XDocumentEx.Load(xsdPath);

        Assert.NotNull(xsd.Root);
        var elements = xsd.Root.Descendants(schemaNamespace + "all").First().Elements().ToList();

        Assert.Equal(2, elements.Count);

        var defaultElem = elements[0];

        Assert.Equal("element", defaultElem.Name.LocalName);
        Assert.Equal("TestWeaver", defaultElem.Attribute("name")?.Value);
        Assert.Equal("xs:anyType", defaultElem.Attribute("type")?.Value);
        Assert.Equal("0", defaultElem.Attribute("minOccurs")?.Value);
        Assert.Equal("1", defaultElem.Attribute("maxOccurs")?.Value);

        var elemWithSchema = elements[1];

        Assert.Equal("element", elemWithSchema.Name.LocalName);
        Assert.Equal("WeaverWithSchema", elemWithSchema.Attribute("name")?.Value);
        Assert.Null(elemWithSchema.Attribute("type"));
        Assert.Equal("0", elemWithSchema.Attribute("minOccurs")?.Value);
        Assert.Equal("1", elemWithSchema.Attribute("maxOccurs")?.Value);

        var elemWithSchemaType = Assert.Single(elemWithSchema.Elements());

        Assert.NotNull(elemWithSchemaType);
        Assert.Equal("complexType", elemWithSchemaType.Name.LocalName);

        var elemWithSchemaTypeAttr = Assert.Single(elemWithSchemaType.Elements());

        Assert.NotNull(elemWithSchemaTypeAttr);
        Assert.Equal("attribute", elemWithSchemaTypeAttr.Name.LocalName);
        Assert.Equal("TestAttribute", elemWithSchemaTypeAttr.Attribute("name")?.Value);
    }
Beispiel #10
0
    void Inner()
    {
        ValidateSolutionPath();
        ValidateProjectPath();
        ValidateAssemblyPath();

        ConfigFiles = ConfigFileFinder.FindWeaverConfigFiles(SolutionDirectory, ProjectDirectory, Logger).ToList();

        if (!ConfigFiles.Any())
        {
            ConfigFiles = new List <WeaverConfigFile>
            {
                ConfigFileFinder.GenerateDefault(ProjectDirectory, Weavers, GenerateXsd)
            };
            Logger.LogWarning($"Could not find a FodyWeavers.xml file at the project level ({ProjectDirectory}). A default file has been created. Please review the file and add it to your project.");
        }

        ConfigEntries = ConfigFileFinder.ParseWeaverConfigEntries(ConfigFiles);

        var extraEntries = ConfigEntries.Values
                           .Where(entry => !entry.ConfigFile.IsGlobal && !Weavers.Any(weaver => string.Equals(weaver.ElementName, entry.ElementName)))
                           .ToArray();

        const string missingWeaversHelp = "Add the desired weavers via their nuget package; see https://github.com/Fody/Fody/wiki on how to migrate InSolution, custom or legacy weavers.";

        if (extraEntries.Any())
        {
            throw new WeavingException($"No weavers found for the configuration entries {string.Join(", ", extraEntries.Select(e => e.ElementName))}. " + missingWeaversHelp);
        }

        if (Weavers.Count == 0)
        {
            throw new WeavingException("No weavers found. " + missingWeaversHelp);
        }

        foreach (var weaver in Weavers)
        {
            if (ConfigEntries.TryGetValue(weaver.ElementName, out var config))
            {
                weaver.Element        = config.Content;
                weaver.ExecutionOrder = config.ExecutionOrder;
            }
            else
            {
                Logger.LogWarning($"No configuration entry found for the installed weaver {weaver.ElementName}. This weaver will be skipped. You may want to add this weaver to your FodyWeavers.xml");
            }
        }

        ConfigFileFinder.EnsureSchemaIsUpToDate(ProjectDirectory, Weavers, GenerateXsd);

        Weavers = Weavers
                  .Where(weaver => weaver.Element != null)
                  .OrderBy(weaver => weaver.ExecutionOrder)
                  .ToList();

        if (TargetAssemblyHasAlreadyBeenProcessed())
        {
            if (WeaversConfigHistory.HasChanged(ConfigFiles) || WeaversHistory.HasChanged(Weavers.Select(x => x.AssemblyPath)))
            {
                Logger.LogError("A re-build is required because a weaver has changed.");

                return;
            }
        }

        lock (mutex)
        {
            ExecuteInOwnAssemblyLoadContext();
        }

        WeaversConfigHistory.RegisterSnapshot(ConfigFiles);
    }