Example #1
0
        public void BuildAssetsUtils_GenerateProjectRelativeAssetsFilePathInOtherDir()
        {
            using (var workingDir = TestDirectory.Create())
            {
                // Arrange
                var filePath   = Path.Combine(workingDir, "obj", "test.props");
                var assetsPath = Path.Combine(workingDir, "nuget", "project.assets.json");

                var doc  = BuildAssetsUtils.GenerateEmptyImportsFile();
                var file = new MSBuildOutputFile(filePath, doc);

                // Act
                BuildAssetsUtils.AddNuGetPropertiesToFirstImport(
                    new[] { file },
                    Enumerable.Empty <string>(),
                    string.Empty,
                    ProjectStyle.PackageReference,
                    assetsPath,
                    success: true);

                var props = TargetsUtility.GetMSBuildProperties(doc);

                Assert.Equal("$(MSBuildThisFileDirectory)project.assets.json".Replace('/', Path.DirectorySeparatorChar), props["ProjectAssetsFile"]);
            }
        }
Example #2
0
        public void AddNuGetProperty_WithPackageFolders_AddsSourceRootItem()
        {
            // Arrange
            var separator      = Path.DirectorySeparatorChar;
            var packageFolders = $"{separator}tmp{separator}gpf;{separator}tmp{separator}fallbackFolder";

            var doc = BuildAssetsUtils.GenerateEmptyImportsFile();

            // Act
            BuildAssetsUtils.AddNuGetProperties(
                doc,
                packageFolders: packageFolders.Split(';'),
                string.Empty,
                ProjectStyle.PackageReference,
                "/tmp/test/project.assets.json",
                success: true);

            var props           = TargetsUtility.GetMSBuildProperties(doc);
            var items           = TargetsUtility.GetMSBuildItems(doc);
            var sourceRootItems = items.Where(e => e.Item1.Equals("SourceRoot")).Select(e => e.Item2).ToList();

            // Assert
            Assert.Equal(packageFolders, props["NuGetPackageFolders"]);
            Assert.Equal(2, items.Count);
            Assert.Equal(2, sourceRootItems.Count);
            Assert.Equal($"{separator}tmp{separator}gpf{separator}", sourceRootItems[0]["Include"]);
            Assert.Equal($"{separator}tmp{separator}fallbackFolder{separator}", sourceRootItems[1]["Include"]);
        }
Example #3
0
        public void HasChanges_WhenXDocumentDoesNotMatchFileContent_Success()
        {
            // Arrange
            using (var randomProjectDirectory = TestDirectory.Create())
            {
                var filePath       = Path.Combine(randomProjectDirectory, "propsXML.xml");
                var internalSubset = @"<!ENTITY greeting ""Hello"">	
   <!ENTITY name ""NuGet Client "">	
   <!ENTITY sayhello ""&greeting; &name;"">";
                var newValue       = "&sayhello;";

                XDocument doc = BuildAssetsUtils.GenerateEmptyImportsFile();
                doc.Save(filePath);

                XDocument newdoc = XDocument.Load(filePath);
                newdoc.AddFirst(new XDocumentType("Project", null, null, internalSubset));
                XAttribute attribute = newdoc.Root.Attributes(XName.Get("ToolsVersion")).FirstOrDefault();
                attribute.Value = newValue;
                newdoc.Save(filePath);

                string newxml = File.ReadAllText(filePath);
                File.WriteAllText(filePath, newxml.Replace("&amp;", "&"));

                //Act & Assert
                Assert.True(BuildAssetsUtils.HasChanges(doc, filePath, NullLogger.Instance));
            }
        }
        public void BuildAssetsUtils_GenerateMSBuildAllProjectsProperty()
        {
            // Arrange & Act
            var doc = BuildAssetsUtils.GenerateEmptyImportsFile();

            var props = TargetsUtility.GetMSBuildProperties(doc);

            // Assert
            Assert.Equal("$(MSBuildAllProjects);$(MSBuildThisFileFullPath)", props["MSBuildAllProjects"]);
        }
        public void HasChanges_WhenXDocumentMatchesFileContent_Fails()
        {
            // Arrange
            using (var randomProjectDirectory = TestDirectory.Create())
            {
                var       filePath = Path.Combine(randomProjectDirectory, "propsXML.xml");
                XDocument doc      = BuildAssetsUtils.GenerateEmptyImportsFile();
                doc.Save(filePath);

                //Act
                bool result = BuildAssetsUtils.HasChanges(doc, filePath, NullLogger.Instance);

                Assert.False(result);
            }
        }
        public void BuildAssetsUtils_GenerateProjectAssetsFilePath()
        {
            // Arrange
            var path = "/tmp/test/project.assets.json";

            var doc = BuildAssetsUtils.GenerateEmptyImportsFile();

            // Act
            BuildAssetsUtils.AddNuGetProperties(
                doc,
                Enumerable.Empty <string>(),
                string.Empty,
                ProjectStyle.PackageReference,
                path,
                success: true);

            var props = TargetsUtility.GetMSBuildProperties(doc);

            // Assert
            Assert.Equal(path, props["ProjectAssetsFile"]);
        }
        public void BuildAssetsUtils_ReplaceWithUserProfileMacro()
        {
            // Arrange
            using (var randomProjectDirectory = TestDirectory.Create())
            {
                var globalPackagesFolder = SettingsUtility.GetGlobalPackagesFolder(NullSettings.Instance);

                if (!string.IsNullOrEmpty(globalPackagesFolder))
                {
                    // Act
                    var xml = BuildAssetsUtils.GenerateEmptyImportsFile();

                    BuildAssetsUtils.AddNuGetProperties(
                        xml,
                        new[] { globalPackagesFolder },
                        globalPackagesFolder,
                        ProjectStyle.PackageReference,
                        assetsFilePath: string.Empty,
                        success: true);

                    // Assert
                    var ns       = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
                    var elements = xml.Root.Descendants(ns + "NuGetPackageRoot");
                    Assert.Single(elements);

                    var    element  = elements.Single();
                    string expected = null;

                    if (RuntimeEnvironmentHelper.IsWindows)
                    {
                        expected = Path.Combine(@"$(UserProfile)", ".nuget", "packages") + Path.DirectorySeparatorChar;
                    }
                    else
                    {
                        expected = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".nuget", "packages") + Path.DirectorySeparatorChar;
                    }
                    Assert.Equal(expected, element.Value);
                }
            }
        }
        public void AddNuGetProperty_WithPackageFolders_AddsSourceRootItem()
        {
            // Arrange
            var packageFolders = @"/tmp/gpf;/tmp/fallbackFolder";

            var doc = BuildAssetsUtils.GenerateEmptyImportsFile();

            // Act
            BuildAssetsUtils.AddNuGetProperties(
                doc,
                packageFolders: packageFolders.Split(';'),
                string.Empty,
                ProjectStyle.PackageReference,
                "/tmp/test/project.assets.json",
                success: true);

            var props = TargetsUtility.GetMSBuildProperties(doc);
            var items = TargetsUtility.GetMSBuildItems(doc);

            // Assert
            Assert.Equal(packageFolders, props["NuGetPackageFolders"]);
            Assert.Equal(1, items.Count);
            Assert.Equal("$([MSBuild]::EnsureTrailingSlash($(NuGetPackageFolders)))", items["SourceRoot"]["Include"]);
        }