Esempio n. 1
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"]);
        }
        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"]);
        }