Exemplo n.º 1
0
        public void GlobalJsonRemover_RemovesJson_WhenExists()
        {
            UnitTestHelper.IsRunningUnitTests = true;
            var solution    = IVsSolutionFactory.CreateWithSolutionDirectory(DirectoryInfoCallback);
            var projectItem = ProjectItemFactory.Create();
            var dteSolution = SolutionFactory.ImplementFindProjectItem(path =>
            {
                Assert.Equal(Path.Combine(Directory, "global.json"), path);
                return(projectItem);
            });
            var dte = DteFactory.ImplementSolution(() => dteSolution);

            var serviceProvider = IServiceProviderFactory.ImplementGetService(t =>
            {
                if (typeof(SVsSolution) == t)
                {
                    return(solution);
                }

                if (typeof(DTE) == t)
                {
                    return(dte);
                }

                Assert.False(true);
                throw new InvalidOperationException();
            });

            var remover = new GlobalJsonRemover(serviceProvider);

            Assert.Equal(VSConstants.S_OK, remover.OnAfterOpenSolution(null, 0));
            Mock.Get(projectItem).Verify(p => p.Remove(), Times.Once);
        }
Exemplo n.º 2
0
        public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
        {
            UIThreadHelper.VerifyOnUIThread();
            var dte      = _serviceProvider.GetService <DTE2, DTE>();
            var solution = _serviceProvider.GetService <IVsSolution, SVsSolution>();

            try
            {
                Verify.HResult(solution.GetSolutionInfo(out string directory, out string solutionFile, out string optsFile));
                var         globalJsonPath = Path.Combine(directory, "global.json");
                ProjectItem globalJson     = dte.Solution.FindProjectItem(globalJsonPath);
                globalJson?.Delete();
                try
                {
                    _fileSystem.RemoveFile(globalJsonPath);
                }
                catch (FileNotFoundException) { }
                return(VSConstants.S_OK);
            }
            finally
            {
                Verify.HResult(solution.UnadviseSolutionEvents(SolutionCookie));
                // Don't keep a static reference around to an object that won't be used again.
                Assumes.True(ReferenceEquals(this, s_remover));
                s_remover = null;
            }
        }
        public void GlobalJsonRemover_NoJson_DoesntCrash()
        {
            UnitTestHelper.IsRunningUnitTests = true;
            var solution    = IVsSolutionFactory.CreateWithSolutionDirectory(DirectoryInfoCallback);
            var dteSolution = SolutionFactory.ImplementFindProjectItem(path =>
            {
                Assert.Equal(Path.Combine(Directory, "global.json"), path);
                return(null);
            });
            var dte = DteFactory.ImplementSolution(() => dteSolution);

            var serviceProvider = IServiceProviderFactory.ImplementGetService(t =>
            {
                if (typeof(SVsSolution) == t)
                {
                    return(solution);
                }

                if (typeof(DTE) == t)
                {
                    return(dte);
                }

                Assert.False(true);
                throw new InvalidOperationException();
            });

            var remover = new GlobalJsonRemover(serviceProvider, IFileSystemFactory.Create());

            GlobalJsonRemover.Remover = remover;
            Assert.Equal(VSConstants.S_OK, remover.OnAfterOpenSolution(null, 0));
        }
Exemplo n.º 4
0
        public void MigrateXprojProjectFactory_GlobalJsonExists_BacksUpAndRemovesGlobalJson()
        {
            var fileSystem = CreateFileSystem(withEntries: true, withGlobalJson: true);

            var migrator = CreateInstance(ProcessRunnerFactory.CreateRunner(), fileSystem);

            GlobalJsonRemover remover = null;

            var solution = IVsSolutionFactory.Implement((IVsSolutionEvents events, out uint cookie) =>
            {
                remover = (GlobalJsonRemover)events;
                cookie  = 1234;
                return(VSConstants.S_OK);
            }, IVsSolutionFactory.DefaultUnadviseCallback, CreateSolutionInfo());

            var loggedMessages     = new List <LogMessage>();
            var logger             = IVsUpgradeLoggerFactory.CreateLogger(loggedMessages);
            var globalJsonBackedUp = Path.Combine(BackupLocation, "global.json");

            migrator.BackupAndDeleteGlobalJson(SlnLocation, solution, BackupLocation, XprojLocation, ProjectName, logger);
            Assert.False(fileSystem.FileExists(GlobalJsonLocation));
            Assert.True(fileSystem.FileExists(globalJsonBackedUp));
            Assert.Equal(1, loggedMessages.Count);
            Assert.Equal(new LogMessage
            {
                File    = GlobalJsonLocation,
                Level   = (uint)__VSUL_ERRORLEVEL.VSUL_INFORMATIONAL,
                Message = string.Format(VSResources.MigrationBackupFile, GlobalJsonLocation, globalJsonBackedUp),
                Project = ProjectName
            }, loggedMessages[0]);
            Assert.NotNull(remover);
            Assert.Equal(1234u, remover.SolutionCookie);
        }
        public void GlobalJsonSetup_ExistingRemover_ReturnsSameRemover()
        {
            UnitTestHelper.IsRunningUnitTests = true;
            var remover = new GlobalJsonRemover(IServiceProviderFactory.Create(), IFileSystemFactory.Create());

            GlobalJsonRemover.Remover = remover;
            Assert.False(new GlobalJsonRemover.GlobalJsonSetup().SetupRemoval(IVsSolutionFactory.Create(),
                                                                              IServiceProviderFactory.Create(), IFileSystemFactory.Create()));
            Assert.True(ReferenceEquals(remover, GlobalJsonRemover.Remover));
        }
Exemplo n.º 6
0
            /// <summary>
            /// Initializes the <see cref="GlobalJsonRemover"/> if not already initialized. This method assumes that it will be called
            /// from the UI thread, and will throw if this isn't true.
            /// </summary>
            /// <returns>True if the remover was set up for the first time. False otherwise.</returns>
            public virtual bool SetupRemoval(IVsSolution solution, IServiceProvider provider, IFileSystem fileSystem)
            {
                UIThreadHelper.VerifyOnUIThread();
                if (s_remover != null)
                {
                    return(false);
                }

                s_remover = new GlobalJsonRemover(provider, fileSystem);
                Verify.HResult(solution.AdviseSolutionEvents(s_remover, out uint cookie));
                s_remover.SolutionCookie = cookie;
                return(true);
            }
        public void GlobalJsonRemover_AfterRemoval_UnadvisesEvents()
        {
            UnitTestHelper.IsRunningUnitTests = true;
            var globalJsonPath = Path.Combine(Directory, "global.json");
            var solution       = IVsSolutionFactory.CreateWithSolutionDirectory(DirectoryInfoCallback);
            var projectItem    = ProjectItemFactory.Create();
            var dteSolution    = SolutionFactory.ImplementFindProjectItem(path =>
            {
                Assert.Equal(globalJsonPath, path);
                return(projectItem);
            });
            var dte = DteFactory.ImplementSolution(() => dteSolution);

            var serviceProvider = IServiceProviderFactory.ImplementGetService(t =>
            {
                if (typeof(SVsSolution) == t)
                {
                    return(solution);
                }

                if (typeof(DTE) == t)
                {
                    return(dte);
                }

                Assert.False(true);
                throw new InvalidOperationException();
            });

            var fileSystem = IFileSystemFactory.Create();

            fileSystem.Create(globalJsonPath);

            var remover = new GlobalJsonRemover(serviceProvider, fileSystem)
            {
                SolutionCookie = 1234
            };

            GlobalJsonRemover.Remover = remover;
            Assert.Equal(VSConstants.S_OK, remover.OnAfterOpenSolution(null, 0));
            Mock.Get(solution).Verify(s => s.UnadviseSolutionEvents(1234), Times.Once);
        }
Exemplo n.º 8
0
        internal HResult BackupAndDeleteGlobalJson(string solutionDirectory, IVsSolution solution, string backupLocation, string xprojLocation, string projectName, IVsUpgradeLogger pLogger)
        {
            var globalJson = Path.Combine(solutionDirectory, "global.json");

            if (_fileSystem.FileExists(globalJson))
            {
                // We want to find the root backup directory that VS created for backup. We can't just assume it's solution/Backup, because VS will create
                // a new directory if that already exists. So just iterate up until we find the correct directory.
                solutionDirectory = solutionDirectory.TrimEnd(Path.DirectorySeparatorChar);
                var rootBackupDirectory    = backupLocation;
                var levelUpBackupDirectory = Path.GetDirectoryName(rootBackupDirectory).TrimEnd(Path.DirectorySeparatorChar);
                while (!StringComparers.Paths.Equals(levelUpBackupDirectory, solutionDirectory))
                {
                    rootBackupDirectory    = levelUpBackupDirectory;
                    levelUpBackupDirectory = Path.GetDirectoryName(levelUpBackupDirectory).TrimEnd(Path.DirectorySeparatorChar);
                }

                // rootBackupDirectory is now the actual root backup directory. Back up the global.json and delete the existing one
                var globalJsonBackupPath = Path.Combine(rootBackupDirectory, "global.json");
                pLogger.LogMessage((uint)__VSUL_ERRORLEVEL.VSUL_INFORMATIONAL, projectName, globalJson,
                                   string.Format(VSResources.MigrationBackupFile, globalJson, globalJsonBackupPath));
                _fileSystem.CopyFile(globalJson, globalJsonBackupPath, true);
                try
                {
                    _fileSystem.RemoveFile(globalJson);

                    var     remover = new GlobalJsonRemover(_serviceProvider);
                    HResult hr      = solution.AdviseSolutionEvents(remover, out uint cookie);
                    if (hr.Succeeded)
                    {
                        remover.SolutionCookie = cookie;
                    }
                }
                catch (IOException e)
                {
                    pLogger.LogMessage((uint)__VSUL_ERRORLEVEL.VSUL_ERROR, projectName, globalJson,
                                       e.Message);
                    return(e.HResult);
                }
            }
            return(VSConstants.S_OK);
        }
Exemplo n.º 9
0
        public void MigrateXprojProjectFactory_E2E_Works()
        {
            var fileSystem    = CreateFileSystem(withEntries: true, withXprojUser: true, withProjectLock: true, withGlobalJson: true);
            var processRunner = ProcessRunnerFactory.ImplementRunner(pInfo =>
            {
                ProcessVerifier(pInfo);
                fileSystem.Create(CsprojLocation);
            });

            GlobalJsonRemover remover = null;
            var solution = IVsSolutionFactory.Implement((IVsSolutionEvents events, out uint cookie) =>
            {
                remover = (GlobalJsonRemover)events;
                cookie  = 1234;
                return(VSConstants.S_OK);
            }, IVsSolutionFactory.DefaultUnadviseCallback, CreateSolutionInfo());

            var loggedMessages = new List <LogMessage>();
            var logger         = IVsUpgradeLoggerFactory.CreateLogger(loggedMessages);

            var migrator = CreateInstance(processRunner, fileSystem, solution);

            Assert.Equal(VSConstants.S_OK, migrator.UpgradeProject(XprojLocation, 0, BackupLocation, out string outCsproj, logger, out int upgradeRequired, out Guid newProjectFactory));
            Assert.True(fileSystem.FileExists(CsprojLocation));
            Assert.True(fileSystem.FileExists(Path.Combine(BackupLocation, $"{ProjectName}.xproj")));
            Assert.True(fileSystem.FileExists(Path.Combine(BackupLocation, "project.json")));
            Assert.True(fileSystem.FileExists(Path.Combine(BackupLocation, $"{ProjectName}.xproj.user")));
            Assert.False(fileSystem.FileExists(XprojLocation));
            Assert.False(fileSystem.FileExists(ProjectJsonLocation));
            Assert.False(fileSystem.FileExists(XprojUserLocation));
            Assert.False(fileSystem.FileExists(ProjectLockJsonLocation));
            Assert.False(fileSystem.FileExists(GlobalJsonLocation));
            Assert.Equal(CsprojLocation, outCsproj);
            Assert.Equal((int)__VSPPROJECTUPGRADEVIAFACTORYREPAIRFLAGS.VSPUVF_PROJECT_ONEWAYUPGRADE, upgradeRequired);
            Assert.Equal(Guid.Parse(CSharpProjectSystemPackage.ProjectTypeGuid), newProjectFactory);
            Assert.NotNull(remover);
            Assert.Equal(1234u, remover.SolutionCookie);
        }