예제 #1
0
        private IInterpreterOptionsService 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(
                                    Guid.NewGuid(),
                                    Path.GetFileName(CommonUtils.TrimEndSeparator(env)),
                                    new InterpreterConfiguration(
                                        env,
                                        CommonUtils.FindFile(env, "python.exe"),
                                        CommonUtils.FindFile(env, "python.exe"),
                                        Path.GetDirectoryName(CommonUtils.FindFile(env, "site.py", 3)),
                                        "PYTHONPATH",
                                        python.Isx64 ? ProcessorArchitecture.Amd64 : ProcessorArchitecture.X86,
                                        python.Version.ToVersion()
                                        )
                                    ));
            service.AddProvider(provider);
            return(service);
        }
        private void ImportWizardVirtualEnvWorker(
            PythonVersion python,
            string venvModuleName,
            string expectedFile,
            bool brokenBaseInterpreter
            )
        {
            var mockService = new MockInterpreterOptionsService();

            mockService.AddProvider(new MockPythonInterpreterFactoryProvider("Test Provider",
                                                                             new MockPythonInterpreterFactory(python.Id, "Test Python", python.Configuration)
                                                                             ));

            using (var wpf = new WpfProxy()) {
                var settings   = wpf.Create(() => new ImportSettings(mockService));
                var sourcePath = TestData.GetTempPath(randomSubPath: true);
                // Create a fake set of files to import
                File.WriteAllText(Path.Combine(sourcePath, "main.py"), "");
                Directory.CreateDirectory(Path.Combine(sourcePath, "A"));
                File.WriteAllText(Path.Combine(sourcePath, "A", "__init__.py"), "");
                // Create a real virtualenv environment to import
                using (var p = ProcessOutput.RunHiddenAndCapture(python.InterpreterPath, "-m", venvModuleName, Path.Combine(sourcePath, "env"))) {
                    Console.WriteLine(p.Arguments);
                    p.Wait();
                    Console.WriteLine(string.Join(Environment.NewLine, p.StandardOutputLines.Concat(p.StandardErrorLines)));
                    Assert.AreEqual(0, p.ExitCode);
                }

                if (brokenBaseInterpreter)
                {
                    var cfgPath = Path.Combine(sourcePath, "env", "Lib", "orig-prefix.txt");
                    if (File.Exists(cfgPath))
                    {
                        File.WriteAllText(cfgPath, string.Format("C:\\{0:N}", Guid.NewGuid()));
                    }
                    else if (File.Exists((cfgPath = Path.Combine(sourcePath, "env", "pyvenv.cfg"))))
                    {
                        File.WriteAllLines(cfgPath, File.ReadAllLines(cfgPath)
                                           .Select(line => {
                            if (line.StartsWith("home = "))
                            {
                                return(string.Format("home = C:\\{0:N}", Guid.NewGuid()));
                            }
                            return(line);
                        })
                                           );
                    }
                }

                Console.WriteLine("All files:");
                foreach (var f in Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories))
                {
                    Console.WriteLine(CommonUtils.GetRelativeFilePath(sourcePath, f));
                }

                Assert.IsTrue(
                    File.Exists(Path.Combine(sourcePath, "env", expectedFile)),
                    "Virtualenv was not created correctly"
                    );

                settings.SourcePath = sourcePath;

                string path = CreateRequestedProject(settings);

                Assert.AreEqual(settings.ProjectPath, path);
                var proj = XDocument.Load(path);

                // Does not include any .py files from the virtualenv
                AssertUtil.ContainsExactly(proj.Descendants(proj.GetName("Compile")).Select(x => x.Attribute("Include").Value),
                                           "main.py",
                                           "A\\__init__.py"
                                           );
                // Does not contain 'env'
                AssertUtil.ContainsExactly(proj.Descendants(proj.GetName("Folder")).Select(x => x.Attribute("Include").Value),
                                           "A"
                                           );

                var env = proj.Descendant("Interpreter");
                Assert.AreEqual("env\\", env.Attribute("Include").Value);
                Assert.AreEqual("lib\\", env.Descendant("LibraryPath").Value, true);
                if (brokenBaseInterpreter)
                {
                    Assert.AreEqual("env", env.Descendant("Description").Value);
                    Assert.AreEqual("", env.Descendant("InterpreterPath").Value);
                    Assert.AreEqual("", env.Descendant("WindowsInterpreterPath").Value);
                    Assert.AreEqual(Guid.Empty.ToString("B"), env.Descendant("BaseInterpreter").Value);
                    Assert.AreEqual("", env.Descendant("PathEnvironmentVariable").Value);
                }
                else
                {
                    Assert.AreEqual("env (Test Python)", env.Descendant("Description").Value);
                    Assert.AreEqual("scripts\\python.exe", env.Descendant("InterpreterPath").Value, true);
                    // The mock configuration uses python.exe for both paths.
                    Assert.AreEqual("scripts\\python.exe", env.Descendant("WindowsInterpreterPath").Value, true);
                    Assert.AreEqual(python.Id.ToString("B"), env.Descendant("BaseInterpreter").Value, true);
                    Assert.AreEqual("PYTHONPATH", env.Descendant("PathEnvironmentVariable").Value, true);
                }
            }
        }