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