builtin_glob.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/python
  2. # Copyright 2014 Steven Watanabe
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
  5. # This tests the GLOB rule.
  6. import os
  7. import BoostBuild
  8. def test_glob(files, glob, expected, setup=""):
  9. t = BoostBuild.Tester(["-ffile.jam"], pass_toolset=0)
  10. t.write("file.jam", setup + """
  11. for local p in [ SORT %s ]
  12. {
  13. ECHO $(p) ;
  14. }
  15. UPDATE ;
  16. """ % glob)
  17. for f in files:
  18. t.write(f, "")
  19. # convert / into \ on windows
  20. expected = [os.path.join(*p.split("/")) for p in expected]
  21. expected.sort()
  22. t.run_build_system(stdout="\n".join(expected + [""]))
  23. t.cleanup()
  24. # one or both arguments empty
  25. test_glob([], "[ GLOB : ]", [])
  26. test_glob([], "[ GLOB . : ]", [])
  27. test_glob([], "[ GLOB : * ]", [])
  28. # a single result
  29. test_glob([], "[ GLOB . : * ]", ["./file.jam"])
  30. # * can match any number of characters
  31. test_glob([], "[ GLOB . : file*.jam ]", ["./file.jam"])
  32. test_glob([], "[ GLOB . : f*am ]", ["./file.jam"])
  33. # ? should match a single character, but not more than one
  34. test_glob([], "[ GLOB . : fi?e.?am ]", ["./file.jam"])
  35. test_glob([], "[ GLOB . : fi?.jam ]", [])
  36. # [abc-fh-j] matches a set of characters
  37. test_glob([], '[ GLOB . : "[f][i][l][e].jam" ]', ["./file.jam"])
  38. test_glob([], '[ GLOB . : "[fghau][^usdrwe][k-o][^f-s].jam" ]', ["./file.jam"])
  39. # \x matches x
  40. test_glob([], "[ GLOB . : \\f\\i\\l\\e.jam ]", ["./file.jam"])
  41. # multiple results
  42. test_glob(["test.txt"], "[ GLOB . : * ]", ["./file.jam", "./test.txt"])
  43. # directories
  44. test_glob(["dir1/dir2/test.txt"], "[ GLOB dir1 : * ]", ["dir1/dir2"]);
  45. # non-existent directory
  46. test_glob([], "[ GLOB dir1 : * ] ", [])
  47. # multiple directories and patterns
  48. test_glob(["dir1/file1.txt", "dir2/file1.txt",
  49. "dir2/file2.txt"],
  50. "[ GLOB dir1 dir2 : file1* file2* ]",
  51. ["dir1/file1.txt", "dir2/file1.txt",
  52. "dir2/file2.txt"])
  53. # The directory can contain . and ..
  54. test_glob(["dir/test.txt"], "[ GLOB dir/. : test.txt ]", ["dir/./test.txt"])
  55. test_glob(["dir/test.txt"], "[ GLOB dir/.. : file.jam ]", ["dir/../file.jam"])
  56. # On case insensitive filesystems, the result should
  57. # be normalized. It should NOT be downcased.
  58. test_glob(["TEST.TXT"], "[ GLOB . : TEST.TXT ]", ["./TEST.TXT"])
  59. case_insensitive = (os.path.normcase("FILE") == "file")
  60. if case_insensitive:
  61. test_glob(["TEST.TXT"], "[ GLOB . : test.txt ]", ["./TEST.TXT"])
  62. # This used to fail because the caching routines incorrectly
  63. # reported that . and .. do not exist.
  64. test_glob(["D1/D2/TEST.TXT"], "[ GLOB D1/./D2 : test.txt ]",
  65. ["D1/./D2/TEST.TXT"])
  66. test_glob(["D1/TEST.TXT", "TEST.TXT"], "[ GLOB D1/../D1 : test.txt ]",
  67. ["D1/../D1/TEST.TXT"])
  68. # This also failed because directories that were first found
  69. # by GLOB were recorded as non-existent.
  70. test_glob(["D1/D2/TEST.TXT"], "[ GLOB d1/d2 : test.txt ]",
  71. ["D1/D2/TEST.TXT"],
  72. "GLOB . : * ;")