public void FactoryWithValidPath()
        {
            using (var wpf = new WpfProxy())
                using (var list = new EnvironmentListProxy(wpf)) {
                    var service  = new MockInterpreterOptionsService();
                    var provider = new MockPythonInterpreterFactoryProvider("Test Provider");
                    service.AddProvider(provider);
                    list.Service = service;

                    foreach (var version in PythonPaths.Versions)
                    {
                        Console.WriteLine("Path: <{0}>", version.InterpreterPath);
                        provider.RemoveAllFactories();
                        provider.AddFactory(new MockPythonInterpreterFactory(
                                                Guid.NewGuid(),
                                                "Test Factory",
                                                version.Configuration
                                                ));
                        var view = list.Environments.Single();
                        Assert.IsTrue(
                            list.CanExecute(DBExtension.StartRefreshDB, view),
                            string.Format("Cannot refresh DB for {0}", version.InterpreterPath)
                            );
                    }
                }
        }
        public void AddFactories()
        {
            var mockService = new MockInterpreterOptionsService();

            using (var wpf = new WpfProxy())
                using (var list = wpf.Invoke(() => new EnvironmentListProxy(wpf))) {
                    var provider = new MockPythonInterpreterFactoryProvider("Test Provider 1",
                                                                            new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Factory 1", MockInterpreterConfiguration(new Version(2, 7))),
                                                                            new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Factory 2", MockInterpreterConfiguration(new Version(3, 0))),
                                                                            new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Factory 3", MockInterpreterConfiguration(new Version(3, 3)))
                                                                            );

                    list.Service = mockService;

                    Assert.AreEqual(0, list.Environments.Count);

                    mockService.AddProvider(provider);
                    Assert.AreEqual(3, list.Environments.Count);
                    provider.AddFactory(new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Factory 4", MockInterpreterConfiguration(new Version(2, 7))));
                    Assert.AreEqual(4, list.Environments.Count);
                    provider.AddFactory(new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Factory 5", MockInterpreterConfiguration(new Version(3, 0))));
                    Assert.AreEqual(5, list.Environments.Count);
                    provider.AddFactory(new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Factory 6", MockInterpreterConfiguration(new Version(3, 3))));
                    Assert.AreEqual(6, list.Environments.Count);
                }
        }
Example #3
0
        private MockInterpreterOptionsService MakeEmptyVEnv()
        {
            var python = PythonPaths.Versions.FirstOrDefault(p =>
                                                             p.IsCPython && Directory.Exists(Path.Combine(p.LibPath, "venv"))
                                                             );

            if (python == null)
            {
                Assert.Inconclusive("Requires Python with venv");
            }

            var env = TestData.GetTempPath(randomSubPath: true);

            if (env.Length > 140)
            {
                env = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                DeleteFolder.Add(env);
            }
            using (var proc = ProcessOutput.RunHiddenAndCapture(
                       python.InterpreterPath, "-m", "venv", env, "--clear"
                       )) {
                Console.WriteLine(proc.Arguments);
                proc.Wait();
                foreach (var line in proc.StandardOutputLines.Concat(proc.StandardErrorLines))
                {
                    Console.WriteLine(line);
                }
                Assert.AreEqual(0, proc.ExitCode ?? -1, "Failed to create venv");
            }

            // Forcibly remove pip so we can reinstall it
            foreach (var dir in Directory.EnumerateDirectories(Path.Combine(env, "lib", "site-packages")))
            {
                Directory.Delete(dir, true);
            }

            var service  = new MockInterpreterOptionsService();
            var provider = new MockPythonInterpreterFactoryProvider("VEnv Provider");

            provider.AddFactory(new MockPythonInterpreterFactory(
                                    new InterpreterConfiguration(
                                        "Mock;" + Guid.NewGuid().ToString(),
                                        Path.GetFileName(PathUtils.TrimEndSeparator(env)),
                                        env,
                                        PathUtils.FindFile(env, "python.exe"),
                                        PathUtils.FindFile(env, "python.exe"),
                                        Path.GetDirectoryName(PathUtils.FindFile(env, "site.py", 3)),
                                        "PYTHONPATH",
                                        python.Isx64 ? ProcessorArchitecture.Amd64 : ProcessorArchitecture.X86,
                                        python.Version.ToVersion()
                                        )
                                    ));
            service.AddProvider(provider);
            return(service);
        }
Example #4
0
        public void NonDefaultInterpreter()
        {
            var mockProvider = new MockPythonInterpreterFactoryProvider("Test Provider 1",
                                                                        new MockPythonInterpreterFactory(MockInterpreterConfiguration("Test Factory 1", new Version(2, 7))),
                                                                        new MockPythonInterpreterFactory(MockInterpreterConfiguration("Test Factory 2", new Version(3, 0), InterpreterUIMode.CannotBeDefault)),
                                                                        new MockPythonInterpreterFactory(MockInterpreterConfiguration("Test Factory 3", new Version(3, 3), InterpreterUIMode.CannotBeAutoDefault))
                                                                        );

            using (var wpf = new WpfProxy())
                using (var list = new EnvironmentListProxy(wpf)) {
                    var container    = CreateCompositionContainer();
                    var service      = container.GetExportedValue <IInterpreterOptionsService>();
                    var interpreters = container.GetExportedValue <IInterpreterRegistryService>();
                    var oldDefault   = service.DefaultInterpreter;
                    var oldProviders = ((InterpreterRegistryService)interpreters).SetProviders(new[] {
                        new Lazy <IPythonInterpreterFactoryProvider, Dictionary <string, object> >(
                            () => mockProvider,
                            new Dictionary <string, object>()
                        {
                            { "InterpreterFactoryId", "Mock" }
                        }
                            )
                    });
                    try {
                        list.Service      = service;
                        list.Interpreters = interpreters;
                        var environments = list.Environments;

                        AssertUtil.AreEqual(
                            wpf.Invoke(() => environments.Select(ev => ev.Description).ToList()),
                            "Test Factory 1", "Test Factory 2", "Test Factory 3"
                            );
                        // TF 1 and 3 can be set as default
                        AssertUtil.AreEqual(
                            wpf.Invoke(() => environments.Select(ev => ev.CanBeDefault).ToList()),
                            true, false, true
                            );
                    } finally {
                        ((InterpreterRegistryService)interpreters).SetProviders(oldProviders);
                        service.DefaultInterpreter = oldDefault;
                    }
                }
        }
Example #5
0
        public void LoadUnloadProjectFactories()
        {
            var service      = new MockInterpreterOptionsService();
            var mockProvider = new MockPythonInterpreterFactoryProvider("Test Provider");

            mockProvider.AddFactory(new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Environment", MockInterpreterConfiguration(new Version(2, 7))));
            service.AddProvider(mockProvider);

            var loaded = new LoadedProjectInterpreterFactoryProvider();

            service.AddProvider(loaded);
            using (var wpf = new WpfProxy())
                using (var list = new EnvironmentListProxy(wpf)) {
                    list.Service = service;

                    // List only contains one entry
                    AssertUtil.ContainsExactly(
                        wpf.Invoke(() => list.Environments.Select(ev => ev.Description).ToList()),
                        "Test Environment"
                        );

                    var project = new MockPythonInterpreterFactoryProvider("Fake Project");
                    project.AddFactory(new MockPythonInterpreterFactory(Guid.NewGuid(), "Fake Environment", MockInterpreterConfiguration(new Version(2, 7))));

                    loaded.ProjectLoaded(project, null);

                    // List now contains two entries
                    AssertUtil.ContainsExactly(
                        wpf.Invoke(() => list.Environments.Select(ev => ev.Description).ToList()),
                        "Test Environment",
                        "Fake Environment"
                        );

                    loaded.ProjectUnloaded(project);

                    // List only has one entry again
                    AssertUtil.ContainsExactly(
                        wpf.Invoke(() => list.Environments.Select(ev => ev.Description).ToList()),
                        "Test Environment"
                        );
                }
        }
Example #6
0
        public void FactoryWithInvalidPath()
        {
            using (var wpf = new WpfProxy())
                using (var list = new EnvironmentListProxy(wpf)) {
                    var service  = new MockInterpreterOptionsService();
                    var provider = new MockPythonInterpreterFactoryProvider("Test Provider");
                    service.AddProvider(provider);
                    list.Service      = service;
                    list.Interpreters = service;

                    foreach (string invalidPath in new string[] {
                        null,
                        "",
                        "NOT A REAL PATH",
                        string.Join("\\", Path.GetInvalidPathChars().Select(c => c.ToString()))
                    })
                    {
                        Console.WriteLine("Path: <{0}>", invalidPath ?? "(null)");
                        provider.RemoveAllFactories();
                        provider.AddFactory(new MockPythonInterpreterFactory(
                                                new InterpreterConfiguration(
                                                    "Mock;" + Guid.NewGuid().ToString(),
                                                    "Test Factory",
                                                    invalidPath,
                                                    invalidPath,
                                                    "",
                                                    "",
                                                    "",
                                                    ProcessorArchitecture.None,
                                                    new Version(2, 7)
                                                    )
                                                ));
                        var view = list.Environments.Single();
                        Assert.IsFalse(
                            list.CanExecute(DBExtension.StartRefreshDB, view),
                            string.Format("Should not be able to refresh DB for {0}", invalidPath)
                            );
                    }
                }
        }
Example #7
0
        public void NonDefaultInterpreter()
        {
            var mockProvider = new MockPythonInterpreterFactoryProvider("Test Provider 1",
                                                                        new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Factory 1", MockInterpreterConfiguration(new Version(2, 7))),
                                                                        new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Factory 2", MockInterpreterConfiguration(new Version(3, 0), InterpreterUIMode.CannotBeDefault)),
                                                                        new MockPythonInterpreterFactory(Guid.NewGuid(), "Test Factory 3", MockInterpreterConfiguration(new Version(3, 3), InterpreterUIMode.CannotBeAutoDefault))
                                                                        );

            using (var wpf = new WpfProxy())
                using (var list = new EnvironmentListProxy(wpf)) {
                    var service      = GetInterpreterOptionsService();
                    var oldDefault   = service.DefaultInterpreter;
                    var oldProviders = ((InterpreterOptionsService)service).SetProviders(new[] { mockProvider });
                    try {
                        list.Service = service;
                        var environments = list.Environments;

                        AssertUtil.AreEqual(
                            wpf.Invoke(() => environments.Select(ev => ev.Description).ToList()),
                            "Test Factory 1", "Test Factory 2", "Test Factory 3"
                            );
                        // TF 1 and 3 can be set as default
                        AssertUtil.AreEqual(
                            wpf.Invoke(() => environments.Select(ev => ev.CanBeDefault).ToList()),
                            true, false, true
                            );

                        // TF 1 should have been selected as the default
                        Assert.AreEqual(
                            "Test Factory 1",
                            wpf.Invoke(() => environments.First(ev => ev.IsDefault).Description)
                            );
                    } finally {
                        ((InterpreterOptionsService)service).SetProviders(oldProviders);
                        service.DefaultInterpreter = oldDefault;
                    }
                }
        }
Example #8
0
        public void AddRemoveProjectFactories()
        {
            var service = new MockInterpreterOptionsService();
            var loaded  = new LoadedProjectInterpreterFactoryProvider();

            service.AddProvider(loaded);
            using (var wpf = new WpfProxy())
                using (var list = new EnvironmentListProxy(wpf)) {
                    list.Service = service;

                    // List should be empty
                    AssertUtil.ContainsExactly(list.Environments);

                    var project = new MockPythonInterpreterFactoryProvider("Fake Project");

                    loaded.ProjectLoaded(project, null);

                    // List is still empty
                    AssertUtil.ContainsExactly(list.Environments);

                    project.AddFactory(new MockPythonInterpreterFactory(Guid.NewGuid(), "Fake Environment", MockInterpreterConfiguration(new Version(2, 7))));

                    // List now contains one project
                    AssertUtil.ContainsExactly(
                        wpf.Invoke(() => list.Environments.Select(ev => ev.Description).ToList()),
                        "Fake Environment"
                        );

                    project.RemoveAllFactories();

                    // List is empty again
                    AssertUtil.ContainsExactly(list.Environments);

                    loaded.ProjectUnloaded(project);
                }
        }
Example #9
0
        public async Task InterpretersRaceCondition()
        {
            var container    = CreateCompositionContainer();
            var service      = container.GetExportedValue <IInterpreterOptionsService>();
            var interpreters = container.GetExportedValue <IInterpreterRegistryService>();
            var factories    = Enumerable.Repeat(0, 5).Select(
                i => new MockPythonInterpreterFactory(
                    MockInterpreterConfiguration(string.Format("Test Factory {0}", i), new Version(2, 7))
                    )
                ).ToList();
            var provider = new MockPythonInterpreterFactoryProvider("Test Provider", factories.ToArray());

            ((InterpreterRegistryService)interpreters).SetProviders(new[] {
                new Lazy <IPythonInterpreterFactoryProvider, Dictionary <string, object> >(
                    () => provider,
                    new Dictionary <string, object>()
                {
                    { "InterpreterFactoryId", "Mock" }
                }
                    )
            });

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
            var ct  = cts.Token;
            ExceptionDispatchInfo edi = null;

            EventHandler interpretersChanged = (s, e) => {
                Task.Run(() => {
                    try {
                        foreach (var f in factories)
                        {
                            Thread.Sleep(1);
                            ct.ThrowIfCancellationRequested();
                            provider.AddFactory(f);
                        }

                        ct.ThrowIfCancellationRequested();
                        var interpretersList = interpreters.Interpreters.ToList();
                        Trace.TraceInformation("Got {0} interpreters", interpretersList.Count);
                    } catch (OperationCanceledException) {
                    } catch (Exception ex) {
                        edi = ExceptionDispatchInfo.Capture(ex);
                    }
                });
            };

            interpreters.InterpretersChanged += interpretersChanged;

            var t1 = Task.Run(() => {
                while (!ct.IsCancellationRequested)
                {
                    provider.AddFactory(factories.First());
                    Thread.Sleep(50);
                    if (edi != null)
                    {
                        edi.Throw();
                    }
                    provider.RemoveAllFactories();
                }
            }, ct);
            var t2 = Task.Run(() => {
                try {
                    while (!ct.IsCancellationRequested)
                    {
                        var interpretersList = interpreters.InterpretersOrDefault.ToList();
                        Trace.TraceInformation("Got {0} interpreters or default", interpretersList.Count);
                        Thread.Sleep(10);
                    }
                } finally {
                    cts.Cancel();
                }
            }, ct);

            try {
                await t1;
            } catch (OperationCanceledException) {
            } finally {
                cts.Cancel();
            }
            try {
                await t2;
            } catch (OperationCanceledException) {
            } finally {
                interpreters.InterpretersChanged -= interpretersChanged;
            }
        }
Example #10
0
        public async Task InterpretersRaceCondition()
        {
            var service   = GetInterpreterOptionsService(defaultProviders: false);
            var provider  = new MockPythonInterpreterFactoryProvider("Test Provider");
            var factories = Enumerable.Repeat(0, 5).Select(
                i => new MockPythonInterpreterFactory(
                    Guid.NewGuid(),
                    string.Format("Test Factory {0}", i),
                    MockInterpreterConfiguration(new Version(2, 7))
                    )
                ).ToList();

            ((InterpreterOptionsService)service).SetProviders(new[] { provider });

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
            var ct  = cts.Token;
            ExceptionDispatchInfo edi = null;

            EventHandler interpretersChanged = (s, e) => {
                Task.Run(() => {
                    try {
                        foreach (var f in factories)
                        {
                            Thread.Sleep(1);
                            ct.ThrowIfCancellationRequested();
                            provider.AddFactory(f);
                        }

                        ct.ThrowIfCancellationRequested();
                        var interpreters = service.Interpreters.ToList();
                        Trace.TraceInformation("Got {0} interpreters", interpreters.Count);
                    } catch (OperationCanceledException) {
                    } catch (Exception ex) {
                        edi = ExceptionDispatchInfo.Capture(ex);
                    }
                });
            };

            service.InterpretersChanged += interpretersChanged;

            var t1 = Task.Run(() => {
                while (!ct.IsCancellationRequested)
                {
                    provider.AddFactory(factories.First());
                    Thread.Sleep(50);
                    if (edi != null)
                    {
                        edi.Throw();
                    }
                    provider.RemoveAllFactories();
                }
            }, ct);
            var t2 = Task.Run(() => {
                try {
                    while (!ct.IsCancellationRequested)
                    {
                        var interpreters = service.InterpretersOrDefault.ToList();
                        Trace.TraceInformation("Got {0} interpreters or default", interpreters.Count);
                        Thread.Sleep(10);
                    }
                } finally {
                    cts.Cancel();
                }
            }, ct);

            try {
                await t1;
            } catch (OperationCanceledException) {
            } finally {
                cts.Cancel();
            }
            try {
                await t2;
            } catch (OperationCanceledException) {
            } finally {
                service.InterpretersChanged -= interpretersChanged;
            }
        }