Beispiel #1
0
        public void GetMsbuildDirectoryFromPath_PATHENVWithQuotes_Succeeds()
        {
            if (RuntimeEnvironmentHelper.IsMono)
            { // Mono does not have SxS installations so it's not relevant to get msbuild from the path.
                return;
            }

            using (var vsPath = TestDirectory.Create())
            {
                var msBuild160BinDir  = Directory.CreateDirectory(Path.Combine(vsPath, "MSBuild", "16.0", "Bin"));
                var msBuild160BinPath = msBuild160BinDir.FullName;
                var msBuild160ExePath = Path.Combine(msBuild160BinPath, "msbuild.exe").ToString();

                File.WriteAllText(msBuild160ExePath, "foo 16.0");

                var newPathValue = new StringBuilder();
                newPathValue.Append('\"');
                newPathValue.Append(msBuild160BinPath);
                newPathValue.Append('\"');
                newPathValue.Append(';');

                var environment = new Mock <NuGet.Common.IEnvironmentVariableReader>(MockBehavior.Strict);
                environment.Setup(s => s.GetEnvironmentVariable("PATH")).Returns(newPathValue.ToString());

                // Act;
                var msBuildPath = MsBuildUtility.GetMSBuild(environment.Object);

                // Assert
                Assert.NotNull(msBuildPath);
                Assert.Equal(msBuild160ExePath, msBuildPath);
            }
        }
Beispiel #2
0
        public void TestGetMsbuildDirectoryFromPATHENV()
        {
            if (RuntimeEnvironmentHelper.IsMono)
            { // Mono does not have SxS installations so it's not relevant to get msbuild from the path.
                return;
            }

            using (var vsPath = TestDirectory.Create())
            {
                var msBuild159BinPath = Directory.CreateDirectory(Path.Combine(vsPath, "MSBuild", "15.9", "Bin")).FullName;

                var msBuild159ExePath = Path.Combine(msBuild159BinPath, "msbuild.exe").ToString();

                using (var fs15 = File.CreateText(msBuild159ExePath))
                {
                    fs15.Write("foo 15.9");
                }

                var pathValue    = Environment.GetEnvironmentVariable("PATH");
                var newPathValue = msBuild159BinPath + ";" + pathValue;

                Environment.SetEnvironmentVariable("PATH", newPathValue);

                // Act;
                var toolset = MsBuildUtility.GetMsBuildToolset(userVersion: null, console: null);
                Environment.SetEnvironmentVariable("PATH", pathValue);

                // Assert
                Assert.NotNull(toolset);
                Assert.Equal(msBuild159BinPath, toolset.Path);
            }
        }
Beispiel #3
0
        public void CombinePathWithVerboseError_IllegalCharacters_MessageContainsBadPath()
        {
            const string badPath   = @"C:\bad:>dir";
            var          exception = Assert.Throws <ArgumentException>(() => MsBuildUtility.CombinePathWithVerboseError(badPath, "file.txt"));

            Assert.Contains(badPath, exception.Message);
        }
Beispiel #4
0
        public void TestGetMsbuildDirectoryForMonoOnMac(string version)
        {
            var os = Environment.GetEnvironmentVariable("OSTYPE");

            if (RuntimeEnvironmentHelper.IsMono && os != null && os.StartsWith("darwin"))
            {
                // Act;
                var toolset = MsBuildUtility.GetMsBuildFromMonoPaths(version);

                var msbuild14 = @"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/msbuild/14.1/bin/";
                var msbuild15 = @"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/msbuild/15.0/bin/";

                // Assert
                if (version == "15.0" || version == "15")
                {
                    Assert.Equal(toolset.Path, msbuild15);
                }
                else if (version == "14.1")
                {
                    Assert.Equal(toolset.Path, msbuild14);
                }
                else if (version == null)
                {
                    Assert.True(new List <string> {
                        msbuild14, msbuild15
                    }.Contains(toolset.Path));
                }
            }
        }
        public void TestVersionMatch(List <MsBuildToolset> toolsets, string userVersion, string expectedPath)
        {
            // Arrange
            // Act
            var directory = MsBuildUtility.GetMsBuildDirectoryInternal(
                userVersion: userVersion,
                console: null,
                installedToolsets: toolsets.OrderByDescending(t => t),
                getMsBuildPathInPathVar: () => null);

            // Assert
            Assert.Equal(expectedPath, directory);
        }
Beispiel #6
0
        public void VersionSelectedThatMatchesPathMsBuildVersion(List <MsBuildToolset> toolsets, string expectedPath)
        {
            // Arrange
            // Act
            var directory = MsBuildUtility.GetMsBuildDirectoryInternal(
                userVersion: null,
                console: null,
                installedToolsets: toolsets.OrderByDescending(t => t),
                getMsBuildPathInPathVar: () => expectedPath).Path;

            // Assert
            Assert.Equal(expectedPath, directory);
        }
Beispiel #7
0
        public void HandlesToolsetsWithInvalidPaths(List <MsBuildToolset> toolsets, string expectedPath)
        {
            // Arrange
            // Act
            var directory = MsBuildUtility.GetMsBuildDirectoryInternal(
                userVersion: null,
                console: null,
                installedToolsets: toolsets.OrderByDescending(t => t),
                getMsBuildPathInPathVar: (reader) => expectedPath).Path;

            // Assert
            Assert.Equal(expectedPath, directory);
        }
Beispiel #8
0
        public void HighestVersionSelectedIfExeInPathDoesntMatchToolsets(List <MsBuildToolset> toolsets, string expectedPath)
        {
            // Arrange
            // Act
            var directory = MsBuildUtility.GetMsBuildDirectoryInternal(
                userVersion: null,
                console: null,
                installedToolsets: toolsets.OrderByDescending(t => t),
                getMsBuildPathInPathVar: (reader) => @"c:\foo").Path;

            // Assert
            Assert.Equal(expectedPath, directory);
        }
Beispiel #9
0
        public void HighestVersionSelectedIfLatestSpecified(List <MsBuildToolset> toolsets, string lowVersionPath, string expectedPath)
        {
            // Arrange
            // Act
            var directory = MsBuildUtility.GetMsBuildDirectoryInternal(
                userVersion: "latest",
                console: null,
                installedToolsets: toolsets.OrderByDescending(t => t),
                getMsBuildPathInPathVar: (reader) => lowVersionPath).Path;

            // Assert
            Assert.Equal(expectedPath, directory);
        }
            public TestInfo(string projectFileContent, string projectName = "proj1")
            {
                ProjectDirectory    = TestDirectory.Create();
                MSBuildDirectory    = MsBuildUtility.GetMsBuildDirectory(null, null);
                NuGetProjectContext = new TestNuGetProjectContext();

                var projectFilePath = Path.Combine(ProjectDirectory, projectName + ".csproj");

                File.WriteAllText(projectFilePath, projectFileContent);

                MSBuildProjectSystem
                    = new MSBuildProjectSystem(MSBuildDirectory, projectFilePath, NuGetProjectContext);
            }
Beispiel #11
0
        public void HighestVersionSelectedIfNoVersionMatch()
        {
            var toolsetV14 = new MsbuildToolSet("14.0", "v14path");
            var toolsetV12 = new MsbuildToolSet("12.0", "v12path");
            var toolsetV4  = new MsbuildToolSet("4.0", "v4path");

            var installedToolsets = new List <MsbuildToolSet> {
                toolsetV14, toolsetV12, toolsetV4
            };

            var selectedToolset = MsBuildUtility.SelectMsbuildToolset(
                msbuildVersion: new System.Version("5.6"),
                installedToolsets: installedToolsets);

            Assert.Equal(selectedToolset, toolsetV14);
        }
Beispiel #12
0
        public void VersionSelectedThatMatchesMSBuildVersionMajor()
        {
            var toolsetV14 = new MsbuildToolSet("14.0", "v14path");
            var toolsetV12 = new MsbuildToolSet("12.0", "v12path");
            var toolsetV4  = new MsbuildToolSet("4.0", "v4path");

            var installedToolsets = new List <MsbuildToolSet> {
                toolsetV14, toolsetV12, toolsetV4
            };

            var selectedToolset = MsBuildUtility.SelectMsbuildToolset(
                msbuildVersion: new Version("4.6"),
                installedToolsets: installedToolsets);

            Assert.Equal(selectedToolset, toolsetV4);
        }
Beispiel #13
0
        public void TestVersionMatchByStringFailure(List <MsBuildToolset> toolsets, string userVersion)
        {
            // Arrange

            // Act
            var ex = Assert.Throws <CommandException>(() =>
            {
                var directory = MsBuildUtility.GetMsBuildDirectoryInternal(
                    userVersion: userVersion,
                    console: null,
                    installedToolsets: toolsets.OrderByDescending(t => t),
                    getMsBuildPathInPathVar: (reader) => null);
            });

            // Assert
            Assert.Equal(
                $"Cannot find the specified version of msbuild: '{userVersion}'",
                ex.Message);
        }
            public TestInfo(ITestOutputHelper testOutputHelper, string projectFileContent, string projectName = "proj1")
            {
                var console = new Mock <IConsole>();

                console.Setup(c => c.WriteLine(It.IsAny <string>(), It.IsAny <object[]>())).Callback <string, object[]>((format, args) => testOutputHelper.WriteLine(format, args));

                console.SetupGet(c => c.Verbosity).Returns(Verbosity.Detailed);

                _projectDirectory    = TestDirectory.Create();
                _msBuildDirectory    = MsBuildUtility.GetMsBuildToolset(null, console.Object).Path;
                _nuGetProjectContext = new TestNuGetProjectContext();

                var projectFilePath = Path.Combine(_projectDirectory, projectName + ".csproj");

                File.WriteAllText(projectFilePath, projectFileContent);

                MSBuildProjectSystem
                    = new MSBuildProjectSystem(_msBuildDirectory, projectFilePath, _nuGetProjectContext);
            }
Beispiel #15
0
        public void TestVersionMatchByNumber()
        {
            var toolsetV14 = new MsbuildToolSet("14.0", "v14path");
            var toolsetV12 = new MsbuildToolSet("12.0", "v12path");
            var toolsetV4  = new MsbuildToolSet("4.0", "v4path");

            var installedToolsets = new List <MsbuildToolSet> {
                toolsetV14, toolsetV12, toolsetV4
            };

            // Act
            var directory = MsBuildUtility.GetMsbuildDirectoryInternal(
                userVersion: "12",
                console: null,
                installedToolsets: installedToolsets);

            // Assert
            Assert.Equal(directory, toolsetV12.ToolsPath);
        }
Beispiel #16
0
        public void TestVersionMatchByString(string userVersion, string expectedDirectory)
        {
            // Arrange
            var toolsetV14  = new MsbuildToolSet("14.0", "v14path");
            var toolsetV12  = new MsbuildToolSet("12.0", "v12path");
            var toolsetFoo4 = new MsbuildToolSet("Foo4.0", "foo4path");

            var installedToolsets = new List <MsbuildToolSet> {
                toolsetV14, toolsetV12, toolsetFoo4
            };

            // Act
            var directory = MsBuildUtility.GetMsbuildDirectoryInternal(
                userVersion: userVersion,
                console: null,
                installedToolsets: installedToolsets);

            // Assert
            Assert.Equal(directory, expectedDirectory);
        }
Beispiel #17
0
        public BaseCommandRunner(BaseArgs baseArgs)
        {
            var packArgs = new PackArgs
            {
                CurrentDirectory = baseArgs.CurrentDirectory,
                Logger           = baseArgs.Console,
                Arguments        = new string[0],
                MsBuildDirectory = new Lazy <string>(() => MsBuildUtility.GetMsBuildDirectoryFromMsBuildPath(null, null, baseArgs.Console).Value.Path)
            };

            // Get the input file
            packArgs.Path = PackCommandRunner.GetInputFile(packArgs);

            // Set the current directory if the files being packed are in a different directory
            PackCommandRunner.SetupCurrentDirectory(packArgs);
            packArgs.Build   = false;
            packArgs.Exclude = new string[0];
            switch (baseArgs.Verbosity)
            {
            case Verbosity.Detailed:
            {
                packArgs.LogLevel = LogLevel.Verbose;
                break;
            }

            case Verbosity.Normal:
            {
                packArgs.LogLevel = LogLevel.Information;
                break;
            }

            case Verbosity.Quiet:
            {
                packArgs.LogLevel = LogLevel.Minimal;
                break;
            }
            }
            this._packArgs = packArgs;
        }
Beispiel #18
0
        private MSBuildNuGetProject GetProject(string project, ICanAddFileStrategy canAddFileStrategy = null)
        {
            MSBuildNuGetProject msBuildNuGetProject;

            if (!_loadedMsBuildNuGetProjects.TryGetValue(project, out msBuildNuGetProject))
            {
                var msbuildNuGetProjectSystem = new CustomMsbuildProjectSystem(
                    msbuildDirectory: MsBuildUtility.GetMsbuildDirectory(null, _console),
                    projectFullPath: project,
                    projectContext: _nuGetProjectContext);

                msBuildNuGetProject = new MSBuildNuGetProject(
                    msbuildNuGetProjectSystem: msbuildNuGetProjectSystem,
                    folderNuGetProjectPath: _packagesFolder,
                    packagesConfigFolderPath: Path.GetDirectoryName(project));

                _loadedMsBuildNuGetProjects.Add(project, msBuildNuGetProject);
            }

            ((CustomMsbuildProjectSystem)msBuildNuGetProject.MSBuildNuGetProjectSystem).ApplyCanAddFileStrategy(canAddFileStrategy);

            return(msBuildNuGetProject);
        }
Beispiel #19
0
        public void TestVersionMatchByStringFailure(string userVersion)
        {
            var toolsetV14  = new MsbuildToolSet("14.0", "v14path");
            var toolsetV12  = new MsbuildToolSet("12.0", "v12path");
            var toolsetFoo4 = new MsbuildToolSet("Foo4.0", "foo4path");

            var installedToolsets = new List <MsbuildToolSet> {
                toolsetV14, toolsetV12, toolsetFoo4
            };

            // Act
            var ex = Assert.Throws <CommandLineException>(() =>
            {
                var directory = MsBuildUtility.GetMsbuildDirectoryInternal(
                    userVersion: userVersion,
                    console: null,
                    installedToolsets: installedToolsets);
            });

            // Assert
            Assert.Equal(
                $"Cannot find the specified version of msbuild: '{userVersion}'",
                ex.Message);
        }
Beispiel #20
0
 public static string GetMsbuildPathOnWindows()
 {
     return(MsBuildUtility.GetMsBuildDirectoryFromMsBuildPath(null, null, null).Value.Path);
 }
Beispiel #21
0
        public void CombinePathWithVerboseError_CombinesPaths()
        {
            var paths = new[] { "C:\\", "directory/", "\\folder", "file.txt" };

            Assert.Equal(Path.Combine(paths), MsBuildUtility.CombinePathWithVerboseError(paths));
        }