Ejemplo n.º 1
0
        public async Task GetSearchPaths()
        {
            Python.AssertInstalled();

            var paths = await PythonLibraryPath.GetUncachedDatabaseSearchPathsAsync(Python.InterpreterPath);

            Console.WriteLine("Paths for {0}", Python.InterpreterPath);
            foreach (var path in paths)
            {
                Console.WriteLine("{0} {1}", path.Path, path.IsStandardLibrary ? "(stdlib)" : "");
            }

            // Python.PrefixPath and LibraryPath should be included.
            // We can't assume anything else
            var expected = Python.IsCPython ? new[] {
                Python.PrefixPath.ToLowerInvariant().TrimEnd('\\'),
                Path.Combine(Python.PrefixPath, "Lib").ToLowerInvariant()
            } : new[] {
                Path.Combine(Python.PrefixPath, "Lib").ToLowerInvariant()
            };

            AssertUtil.ContainsAtLeast(paths.Select(p => p.Path.ToLowerInvariant().TrimEnd('\\')), expected);

            // All paths should exist
            AssertUtil.ArrayEquals(paths.Where(p => !Directory.Exists(p.Path)).ToList(), new PythonLibraryPath[0]);

            // Ensure we can round-trip the entries via ToString/Parse
            var asStrings  = paths.Select(p => p.ToString()).ToList();
            var asPaths    = asStrings.Select(PythonLibraryPath.Parse).ToList();
            var asStrings2 = asPaths.Select(p => p.ToString()).ToList();

            AssertUtil.ArrayEquals(asStrings, asStrings2);
            AssertUtil.ArrayEquals(paths, asPaths, (o1, o2) => {
                PythonLibraryPath p1 = (PythonLibraryPath)o1, p2 = (PythonLibraryPath)o2;
                return(p1.Path == p2.Path && p1.IsStandardLibrary == p2.IsStandardLibrary);
            });

            var dbPath = Path.Combine(TestData.GetTempPath(), "database.path");

            Assert.IsNull(PythonLibraryPath.GetCachedDatabaseSearchPaths(dbPath),
                          "Should not have found cached paths in an empty directory");

            PythonLibraryPath.WriteDatabaseSearchPaths(dbPath, paths);
            Assert.IsTrue(File.Exists(dbPath));
            var paths2 = PythonLibraryPath.GetCachedDatabaseSearchPaths(dbPath);

            AssertUtil.ArrayEquals(paths, paths2, (o1, o2) => {
                PythonLibraryPath p1 = (PythonLibraryPath)o1, p2 = (PythonLibraryPath)o2;
                return(p1.Path == p2.Path && p1.IsStandardLibrary == p2.IsStandardLibrary);
            });
        }