Пример #1
0
        public async Task TestGetAllProjects_FindsNestedProject()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var solution = MockRepository.GenerateStub <Solution>();

            var legitProject1           = MockRepository.GenerateStub <Project>();
            var solutionFolder          = MockRepository.GenerateStub <Project>();
            var projectInSolutionFolder = MockRepository.GenerateStub <Project>();

            var projectList = new List <Project> {
                legitProject1, solutionFolder
            };

            solutionFolder.Stub(x => x.Kind).Return(ProjectKinds.vsProjectKindSolutionFolder);
            solutionFolder.Stub(x => x.ProjectItems.Count).Return(1);
            solutionFolder.Stub(x => x.ProjectItems.Item(1).SubProject).Return(projectInSolutionFolder);

            solution.Stub(x => x.GetEnumerator()).Return(projectList.GetEnumerator());


            var allSolutionProjectsSearcher = new AllSolutionProjectsSearcher();
            var returnedProjects            = allSolutionProjectsSearcher.GetAllProjects(solution);


            Assert.Contains(projectInSolutionFolder, returnedProjects.ToList());
        }
        public async Task HandleAsync_NoLicenseHeaderFileGiven_MessageInformsAboutFilesThatCouldNotBeProcessed()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var extension             = MockRepository.GenerateStub <ILicenseHeaderExtension>();
            var licenseHeaderReplacer = MockRepository.GenerateStrictMock <LicenseHeaderReplacer>();

            extension.Expect(x => x.LicenseHeaderReplacer).Return(licenseHeaderReplacer);

            var noLicenseHeaderFile = MockRepository.GenerateMock <ProjectItem>();

            noLicenseHeaderFile.Expect(x => x.Name).Return("projectItem1.cs");

            var notInSolution = MockRepository.GenerateMock <ProjectItem>();

            notInSolution.Expect(x => x.Name).Return("projectItem2.cs");

            var linkedFileFilter = MockRepository.GenerateMock <ILinkedFileFilter>();

            linkedFileFilter.Expect(x => x.NoLicenseHeaderFile).Return(new List <ProjectItem> {
                noLicenseHeaderFile
            });
            linkedFileFilter.Expect(x => x.ToBeProgressed).Return(new List <ProjectItem>());
            linkedFileFilter.Expect(x => x.NotInSolution).Return(new List <ProjectItem> {
                notInSolution
            });

            var linkedFileHandler = new LinkedFileHandler(extension);
            await linkedFileHandler.HandleAsync(linkedFileFilter);

            const string expectedMessage = "We could not update following linked files, because there is no license header definition file in their original project, or the "
                                           + "original project is not part of this solution.\n\nprojectItem1.cs\nprojectItem2.cs";

            Assert.That(linkedFileHandler.Message, Is.EqualTo(expectedMessage));
        }
Пример #3
0
        public async Task TestGetAllProjects_ReturnsOnlyProjects()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var solution = MockRepository.GenerateStub <Solution>();

            var legitProject1  = MockRepository.GenerateStub <Project>();
            var solutionFolder = MockRepository.GenerateStub <Project>();

            var projectList = new List <Project> {
                legitProject1, solutionFolder
            };

            solutionFolder.Stub(x => x.Kind).Return(ProjectKinds.vsProjectKindSolutionFolder);
            solutionFolder.Stub(x => x.ProjectItems.Count).Return(0);

            solution.Stub(x => x.GetEnumerator()).Return(projectList.GetEnumerator());


            var allSolutionProjectsSearcher = new AllSolutionProjectsSearcher();
            var returnedProjects            = allSolutionProjectsSearcher.GetAllProjects(solution);


            Assert.That(legitProject1, Is.EqualTo(returnedProjects.First()));
        }
        public void SetUp()
        {
            // In order to make the "await LicenseHeadersPackage.Instance.JoinableTaskFactory.SwitchToMainThreadAsync();" call in
            // LinkedFileHandler.HandleAsync work, we need to set the private JoinableTaskFactory property accordingly.
            // Source: https://github.com/microsoft/vs-threading/blob/main/doc/testing_vs.md
#pragma warning disable VSSDK005
            var jtc = new JoinableTaskContext();
#pragma warning restore VSSDK005
            VisualStudioTestContext.SetPrivateSetPackageProperty(nameof(ILicenseHeaderExtension.JoinableTaskFactory), jtc.Factory);
        }
Пример #5
0
        public async Task IsPhysicalFile_GivenProjectItem_DeterminesIfPhysicalFile(string projectItemKind, bool isPhysicalFile)
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var physicalFile = MockRepository.GenerateMock <ProjectItem>();

            physicalFile.Expect(x => x.Kind).Return(projectItemKind);

            Assert.That(ProjectItemInspection.IsPhysicalFile(physicalFile), Is.EqualTo(isPhysicalFile));
        }
Пример #6
0
        public async Task IsLicenseHeader_FileWithExtensionGiven_DeterminesIfLicenseHeaderDefinitionFile(string name, bool isLicenseHeader)
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var licenseHeader = MockRepository.GenerateMock <ProjectItem>();

            licenseHeader.Expect(x => x.Name).Return(name);

            Assert.That(ProjectItemInspection.IsLicenseHeader(licenseHeader), Is.EqualTo(isLicenseHeader));
        }
Пример #7
0
        public async Task IsLink_GivenNonLinkedItem_ReturnsFalse()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var linkedProjectItem = MockRepository.GenerateMock <ProjectItem>();

            linkedProjectItem.Stub(x => x.Properties).Return(MockRepository.GenerateStub <Properties>());
            linkedProjectItem.Properties.Stub(x => x.Item("IsLink")).Throw(new ArgumentException());

            Assert.That(ProjectItemInspection.IsLink(linkedProjectItem), Is.False);
        }
Пример #8
0
        public async Task GeneralOptionsPage_AddLinkedCommand_LinkedCommandsChangedEventTriggered()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var optionsPage = new GeneralOptionsPageModel();
            var wasCalled   = false;

            optionsPage.LinkedCommandsChanged += (sender, args) => wasCalled = true;
            optionsPage.LinkedCommands.Add(new LinkedCommand());

            Assert.That(wasCalled, Is.True);
        }
        public async Task Filter_GivenEmptyList_YieldsEmptyProperties()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var solution         = MockRepository.GenerateMock <Solution>();
            var linkedFileFilter = new LinkedFileFilter(solution);

            linkedFileFilter.Filter(new List <ProjectItem>());

            Assert.That(linkedFileFilter.ToBeProgressed, Is.Empty);
            Assert.That(linkedFileFilter.NoLicenseHeaderFile, Is.Empty);
            Assert.That(linkedFileFilter.NotInSolution, Is.Empty);
        }
        public async Task Filter_GivenLicenseHeaderFile_PopulatesToBeProgressedProperty()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            const string licenseHeaderFileName = "test.licenseheader";

            try
            {
                var solution   = MockRepository.GenerateMock <Solution>();
                var linkedFile = MockRepository.GenerateMock <ProjectItem>();

                var licenseHeaderFile = MockRepository.GenerateStub <ProjectItem>();
                licenseHeaderFile.Expect(x => x.FileCount).Return(1);
                licenseHeaderFile.Expect(x => x.FileNames[0]).Return(licenseHeaderFileName);

                using (var writer = new StreamWriter(licenseHeaderFileName))
                {
                    writer.WriteLine("extension: .cs");
                    writer.WriteLine("//test");
                }

                var projectItems = MockRepository.GenerateStub <ProjectItems>();
                projectItems.Stub(x => x.GetEnumerator())
                .Return(null)
                .WhenCalled(x => x.ReturnValue = new List <ProjectItem> {
                    licenseHeaderFile
                }.GetEnumerator());

                linkedFile.Expect(x => x.ProjectItems).Return(projectItems);
                linkedFile.Expect(x => x.Name).Return("linkedFile.cs");
                solution.Expect(x => x.FindProjectItem("linkedFile.cs")).Return(linkedFile);

                // LicenseHeaderFinder is invoked by LinkedFileFilter and uses LicenseHeadersPackage.Instance.LicenseHeaderExtractor.
                // Since LicenseHeaderExtractor is set during MEF-controlled initialization, set the private-set LicenseHeaderExtractor
                // to make it work via tests as well.
                VisualStudioTestContext.SetPrivateSetPackageProperty(nameof(ILicenseHeaderExtension.LicenseHeaderExtractor), new LicenseHeaderExtractor());

                var linkedFileFilter = new LinkedFileFilter(solution);
                linkedFileFilter.Filter(new List <ProjectItem> {
                    linkedFile
                });

                Assert.That(linkedFileFilter.ToBeProgressed, Is.Not.Empty);
                Assert.That(linkedFileFilter.NoLicenseHeaderFile, Is.Empty);
                Assert.That(linkedFileFilter.NotInSolution, Is.Empty);
            }
            finally
            {
                File.Delete(licenseHeaderFileName);
            }
        }
Пример #11
0
        public async Task IsLink_GivenLinkedItem_ReturnsTrue()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var linkedProjectItem = MockRepository.GenerateMock <ProjectItem>();

            var propertyStub = MockRepository.GenerateStub <Property>();

            propertyStub.Value = true;

            linkedProjectItem.Stub(x => x.Properties).Return(MockRepository.GenerateStub <Properties>());
            linkedProjectItem.Properties.Stub(x => x.Item("IsLink")).Return(propertyStub);

            Assert.That(ProjectItemInspection.IsLink(linkedProjectItem), Is.True);
        }
        public async Task HandleAsync_NoProjectItemsGiven_NothingToBeDoneAndErrorMessageEmpty()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var solution              = MockRepository.GenerateStub <Solution>();
            var extension             = MockRepository.GenerateStub <ILicenseHeaderExtension>();
            var linkedFileFilter      = MockRepository.GenerateStrictMock <LinkedFileFilter> (solution);
            var licenseHeaderReplacer = MockRepository.GenerateStrictMock <LicenseHeaderReplacer>();

            extension.Expect(x => x.LicenseHeaderReplacer).Return(licenseHeaderReplacer);

            var linkedFileHandler = new LinkedFileHandler(extension);
            await linkedFileHandler.HandleAsync(linkedFileFilter);

            Assert.That(linkedFileHandler.Message, Is.EqualTo(string.Empty));
        }
Пример #13
0
        public async Task GeneralOptionsPage_CreateNewLinkedCommandCollection_LinkedCommandsChangedEventTriggered()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var optionsPage = new GeneralOptionsPageModel();
            var wasCalled   = false;

            optionsPage.LinkedCommandsChanged += (sender, args) => wasCalled = true;
            const string emptySerializedLinkedCommands = "1*System.String*<LinkedCommands/>";
            var          converter = new LinkedCommandConverter();

            optionsPage.LinkedCommands = new ObservableCollection <LinkedCommand> (converter.FromXml(emptySerializedLinkedCommands))
            {
                new LinkedCommand()
            };

            Assert.That(wasCalled, Is.True);
        }
        public async Task Filter_GivenNonSolutionItem_PopulatesNotInSolutionProperty()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var solution   = MockRepository.GenerateMock <Solution>();
            var linkedFile = MockRepository.GenerateMock <ProjectItem>();

            solution.Expect(x => x.FindProjectItem("linkedFile.cs")).Return(null);
            linkedFile.Expect(x => x.Name).Return("linkedFile.cs");

            var linkedFileFilter = new LinkedFileFilter(solution);

            linkedFileFilter.Filter(new List <ProjectItem> {
                linkedFile
            });

            Assert.That(linkedFileFilter.ToBeProgressed, Is.Empty);
            Assert.That(linkedFileFilter.NoLicenseHeaderFile, Is.Empty);
            Assert.That(linkedFileFilter.NotInSolution, Is.Not.Empty);
        }
Пример #15
0
        public async Task TestGetAllProjects_ShouldReturnListOfProjects()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var solution = MockRepository.GenerateStub <Solution>();

            var legitProject1 = MockRepository.GenerateStub <Project>();
            var legitProject2 = MockRepository.GenerateStub <Project>();

            var projectList = new List <Project> {
                legitProject1, legitProject2
            };

            solution.Stub(x => x.GetEnumerator()).Return(projectList.GetEnumerator());


            var allSolutionProjectsSearcher = new AllSolutionProjectsSearcher();
            var returnedProjects            = allSolutionProjectsSearcher.GetAllProjects(solution);


            Assert.That(returnedProjects.Count, Is.EqualTo(2));
        }
        public async Task Filter_GivenNonLicenseHeaderFile_PopulatesNoLicenseHeaderFileProperty()
        {
            await VisualStudioTestContext.SwitchToMainThread();

            var solution = MockRepository.GenerateMock <Solution>();

            solution.Expect(x => x.FullName).Return(@"d:\projects\Stuff.sln");

            var dte = MockRepository.GenerateMock <DTE>();

            dte.Expect(x => x.Solution).Return(solution);

            var projectItems = MockRepository.GenerateMock <ProjectItems>();

            var linkedFile = MockRepository.GenerateMock <ProjectItem>();

            linkedFile.Expect(x => x.DTE).Return(dte);
            projectItems.Expect(x => x.Parent).Return(new object());
            linkedFile.Expect(x => x.Collection).Return(projectItems);

            solution.Expect(x => x.FindProjectItem("linkedFile.cs")).Return(linkedFile);


            linkedFile.Expect(x => x.Name).Return("linkedFile.cs");
            linkedFile.Expect(x => x.Properties).Return(null);


            var linkedFileFilter = new LinkedFileFilter(solution);

            linkedFileFilter.Filter(new List <ProjectItem> {
                linkedFile
            });

            Assert.That(linkedFileFilter.ToBeProgressed, Is.Empty);
            Assert.That(linkedFileFilter.NoLicenseHeaderFile, Is.Not.Empty);
            Assert.That(linkedFileFilter.NotInSolution, Is.Empty);
        }