Example #1
0
        public void CommandLineIdPropertyOverridesAssemblyNameForPreprocessor()
        {
            // Arrange
            using (var workingDirectory = TestDirectory.Create())
            {
                var          testAssembly = Assembly.GetExecutingAssembly();
                const string inputSpec    = @"<?xml version=""1.0""?>
	<package>
	    <metadata>
	        <id>$id$</id>
	        <version>$version$</version>
	        <description>$description$</description>
	        <authors>Outercurve</authors>
	        <copyright>$copyright$</copyright>
	        <licenseUrl>https://github.com/NuGet/NuGet.Client/blob/master/LICENSE.txt</licenseUrl>
	        <projectUrl>https://github.com/NuGet/NuGet.Client</projectUrl>
	        <tags>nuget</tags>
	    </metadata>
	</package>"    ;
                var          projectXml   = @"<?xml version=""1.0"" encoding=""utf-8""?>
	<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
	    <PropertyGroup>
	        <ProjectGuid>{F879F274-EFA0-4157-8404-33A19B4E6AEC}</ProjectGuid>
	        <OutputType>Library</OutputType>
	        <RootNamespace>NuGet.Test</RootNamespace>
	        <AssemblyName>"     + testAssembly.GetName().Name + @"</AssemblyName>
	        <TargetFrameworkProfile Condition="" '$(TargetFrameworkVersion)' == 'v4.0' "">Client</TargetFrameworkProfile>    
	        <OutputPath>.</OutputPath> <!-- Force it to look for the assembly in the base path -->
	        <TargetPath>"     + testAssembly.ManifestModule.FullyQualifiedName + @"</TargetPath>
	    </PropertyGroup>
	    
	    <ItemGroup>
	        <Compile Include=""..\..\Dummy.cs"">
	          <Link>Dummy.cs</Link>
	        </Compile>
	    </ItemGroup>
	 
	    <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
	</Project>"    ;

                // Set base path to the currently assembly's folder so that it will find the test assembly
                var basePath          = Path.GetDirectoryName(testAssembly.CodeBase);
                var cmdLineProperties = new Dictionary <string, string>
                {
                    { "id", "Outercurve" }
                };
                var projectPath = Path.Combine(workingDirectory, "test.csproj");
                File.WriteAllText(projectPath, projectXml);

                var msbuildPath = Util.GetMsbuildPathOnWindows();
                if (RuntimeEnvironmentHelper.IsMono && Util.IsRunningOnMac())
                {
                    msbuildPath = @"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/msbuild/15.0/bin/";
                }
                var factory = new ProjectFactory(msbuildPath, projectPath, cmdLineProperties)
                {
                    Build = false
                };
                // Cmdline properties are added to the factory, see PackCommand.cs
                factory.ProjectProperties.AddRange(cmdLineProperties);

                // Act
                var packageBuilder = factory.CreateBuilder(basePath, new NuGetVersion("3.0.0"), "", true);
                var actual         = Preprocessor.Process(inputSpec.AsStream(), factory, false);

                var xdoc = XDocument.Load(new StringReader(actual));
                Assert.Equal(cmdLineProperties["id"], xdoc.XPathSelectElement("/package/metadata/id").Value);
            }
        }
Example #2
0
        public void CommandLineVersionPopulatesVersionPlaceholderForDependenciesInNuspec(string version)
        {
            // Arrange
            // Arrange
            using (var workingDirectory = TestDirectory.Create())
            {
                var          testAssembly = Assembly.GetExecutingAssembly();
                const string inputSpec    = @"<?xml version=""1.0""?>
	<package>
	    <metadata>
	        <id>$id$</id>
	        <version>$version$</version>
	        <description>$description$</description>
	        <authors>Outercurve</authors>
	        <copyright>$copyright$</copyright>
	        <licenseUrl>https://aka.ms/nugetlicense</licenseUrl>
	        <projectUrl>https://aka.ms/nugetprj</projectUrl>
	        <tags>nuget</tags>
            <dependencies>
                <dependency id=""NeverGonnaGiveYouUp"" version =""$version$"" />
            </dependencies>
        </metadata>
	</package>"    ;
                var          projectXml   = @"<?xml version=""1.0"" encoding=""utf-8""?>
	<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
	    <PropertyGroup>
	        <ProjectGuid>{F879F274-EFA0-4157-8404-33A19B4E6AEC}</ProjectGuid>
	        <OutputType>Library</OutputType>
	        <RootNamespace>NuGet.Test</RootNamespace>
	        <AssemblyName>"     + testAssembly.GetName().Name + @"</AssemblyName>
	        <TargetFrameworkProfile Condition="" '$(TargetFrameworkVersion)' == 'v4.0' "">Client</TargetFrameworkProfile>    
	        <OutputPath>.</OutputPath> <!-- Force it to look for the assembly in the base path -->
	        <TargetPath>"     + testAssembly.ManifestModule.FullyQualifiedName + @"</TargetPath>
	    </PropertyGroup>
	    
	    <ItemGroup>
	        <Compile Include=""..\..\Dummy.cs"">
	          <Link>Dummy.cs</Link>
	        </Compile>
	    </ItemGroup>
	 
	    <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
	</Project>"    ;

                // Version that we expect to be replaced
                var cmdLineVersion = NuGetVersion.Parse(version);

                // Set base path to the currently assembly's folder so that it will find the test assembly
                var basePath    = Path.GetDirectoryName(testAssembly.CodeBase);
                var projectPath = Path.Combine(workingDirectory, $"test-{version}.csproj");
                File.WriteAllText(projectPath, projectXml);

                // Act
                var msbuildPath = Util.GetMsbuildPathOnWindows();
                var factory     = new ProjectFactory(msbuildPath, projectPath, null)
                {
                    Build = false
                };
                var packageBuilder = factory.CreateBuilder(basePath, cmdLineVersion, "", true);
                var actual         = Preprocessor.Process(inputSpec.AsStream(), factory, false);

                var xdoc = XDocument.Load(new StringReader(actual));
                Assert.Equal(testAssembly.GetName().Name, xdoc.XPathSelectElement("/package/metadata/id").Value);
                Assert.Equal(cmdLineVersion.ToString(), xdoc.XPathSelectElement("/package/metadata/version").Value);
                Assert.Equal(cmdLineVersion.ToString(), xdoc.XPathSelectElement("/package/metadata/dependencies/dependency").Attribute("version").Value);
            }
        }
Example #3
0
        public void ProjectFactoryInitializesPropertiesForPreprocessorFromAssemblyMetadata()
        {
            // Arrange
            var          testAssembly = Assembly.GetExecutingAssembly();
            const string inputSpec    = @"<?xml version=""1.0""?>
<package>
    <metadata>
        <id>$id$</id>
        <version>$version$</version>
        <description>$description$</description>
        <authors>$owner$</authors>
        <copyright>$copyright$</copyright>
        <licenseUrl>http://nuget.codeplex.com/license</licenseUrl>
        <projectUrl>http://nuget.codeplex.com</projectUrl>
        <tags>nuget</tags>
    </metadata>
</package>";
            var          projectXml   = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
    <PropertyGroup>
        <ProjectGuid>{F879F274-EFA0-4157-8404-33A19B4E6AEC}</ProjectGuid>
        <OutputType>Library</OutputType>
        <RootNamespace>NuGet.Test</RootNamespace>
        <AssemblyName>" + testAssembly.GetName().Name + @"</AssemblyName>
        <TargetFrameworkProfile Condition="" '$(TargetFrameworkVersion)' == 'v4.0' "">Client</TargetFrameworkProfile>    
        <OutputPath>.</OutputPath> <!-- Force it to look for the assembly in the base path -->
    </PropertyGroup>
    
    <ItemGroup>
        <Compile Include=""..\..\Dummy.cs"">
          <Link>Dummy.cs</Link>
        </Compile>
    </ItemGroup>

    <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
</Project>";

            // Set base path to the currently assembly's folder so that it will find the test assembly
            var basePath = Path.GetDirectoryName(testAssembly.CodeBase);

            var project = new Project(XmlReader.Create(new StringReader(projectXml)));

            project.FullPath = Path.Combine(project.DirectoryPath, "test.csproj");

            // Act
            var factory = new ProjectFactory(project)
            {
                Build = false
            };
            var packageBuilder = factory.CreateBuilder(basePath);
            var actual         = Preprocessor.Process(inputSpec.AsStream(), factory, false);

            // assert
            var expected = @"<?xml version=""1.0""?>
<package>
    <metadata>
        <id>" + testAssembly.GetName().Name + @"</id>
        <version>" + testAssembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>().InformationalVersion + @"</version>
        <description></description>
        <authors>Outercurve</authors>
        <copyright>" + testAssembly.GetCustomAttribute <AssemblyCopyrightAttribute>().Copyright + @"</copyright>
        <licenseUrl>http://nuget.codeplex.com/license</licenseUrl>
        <projectUrl>http://nuget.codeplex.com</projectUrl>
        <tags>nuget</tags>
    </metadata>
</package>";

            Assert.Equal(expected, actual);
        }
Example #4
0
        public void ProjectFactoryInitializesPropertiesForPreprocessorFromAssemblyMetadata()
        {
            // Arrange
            using (var workingDirectory = TestDirectory.Create())
            {
                var          testAssembly = Assembly.GetExecutingAssembly();
                const string inputSpec    = @"<?xml version=""1.0""?>
	<package>
	    <metadata>
	        <id>$id$</id>
	        <version>$version$</version>
	        <description>$description$</description>
	        <authors>$owner$</authors>
	        <copyright>$copyright$</copyright>
	        <licenseUrl>https://aka.ms/nugetlicense</licenseUrl>
	        <projectUrl>https://aka.ms/nugetprj</projectUrl>
	        <tags>nuget</tags>
	    </metadata>
	</package>"    ;
                var          projectXml   = @"<?xml version=""1.0"" encoding=""utf-8""?>
	<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
	    <PropertyGroup>
	        <ProjectGuid>{F879F274-EFA0-4157-8404-33A19B4E6AEC}</ProjectGuid>
	        <OutputType>Library</OutputType>
	        <RootNamespace>NuGet.Test</RootNamespace>
	        <AssemblyName>"     + testAssembly.GetName().Name + @"</AssemblyName>
	        <TargetFrameworkProfile Condition="" '$(TargetFrameworkVersion)' == 'v4.0' "">Client</TargetFrameworkProfile>    
	        <OutputPath>.</OutputPath> <!-- Force it to look for the assembly in the base path -->
	        <TargetPath>"     + testAssembly.ManifestModule.FullyQualifiedName + @"</TargetPath>
	    </PropertyGroup>
	    
	    <ItemGroup>
	        <Compile Include=""..\..\Dummy.cs"">
	          <Link>Dummy.cs</Link>
	        </Compile>
	    </ItemGroup>
	 
	    <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
	</Project>"    ;

                // Set base path to the currently assembly's folder so that it will find the test assembly
                var basePath    = Path.GetDirectoryName(testAssembly.CodeBase);
                var projectPath = Path.Combine(workingDirectory, "test.csproj");
                File.WriteAllText(projectPath, projectXml);

                // Act
                var msbuildPath = Util.GetMsbuildPathOnWindows();
                if (RuntimeEnvironmentHelper.IsMono && RuntimeEnvironmentHelper.IsMacOSX)
                {
                    msbuildPath = @"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/msbuild/15.0/bin/";
                }
                var factory = new ProjectFactory(msbuildPath, projectPath, null)
                {
                    Build = false
                };
                var packageBuilder = factory.CreateBuilder(basePath, null, "", true);
                var actual         = Preprocessor.Process(inputSpec.AsStream(), factory, false);

                var xdoc = XDocument.Load(new StringReader(actual));
                Assert.Equal(testAssembly.GetName().Name, xdoc.XPathSelectElement("/package/metadata/id").Value);
                Assert.Equal(testAssembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>().InformationalVersion, xdoc.XPathSelectElement("/package/metadata/version").Value);
                Assert.Equal("NuGet client library.", xdoc.XPathSelectElement("/package/metadata/description").Value);
                Assert.Equal(testAssembly.GetCustomAttribute <AssemblyCopyrightAttribute>().Copyright, xdoc.XPathSelectElement("/package/metadata/copyright").Value);
                Assert.Equal(
                    testAssembly.GetCustomAttributes <AssemblyMetadataAttribute>()
                    .Where(attr => attr.Key == "owner")
                    .Select(attr => attr.Value)
                    .FirstOrDefault() ?? "",
                    xdoc.XPathSelectElement("/package/metadata/authors").Value);
            }
        }
Example #5
0
        public void CommandLinePropertiesOverrideAssemblyMetadataForPreprocessor()
        {
            // Arrange
            var          testAssembly = Assembly.GetExecutingAssembly();
            const string inputSpec    = @"<?xml version=""1.0""?>
	<package>
	    <metadata>
	        <id>$id$</id>
	        <version>$version$</version>
	        <description>$description$</description>
	        <authors>$owner$</authors>
	        <copyright>$copyright$</copyright>
	        <licenseUrl>https://github.com/NuGet/NuGet.Client/blob/master/LICENSE.txt</licenseUrl>
	        <projectUrl>https://github.com/NuGet/NuGet.Client</projectUrl>
	        <tags>nuget</tags>
	    </metadata>
	</package>"    ;
            var          projectXml   = @"<?xml version=""1.0"" encoding=""utf-8""?>
	<Project ToolsVersion=""4.0"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
	    <PropertyGroup>
	        <ProjectGuid>{F879F274-EFA0-4157-8404-33A19B4E6AEC}</ProjectGuid>
	        <OutputType>Library</OutputType>
	        <RootNamespace>NuGet.Test</RootNamespace>
	        <AssemblyName>"     + testAssembly.GetName().Name + @"</AssemblyName>
	        <TargetFrameworkProfile Condition="" '$(TargetFrameworkVersion)' == 'v4.0' "">Client</TargetFrameworkProfile>    
	        <OutputPath>.</OutputPath> <!-- Force it to look for the assembly in the base path -->
	        <TargetPath>"     + testAssembly.ManifestModule.FullyQualifiedName + @"</TargetPath>
	    </PropertyGroup>
	    
	    <ItemGroup>
	        <Compile Include=""..\..\Dummy.cs"">
	          <Link>Dummy.cs</Link>
	        </Compile>
	    </ItemGroup>
	 
	    <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
	</Project>"    ;

            // Set base path to the currently assembly's folder so that it will find the test assembly
            var basePath          = Path.GetDirectoryName(testAssembly.CodeBase);
            var cmdLineProperties = new Dictionary <string, string>
            {
                { "owner", "overriden" }
            };
            var project = new Project(XmlReader.Create(new StringReader(projectXml)), cmdLineProperties, null);

            project.FullPath = Path.Combine(project.DirectoryPath, "test.csproj");

            var factory = new ProjectFactory(@"C:\Program Files (x86)\MSBuild\14.0\Bin", project)
            {
                Build = false
            };

            // Cmdline properties are added to the factory, see PackCommand.cs(351)
            factory.ProjectProperties["owner"] = "overriden";

            // Act
            var packageBuilder = factory.CreateBuilder(basePath, new NuGetVersion("3.0.0"), null, true);
            var actual         = Preprocessor.Process(inputSpec.AsStream(), factory, false);

            var xdoc = XDocument.Load(new StringReader(actual));

            Assert.Equal(testAssembly.GetName().Name, xdoc.XPathSelectElement("/package/metadata/id").Value);
            Assert.Equal(testAssembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>().InformationalVersion, xdoc.XPathSelectElement("/package/metadata/version").Value);
            Assert.Equal("", xdoc.XPathSelectElement("/package/metadata/description").Value);
            Assert.Equal(testAssembly.GetCustomAttribute <AssemblyCopyrightAttribute>().Copyright, xdoc.XPathSelectElement("/package/metadata/copyright").Value);
            Assert.Equal(
                cmdLineProperties["owner"],
                xdoc.XPathSelectElement("/package/metadata/authors").Value);
        }