summaryrefslogtreecommitdiff
path: root/tools/build/test/builtin_glob.py
blob: 6f55a4d9c1f519349ad1cc65348c019ba87552ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python

# Copyright 2014 Steven Watanabe
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)

# This tests the GLOB rule.

import os
import BoostBuild

def test_glob(files, glob, expected, setup=""):
    t = BoostBuild.Tester(["-ffile.jam"], pass_toolset=0)
    t.write("file.jam", setup + """
    for local p in [ SORT %s ]
    {
        ECHO $(p) ;
    }
    UPDATE ;
    """ % glob)
    for f in files:
        t.write(f, "")
    # convert / into \ on windows
    expected = [os.path.join(*p.split("/")) for p in expected]
    expected.sort()
    t.run_build_system(stdout="\n".join(expected + [""]))
    t.cleanup()

# one or both arguments empty
test_glob([], "[ GLOB : ]", [])
test_glob([], "[ GLOB . : ]", [])
test_glob([], "[ GLOB : * ]", [])

# a single result
test_glob([], "[ GLOB . : * ]", ["./file.jam"])

# * can match any number of characters
test_glob([], "[ GLOB . : file*.jam ]", ["./file.jam"])
test_glob([], "[ GLOB . : f*am ]", ["./file.jam"])
# ? should match a single character, but not more than one
test_glob([], "[ GLOB . : fi?e.?am ]", ["./file.jam"])
test_glob([], "[ GLOB . : fi?.jam ]", [])
# [abc-fh-j] matches a set of characters
test_glob([], "[ GLOB . : [f][i][l][e].jam ]", ["./file.jam"])
test_glob([], "[ GLOB . : [fghau][^usdrwe][k-o][^f-s].jam ]", ["./file.jam"])
# \x matches x
test_glob([], "[ GLOB . : \\f\\i\\l\\e.jam ]", ["./file.jam"])

# multiple results
test_glob(["test.txt"], "[ GLOB . : * ]", ["./file.jam", "./test.txt"])

# directories
test_glob(["dir1/dir2/test.txt"], "[ GLOB dir1 : * ]", ["dir1/dir2"]);

# non-existent directory
test_glob([], "[ GLOB dir1 : * ] ", [])

# multiple directories and patterns
test_glob(["dir1/file1.txt", "dir2/file1.txt",
           "dir2/file2.txt"],
          "[ GLOB dir1 dir2 : file1* file2* ]",
          ["dir1/file1.txt", "dir2/file1.txt",
           "dir2/file2.txt"])

# The directory can contain . and ..
test_glob(["dir/test.txt"], "[ GLOB dir/. : test.txt ]", ["dir/./test.txt"])
test_glob(["dir/test.txt"], "[ GLOB dir/.. : file.jam ]", ["dir/../file.jam"])

# On case insensitive filesystems, the result should
# be normalized.  It should NOT be downcased.
test_glob(["TEST.TXT"], "[ GLOB . : TEST.TXT ]", ["./TEST.TXT"])

case_insensitive = (os.path.normcase("FILE") == "file")

if case_insensitive:
    test_glob(["TEST.TXT"], "[ GLOB . : test.txt ]", ["./TEST.TXT"])
    # This used to fail because the caching routines incorrectly
    # reported that . and .. do not exist.
    test_glob(["D1/D2/TEST.TXT"], "[ GLOB D1/./D2 : test.txt ]",
              ["D1/./D2/TEST.TXT"])
    test_glob(["D1/TEST.TXT", "TEST.TXT"], "[ GLOB D1/../D1 : test.txt ]",
              ["D1/../D1/TEST.TXT"])
    # This also failed because directories that were first found
    # by GLOB were recorded as non-existent.
    test_glob(["D1/D2/TEST.TXT"], "[ GLOB d1/d2 : test.txt ]",
              ["D1/D2/TEST.TXT"],
              "GLOB . : * ;")