test_locate.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # coding=utf-8
  2. import pytest
  3. import osex.locate
  4. import os
  5. def test_local_db_files(tmpdir):
  6. test_dir_path = tmpdir.strpath
  7. os.chdir(test_dir_path)
  8. # prepare dir
  9. open('file1', 'w').close()
  10. open('file2', 'w').close()
  11. os.makedirs(os.path.join('dir', 'subdir1'))
  12. os.makedirs(os.path.join('dir', 'subdir2'))
  13. # update db
  14. database_path = os.path.join(test_dir_path, 'db')
  15. osex.locate.update_locate_database(database_path = database_path, root_path = test_dir_path, require_visibility = False)
  16. # locate
  17. results = osex.locate.locate(
  18. [test_dir_path + '*file*'],
  19. database_path = database_path
  20. )
  21. assert results == [os.path.join(test_dir_path, p) for p in [
  22. 'file1',
  23. 'file2',
  24. ]]
  25. def test_local_db_files_unicode(tmpdir):
  26. test_dir_path = tmpdir.strpath
  27. os.chdir(test_dir_path)
  28. # prepare dir
  29. open(u'file-ä', 'w').close()
  30. open(u'file-Ö', 'w').close()
  31. open(u'file-ß', 'w').close()
  32. # update db
  33. database_path = os.path.join(test_dir_path, 'db')
  34. osex.locate.update_locate_database(database_path = database_path, root_path = test_dir_path, require_visibility = False)
  35. # locate
  36. results = osex.locate.locate(
  37. [test_dir_path + '*file*'],
  38. database_path = database_path
  39. )
  40. expected_results = [os.path.join(test_dir_path, p) for p in [
  41. u'file-ä',
  42. u'file-Ö',
  43. u'file-ß',
  44. ]]
  45. assert results.sort() == expected_results.sort()
  46. def test_local_db_dirs(tmpdir):
  47. test_dir_path = tmpdir.strpath
  48. os.chdir(test_dir_path)
  49. # prepare dir
  50. open('file1', 'w').close()
  51. open('file2', 'w').close()
  52. os.makedirs(os.path.join('dir', 'subdir1'))
  53. os.makedirs(os.path.join('dir', 'subdir2'))
  54. # update db
  55. database_path = os.path.join(test_dir_path, 'db')
  56. osex.locate.update_locate_database(database_path = database_path, root_path = test_dir_path, require_visibility = False)
  57. # locate
  58. results = osex.locate.locate(
  59. [test_dir_path + '*dir*'],
  60. database_path = database_path
  61. )
  62. assert results == [os.path.join(test_dir_path, p) for p in [
  63. 'dir',
  64. os.path.join('dir', 'subdir1'),
  65. os.path.join('dir', 'subdir2'),
  66. ]]
  67. def test_local_db_subtree(tmpdir):
  68. test_dir_path = tmpdir.strpath
  69. os.chdir(test_dir_path)
  70. # prepare dir
  71. os.makedirs(os.path.join('dir1', 'subdir1'))
  72. os.makedirs(os.path.join('dir1', 'subdir2'))
  73. os.makedirs(os.path.join('dir2', 'subdir3'))
  74. os.makedirs(os.path.join('dir2', 'subdir4'))
  75. # update db
  76. database_path = os.path.join(test_dir_path, 'db')
  77. osex.locate.update_locate_database(
  78. database_path = database_path,
  79. root_path = os.path.join(test_dir_path, 'dir2'),
  80. require_visibility = False
  81. )
  82. # locate
  83. results = osex.locate.locate(
  84. [test_dir_path + '*subdir*'],
  85. database_path = database_path
  86. )
  87. assert results == [os.path.join(test_dir_path, p) for p in [
  88. os.path.join('dir2', 'subdir3'),
  89. os.path.join('dir2', 'subdir4'),
  90. ]]