void CreateProjectManagerWithPackageReferenceRepository() { var sharedRepository = new FakeSharedPackageRepository(); var repository = new PackageReferenceRepository(fileSystem, "MyProject", sharedRepository); projectManager = new TestableProjectManager(repository); }
public void RemovingAndAddingPackageReferenceWithSameIdPreservesDevelopmentFlag() { // Arrange var sharedRepository = new Mock <ISharedPackageRepository>(); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" developmentDependency=""false"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: sharedRepository.Object); var A10 = PackageUtility.CreatePackage("A"); var A20 = PackageUtility.CreatePackage("A", "2.0"); // Act referenceRepository.RemovePackage(A10); referenceRepository.AddPackage(A20); // Assert Assert.True(fileSystem.FileExists("packages.config")); AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""2.0"" developmentDependency=""false"" /> </packages>", fileSystem.ReadAllText("packages.config")); }
private IProjectManager CreateProjectManager(Project project) { // Create the project system IProjectSystem projectSystem = VsProjectSystemFactory.CreateProjectSystem(project, _fileSystemProvider); #if VS14 if (projectSystem is INuGetPackageManager) { var nugetAwareRepo = new NuGetAwareProjectPackageRepository((INuGetPackageManager)projectSystem, _sharedRepository); return new ProjectManager(this, PathResolver, projectSystem, nugetAwareRepo); } #endif PackageReferenceRepository repository = new PackageReferenceRepository(projectSystem, project.GetProperName(), _sharedRepository); // Ensure the logger is null while registering the repository FileSystem.Logger = null; Logger = null; // Ensure that this repository is registered with the shared repository if it needs to be if (repository != null) { repository.RegisterIfNecessary(); } var projectManager = new VsProjectManager(this, PathResolver, projectSystem, repository); // The package reference repository also provides constraints for packages (via the allowedVersions attribute) projectManager.ConstraintProvider = repository; return projectManager; }
void CreatePackageReferenceRepository() { helper = new PackageReferenceRepositoryHelper(); fakeFileSystem = helper.FakeProjectSystem; sourceRepository = helper.FakeSharedSourceRepository; repository = new PackageReferenceRepository(fakeFileSystem, sourceRepository); }
public void RemovePackageRemovesEntryFromPackagesConfigDeletesFileAndUnregistersRepositoryIfLastEntry() { // Arrange var sharedRepository = new Mock <ISharedPackageRepository>(); string path = null; sharedRepository.Setup(m => m.UnregisterRepository(It.IsAny <string>())) .Callback <string>(p => path = p); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: sharedRepository.Object); var package = PackageUtility.CreatePackage("A"); // Act referenceRepository.RemovePackage(package); // Assert Assert.False(fileSystem.FileExists("packages.config")); Assert.NotNull(path); Assert.Equal(@"C:\MockFileSystem\packages.config", path); }
public void GetPackagesLoadsPlainPackagesConfigIfProjectPackagesConfigDoesNotExist() { // Arrange var repository = new Mock <MockPackageRepository>() { CallBase = true }.As <ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.cool.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>"); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""B"" version=""1.0"" /> <package id=""C"" version=""1.0"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: "cool", sourceRepository: repository.Object); // Act var packages = referenceRepository.GetPackages().ToList(); // Assert Assert.Equal(1, packages.Count); Assert.Same(packageA, packages[0]); Assert.True(fileSystem.FileExists("packages.cool.config")); }
public void GetPackagesReturnsPackagesFromSourceRepositoryListedInPackagesConfig() { // Arrange var repository = new Mock <MockPackageRepository>() { CallBase = true }.As <ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: repository.Object); // Act var packages = referenceRepository.GetPackages().ToList(); // Assert Assert.Equal(1, packages.Count); Assert.Same(packageA, packages[0]); }
public void RemovePackagePreservesProjectConfigFile() { // Arrange var repository = new Mock <MockPackageRepository>() { CallBase = true }.As <ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.cool.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> <package id=""B"" version=""3.0"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: "cool", sourceRepository: repository.Object); var packageB = PackageUtility.CreatePackage("B", "3.0"); // Act referenceRepository.RemovePackage(packageB); // Assert Assert.False(fileSystem.FileExists("packages.config")); Assert.True(fileSystem.FileExists("packages.cool.config")); string content = fileSystem.ReadAllText("packages.cool.config"); AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>", content); }
public void GetConstraintReturnsConstraintListedForPackageIdInPackagesConfig() { // Arrange var repository = new Mock <MockPackageRepository>() { CallBase = true }.As <ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" allowedVersions=""[1.0, 3.0)"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: repository.Object); // Act IVersionSpec constraint = referenceRepository.GetConstraint("A"); // Assert Assert.NotNull(constraint); Assert.True(constraint.IsMinInclusive); Assert.False(constraint.IsMaxInclusive); Assert.Equal(new SemanticVersion("1.0"), constraint.MinVersion); Assert.Equal(new SemanticVersion("3.0"), constraint.MaxVersion); }
public void RemovingAndAddingPackageReferenceWithSameIdPreservesConstraint() { // Arrange var sharedRepository = new Mock <ISharedPackageRepository>(); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" allowedVersions=""[1.0, 5.0)"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object); var A10 = PackageUtility.CreatePackage("A"); var A20 = PackageUtility.CreatePackage("A", "2.0"); // Act referenceRepository.RemovePackage(A10); referenceRepository.AddPackage(A20); // Assert Assert.True(fileSystem.FileExists("packages.config")); AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""2.0"" allowedVersions=""[1.0, 5.0)"" /> </packages>", fileSystem.ReadAllText("packages.config")); }
public void ProjectManager_InstanceCreated_LocalRepositoryIsPackageReferenceRepository() { CreatePackageManager(); PackageReferenceRepository packageRefRepository = packageManager.ProjectManager.LocalRepository as PackageReferenceRepository; Assert.IsNotNull(packageRefRepository); }
PackageReferenceRepository CreatePackageReferenceRepository() { var sharedRepository = LocalRepository as ISharedPackageRepository; var packageRefRepository = new PackageReferenceRepository(projectSystem, projectSystem.ProjectName, sharedRepository); packageRefRepository.RegisterIfNecessary(); return(packageRefRepository); }
void CreatePackageReferenceRepository(string path) { helper = new PackageReferenceRepositoryHelper(); fakeFileSystem = helper.FakeProjectSystem; fakeFileSystem.PathToReturnFromGetFullPath = path; sourceRepository = helper.FakeSharedSourceRepository; repository = new PackageReferenceRepository(fakeFileSystem, sourceRepository); }
public TestableProjectManager(PackageReferenceRepository repository) : base( new FakePackageRepository(), new FakePackagePathResolver(), new FakeProjectSystem(), repository) { }
public IEnumerable <IPackageRepository> LoadProjectRepositories() { return(_projectRepositories.Select( f => { var projectRepository = new PackageReferenceRepository( f.FullPath, sourceRepository: this); return projectRepository; })); }
public void GetPackagesNoPackagesConfigReturnsEmptyList() { // Arrange var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object); // Act var packages = referenceRepository.GetPackages().ToList(); // Assert Assert.AreEqual(0, packages.Count); }
public void RegisterIfNecessaryDoesNotRegistersWithSharedRepositoryIfRepositoryDoesNotContainsPackages() { // Arrange var sharedRepository = new Mock<ISharedPackageRepository>(); string path = null; sharedRepository.Setup(m => m.RegisterRepository(It.IsAny<string>())) .Callback<string>(p => path = p); var fileSystem = new MockFileSystem(); // Act var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object); referenceRepository.RegisterIfNecessary(); // Assert Assert.Null(path); }
private IProjectManager CreateProjectManager(Project project) { // Create the projet system IProjectSystem projectSystem = VsProjectSystemFactory.CreateProjectSystem(project); var repository = new PackageReferenceRepository(projectSystem, _sharedRepository); // Ensure the logger is null while registering the repository FileSystem.Logger = null; Logger = null; // Ensure that this repository is registered with the shared repository if it needs to be repository.RegisterIfNecessary(); // Create the project manager with the shared repository return(new ProjectManager(_sharedRepository, PathResolver, projectSystem, repository)); }
public void UninstallingSatellitePackageRemovesCollidingRuntimeFiles() { // Arrange // Create a runtime package and a satellite package that has a dependency on the runtime package, and uses the // local suffix convention. var runtimePackage = PackageUtility.CreatePackage( "foo", "1.0.0", content: Enumerable.Empty <string>(), assemblyReferences: new[] { @"lib\ja-jp\collision.txt" }); var satellitePackage = PackageUtility.CreatePackage( "foo.ja-jp", "1.0.0", language: "ja-jp", content: Enumerable.Empty <string>(), satelliteAssemblies: new[] { @"lib\ja-jp\collision.txt" }, dependencies: new[] { new PackageDependency("foo", VersionUtility.ParseVersionSpec("[1.0.0]")) }); var packagesFolder = new MockFileSystem(@"c:\packagesFolder"); var sharedRepository = new MockSharedPackageRepository2(); var sourceRepository = new MockPackageRepository(); var packageManager = new PackageManager(sourceRepository, new DefaultPackagePathResolver(packagesFolder), packagesFolder, sharedRepository); sourceRepository.AddPackage(runtimePackage); sourceRepository.AddPackage(satellitePackage); var projectSystem = new MockProjectSystem(); var projectLocalRepo = new PackageReferenceRepository( new MockFileSystem(@"c:\project"), "projectName", sharedRepository); var projectManager = new ProjectManager( packageManager, packageManager.PathResolver, projectSystem, projectLocalRepo); // Act Install("foo", projectManager); Install("foo.ja-jp", projectManager); Uninstall("foo.ja-jp", packageManager, projectManager); // Assert Assert.Equal(0, packagesFolder.Paths.Count); Assert.False(packagesFolder.FileExists(@"foo.1.0.0\lib\ja-jp\collision.txt")); }
public void RegisterIfNecessaryDoesNotRegistersWithSharedRepositoryIfRepositoryDoesNotContainsPackages() { // Arrange var sharedRepository = new Mock <ISharedPackageRepository>(); string path = null; sharedRepository.Setup(m => m.RegisterRepository(It.IsAny <string>())) .Callback <string>(p => path = p); var fileSystem = new MockFileSystem(); // Act var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: sharedRepository.Object); referenceRepository.RegisterIfNecessary(); // Assert Assert.Null(path); }
private void UpdatePackages(IMSBuildProjectSystem projectSystem, string repositoryPath = null, IPackageRepository sourceRepository = null) { // Resolve the repository path repositoryPath = repositoryPath ?? GetRepositoryPath(projectSystem.Root); var sharedRepositoryFileSystem = new PhysicalFileSystem(repositoryPath); var pathResolver = new DefaultPackagePathResolver(sharedRepositoryFileSystem); // Create the local and source repositories var sharedPackageRepository = new SharedPackageRepository(pathResolver, sharedRepositoryFileSystem, sharedRepositoryFileSystem); var localRepository = new PackageReferenceRepository(projectSystem, projectSystem.ProjectName, sharedPackageRepository); sourceRepository = sourceRepository ?? AggregateRepositoryHelper.CreateAggregateRepositoryFromSources(RepositoryFactory, SourceProvider, Source); Console.WriteLine(LocalizedResourceManager.GetString("UpdatingProject"), projectSystem.ProjectName); UpdatePackages(localRepository, sharedRepositoryFileSystem, sharedPackageRepository, sourceRepository, localRepository, pathResolver, projectSystem); projectSystem.Save(); }
public void GetPackagesReturnsOnlyValidPackagesFromSourceRepositoryListedInPackagesConfig() { // Arrange var repository = new Mock <MockPackageRepository>() { CallBase = true }.As <ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var packageC = PackageUtility.CreatePackage("C", "2.0"); repository.Object.AddPackage(packageC); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""C"" version=""2.0"" /> <package id=""B"" version=""1.0"" /> <package id=""A"" version=""1.0"" /> <package id="""" version=""1.0"" /> <package id=""G"" version="""" /> <package /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object); // Act var packages = referenceRepository.GetPackages().ToList(); // Assert Assert.Equal(2, packages.Count); Assert.Same(packageC, packages[0]); Assert.Same(packageA, packages[1]); Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""C"" version=""2.0"" /> <package id=""B"" version=""1.0"" /> <package id=""A"" version=""1.0"" /> <package id="""" version=""1.0"" /> <package id=""G"" version="""" /> <package /> </packages>", fileSystem.ReadAllText("packages.config")); }
public void RegisterIfNecessaryRegistersWithSharedRepositoryIfRepositoryContainsPackages() { // Arrange var sharedRepository = new Mock<MockPackageRepository>().As<ISharedPackageRepository>(); string path = null; sharedRepository.Setup(m => m.RegisterRepository(It.IsAny<string>())) .Callback<string>(p => path = p); var fileSystem = new MockFileSystem(); IPackage package = PackageUtility.CreatePackage("A"); sharedRepository.Object.AddPackage(package); // Act var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object); referenceRepository.AddPackage(package); referenceRepository.RegisterIfNecessary(); // Assert Assert.Equal(@"C:\MockFileSystem\packages.config", path); }
public void GetPackagesNoPackagesConfigReturnsEmptyList() { // Arrange var repository = new Mock <MockPackageRepository>() { CallBase = true }.As <ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: repository.Object); // Act var packages = referenceRepository.GetPackages().ToList(); // Assert Assert.Equal(0, packages.Count); }
private IEnumerable <PackageDependency> GetReferences(string packagesConfigPath, IMSBuildProjectSystem project = null, string repositoryPath = null, IPackageRepository sourceRepository = null) { // Get the msbuild project project = project ?? NugetUpdateTask.GetMSBuildProject(packagesConfigPath); // Resolve the repository path repositoryPath = repositoryPath ?? GetRepositoryPath(project.Root); var sharedRepositoryFileSystem = new PhysicalFileSystem(repositoryPath); var pathResolver = new DefaultPackagePathResolver(sharedRepositoryFileSystem); // Create the local and source repositories var sharedPackageRepository = new SharedPackageRepository(pathResolver, sharedRepositoryFileSystem, sharedRepositoryFileSystem); var localRepository = new PackageReferenceRepository(project, sharedPackageRepository); sourceRepository = sourceRepository ?? AggregateRepositoryHelper.CreateAggregateRepositoryFromSources(RepositoryFactory, SourceProvider, Source); IPackageConstraintProvider constraintProvider = localRepository; return(GetReferences(localRepository, sharedRepositoryFileSystem, sharedPackageRepository, sourceRepository, constraintProvider, pathResolver, project)); }
private void UpdatePackages(string packagesConfigPath, IMSBuildProjectSystem project = null, string repositoryPath = null, IPackageRepository sourceRepository = null) { // Get the msbuild project project = project ?? GetMSBuildProject(packagesConfigPath); // Resolve the repository path repositoryPath = repositoryPath ?? GetReposioryPath(project.Root); var pathResolver = new DefaultPackagePathResolver(repositoryPath); // Create the local and source repositories var localRepository = new PackageReferenceRepository(project, new SharedPackageRepository(repositoryPath)); sourceRepository = sourceRepository ?? AggregateRepositoryHelper.CreateAggregateRepositoryFromSources(RepositoryFactory, SourceProvider, Source); IPackageConstraintProvider constraintProvider = localRepository; Console.WriteLine(NuGetResources.UpdatingProject, project.ProjectName); UpdatePackages(localRepository, sourceRepository, constraintProvider, pathResolver, project); project.Save(); }
public void GetPackagesWithMalformedPackagesConfigThrows() { // Arrange var repository = new Mock <MockPackageRepository>() { CallBase = true }.As <ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package "); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: repository.Object); // Act ExceptionAssert.Throws <InvalidOperationException>(() => referenceRepository.GetPackages().ToList(), @"Error reading 'C:\MockFileSystem\packages.config'."); }
public IProjectManager CreateProjectManager(IEnumerable <string> remoteSources, string packagesPath, IProjectSystem project) { if (remoteSources == null) { throw new ArgumentNullException("remoteSources"); } if (!remoteSources.Any()) { throw new ArgumentException("Must provide at least one remote source"); } var sourceRepo = new AggregateRepository(PackageRepositoryFactory.Default, remoteSources, true); var pathResolver = new DefaultPackagePathResolver(packagesPath); var packagesConfigRepo = new PackageReferenceRepository(project, project.ProjectName, new SharedPackageRepository(packagesPath)); return(new ProjectManager(sourceRepository: sourceRepo, pathResolver: pathResolver, localRepository: packagesConfigRepo, project: project)); }
public override IProjectManager GetProjectManager(Project project) { IProjectManager projectManager; if (_projectManagers.TryGetValue(project, out projectManager)) { return projectManager; } var projectSystem = new MockVsProjectSystem(project); var localRepository = new PackageReferenceRepository( new MockFileSystem(project.GetFullPath()), project.Name, LocalRepository); projectManager = new ProjectManager( this, PathResolver, projectSystem, localRepository); _projectManagers[project] = projectManager; return projectManager; }
public void AddPackageAddsEntryToPackagesConfig() { // Arrange var sharedRepository = new Mock<ISharedPackageRepository>(); string path = null; sharedRepository.Setup(m => m.RegisterRepository(It.IsAny<string>())) .Callback<string>(p => path = p); var fileSystem = new MockFileSystem(); var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object); var package = PackageUtility.CreatePackage("A"); // Act referenceRepository.AddPackage(package); // Assert Assert.Equal(@"C:\MockFileSystem\packages.config", path); Assert.True(fileSystem.FileExists("packages.config")); Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>", fileSystem.ReadAllText("packages.config")); }
public void AddPackageAddsEntryToPackagesConfigWithTargetFramework() { // Arrange var sharedRepository = new Mock<ISharedPackageRepository>(); string path = null; sharedRepository.Setup(m => m.RegisterRepository(It.IsAny<string>())) .Callback<string>(p => path = p); var fileSystem = new MockFileSystem(); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: sharedRepository.Object); //var package = PackageUtility.CreatePackage("A"); // Act referenceRepository.AddPackage("A", new SemanticVersion("1.0"), false, new FrameworkName("Silverlight, Version=2.0")); // Assert Assert.Equal(@"C:\MockFileSystem\packages.config", path); Assert.True(fileSystem.FileExists("packages.config")); AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" targetFramework=""sl20"" /> </packages>", fileSystem.ReadAllText("packages.config")); }
public void RegisterIfNecessaryRegistersWithSharedRepositoryIfRepositoryContainsPackages() { // Arrange var sharedRepository = new Mock <MockPackageRepository>().As <ISharedPackageRepository>(); string path = null; sharedRepository.Setup(m => m.RegisterRepository(It.IsAny <string>())) .Callback <string>(p => path = p); var fileSystem = new MockFileSystem(); IPackage package = PackageUtility.CreatePackage("A"); sharedRepository.Object.AddPackage(package); // Act var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: sharedRepository.Object); referenceRepository.AddPackage(package); referenceRepository.RegisterIfNecessary(); // Assert Assert.Equal(@"C:\MockFileSystem\packages.config", path); }
private void UpdatePackages(string packagesConfigPath, IMSBuildProjectSystem project = null, string repositoryPath = null, IPackageRepository sourceRepository = null) { // Get the msbuild project project = project ?? GetMSBuildProject(packagesConfigPath); // Resolve the repository path repositoryPath = repositoryPath ?? GetRepositoryPath(project.Root); var sharedRepositoryFileSystem = new PhysicalFileSystem(repositoryPath); var pathResolver = new DefaultPackagePathResolver(sharedRepositoryFileSystem); // Create the local and source repositories var sharedPackageRepository = new SharedPackageRepository(pathResolver, sharedRepositoryFileSystem, sharedRepositoryFileSystem); var localRepository = new PackageReferenceRepository(project, sharedPackageRepository); sourceRepository = sourceRepository ?? AggregateRepositoryHelper.CreateAggregateRepositoryFromSources(RepositoryFactory, SourceProvider, Source); IPackageConstraintProvider constraintProvider = localRepository; Log(Level.Info, "Updating project {0}", project.ProjectName); UpdatePackages(localRepository, sharedRepositoryFileSystem, sharedPackageRepository, sourceRepository, constraintProvider, pathResolver, project); project.Save(); }
public void GetConstraintThrowsIfConstrainInvalid() { // Arrange var repository = new Mock <MockPackageRepository>() { CallBase = true }.As <ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" allowedVersions=""[-1.3, 3)"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: repository.Object); // Act & Assert ExceptionAssert.Throws <InvalidDataException>(() => referenceRepository.GetConstraint("A"), "Unable to parse version value '[-1.3, 3)' from 'packages.config'."); }
public void AddPackageDoesNotAddEntryToPackagesConfigIfExists() { // Arrange var sharedRepository = new Mock<ISharedPackageRepository>(); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object); var package = PackageUtility.CreatePackage("A"); // Act referenceRepository.AddPackage(package); // Assert Assert.True(fileSystem.FileExists("packages.config")); Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>", fileSystem.ReadAllText("packages.config")); }
public void PackageReferenceRepositoryDoNotFindLatestPackageIfItDoesNotExist() { // Arrange var repository = new Mock <MockPackageRepository>() { CallBase = true }.As <ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A", "1.0"); var packageB = PackageUtility.CreatePackage("B", "2.0"); var packageC = PackageUtility.CreatePackage("A", "1.5"); var packageD = PackageUtility.CreatePackage("A", "2.0-beta"); repository.Object.AddPackage(packageA); repository.Object.AddPackage(packageB); repository.Object.AddPackage(packageC); repository.Object.AddPackage(packageD); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> <package id=""B"" version=""2.0"" /> <package id=""A"" version=""1.5"" /> <package id=""A"" version=""2.0-beta"" /> </packages>"); ILatestPackageLookup referenceRepository = new PackageReferenceRepository( fileSystem, repository.Object); // Act SemanticVersion latestVersion; bool result = referenceRepository.TryFindLatestPackageById("C", out latestVersion); // Assert Assert.False(result); Assert.Null(latestVersion); }
public void AddPackageAddsEntryToPackagesConfig() { // Arrange var sharedRepository = new Mock <ISharedPackageRepository>(); string path = null; sharedRepository.Setup(m => m.RegisterRepository(It.IsAny <string>())) .Callback <string>(p => path = p); var fileSystem = new MockFileSystem(); var referenceRepository = new PackageReferenceRepository(fileSystem, projectName: null, sourceRepository: sharedRepository.Object); var package = PackageUtility.CreatePackage("A"); // Act referenceRepository.AddPackage(package); // Assert Assert.Equal(@"C:\MockFileSystem\packages.config", path); Assert.True(fileSystem.FileExists("packages.config")); AssertConfig(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>", fileSystem.ReadAllText("packages.config")); }
private IProjectManager CreateProjectManager(Project project) { // Create the project system IProjectSystem projectSystem = VsProjectSystemFactory.CreateProjectSystem(project, _fileSystemProvider); var repository = new PackageReferenceRepository(projectSystem, _sharedRepository); // Ensure the logger is null while registering the repository FileSystem.Logger = null; Logger = null; // Ensure that this repository is registered with the shared repository if it needs to be repository.RegisterIfNecessary(); // The source repository of the project is an aggregate since it might need to look for all // available packages to perform updates on dependent packages var sourceRepository = CreateProjectManagerSourceRepository(); var projectManager = new ProjectManager(sourceRepository, PathResolver, projectSystem, repository); // The package reference repository also provides constraints for packages (via the allowedVersions attribute) projectManager.ConstraintProvider = repository; return projectManager; }
public void GetConstraintReturnsConstraintListedForPackageIdInPackagesConfig() { // Arrange var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" allowedVersions=""[1.0, 3.0)"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object); // Act IVersionSpec constraint = referenceRepository.GetConstraint("A"); // Assert Assert.NotNull(constraint); Assert.True(constraint.IsMinInclusive); Assert.False(constraint.IsMaxInclusive); Assert.Equal(new SemanticVersion("1.0"), constraint.MinVersion); Assert.Equal(new SemanticVersion("3.0"), constraint.MaxVersion); }
public void GetPackageTargetFrameworkReturnsCorrectValue() { // Arrange var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" targetFramework=""winrt45"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object); // Act var targetFramework = referenceRepository.GetPackageTargetFramework("A"); // Assert Assert.Equal(new FrameworkName(".NETCore, Version=4.5"), targetFramework); }
public void ReinstallPackagesDoesNotThrowWithNewDependencyWhenProjectFrameworkChangesIfAllowPrereleaseParameterIsFalseAndPackageVersionIsPrerelease() { // Arrange var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object; var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(new FrameworkName(".NETFramework, Version=v4.0")); var pathResolver = new DefaultPackagePathResolver(projectSystem); var packageReferenceRepository = new PackageReferenceRepository(projectSystem, localRepository); var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, packageReferenceRepository); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); var packageA = PackageUtility.CreatePackageWithDependencySets( "A", "1.2-alpha", new[] { "contentA.txt" }, dependencySets: new PackageDependencySet[] { new PackageDependencySet(new FrameworkName(".NETFramework, Version=v4.0"), new [] { new PackageDependency("B")}), new PackageDependencySet(new FrameworkName("Silverlight, Version=v5.0"), new [] { new PackageDependency("C")}) }); var packageB = PackageUtility.CreatePackage( "B", "1.0", new[] { "contentB.txt" }); var packageC = PackageUtility.CreatePackage( "C", "2.0-beta", new[] { "contentC.txt" }); sourceRepository.Add(packageA); sourceRepository.Add(packageB); sourceRepository.Add(packageC); packageManager.InstallPackage(projectManager, "A", new SemanticVersion("1.2-alpha"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); Assert.True(packageManager.LocalRepository.Exists("A")); Assert.True(projectManager.LocalRepository.Exists("A")); Assert.True(packageManager.LocalRepository.Exists("B")); Assert.True(projectManager.LocalRepository.Exists("B")); Assert.False(packageManager.LocalRepository.Exists("C")); Assert.False(projectManager.LocalRepository.Exists("C")); Assert.True(projectSystem.FileExists("contentA.txt")); Assert.True(projectSystem.FileExists("contentB.txt")); Assert.False(projectSystem.FileExists("contentC.txt")); // now change project's target framework to silverilght projectSystem.ChangeTargetFramework(new FrameworkName("Silverlight, Version=v5.0")); // Act packageManager.ReinstallPackage(projectManager, "A", updateDependencies: true, allowPrereleaseVersions: false, logger: null); // Assert Assert.True(packageManager.LocalRepository.Exists("A")); Assert.True(projectManager.LocalRepository.Exists("A")); Assert.False(packageManager.LocalRepository.Exists("B")); Assert.False(projectManager.LocalRepository.Exists("B")); Assert.True(packageManager.LocalRepository.Exists("C")); Assert.True(projectManager.LocalRepository.Exists("C")); }
private void UpdatePackages(string packagesConfigPath, IMSBuildProjectSystem project = null, string repositoryPath = null, IPackageRepository sourceRepository = null) { // Get the msbuild project project = project ?? GetMSBuildProject(packagesConfigPath); // Resolve the repository path repositoryPath = repositoryPath ?? GetReposioryPath(project.Root); var pathResolver = new DefaultPackagePathResolver(repositoryPath); // Create the local and source repositories var localRepository = new PackageReferenceRepository(project, new SharedPackageRepository(repositoryPath)); sourceRepository = sourceRepository ?? AggregateRepositoryHelper.CreateAggregateRepositoryFromSources(RepositoryFactory, SourceProvider, Source); IPackageConstraintProvider constraintProvider = localRepository; Console.WriteLine(NuGetResources.UpdatingProject, project.ProjectName); UpdatePackages(localRepository, sourceRepository, constraintProvider, pathResolver, project); project.Save(); }
public void GetPackagesWithMalformedPackagesConfigThrows() { // Arrange var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package "); var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object); // Act ExceptionAssert.Throws<InvalidOperationException>(() => referenceRepository.GetPackages().ToList(), @"Error reading 'C:\MockFileSystem\packages.config'."); }
public void GetPackageReferencesReturnsFalseForNonExistentId() { // Arrange var config = @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.3.4"" /> <package id=""A"" version=""2.5-beta"" /> <package id=""B"" version=""1.0"" /> <package id=""C"" version=""2.1.4"" /> </packages>"; var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", config); var sharedRepository = new Mock<MockPackageRepository>().As<ISharedPackageRepository>(); var packageReferenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object); // Act SemanticVersion version; bool result = packageReferenceRepository.TryFindLatestPackageById("does-not-exist", out version); // Assert Assert.False(result); Assert.Null(version); }
public void PackageReferenceRepositoryImplementsILatestPackageLookupInterface() { // Arrange var repository = new PackageReferenceRepository(new Mock<IFileSystem>().Object, new Mock<ISharedPackageRepository>().Object); // Assert Assert.True(repository is ILatestPackageLookup); }
public void CallingCheckForMissingPackagesRaisesThePackagesMissingStatusChangedEventWithFalseValue() { // Scenario: // Project's packages.config specifies: A[1.0], B[1.2-alpha] // The solution's packages folder contains only A[1.0], B[1.2-alpha] // Arrange string tempSolutionPath = "x:\\project1"; var project = new Mock<Project>(); // setup SolutionManager var solutionManager = new Mock<ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); solutionManager.Setup(p => p.GetProjects()).Returns(new[] { project.Object }); // setup file system var fileSystem = new Mock<IFileSystem>(); fileSystem.Setup(p => p.DirectoryExists(".nuget")).Returns(true); fileSystem.Setup(p => p.FileExists(".nuget\\NuGet.exe")).Returns(true); fileSystem.Setup(p => p.FileExists(".nuget\\NuGet.targets")).Returns(true); var fileSystemProvider = new Mock<IFileSystemProvider>(); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem.Object); // setup VsPackageManager var projectFileSystem = new MockFileSystem(); projectFileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0.0.0"" /> <package id=""B"" version=""1.2-alpha"" /> </packages>"); var packageReferenceRepository = new PackageReferenceRepository(projectFileSystem, new Mock<ISharedPackageRepository>().Object); var projectManager = new Mock<IProjectManager>(); projectManager.Setup(p => p.LocalRepository).Returns(packageReferenceRepository); var localRepository = new MockPackageRepository(); localRepository.AddPackage(PackageUtility.CreatePackage("A", "1.0")); localRepository.AddPackage(PackageUtility.CreatePackage("B", "1.2-alpha")); var packageManager = new Mock<IVsPackageManager>(); packageManager.Setup(p => p.GetProjectManager(project.Object)).Returns(projectManager.Object); packageManager.Setup(p => p.LocalRepository).Returns(localRepository); var packageManagerFactory = new Mock<IVsPackageManagerFactory>(); packageManagerFactory.Setup(p => p.CreatePackageManager()).Returns(packageManager.Object); var packageRestore = CreateInstance( fileSystemProvider: fileSystemProvider.Object, solutionManager: solutionManager.Object, packageManagerFactory: packageManagerFactory.Object); bool? packagesMissing = null; packageRestore.PackagesMissingStatusChanged += (o, e) => { packagesMissing = e.PackagesMissing; }; // Act packageRestore.CheckForMissingPackages(); // Assert Assert.Equal(false, packagesMissing); }
public void CallingCheckForMissingPackagesRaisesThePackagesMissingStatusChangedEventWithTrueValueIfPackagesFolderIsMissing() { // Scenario: // Project's packages.config specifies: A[1.0] // The solution's packages folder doesn't contain any packages // Arrange string tempSolutionPath = "x:\\project1"; var project = new Mock<Project>(); // setup SolutionManager var solutionManager = new Mock<ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); solutionManager.Setup(p => p.GetProjects()).Returns(new[] { project.Object }); // setup file system var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict); var fileSystemProvider = new Mock<IFileSystemProvider>(); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem.Object); // setup VsPackageManager var projectFileSystem = new MockFileSystem(); projectFileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0.0.0"" /> </packages>"); var packageReferenceRepository = new PackageReferenceRepository(projectFileSystem, projectName: null, sourceRepository: new Mock<ISharedPackageRepository>().Object); var projectManager = new Mock<IProjectManager>(); projectManager.Setup(p => p.LocalRepository).Returns(packageReferenceRepository); var localRepository = new MockSharedPackageRepository(); var packageManager = new Mock<IVsPackageManager>(); packageManager.Setup(p => p.GetProjectManager(project.Object)).Returns(projectManager.Object); packageManager.Setup(p => p.LocalRepository).Returns(localRepository); var packageManagerFactory = new Mock<IVsPackageManagerFactory>(); packageManagerFactory.Setup(p => p.CreatePackageManager()).Returns(packageManager.Object); var packageRestore = CreateInstance( fileSystemProvider: fileSystemProvider.Object, solutionManager: solutionManager.Object, packageManagerFactory: packageManagerFactory.Object); bool? packagesMissing = null; packageRestore.PackagesMissingStatusChanged += (o, e) => { packagesMissing = e.PackagesMissing; }; // Act packageRestore.CheckForMissingPackages(); // Assert Assert.Equal(true, packagesMissing); }
public void RemovePackageRemovesEntryFromPackagesConfigDeletesFileAndUnregistersRepositoryIfLastEntry() { // Arrange var sharedRepository = new Mock<ISharedPackageRepository>(); string path = null; sharedRepository.Setup(m => m.UnregisterRepository(It.IsAny<string>())) .Callback<string>(p => path = p); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object); var package = PackageUtility.CreatePackage("A"); // Act referenceRepository.RemovePackage(package); // Assert Assert.False(fileSystem.FileExists("packages.config")); Assert.NotNull(path); Assert.Equal(@"C:\MockFileSystem\packages.config", path); }
public void RemovingAndAddingPackageReferenceWithSameIdPreservesConstraint() { // Arrange var sharedRepository = new Mock<ISharedPackageRepository>(); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" allowedVersions=""[1.0, 5.0)"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object); var A10 = PackageUtility.CreatePackage("A"); var A20 = PackageUtility.CreatePackage("A", "2.0"); // Act referenceRepository.RemovePackage(A10); referenceRepository.AddPackage(A20); // Assert Assert.True(fileSystem.FileExists("packages.config")); Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""2.0"" allowedVersions=""[1.0, 5.0)"" /> </packages>", fileSystem.ReadAllText("packages.config")); }
public void GetPackagesReturnsOnlyValidPackagesFromSourceRepositoryListedInPackagesConfig() { // Arrange var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var packageC = PackageUtility.CreatePackage("C", "2.0"); repository.Object.AddPackage(packageC); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""C"" version=""2.0"" /> <package id=""B"" version=""1.0"" /> <package id=""A"" version=""1.0"" /> <package id="""" version=""1.0"" /> <package id=""G"" version="""" /> <package /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object); // Act var packages = referenceRepository.GetPackages().ToList(); // Assert Assert.Equal(2, packages.Count); Assert.Same(packageC, packages[0]); Assert.Same(packageA, packages[1]); Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""C"" version=""2.0"" /> <package id=""B"" version=""1.0"" /> <package id=""A"" version=""1.0"" /> <package id="""" version=""1.0"" /> <package id=""G"" version="""" /> <package /> </packages>", fileSystem.ReadAllText("packages.config")); }
public void ReinstallPackagesSkipReinstallingForPackagesThatDoNotExistInSource() { // Arrange var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object; var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(new FrameworkName(".NETFramework, Version=v3.0")); var pathResolver = new DefaultPackagePathResolver(projectSystem); var packageReferenceRepository = new PackageReferenceRepository(projectSystem, localRepository); var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, packageReferenceRepository); var installerEvents = new Mock<VsPackageInstallerEvents>(MockBehavior.Strict); int eventCount = 0; RegisterInstallerEvents(installerEvents, _ => eventCount++); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, installerEvents.Object); var packageA = PackageUtility.CreatePackage( "A", "1.2-alpha", new[] { "net30\\content.txt", "silverlight40\\content4.txt" }, new[] { "lib\\net30\\ref.dll", "lib\\silverlight40\\refsl.dll" }); var packageB = PackageUtility.CreatePackage( "B", "2.0", new[] { "net30\\contentB.txt", "silverlight40\\content4B.txt" }, new[] { "lib\\net30\\refB.dll", "lib\\silverlight40\\refslB.dll" }); localRepository.AddPackage(packageA); localRepository.AddPackage(packageB); projectManager.LocalRepository.AddPackage(packageA); projectManager.LocalRepository.AddPackage(packageB); // now change project's target framework to silverlight projectSystem.ChangeTargetFramework(new FrameworkName("Silverlight, Version=v4.0")); var logger = new Mock<ILogger>(); logger.Setup(s => s.Log( MessageLevel.Warning, "Skipped reinstalling package '{0}' in project '{1}' because the package does not exist in the package source.", "B 2.0", "x:\\MockFileSystem") ).Verifiable(); logger.Setup(s => s.Log( MessageLevel.Warning, "Skipped reinstalling package '{0}' in project '{1}' because the package does not exist in the package source.", "A 1.2-alpha", "x:\\MockFileSystem") ).Verifiable(); // Act packageManager.ReinstallPackages(projectManager, updateDependencies: false, allowPrereleaseVersions: true, logger: logger.Object); // Assert logger.Verify(); Assert.Equal(0, eventCount); Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(packageManager.LocalRepository.Exists("B")); Assert.True(projectManager.LocalRepository.Exists("B")); }
public void ReinstallPackagesRestoresPackageInAllProjectsWithNewContentIfSourcePackageChanges() { // Arrange var localRepositoryMock = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var localRepository = localRepositoryMock.Object; var sourceRepository = new MockPackageRepository(); var projectSystem1 = new MockProjectSystem(); var pathResolver1 = new DefaultPackagePathResolver(projectSystem1); var packageReferenceRepository1 = new PackageReferenceRepository(projectSystem1, localRepository); var projectManager1 = new ProjectManager(localRepository, pathResolver1, projectSystem1, packageReferenceRepository1); var projectSystem2 = new MockProjectSystem(); var pathResolver2 = new DefaultPackagePathResolver(projectSystem2); var packageReferenceRepository2 = new PackageReferenceRepository(projectSystem2, localRepository); var projectManager2 = new ProjectManager(localRepository, pathResolver2, projectSystem2, packageReferenceRepository2); var project1 = TestUtils.GetProject("Project1"); var project2 = TestUtils.GetProject("Project2"); var packageManager = new MockVsPackageManager( TestUtils.GetSolutionManager(projects: new[] { project1, project2 }), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem2, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); packageManager.RegisterProjectManager(project1, projectManager1); packageManager.RegisterProjectManager(project2, projectManager2); var packageA = PackageUtility.CreatePackage( "A", "1.2-alpha", new[] { "content.txt" }, new[] { "lib\\ref.dll" }); var packageA2 = PackageUtility.CreatePackage( "A", "1.2-alpha", new[] { "foo.txt" }, new[] { "lib\\bar.dll" }); var packageB = PackageUtility.CreatePackage( "B", "2.0", new[] { "hello.txt" }, new[] { "lib\\comma.dll" }); var packageB2 = PackageUtility.CreatePackage( "B", "2.0", new[] { "world.txt" }, new[] { "lib\\dude.dll" }); sourceRepository.Add(packageA); sourceRepository.Add(packageB); // install package A -> project 1 // and package B -> project 2 packageManager.InstallPackage(projectManager1, "A", new SemanticVersion("1.2-alpha"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); packageManager.InstallPackage(projectManager2, "B", new SemanticVersion("2.0"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager1.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectSystem1.FileExists("content.txt")); Assert.True(projectSystem1.ReferenceExists("ref.dll")); Assert.True(packageManager.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectManager2.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectSystem2.FileExists("hello.txt")); Assert.True(projectSystem2.ReferenceExists("comma.dll")); // now change the package A and B to different packages sourceRepository.RemovePackage(packageA); sourceRepository.RemovePackage(packageB); sourceRepository.AddPackage(packageA2); sourceRepository.AddPackage(packageB2); localRepositoryMock.Setup(p => p.IsReferenced("A", new SemanticVersion("1.2-alpha"))).Returns((string id, SemanticVersion version) => projectManager1.LocalRepository.Exists(id, version)); localRepositoryMock.Setup(p => p.IsReferenced("B", new SemanticVersion("2.0"))).Returns((string id, SemanticVersion version) => projectManager2.LocalRepository.Exists(id, version)); // Act packageManager.ReinstallPackages(updateDependencies: true, allowPrereleaseVersions: false, logger: NullLogger.Instance, eventListener: NullPackageOperationEventListener.Instance); // Assert Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager1.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.False(projectSystem1.FileExists("content.txt")); Assert.True(projectSystem1.FileExists("foo.txt")); Assert.False(projectSystem1.ReferenceExists("ref.dll")); Assert.True(projectSystem1.ReferenceExists("bar.dll")); Assert.True(packageManager.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectManager2.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.False(projectSystem2.FileExists("hello.txt")); Assert.True(projectSystem2.FileExists("world.txt")); Assert.False(projectSystem2.ReferenceExists("comma.dll")); Assert.True(projectSystem2.ReferenceExists("dude.dll")); }
public void ReinstallPackagesRestoresAllPackagesInAProjectWithNewContentIfProjectFrameworkChanges() { // Arrange var localRepository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>().Object; var sourceRepository = new MockPackageRepository(); var projectSystem = new MockProjectSystem(new FrameworkName(".NETFramework, Version=v3.0")); var pathResolver = new DefaultPackagePathResolver(projectSystem); var packageReferenceRepository = new PackageReferenceRepository(projectSystem, localRepository); var projectManager = new ProjectManager(localRepository, pathResolver, projectSystem, packageReferenceRepository); var packageManager = new VsPackageManager( TestUtils.GetSolutionManager(), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); var packageA = PackageUtility.CreatePackage( "A", "1.2-alpha", new[] { "net30\\content.txt", "silverlight40\\content4.txt" }, new[] { "lib\\net30\\ref.dll", "lib\\silverlight40\\refsl.dll" }); var packageB = PackageUtility.CreatePackage( "B", "2.0", new[] { "net30\\contentB.txt", "silverlight40\\content4B.txt" }, new[] { "lib\\net30\\refB.dll", "lib\\silverlight40\\refslB.dll" }); sourceRepository.Add(packageA); sourceRepository.Add(packageB); packageManager.InstallPackage(projectManager, "A", new SemanticVersion("1.2-alpha"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); packageManager.InstallPackage(projectManager, "B", new SemanticVersion("2.0"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectSystem.FileExists("content.txt")); Assert.False(projectSystem.FileExists("content4.txt")); Assert.True(projectSystem.ReferenceExists("ref.dll")); Assert.False(projectSystem.ReferenceExists("refsl.dll")); Assert.True(packageManager.LocalRepository.Exists("B")); Assert.True(projectManager.LocalRepository.Exists("B")); Assert.True(projectSystem.FileExists("contentB.txt")); Assert.False(projectSystem.FileExists("content4B.txt")); Assert.True(projectSystem.ReferenceExists("refB.dll")); Assert.False(projectSystem.ReferenceExists("refslB.dll")); // now change project's target framework to silverlight projectSystem.ChangeTargetFramework(new FrameworkName("Silverlight, Version=v4.0")); // Act packageManager.ReinstallPackages(projectManager, updateDependencies: true, allowPrereleaseVersions: false, logger: null); // Assert Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.False(projectSystem.FileExists("content.txt")); Assert.True(projectSystem.FileExists("content4.txt")); Assert.False(projectSystem.ReferenceExists("ref.dll")); Assert.True(projectSystem.ReferenceExists("refsl.dll")); Assert.True(packageManager.LocalRepository.Exists("B")); Assert.True(projectManager.LocalRepository.Exists("B")); Assert.False(projectSystem.FileExists("contentB.txt")); Assert.True(projectSystem.FileExists("content4B.txt")); Assert.False(projectSystem.ReferenceExists("refB.dll")); Assert.True(projectSystem.ReferenceExists("refslB.dll")); }
public void GetPackageTargetFrameworkReturnsNullIfPackageIdIsNotPresent() { // Arrange var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object); // Act var targetFramework = referenceRepository.GetPackageTargetFramework("B"); // Assert Assert.Null(targetFramework); }
public void ReinstallPackagesSkipRestallingForOneProjectButProceedWithTheOther() { // Arrange var localRepositoryMock = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var localRepository = localRepositoryMock.Object; var sourceRepository = new MockPackageRepository(); var projectSystem1 = new MockProjectSystem(new FrameworkName(".NETFramework, Version=v3.0")); var pathResolver1 = new DefaultPackagePathResolver(projectSystem1); var packageReferenceRepository1 = new PackageReferenceRepository(projectSystem1, localRepository); var projectManager1 = new ProjectManager(localRepository, pathResolver1, projectSystem1, packageReferenceRepository1); var projectSystem2 = new MockProjectSystem(new FrameworkName(".NETCore, Version=v4.5")); var pathResolver2 = new DefaultPackagePathResolver(projectSystem2); var packageReferenceRepository2 = new PackageReferenceRepository(projectSystem2, localRepository); var projectManager2 = new ProjectManager(localRepository, pathResolver2, projectSystem2, packageReferenceRepository2); var project1 = TestUtils.GetProject("Project1"); var project2 = TestUtils.GetProject("Project2"); var packageManager = new MockVsPackageManager( TestUtils.GetSolutionManager(projects: new[] { project1, project2 }), sourceRepository, new Mock<IFileSystemProvider>().Object, projectSystem2, localRepository, new Mock<IDeleteOnRestartManager>().Object, new Mock<VsPackageInstallerEvents>().Object); packageManager.RegisterProjectManager(project1, projectManager1); packageManager.RegisterProjectManager(project2, projectManager2); var packageA = PackageUtility.CreatePackage( "A", "1.2-alpha", new[] { "net30\\content.txt", "silverlight40\\content4.txt" }, new[] { "lib\\net30\\ref.dll", "lib\\silverlight40\\refsl.dll" }); var packageB = PackageUtility.CreatePackage( "B", "2.0", new[] { "winrt45\\hello.txt", "sl4-wp71\\world.txt" }, new[] { "lib\\winrt45\\comma.dll", "lib\\sl4-wp71\\dude.dll" }); sourceRepository.Add(packageA); localRepository.AddPackage(packageA); localRepository.AddPackage(packageB); // install package A -> project 1 // and package B -> project 2 packageManager.InstallPackage(projectManager1, "A", new SemanticVersion("1.2-alpha"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); packageManager.InstallPackage(projectManager2, "B", new SemanticVersion("2.0"), ignoreDependencies: false, allowPrereleaseVersions: true, logger: null); // now change project's target framework to silverlight projectSystem1.ChangeTargetFramework(new FrameworkName("Silverlight, Version=v4.0")); projectSystem2.ChangeTargetFramework(new FrameworkName("Silverlight, Version=v4.0, Profile=WindowsPhone71")); localRepositoryMock.Setup(p => p.IsReferenced("A", new SemanticVersion("1.2-alpha"))).Returns((string id, SemanticVersion version) => projectManager1.LocalRepository.Exists(id, version)); localRepositoryMock.Setup(p => p.IsReferenced("B", new SemanticVersion("2.0"))).Returns((string id, SemanticVersion version) => projectManager2.LocalRepository.Exists(id, version)); var logger = new Mock<ILogger>(); logger.Setup(s => s.Log( MessageLevel.Warning, "Skipped reinstalling package '{0}' in project '{1}' because the package does not exist in the package source.", "B 2.0", "Project2") ).Verifiable(); // Act packageManager.ReinstallPackages(updateDependencies: true, allowPrereleaseVersions: false, logger: logger.Object, eventListener: NullPackageOperationEventListener.Instance); // Assert logger.Verify(); Assert.True(packageManager.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.True(projectManager1.LocalRepository.Exists("A", new SemanticVersion("1.2-alpha"))); Assert.False(projectSystem1.FileExists("content.txt")); Assert.True(projectSystem1.FileExists("content4.txt")); Assert.False(projectSystem1.ReferenceExists("ref.dll")); Assert.True(projectSystem1.ReferenceExists("refsl.dll")); Assert.True(packageManager.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectManager2.LocalRepository.Exists("B", new SemanticVersion("2.0"))); Assert.True(projectSystem2.FileExists("hello.txt")); Assert.False(projectSystem2.FileExists("world.txt")); Assert.True(projectSystem2.ReferenceExists("comma.dll")); Assert.False(projectSystem2.ReferenceExists("dude.dll")); }
public void GetConstraintThrowsIfConstrainInvalid() { // Arrange var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A"); repository.Object.AddPackage(packageA); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" allowedVersions=""[-1.3, 3)"" /> </packages>"); var referenceRepository = new PackageReferenceRepository(fileSystem, repository.Object); // Act & Assert ExceptionAssert.Throws<InvalidDataException>(() => referenceRepository.GetConstraint("A"), "Unable to parse version value '[-1.3, 3)' from 'packages.config'."); }
public void CallingRestoreMissingPackagesMethodInstallMissingPackages() { // Scenario: // Project's packages.config specifies: A[1.0] // The solution's packages folder contains only A[1.0], B[1.2-alpha], C[2.0-RC1] // Call RestoreMissingPackages() will install B[1.2-alpha] and C[2.0-RC1] into the solution // Arrange string tempSolutionPath = "x:\\project1"; var project = new Mock<Project>(); // setup SolutionManager var solutionManager = new Mock<ISolutionManager>(); solutionManager.Setup(p => p.IsSolutionOpen).Returns(true); solutionManager.Setup(p => p.SolutionDirectory).Returns(tempSolutionPath); solutionManager.Setup(p => p.GetProjects()).Returns(new[] { project.Object }); // setup file system var fileSystem = new Mock<IFileSystem>(); fileSystem.Setup(p => p.DirectoryExists(".nuget")).Returns(true); fileSystem.Setup(p => p.FileExists(".nuget\\NuGet.exe")).Returns(true); fileSystem.Setup(p => p.FileExists(".nuget\\NuGet.targets")).Returns(true); var fileSystemProvider = new Mock<IFileSystemProvider>(); fileSystemProvider.Setup(p => p.GetFileSystem(tempSolutionPath)).Returns(fileSystem.Object); // setup VsPackageManager string tempFile = @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0.0.0"" /> <package id=""B"" version=""1.2-alpha"" /> <package id=""C"" version=""2.0-RC1"" /> </packages>"; var projectFileSystem = new MockFileSystem(); projectFileSystem.AddFile("packages.config", tempFile); var packageReferenceRepository = new PackageReferenceRepository(projectFileSystem, new Mock<ISharedPackageRepository>().Object); var projectManager = new Mock<IProjectManager>(); projectManager.Setup(p => p.LocalRepository).Returns(packageReferenceRepository); var localRepository = new MockPackageRepository(); localRepository.AddPackage(PackageUtility.CreatePackage("A", "1.0")); var packageManager = new Mock<IVsPackageManager>(); packageManager.Setup(p => p.GetProjectManager(project.Object)).Returns(projectManager.Object); packageManager.Setup(p => p.LocalRepository).Returns(localRepository); var packageManagerFactory = new Mock<IVsPackageManagerFactory>(); packageManagerFactory.Setup(p => p.CreatePackageManager()).Returns(packageManager.Object); var packageRestore = CreateInstance( fileSystemProvider: fileSystemProvider.Object, solutionManager: solutionManager.Object, packageManagerFactory: packageManagerFactory.Object); Exception exception = null; var manualEvent = new ManualResetEventSlim(); // Act packageRestore.RestoreMissingPackages().ContinueWith( task => { if (task.IsFaulted) { exception = task.Exception; manualEvent.Set(); return; } try { // Assert packageManager.Verify(p => p.InstallPackage("A", new SemanticVersion("1.0.0.0"), true, true), Times.Never()); packageManager.Verify(p => p.InstallPackage("B", new SemanticVersion("1.2-alpha"), true, true), Times.Once()); packageManager.Verify(p => p.InstallPackage("C", new SemanticVersion("2.0-RC1"), true, true), Times.Once()); } catch (Exception testException) { exception = testException; } finally { manualEvent.Set(); } }); manualEvent.Wait(); Assert.Null(exception); }
public void PackageReferenceRepositoryDoNotFindLatestPackageIfItDoesNotExist() { // Arrange var repository = new Mock<MockPackageRepository>() { CallBase = true }.As<ISharedPackageRepository>(); var packageA = PackageUtility.CreatePackage("A", "1.0"); var packageB = PackageUtility.CreatePackage("B", "2.0"); var packageC = PackageUtility.CreatePackage("A", "1.5"); var packageD = PackageUtility.CreatePackage("A", "2.0-beta"); repository.Object.AddPackage(packageA); repository.Object.AddPackage(packageB); repository.Object.AddPackage(packageC); repository.Object.AddPackage(packageD); var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.0"" /> <package id=""B"" version=""2.0"" /> <package id=""A"" version=""1.5"" /> <package id=""A"" version=""2.0-beta"" /> </packages>"); ILatestPackageLookup referenceRepository = new PackageReferenceRepository( fileSystem, repository.Object); // Act SemanticVersion latestVersion; bool result = referenceRepository.TryFindLatestPackageById("C", out latestVersion); // Assert Assert.False(result); Assert.Null(latestVersion); }
private void UpdatePackages(IMSBuildProjectSystem project, string repositoryPath = null, IPackageRepository sourceRepository = null) { // Resolve the repository path repositoryPath = repositoryPath ?? GetRepositoryPath(project.Root); var sharedRepositoryFileSystem = new PhysicalFileSystem(repositoryPath); var pathResolver = new DefaultPackagePathResolver(sharedRepositoryFileSystem); // Create the local and source repositories var sharedPackageRepository = new SharedPackageRepository(pathResolver, sharedRepositoryFileSystem, sharedRepositoryFileSystem); var localRepository = new PackageReferenceRepository(project, project.ProjectName, sharedPackageRepository); sourceRepository = sourceRepository ?? AggregateRepositoryHelper.CreateAggregateRepositoryFromSources(RepositoryFactory, SourceProvider, Source); Console.WriteLine(LocalizedResourceManager.GetString("UpdatingProject"), project.ProjectName); UpdatePackages(localRepository, sharedRepositoryFileSystem, sharedPackageRepository, sourceRepository, localRepository, pathResolver, project); project.Save(); }
public void GetPackageReferencesFindTheOnlyVersionAsLatestVersion() { // Arrange var config = @"<?xml version=""1.0"" encoding=""utf-8""?> <packages> <package id=""A"" version=""1.3.4"" /> <package id=""A"" version=""2.5-beta"" /> <package id=""B"" version=""1.0"" /> <package id=""C"" version=""2.1.4"" /> </packages>"; var fileSystem = new MockFileSystem(); fileSystem.AddFile("packages.config", config); var sharedRepository = new Mock<MockPackageRepository>().As<ISharedPackageRepository>(); var packageReferenceRepository = new PackageReferenceRepository(fileSystem, sharedRepository.Object); // Act SemanticVersion version; bool result = packageReferenceRepository.TryFindLatestPackageById("B", out version); // Assert Assert.True(result); Assert.Equal(new SemanticVersion("1.0"), version); }